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.KxsMachineTransfer;
import com.kxs.system.api.model.KxsAppModal;
import com.kxs.transfer.api.mapper.product.KxsMachineMapper;
import com.kxs.transfer.api.mapper.product.KxsMachineTransferMapper;
import com.kxs.transfer.api.mapper.user.KxsUserMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import com.kxs.transfer.api.service.product.KxsMachineTransferService;
import com.kxs.user.api.model.KxsUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 客小爽机具划拨表(KxsMachineTransfer)表服务实现类
 *
 * @author 系统
 * @since 2023-12-20 10:09:35
 */
@Service("kxsMachineTransferService")
@RequiredArgsConstructor
public class KxsMachineTransferServiceImpl extends ServiceImpl<KxsMachineTransferMapper, KxsMachineTransfer> implements KxsMachineTransferService {

    private final KxsMachineMapper kxsMachineMapper;

    private final KxsUserMapper kxsUserMapper;
    @Override
    public void changeData(DMLData dmlData) {
        //修改的字段值
        Map<String, Object> validFieldDataMap = dmlData.getValidFieldDataMap();
        //全部字段值
        Map<String, FieldData> fieldDataMap = dmlData.getFieldDataMap();
        //修改的字段
        List<String> changeFieldList = dmlData.getChangeFieldList();
        //操作的主键
        int id = Integer.parseInt(dmlData.getId());
        if(fieldDataMap.get("TransType") == null){
            return;
        }
        if(fieldDataMap.get("TransType").getValue() == null){
            return;
        }
        //创客划拨
        int transType = Integer.parseInt(fieldDataMap.get("TransType").getValue().toString());
        if(transType != 11){
            return;
        }
        KxsMachineTransfer machineTransfer = new KxsMachineTransfer();
        machineTransfer.setId(id);

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

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

            switch (field) {
                case "FromUserId":
                    machineTransfer.setSendUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "ToUserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    KxsUser user = kxsUserMapper.selectById(userId);
                    if(user == null){
                        return;
                    }
                    machineTransfer.setToUserId(userId);
                    machineTransfer.setToUserName(user.getUsername());
                    machineTransfer.setToUserCode(user.getUserCode());
                    break;
                case "SnNo":
                    String sn = validFieldDataMap.get(field).toString();
                    KxsMachine kxsMachine = kxsMachineMapper.selectOne(Wrappers.<KxsMachine>lambdaQuery().eq(KxsMachine::getPosSn, sn));
                    if(kxsMachine == null){
                        return;
                    }
                    machineTransfer.setMachineSn(sn);
                    machineTransfer.setMachineId(kxsMachine.getId());
                    machineTransfer.setMachineType(kxsMachine.getMachineType());
                    break;
                case "BrandId":
                    machineTransfer.setBrandId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "CreateDate":
                    machineTransfer.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    machineTransfer.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsMachineTransfer kxsMachineTransfer = baseMapper.selectById(id);
            if(kxsMachineTransfer == null){
                baseMapper.insert(machineTransfer);
            }
        }
        if(operation.equals(OperationType.UPDATE)){
            baseMapper.updateById(machineTransfer);
        }
    }
}

