package com.kxs.transfer.api.service.product.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.product.api.model.KxsMachinePledge;
import com.kxs.product.api.model.KxsMachineRatio;
import com.kxs.transfer.api.mapper.product.KxsMachineRatioMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.product.KxsMachineRatioService;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽费率调整记录表(KxsMachineRatio)表服务实现类
 *
 * @author 系统
 * @since 2023-12-20 10:09:35
 */
@Service("kxsMachineRatioService")
public class KxsMachineRatioServiceImpl extends ServiceImpl<KxsMachineRatioMapper, KxsMachineRatio> implements KxsMachineRatioService {

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

        KxsMachineRatio machineRatio = new KxsMachineRatio();
        machineRatio.setId(id);

        OperationType operation = dmlData.getOperation();
        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }

        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    machineRatio.setUserId(userId);
                    break;
                case "PosUserId":
                    int posUserId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    machineRatio.setPosUserId(posUserId);
                    break;
                case "Status":
                    int status = Integer.parseInt(validFieldDataMap.get(field).toString());
                    machineRatio.setStatus(status == -1 ? 0 : 1);
                    break;
                case "ChangeFee":
                    machineRatio.setRatio(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "SeoDescription":
                    machineRatio.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "Sort":
                    machineRatio.setBrandId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "PosId":
                    machineRatio.setMachineId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "MerchantId":
                    machineRatio.setMerchantId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "PosSn":
                    machineRatio.setPosSn(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    machineRatio.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    machineRatio.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        KxsMachineRatio kxsMachineRatio = baseMapper.selectById(machineRatio.getId());

        if(operation.equals(OperationType.INSERT)){
            if(kxsMachineRatio == null){
                baseMapper.insert(machineRatio);
            }
        }

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

            baseMapper.updateById(machineRatio);
        }


    }
}

