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.transfer.api.mapper.user.KxsUserAmountMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import com.kxs.transfer.api.service.user.KxsUserAmountService;
import com.kxs.user.api.model.KxsUserAmount;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 用户账户表(KxsUserAmount)表服务实现类
 *
 * @author 系统
 * @since 2023-12-11 23:33:30
 */
@Service("kxsUserAmountService")
@RequiredArgsConstructor
public class KxsUserAmountServiceImpl extends ServiceImpl<KxsUserAmountMapper, KxsUserAmount> implements KxsUserAmountService {


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

        KxsUserAmount userAmount = new KxsUserAmount();
        userAmount.setId(id);

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

            switch (field) {
                case "UserId":
                    userAmount.setUserId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "TotalAmount":
                    userAmount.setTotalAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "FreezeAmount":
                    userAmount.setFrozenAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "WithdrawAmount":
                    userAmount.setCashWithdrawalAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "BalanceAmount":
                    userAmount.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "ToChargeAmount":
                    userAmount.setAdvanceAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "LeaderReserve":
                    userAmount.setLeaderReserve(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "LeaderBalanceAmount":
                    userAmount.setLeaderBalanceAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "LeaderToChargeAmount":
                    userAmount.setLeaderAdvanceAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "OperateToChargeAmount":
                    userAmount.setPartnerAdvanceAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "CreateDate":
                    userAmount.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    userAmount.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.UPDATE)){
            // 更新数据
            baseMapper.updateById(userAmount);
        }
        if(operation.equals(OperationType.INSERT)){
            KxsUserAmount kxsUserAmount = baseMapper.selectById(userAmount.getId());
            if (kxsUserAmount != null){
                baseMapper.updateById(userAmount);
            }else{
                // 插入数据
                baseMapper.insert(userAmount);
            }
        }
    }
}

