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.KxsUserAdvanceMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import com.kxs.transfer.api.service.user.KxsUserAdvanceService;
import com.kxs.user.api.model.KxsUserAdvance;
import com.kxs.user.api.model.KxsUserPresetLog;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽用户预扣款表(KxsUserAdvance)表服务实现类
 *
 * @author 系统
 * @since 2024-01-03 09:39:56
 */
@Service("kxsUserAdvanceService")
@RequiredArgsConstructor
public class KxsUserAdvanceServiceImpl extends ServiceImpl<KxsUserAdvanceMapper, KxsUserAdvance> implements KxsUserAdvanceService {


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

        KxsUserAdvance advance = new KxsUserAdvance();
        advance.setId(id);
        //操作类型
        OperationType operation = dmlData.getOperation();
        if(operation.equals(OperationType.DELETE)){
            // 更新数据
            baseMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    advance.setUserId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "ChargeAmount":
                    advance.setAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "Remark":
                    advance.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "SeoTitle":
                    advance.setCreateBy(validFieldDataMap.get(field).toString());
                    break;
                case "ChargeType":
                    int advanceType = Integer.parseInt(validFieldDataMap.get(field).toString());
                    advance.setAdvanceType(advanceType);
                    break;
                case "Kind":
                    int kind = Integer.parseInt(validFieldDataMap.get(field).toString());
                    advance.setKind(kind);
                    break;
                case "Status":
                    int status = Integer.parseInt(validFieldDataMap.get(field).toString());
                    if(status == -1){
                        advance.setDelFlag("1");
                    }
                    advance.setStatus(status);
                    break;
                case "CreateDate":
                    advance.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    advance.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.UPDATE)){
            // 更新数据
            baseMapper.updateById(advance);
        }
        if(operation.equals(OperationType.INSERT)){

            KxsUserAdvance userAdvance = baseMapper.selectById(advance.getId());
            if (userAdvance != null){
                baseMapper.updateById(advance);
            }else{
                // 插入数据
                baseMapper.insert(advance);
            }
        }
    }
}

