package com.kxs.transfer.api.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.aliyun.dts.subscribe.clients.record.OperationType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.transfer.api.mapper.user.KxsUserLevelLogMapper;
import com.kxs.transfer.api.mapper.user.KxsUserPresetLogMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import com.kxs.transfer.api.service.user.KxsUserPresetLogService;
import com.kxs.user.api.model.KxsUserAddress;
import com.kxs.user.api.model.KxsUserLevelLog;
import com.kxs.user.api.model.KxsUserPresetLog;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用户预设职级日志表(KxsUserPresetLog)表服务实现类
 *
 * @author 系统
 * @since 2024-01-03 09:39:56
 */
@Service("kxsUserPresetLogService")
@RequiredArgsConstructor
public class KxsUserPresetLogServiceImpl extends ServiceImpl<KxsUserPresetLogMapper, KxsUserPresetLog> implements KxsUserPresetLogService {

    private final KxsUserLevelLogMapper kxsUserLevelLogMapper;

    @Override
    public void changeUser(DMLData dmlData) {
        //修改的字段值
        Map<String, Object> validFieldDataMap = dmlData.getValidFieldDataMap();
        //全部字段值
        Map<String, FieldData> fieldDataMap = dmlData.getFieldDataMap();
        //修改的字段
        List<String> changeFieldList = dmlData.getChangeFieldList();
        //操作的主键
        int id = Integer.parseInt(dmlData.getId());

        KxsUserPresetLog presetLog = new KxsUserPresetLog();
        presetLog.setId(id);
        //操作类型
        OperationType operation = dmlData.getOperation();
        if (operation.equals(OperationType.DELETE)) {
            // 更新数据
            baseMapper.deleteById(id);
            return;
        }

        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    presetLog.setUserId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Rank":
                    presetLog.setPresetLevel(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "QueryCount":
                    presetLog.setCreateById(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "CreateDate":
                    presetLog.setStartTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    presetLog.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    presetLog.setEndTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if (operation.equals(OperationType.UPDATE)) {
            // 更新数据
            baseMapper.updateById(presetLog);
        }
        if (operation.equals(OperationType.INSERT)) {

            KxsUserPresetLog userPresetLog = baseMapper.selectById(presetLog.getId());
            if (userPresetLog != null) {
                baseMapper.updateById(presetLog);
            } else {
                // 插入数据
                baseMapper.insert(presetLog);
            }
        }

        //新增记录
        KxsUserLevelLog kxsUserLevelLog = new KxsUserLevelLog();

        if (fieldDataMap.get("UserId").getValue() != null) {
            kxsUserLevelLog.setUserId(Integer.valueOf(fieldDataMap.get("UserId").getValue().toString()));
        }
        if (fieldDataMap.get("Rank").getValue() != null) {
            kxsUserLevelLog.setPresetLevel(Integer.valueOf(fieldDataMap.get("Rank").getValue().toString()));
        }
        if (fieldDataMap.get("QueryCount").getValue() != null) {
            kxsUserLevelLog.setCreateById(Integer.valueOf(fieldDataMap.get("QueryCount").getValue().toString()));
        }
        if (fieldDataMap.get("CreateDate").getValue() != null) {
            LocalDateTime date = LocalDateTimeUtil.parse(fieldDataMap.get("CreateDate").getValue().toString(), DatePattern.NORM_DATETIME_PATTERN);
            kxsUserLevelLog.setStartTime(date);
            kxsUserLevelLog.setCreateTime(date);
        }
        if (fieldDataMap.get("UpdateDate").getValue() != null) {
            LocalDateTime date = LocalDateTimeUtil.parse(fieldDataMap.get("UpdateDate").getValue().toString(), DatePattern.NORM_DATETIME_PATTERN);
            kxsUserLevelLog.setEndTime(date);
        }
        // 插入数据
        if(kxsUserLevelLog.getEndTime() != null){
            kxsUserLevelLogMapper.insert(kxsUserLevelLog);
        }
    }
}

