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.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.product.api.model.KxsTicket;
import com.kxs.transfer.api.mapper.user.KxsUserByStageLogMapper;
import com.kxs.transfer.api.mapper.user.KxsUserByStageMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import com.kxs.transfer.api.service.user.KxsUserByStageService;
import com.kxs.user.api.model.KxsUserByStage;
import com.kxs.user.api.model.KxsUserByStageLog;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 用户分期表(KxsUserByStage)表服务实现类
 *
 * @author 系统
 * @since 2024-01-03 09:39:56
 */
@Service("kxsUserByStageService")
@RequiredArgsConstructor
public class KxsUserByStageServiceImpl extends ServiceImpl<KxsUserByStageMapper, KxsUserByStage> implements KxsUserByStageService {

    private final KxsUserByStageLogMapper kxsUserByStageLogMapper;
    @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());

        KxsUserByStage userByStage = new KxsUserByStage();
        userByStage.setId(id);

        OperationType operation = dmlData.getOperation();
        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    userByStage.setUserId(userId);
                    break;
                case "Status":
                    int status = Integer.parseInt(validFieldDataMap.get(field).toString());
                    int timeNumber = Integer.parseInt(fieldDataMap.get("TimeNumber").getValue().toString());
                    int queryCount = Integer.parseInt(fieldDataMap.get("QueryCount").getValue().toString());
                    if(status == -1){
                        baseMapper.deleteById(id);
                        return;
                    } else if (status == 0 && queryCount == 0) {
                        userByStage.setStatus(0);
                    } else if (status == 1 && timeNumber == queryCount) {
                        userByStage.setStatus(2);
                    }else {
                        userByStage.setStatus(1);
                    }
                    break;
                case "SeoTitle":
                    userByStage.setCreateBy(validFieldDataMap.get(field).toString());
                    break;
                case "SeoKeyword":
                    userByStage.setStageSn(validFieldDataMap.get(field).toString());
                    break;
                case "TotalAmount":
                    userByStage.setStageAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "ChargeAmount":
                    userByStage.setDeductAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "TimeNumber":
                    userByStage.setNumber(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Remark":
                    userByStage.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "StartDate":
                    userByStage.setDeductStartTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "CreateDate":
                    userByStage.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    userByStage.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsUserByStage kxsUserByStage = baseMapper.selectById(userByStage.getId());
            if(kxsUserByStage == null){
                baseMapper.insert(userByStage);
            }
        }
        if(operation.equals(OperationType.UPDATE)){

            baseMapper.updateById(userByStage);
        }
    }

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

        KxsUserByStageLog userByStageLog = new KxsUserByStageLog();
        userByStageLog.setId(id);

        OperationType operation = dmlData.getOperation();
        if(operation.equals(OperationType.DELETE)){
            kxsUserByStageLogMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "ParentId":
                    userByStageLog.setStageId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "Status":
                    int status = Integer.parseInt(validFieldDataMap.get(field).toString());
                    if(status == -1){
                        kxsUserByStageLogMapper.deleteById(id);
                        return;
                    }else {
                        userByStageLog.setStatus(status);
                    }
                    break;
                case "SeoTitle":
                    userByStageLog.setCreateBy(validFieldDataMap.get(field).toString());
                    break;
                case "ChargeAmount":
                    userByStageLog.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "TimeNumber":
                    int parentId = Integer.parseInt(fieldDataMap.get("ParentId").getValue().toString());
                    //查询当前入库最新的分期记录
                    KxsUserByStageLog kxsUserByStageLog = kxsUserByStageLogMapper.selectOne(Wrappers.<KxsUserByStageLog>lambdaQuery()
                            .eq(KxsUserByStageLog::getStageId, parentId)
                            .orderByDesc(KxsUserByStageLog::getDeductTime).last("limit 1"));
                    if(kxsUserByStageLog == null){
                        userByStageLog.setThisNumber(1);
                    }else{
                        userByStageLog.setThisNumber(kxsUserByStageLog.getThisNumber() + 1);
                    }
                    break;
                case "Remark":
                    userByStageLog.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "StartDate":
                    userByStageLog.setDeductTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "CreateDate":
                    userByStageLog.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    userByStageLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsUserByStageLog kxsUserByStageLog = kxsUserByStageLogMapper.selectById(id);
            if(kxsUserByStageLog == null){
                kxsUserByStageLogMapper.insert(userByStageLog);
            }
        }
        if(operation.equals(OperationType.UPDATE)){

            kxsUserByStageLogMapper.updateById(userByStageLog);
        }
    }
}

