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.common.core.constant.enums.AmountLogChangeTypesEnum;
import com.kxs.common.core.constant.enums.KindTypeEnum;
import com.kxs.common.mybatis.handler.MonthTableNameHandler;
import com.kxs.transfer.api.mapper.user.KxsUserAmountLogMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import com.kxs.transfer.api.service.user.KxsUserAmountLogService;
import com.kxs.user.api.model.KxsUserAmount;
import com.kxs.user.api.model.KxsUserAmountLog;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 用户余额记录日志表(KxsUserAmountLog)表服务实现类
 *
 * @author 系统
 * @since 2024-01-03 09:39:56
 */
@Service("kxsUserAmountLogService")
public class KxsUserAmountLogServiceImpl extends ServiceImpl<KxsUserAmountLogMapper, KxsUserAmountLog> implements KxsUserAmountLogService {

    @Override
    public void changeData(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());

        //增加的changeType
        List<Integer> adds = Arrays.asList(0, 1, 6, 12, 21, 30, 31, 50, 60, 62, 64, 66, 111, 112, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 126, 127, 128, 129, 130, 131, 133, 134, 135, 136, 137, 301, 311, 312, 313, 314, 315);

        KxsUserAmountLog userAmountLog = new KxsUserAmountLog();
        userAmountLog.setId(id);

        String tradeMonth;
        if(fieldDataMap.get("CreateDate") != null && fieldDataMap.get("CreateDate").getValue() != null){
            LocalDateTime createTime = LocalDateTimeUtil.parse(fieldDataMap.get("CreateDate").getValue().toString(), DatePattern.NORM_DATETIME_PATTERN);
            tradeMonth = LocalDateTimeUtil.format(createTime, DatePattern.SIMPLE_MONTH_PATTERN);
        }else{
            tradeMonth = MonthTableNameHandler.getThisMoth();
        }
        //每个请求线程维护一个month数据，避免多线程数据冲突
        MonthTableNameHandler.setData(tradeMonth);


        //操作类型
        OperationType operation = dmlData.getOperation();
        if (operation.equals(OperationType.DELETE)) {
            // 删除数据
            baseMapper.deleteById(id);
            //删除线程，避免线程污染
            MonthTableNameHandler.removeData();
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    userAmountLog.setUserId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "ProductType":
                    userAmountLog.setBrandId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "ChangeType":
                    Integer changeType = Integer.valueOf(validFieldDataMap.get(field).toString());
                    userAmountLog.setVariationType(changeType);
                    if (adds.contains(changeType)) {
                        userAmountLog.setKind(KindTypeEnum.ADD.getType());
                    }else{
                        userAmountLog.setKind(KindTypeEnum.SUB.getType());
                    }

                    break;
                case "ChangeAmount":
                    userAmountLog.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeTotalAmount":
                    userAmountLog.setBeforeTotalAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterTotalAmount":
                    userAmountLog.setAfterTotalAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeBalanceAmount":
                    userAmountLog.setBeforeAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterBalanceAmount":
                    userAmountLog.setAfterAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "QueryCount":
                    userAmountLog.setVariationCause(validFieldDataMap.get(field).toString());
                    break;
                case "Remark":
                    userAmountLog.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    userAmountLog.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    userAmountLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        if (operation.equals(OperationType.UPDATE)) {
            // 更新数据
            baseMapper.updateById(userAmountLog);
        }
        if (operation.equals(OperationType.INSERT)) {
            KxsUserAmountLog kxsUserAmountLog = baseMapper.selectById(userAmountLog.getId());
            if(kxsUserAmountLog == null){
                // 插入数据
                baseMapper.insert(userAmountLog);
            }else{
                baseMapper.updateById(userAmountLog);
            }
        }
        //删除线程，避免线程污染
        MonthTableNameHandler.removeData();
    }
}

