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

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

/**
 * 用户提现申请表(KxsUserWithdrawal)表服务实现类
 *
 * @author 系统
 * @since 2024-01-03 09:39:56
 */
@Service("kxsUserWithdrawalService")
@RequiredArgsConstructor
public class KxsUserWithdrawalServiceImpl extends ServiceImpl<KxsUserWithdrawalMapper, KxsUserWithdrawal> implements KxsUserWithdrawalService {

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

        KxsUserWithdrawal userWithdrawal = new KxsUserWithdrawal();
        userWithdrawal.setId(id);

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

            switch (field) {
                case "UserId":
                    userWithdrawal.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "TradeType":
                    int withdrawalType = Integer.parseInt(validFieldDataMap.get(field).toString());
                    userWithdrawal.setWithdrawalType(withdrawalType == 2 ? 0 : withdrawalType);
                    break;
                case "QueryCount":
                    int applyStatus = Integer.parseInt(validFieldDataMap.get(field).toString());
                    userWithdrawal.setApplyStatus(applyStatus == 0 ? 0 : 1);
                    break;
                case "Status":
                    userWithdrawal.setStatus(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "SettleBankCardNo":
                    userWithdrawal.setBankCode(validFieldDataMap.get(field).toString());
                    break;
                case "SettleBankName":
                    userWithdrawal.setBankName(validFieldDataMap.get(field).toString());
                    break;
                case "CashOrderNo":
                    userWithdrawal.setApplyNo(validFieldDataMap.get(field).toString());
                    break;
                case "UpdateMan":
                    userWithdrawal.setChannelName(validFieldDataMap.get(field).toString());
                    break;
                case "TradeAmount":
                    userWithdrawal.setApplyAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "ActualTradeAmount":
                    userWithdrawal.setRealAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "TradeFee":
                    userWithdrawal.setServiceAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "ManageFee":
                    userWithdrawal.setTaxAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "Remark":
                    userWithdrawal.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "ResData":
                    userWithdrawal.setResult(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    userWithdrawal.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    userWithdrawal.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

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

}

