package com.kxs.transfer.api.service.store.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.KxsBrand;
import com.kxs.product.api.model.KxsMachine;
import com.kxs.store.api.model.KxsMachineRecycle;
import com.kxs.store.api.model.KxsMachineRecycleInfo;
import com.kxs.store.api.model.KxsWarehouse;
import com.kxs.transfer.api.mapper.product.KxsBrandMapper;
import com.kxs.transfer.api.mapper.product.KxsMachineMapper;
import com.kxs.transfer.api.mapper.store.KxsMachineRecycleInfoMapper;
import com.kxs.transfer.api.mapper.store.KxsMachineRecycleMapper;
import com.kxs.transfer.api.mapper.store.KxsWarehouseMapper;
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.store.KxsMachineRecycleService;
import com.kxs.user.api.model.KxsUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽仓库机具回收申请表(KxsMachineRecycle)表服务实现类
 *
 * @author 系统
 * @since 2024-01-08 09:46:02
 */
@Service("kxsMachineRecycleService")
@RequiredArgsConstructor
public class KxsMachineRecycleServiceImpl extends ServiceImpl<KxsMachineRecycleMapper, KxsMachineRecycle> implements KxsMachineRecycleService {

    private final KxsBrandMapper kxsBrandMapper;
    private final KxsUserMapper kxsUserMapper;
    private final KxsWarehouseMapper kxsWarehouseMapper;
    private final KxsMachineMapper kxsMachineMapper;

    private final KxsMachineRecycleInfoMapper kxsMachineRecycleInfoMapper;



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

        KxsMachineRecycle machineRecycle = new KxsMachineRecycle();
        machineRecycle.setId(id);

        //操作类型
        OperationType operation = dmlData.getOperation();
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    KxsUser user = kxsUserMapper.selectById(userId);
                    if (user != null) {
                        machineRecycle.setUsername(user.getUsername());
                        machineRecycle.setPhone(user.getPhone());
                        machineRecycle.setUserCode(user.getUserCode());
                    }
                    machineRecycle.setUserId(userId);
                    break;
                case "RecycStoreId":
                    int storeId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    KxsWarehouse warehouse = kxsWarehouseMapper.selectById(storeId);
                    if (warehouse != null) {
                        machineRecycle.setWarehouseUserId(warehouse.getUserId());
                    }
                    machineRecycle.setWarehouseId(storeId);
                    break;
                case "PostType":
                    int posType = Integer.parseInt(validFieldDataMap.get(field).toString());
                    if (posType == 2) {
                        machineRecycle.setGetMode(0);
                    } else {
                        machineRecycle.setGetMode(1);
                    }
                    break;
                case "RecycOrderNo":
                    machineRecycle.setOrderSn(validFieldDataMap.get(field).toString());
                    break;
                case "ErpCode":
                    machineRecycle.setTrackNo(validFieldDataMap.get(field).toString());
                    break;
                case "BrandId":
                    Integer brandId = Integer.valueOf(validFieldDataMap.get(field).toString());
                    KxsBrand brand = kxsBrandMapper.selectById(brandId);
                    if (brand != null) {
                        machineRecycle.setBrandName(brand.getName());
                    }
                    machineRecycle.setBrandId(brandId);
                    break;
                case "CreateDate":
                    machineRecycle.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    machineRecycle.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        KxsMachineRecycle kxsMachineRecycle = baseMapper.selectById(machineRecycle.getId());
        if(operation.equals(OperationType.INSERT)){
            if(kxsMachineRecycle == null){
                baseMapper.insert(machineRecycle);
            }
        }

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

            baseMapper.updateById(machineRecycle);
        }


    }

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

        KxsMachineRecycleInfo machineRecycle = new KxsMachineRecycleInfo();
        machineRecycle.setId(id);
        OperationType operation = dmlData.getOperation();

        for (String field : changeFieldList) {

            switch (field) {
                case "SnNo":
                    KxsMachine machine = kxsMachineMapper.selectOne(Wrappers.<KxsMachine>lambdaQuery().eq(KxsMachine::getPosSn, validFieldDataMap.get(field).toString()));
                    if (machine != null) {
                        machineRecycle.setRecycleEndTime(machine.getRecycleEndTime());
                    }
                    machineRecycle.setPosSn(validFieldDataMap.get(field).toString());
                    break;
                case "OrderId":
                    machineRecycle.setOrderId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "BrandId":
                    machineRecycle.setBrandId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "ResultDate":
                    machineRecycle.setConfirmTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "CancelDate":
                    machineRecycle.setCancelTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "Status":
                    int status = Integer.parseInt(validFieldDataMap.get(field).toString());
                    if (status == -1) {
                        machineRecycle.setStatus(3);
                    } else {
                        machineRecycle.setStatus(status);
                    }
                    break;
                case "CreateDate":
                    machineRecycle.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    machineRecycle.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        KxsMachineRecycleInfo kxsMachineRecycle = kxsMachineRecycleInfoMapper.selectById(machineRecycle.getId());
        if(operation.equals(OperationType.INSERT)){
            if(kxsMachineRecycle == null){
                kxsMachineRecycleInfoMapper.insert(machineRecycle);
            }
        }

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

            kxsMachineRecycleInfoMapper.updateById(machineRecycle);
        }

    }
}


