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.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.KxsLeaderAmountLog;
import com.kxs.user.api.model.KxsPartner;
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")
public class KxsPartnerServiceImpl extends ServiceImpl<KxsPartnerMapper, KxsPartner> implements KxsPartnerService {

    @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);
        }
    }
}

