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.KxsBankChangeLogMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.user.KxsBankChangeLogService;
import com.kxs.user.api.model.KxsBankChangeLog;
import com.kxs.user.api.model.KxsUserAdvance;
import org.springframework.stereotype.Service;

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

/**
 * 用户结算卡变更记录(KxsBankChangeLog)表服务实现类
 *
 * @author 系统
 * @since 2024-01-03 09:39:56
 */
@Service("kxsBankChangeLogService")
public class KxsBankChangeLogServiceImpl extends ServiceImpl<KxsBankChangeLogMapper, KxsBankChangeLog> implements KxsBankChangeLogService {

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

        KxsBankChangeLog bankChangeLog = new KxsBankChangeLog();
        bankChangeLog.setId(id);
        //操作类型
        OperationType operation = dmlData.getOperation();
        if(operation.equals(OperationType.DELETE)){
            // 更新数据
            baseMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    bankChangeLog.setUserId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeCardNo":
                    bankChangeLog.setBeforeCardNo(validFieldDataMap.get(field).toString());
                    break;
                case "AfterCardNo":
                    bankChangeLog.setAfterCardNo(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    bankChangeLog.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    bankChangeLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.UPDATE)){

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

            KxsBankChangeLog kxsBankChangeLog = baseMapper.selectById(id);
            if (kxsBankChangeLog != null){
                baseMapper.updateById(bankChangeLog);
            }else{
                // 插入数据
                baseMapper.insert(bankChangeLog);
            }
        }

    }
}

