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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.aliyun.dts.subscribe.clients.record.OperationType;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.constant.enums.KindTypeEnum;
import com.kxs.common.core.util.TotalDateUtil;
import com.kxs.transfer.api.mapper.user.KxsChangeTypeKindMapper;
import com.kxs.transfer.api.mapper.user.KxsLeaderAccountLogMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import com.kxs.user.api.model.KxsChangeTypeKind;
import com.kxs.user.api.model.KxsLeaderAccountLog;
import com.kxs.user.api.model.KxsLeaderAmountLog;
import com.kxs.user.api.model.KxsPartnerAmountLog;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 盟主金额变动记录(KxsLeaderAccountLog)表服务实现类
 *
 * @author 系统
 * @since 2024-06-26 14:12:12
 */
@Service("kxsLeaderAccountLogService")
@RequiredArgsConstructor
public class KxsLeaderAccountLogService extends ServiceImpl<KxsLeaderAccountLogMapper, KxsLeaderAccountLog>{

    private final KxsChangeTypeKindMapper kxsChangeTypeKindMapper;

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

        KxsLeaderAccountLog leaderAccountLog = new KxsLeaderAccountLog();

        //操作类型
        OperationType operation = dmlData.getOperation();
        if (operation.equals(OperationType.DELETE) || operation.equals(OperationType.UPDATE)) {
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    leaderAccountLog.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "SourceUserId":
                    leaderAccountLog.setSourceUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "Sort":
                    leaderAccountLog.setChangeType(1);
                    break;
                case "ChangeType":
                    int type = Integer.parseInt(validFieldDataMap.get(field).toString());
                    if(type == 1){
                        leaderAccountLog.setKind(KindTypeEnum.SUB.getType());
                    } else {
                        leaderAccountLog.setKind(KindTypeEnum.ADD.getType());
                    }
                    break;
                case "ChangeAmt":
                    leaderAccountLog.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "TradeMonth":
                    leaderAccountLog.setTradeMonth(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "TradeDate":
                    leaderAccountLog.setTradeDate(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "OrderId":
                    leaderAccountLog.setDataId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeAmt":
                    leaderAccountLog.setBeforeAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterAmt":
                    leaderAccountLog.setAfterAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "Remark":
                    leaderAccountLog.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "SeoKeyword":
                    leaderAccountLog.setVariationCause(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    leaderAccountLog.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    leaderAccountLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

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

            // 插入数据
            baseMapper.insert(leaderAccountLog);
        }
    }

    public void change2Data(DMLData dmlData) {
        //修改的字段值
        Map<String, Object> validFieldDataMap = dmlData.getValidFieldDataMap();
        //修改的字段
        List<String> changeFieldList = dmlData.getChangeFieldList();
        Map<String, FieldData> fieldDataMap = dmlData.getFieldDataMap();
        //操作的主键
        int id = Integer.parseInt(dmlData.getId());
        List<KxsChangeTypeKind> kxsChangeTypeKinds = kxsChangeTypeKindMapper.selectList(Wrappers.<KxsChangeTypeKind>lambdaQuery().eq(KxsChangeTypeKind::getKind, 1));
        List<Integer> adds = kxsChangeTypeKinds.stream().map(KxsChangeTypeKind::getId).toList();

        KxsLeaderAccountLog leaderAccountLog = new KxsLeaderAccountLog();
        leaderAccountLog.setChangeType(0);
        //操作类型
        OperationType operation = dmlData.getOperation();
        if (operation.equals(OperationType.DELETE) || operation.equals(OperationType.UPDATE)) {
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    leaderAccountLog.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "ChangeType":
                    int type = Integer.parseInt(validFieldDataMap.get(field).toString());
                    if (adds.contains(type)) {
                        leaderAccountLog.setKind(KindTypeEnum.ADD.getType());
                    }else{
                        leaderAccountLog.setKind(KindTypeEnum.SUB.getType());
                    }
                    leaderAccountLog.setVariationType(type);
                    break;
                case "ChangeAmount":
                    leaderAccountLog.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeBalanceAmount":
                    leaderAccountLog.setBeforeAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterBalanceAmount":
                    leaderAccountLog.setAfterAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "SeoDescription":
                    leaderAccountLog.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    LocalDateTime parse = LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN);
                    Integer tradeMonth = TotalDateUtil.getMonthNumber(parse.toLocalDate());
                    Integer tradeDate = TotalDateUtil.getDateNumber(parse.toLocalDate());
                    leaderAccountLog.setTradeMonth(tradeMonth);
                    leaderAccountLog.setTradeDate(tradeDate);
                    leaderAccountLog.setCreateTime(parse);
                    break;
                case "UpdateDate":
                    leaderAccountLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

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

            // 插入数据
            baseMapper.insert(leaderAccountLog);
        }
    }

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


        //操作类型
        OperationType operation = dmlData.getOperation();
        if (operation.equals(OperationType.DELETE) || operation.equals(OperationType.UPDATE)) {
            return;
        }
        int operateType = 1;
        if (fieldDataMap.get("OperateType").getValue() != null) {
            operateType = Integer.parseInt(fieldDataMap.get("OperateType").getValue().toString());
        }
        //储蓄金变动
        if (operateType == 0) {
            KxsLeaderAccountLog leaderAccountLog = new KxsLeaderAccountLog();
            KxsLeaderAccountLog leaderAccountLog1 = new KxsLeaderAccountLog();
            leaderAccountLog1.setChangeType(1);
            leaderAccountLog1.setKind(KindTypeEnum.SUB.getType());

            for (String field : changeFieldList) {

                switch (field) {
                    case "UserId":
                        leaderAccountLog1.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                        break;
                    case "UseTotalAmt":
                        leaderAccountLog1.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                        break;
                    case "AfterTotalAmt":
                        leaderAccountLog1.setAfterAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                        break;
                    case "DataId":
                        leaderAccountLog1.setDataId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                        break;
                    case "ChangeTypeId":
                        leaderAccountLog1.setVariationType(Integer.parseInt(validFieldDataMap.get(field).toString()) + 500);
                        break;
                    case "Remark":
                        leaderAccountLog1.setRemark(validFieldDataMap.get(field).toString());
                        break;
                    case "CreateDate":
                        LocalDateTime parse = LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN);
                        Integer tradeMonth = TotalDateUtil.getMonthNumber(parse.toLocalDate());
                        Integer tradeDate = TotalDateUtil.getDateNumber(parse.toLocalDate());
                        leaderAccountLog1.setTradeMonth(tradeMonth);
                        leaderAccountLog1.setTradeDate(tradeDate);
                        leaderAccountLog1.setCreateTime(parse);
                        break;
                    case "UpdateDate":
                        leaderAccountLog1.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                        break;
                }
            }
            if (operation.equals(OperationType.INSERT)) {
                // 插入数据
                baseMapper.insert(leaderAccountLog1);
            }

            leaderAccountLog.setChangeType(0);
            leaderAccountLog1.setKind(KindTypeEnum.ADD.getType());
            for (String field : changeFieldList) {

                switch (field) {
                    case "UserId":
                        leaderAccountLog.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                        break;
                    case "UseValidForGetAmount":
                        leaderAccountLog.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                        break;
                    case "AfterValidForGetAmount":
                        leaderAccountLog.setAfterAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                        break;
                    case "DataId":
                        leaderAccountLog.setDataId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                        break;
                    case "ChangeTypeId":
                        leaderAccountLog.setVariationType(Integer.parseInt(validFieldDataMap.get(field).toString()) + 500);
                        break;
                    case "Remark":
                        leaderAccountLog.setRemark(validFieldDataMap.get(field).toString());
                        break;
                    case "CreateDate":
                        LocalDateTime parse = LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN);
                        Integer tradeMonth = TotalDateUtil.getMonthNumber(parse.toLocalDate());
                        Integer tradeDate = TotalDateUtil.getDateNumber(parse.toLocalDate());
                        leaderAccountLog.setTradeMonth(tradeMonth);
                        leaderAccountLog.setTradeDate(tradeDate);
                        leaderAccountLog.setCreateTime(parse);
                        break;
                    case "UpdateDate":
                        leaderAccountLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                        break;
                }
            }
            if (operation.equals(OperationType.INSERT)) {
                // 插入数据
                baseMapper.insert(leaderAccountLog);
            }
        } else {

            KxsLeaderAccountLog leaderAccountLog = new KxsLeaderAccountLog();

            for (String field : changeFieldList) {

                switch (field) {
                    case "UserId":
                        leaderAccountLog.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                        break;
                    case "UseValidForGetAmount":
                        leaderAccountLog.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                        break;
                    case "AfterValidForGetAmount":
                        leaderAccountLog.setAfterAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                        break;
                    case "DataId":
                        leaderAccountLog.setDataId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                        break;
                    case "ChangeTypeId":
                        leaderAccountLog.setVariationType(Integer.parseInt(validFieldDataMap.get(field).toString()) + 500);
                        break;
                    case "Remark":
                        leaderAccountLog.setRemark(validFieldDataMap.get(field).toString());
                        break;
                    case "OperateType":
                        int type = Integer.parseInt(validFieldDataMap.get(field).toString());
                        if (type == 1) {
                            leaderAccountLog.setKind(KindTypeEnum.ADD.getType());
                        } else {
                            leaderAccountLog.setKind(KindTypeEnum.SUB.getType());
                        }
                        break;
                    case "CreateDate":
                        LocalDateTime parse = LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN);
                        Integer tradeMonth = TotalDateUtil.getMonthNumber(parse.toLocalDate());
                        Integer tradeDate = TotalDateUtil.getDateNumber(parse.toLocalDate());
                        leaderAccountLog.setTradeMonth(tradeMonth);
                        leaderAccountLog.setTradeDate(tradeDate);
                        leaderAccountLog.setCreateTime(parse);
                        break;
                    case "UpdateDate":
                        leaderAccountLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                        break;
                }
            }

            if (operation.equals(OperationType.INSERT)) {
                // 插入数据
                baseMapper.insert(leaderAccountLog);
            }
        }
    }
}

