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

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.KxsLeaderAmountLogMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.user.KxsLeaderAmountLogService;
import com.kxs.user.api.model.KxsLeader;
import com.kxs.user.api.model.KxsLeaderAmountLog;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 盟主储备金变动记录(KxsLeaderAmountLog)表服务实现类
 *
 * @author 系统
 * @since 2024-02-22 15:39:26
 */
@Service("kxsLeaderAmountLogService")
public class KxsLeaderAmountLogServiceImpl extends ServiceImpl<KxsLeaderAmountLogMapper, KxsLeaderAmountLog> implements KxsLeaderAmountLogService {

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

        KxsLeaderAmountLog leaderAmountLog = new KxsLeaderAmountLog();
        leaderAmountLog.setId(id);

        //操作类型
        OperationType operation = dmlData.getOperation();
        if (operation.equals(OperationType.DELETE)) {
            // 删除数据
            baseMapper.deleteById(id);
            return;
        }

        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    leaderAmountLog.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "SourceUserId":
                    leaderAmountLog.setSourceUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "Sort":
                    leaderAmountLog.setVariationType(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "ChangeAmt":
                    leaderAmountLog.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeAmt":
                    leaderAmountLog.setBeforeAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterAmt":
                    leaderAmountLog.setAfterAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "Remark":
                    leaderAmountLog.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "SeoKeyword":
                    leaderAmountLog.setVariationCause(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    leaderAmountLog.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    leaderAmountLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        if(operation.equals(OperationType.UPDATE)){
            // 更新数据
            baseMapper.updateById(leaderAmountLog);
        }
        if(operation.equals(OperationType.INSERT)){
            KxsLeaderAmountLog kxsLeaderAmountLog = baseMapper.selectById(leaderAmountLog.getId());
            if (kxsLeaderAmountLog != null){
                baseMapper.updateById(leaderAmountLog);
            }else{
                // 插入数据
                baseMapper.insert(leaderAmountLog);
            }
        }
    }
}

