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.core.toolkit.Wrappers;
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.model.table.FieldData;
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();
        //全部字段值
        Map<String, FieldData> fieldDataMap = dmlData.getFieldDataMap();
        //修改的字段
        List<String> changeFieldList = dmlData.getChangeFieldList();

        KxsMachineRatio machineRatio = new KxsMachineRatio();

        if (fieldDataMap.get("UserId").getValue() != null) {
            machineRatio.setUserId(Integer.valueOf(fieldDataMap.get("UserId").getValue().toString()));
        }
        if (fieldDataMap.get("PosUserId").getValue() != null) {
            machineRatio.setPosUserId(Integer.valueOf(fieldDataMap.get("PosUserId").getValue().toString()));
        }
        if (fieldDataMap.get("Status").getValue() != null) {
            int status = Integer.parseInt(fieldDataMap.get("Status").getValue().toString());
            machineRatio.setStatus(status == -1 ? 0 : 1);
        }
        if (fieldDataMap.get("ChangeFee").getValue() != null) {
            machineRatio.setRatio(new BigDecimal(fieldDataMap.get("ChangeFee").getValue().toString()));
        }
        if (fieldDataMap.get("SeoDescription").getValue() != null) {
            machineRatio.setRemark(fieldDataMap.get("SeoDescription").getValue().toString());
        }
        if (fieldDataMap.get("Sort").getValue() != null) {
            machineRatio.setBrandId(Integer.valueOf(fieldDataMap.get("Sort").getValue().toString()));
        }
        if (fieldDataMap.get("PosId").getValue() != null) {
            machineRatio.setMachineId(Integer.valueOf(fieldDataMap.get("PosId").getValue().toString()));
        }
        if (fieldDataMap.get("MerchantId").getValue() != null) {
            machineRatio.setMerchantId(Integer.valueOf(fieldDataMap.get("MerchantId").getValue().toString()));
        }
        if (fieldDataMap.get("PosSn").getValue() != null) {
            machineRatio.setPosSn(fieldDataMap.get("PosSn").getValue().toString());
        }
        if (fieldDataMap.get("CreateDate").getValue() != null) {
            machineRatio.setCreateTime(LocalDateTimeUtil.parse(fieldDataMap.get("CreateDate").getValue().toString(), DatePattern.NORM_DATETIME_PATTERN));
        }
        if (fieldDataMap.get("UpdateDate").getValue() != null) {
            machineRatio.setUpdateTime(LocalDateTimeUtil.parse(fieldDataMap.get("UpdateDate").getValue().toString(), DatePattern.NORM_DATETIME_PATTERN));
        }

//        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.selectOne(Wrappers.<KxsMachineRatio>lambdaQuery()
                .eq(KxsMachineRatio::getMachineId, machineRatio.getBrandId())
                .eq(KxsMachineRatio::getMerchantId, machineRatio.getMerchantId()));

        if(kxsMachineRatio == null){
            baseMapper.insert(machineRatio);
        }else{
            machineRatio.setId(kxsMachineRatio.getId());
            baseMapper.updateById(machineRatio);
        }
    }
}

