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.KxsBrand;
import com.kxs.product.api.model.KxsMachinePledge;
import com.kxs.product.api.model.KxsMachineRatio;
import com.kxs.store.api.model.KxsMachineRecycle;
import com.kxs.store.api.model.KxsWarehouse;
import com.kxs.transfer.api.mapper.product.KxsMachinePledgeMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.product.KxsMachinePledgeService;
import com.kxs.user.api.model.KxsUser;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽押金调整记录表(KxsMachinePledge)表服务实现类
 *
 * @author 系统
 * @since 2023-12-20 10:09:35
 */
@Service("kxsMachinePledgeService")
public class KxsMachinePledgeServiceImpl extends ServiceImpl<KxsMachinePledgeMapper, KxsMachinePledge> implements KxsMachinePledgeService {

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

        OperationType operation = dmlData.getOperation();

        KxsMachinePledge machinePledge = new KxsMachinePledge();
        machinePledge.setId(id);

        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    machinePledge.setUserId(userId);
                    break;
                case "SeoTitle":
                    machinePledge.setBeforePledge(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "DepositAmount":
                    machinePledge.setAfterPledge(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "ReturnNote":
                    machinePledge.setResult(validFieldDataMap.get(field).toString());
                    break;
                case "Sort":
                    machinePledge.setBrandId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "SnNo":
                    machinePledge.setPosSn(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    machinePledge.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    machinePledge.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        KxsMachinePledge kxsMachinePledge = baseMapper.selectById(machinePledge.getId());

        if(operation.equals(OperationType.INSERT)){
            if(kxsMachinePledge == null){
                baseMapper.insert(machinePledge);
            }
        }

        if(operation.equals(OperationType.UPDATE)){
            baseMapper.updateById(machinePledge);
        }
    }
}

