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.KxsMachine;
import com.kxs.product.api.model.KxsMachineTrack;
import com.kxs.product.api.model.KxsMachineTransfer;
import com.kxs.transfer.api.mapper.product.KxsMachineMapper;
import com.kxs.transfer.api.mapper.product.KxsMachineTrackMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import com.kxs.user.api.model.KxsUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 客小爽机具轨迹记录表(KxsMachineTrack)表服务实现类
 *
 * @author 系统
 * @since 2023-12-20 10:09:35
 */
@Service("kxsMachineTrackService")
@RequiredArgsConstructor
public class KxsMachineTrackService extends ServiceImpl<KxsMachineTrackMapper, KxsMachineTrack> {

    private final KxsMachineMapper kxsMachineMapper;

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

        KxsMachineTrack machineTrack = new KxsMachineTrack();
        machineTrack.setId(id);

        //操作类型
        OperationType operation = dmlData.getOperation();

        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "FromUserId":
                    machineTrack.setBeforeUserId(Long.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "ToUserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    machineTrack.setAfterUserId(userId);
                    break;
                case "SnNo":
                    String sn = validFieldDataMap.get(field).toString();
                    KxsMachine kxsMachine = kxsMachineMapper.selectOne(Wrappers.<KxsMachine>lambdaQuery().eq(KxsMachine::getPosSn, sn));
                    if(kxsMachine == null){
                        return;
                    }
                    machineTrack.setPosSn(sn);
                    machineTrack.setMachineId(kxsMachine.getId());
                    break;
                case "BrandId":
                    machineTrack.setBrandId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "TransType":
                    machineTrack.setTrackType(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "OpId":
                    machineTrack.setPartnerId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "CreateDate":
                    machineTrack.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    machineTrack.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsMachineTrack kxsMachineTrack = baseMapper.selectById(id);
            if(kxsMachineTrack == null){
                baseMapper.insert(machineTrack);
            }
        }
        if(operation.equals(OperationType.UPDATE)){
            baseMapper.updateById(machineTrack);
        }
    }
}

