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.common.core.constant.enums.KindTypeEnum;
import com.kxs.transfer.api.mapper.user.KxsPartnerAmountLogMapper;
import com.kxs.transfer.api.mapper.user.KxsPartnerMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.user.KxsPartnerService;
import com.kxs.user.api.model.KxsBankChangeLog;
import com.kxs.user.api.model.KxsLeaderAmountLog;
import com.kxs.user.api.model.KxsPartner;
import com.kxs.user.api.model.KxsPartnerAmountLog;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

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

/**
 * 合伙人表(KxsPartner)表服务实现类
 *
 * @author 系统
 * @since 2024-02-22 15:39:26
 */
@Service("kxsPartnerService")
@RequiredArgsConstructor
public class KxsPartnerServiceImpl extends ServiceImpl<KxsPartnerMapper, KxsPartner> implements KxsPartnerService {

    private final KxsPartnerAmountLogMapper kxsPartnerAmountLogMapper;

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

        KxsPartner partner = new KxsPartner();
        partner.setId(id);

        //操作类型
        OperationType operation = dmlData.getOperation();

        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(partner.getId());
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    partner.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "Rank":
                    partner.setPartnerLevel(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "CreateDate":
                    partner.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    partner.setExpiredDate(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        KxsPartner kxsPartner = baseMapper.selectById(partner.getId());
        if(kxsPartner == null){
            baseMapper.insert(partner);
        }else{
            baseMapper.updateById(partner);
        }

    }

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

        KxsPartner partner = new KxsPartner();
        partner.setId(id);

        //操作类型
        OperationType operation = dmlData.getOperation();
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    partner.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "ValidForGetAmount":
                    partner.setWithdrawalAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "TotalAmt":
                    partner.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "ValidAmount":
                    partner.setStoreAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
            }
        }

        KxsPartner kxsPartner = baseMapper.selectById(partner.getId());
        if(kxsPartner == null){
            //用户ID为空过滤
            if(ObjectUtils.isEmpty(partner.getUserId())){
                return;
            }
            partner.setPartnerLevel(9);
            baseMapper.insert(partner);
        }else{
            baseMapper.updateById(partner);
        }
    }

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

        KxsPartnerAmountLog partnerAmountLog = new KxsPartnerAmountLog();
        partnerAmountLog.setId(id);

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

            switch (field) {
                case "UserId":
                    partnerAmountLog.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "UseAmount":
                    partnerAmountLog.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeAmount":
                    partnerAmountLog.setBeforeAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterAmount":
                    partnerAmountLog.setAfterAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterTotalAmt":
                    partnerAmountLog.setAfterTotalAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterValidForGetAmount":
                    partnerAmountLog.setAfterWithdrawalAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterValidAmount":
                    partnerAmountLog.setAfterStoreAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "UseValidAmount":
                    partnerAmountLog.setUseStoreAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "UseTotalAmt":
                    partnerAmountLog.setUseTotalAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "UseValidForGetAmount":
                    partnerAmountLog.setUseWithdrawalAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "Remark":
                    partnerAmountLog.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "ChangeTypeId":
                    partnerAmountLog.setChangeType(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "OperateType":
                    int type = Integer.parseInt(validFieldDataMap.get(field).toString());
                    if(type == 0){
                        partnerAmountLog.setKind(KindTypeEnum.ADD.getType());
                    } else if (type == 1) {
                        partnerAmountLog.setKind(KindTypeEnum.ADD.getType());
                    }else{
                        partnerAmountLog.setKind(KindTypeEnum.SUB.getType());
                    }
                    break;
                case "CreateDate":
                    partnerAmountLog.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    partnerAmountLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

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

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

            KxsPartnerAmountLog kxsPartnerAmountLog = kxsPartnerAmountLogMapper.selectById(id);
            if (kxsPartnerAmountLog != null){
                kxsPartnerAmountLogMapper.updateById(partnerAmountLog);
            }else{
                // 插入数据
                kxsPartnerAmountLogMapper.insert(partnerAmountLog);
            }
        }
    }
}

