package com.kxs.transfer.api.service.store.impl;

import cn.hutool.core.bean.BeanUtil;
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.store.api.model.*;
import com.kxs.transfer.api.mapper.product.KxsBrandMapper;
import com.kxs.transfer.api.mapper.store.KxsMachineAdvanceInfoMapper;
import com.kxs.transfer.api.mapper.store.KxsMachineAdvanceMapper;
import com.kxs.transfer.api.mapper.store.KxsMachineApplyDetailMapper;
import com.kxs.transfer.api.mapper.store.KxsWarehouseMapper;
import com.kxs.transfer.api.mapper.user.KxsUserAdvanceMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import com.kxs.user.api.model.KxsUser;
import com.kxs.user.api.model.KxsUserAdvance;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽预发机表(KxsMachineAdvance)表服务实现类
 *
 * @author 系统
 * @since 2024-01-08 09:46:02
 */
@Service("kxsMachineAdvanceService")
@RequiredArgsConstructor
public class KxsMachineAdvanceService extends ServiceImpl<KxsMachineAdvanceMapper, KxsMachineAdvance>  {

    private final KxsMachineAdvanceInfoMapper kxsMachineAdvanceInfoMapper;

    private final KxsBrandMapper kxsBrandMapper;

    private final KxsWarehouseMapper kxsWarehouseMapper;

    private final KxsUserAdvanceMapper kxsUserAdvanceMapper;

    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());

        KxsMachineAdvance machineAdvance = new KxsMachineAdvance();

        KxsMachineAdvanceInfo machineAdvanceInfo = new KxsMachineAdvanceInfo();

        //操作类型
        OperationType operation = dmlData.getOperation();
        if (operation.equals(OperationType.DELETE)) {
            baseMapper.deleteById(id);
            kxsMachineAdvanceInfoMapper.delete(Wrappers.<KxsMachineAdvanceInfo>lambdaQuery().eq(KxsMachineAdvanceInfo::getOrderId, id));
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "ToUserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    machineAdvance.setToUserId(userId);
                    break;
                case "FromUserId":
                    machineAdvance.setSendUserId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "BrandId":
                    KxsBrand brand = kxsBrandMapper.selectById(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    machineAdvance.setBrandId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    machineAdvance.setBrandName(brand.getName());
                    break;
                case "ProductName":
                    machineAdvance.setBrandName(validFieldDataMap.get(field).toString());
                    break;
                case "FromStoreId":
                    Integer storeId = Integer.valueOf(validFieldDataMap.get(field).toString());
                    KxsWarehouse warehouse = kxsWarehouseMapper.selectById(storeId);
                    machineAdvance.setWarehouseId(storeId);
                    machineAdvance.setWarehouseName(warehouse.getWarehouseName());
                    break;
                case "SnId":
                    machineAdvanceInfo.setMachineId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "ApplyFlag":
                    machineAdvanceInfo.setIsApply(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "SnNo":
                    machineAdvanceInfo.setPosSn(validFieldDataMap.get(field).toString());
                    break;
                case "AuthFlag":
                    machineAdvanceInfo.setIsUseAmount(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "WithholdFlag":
                    machineAdvanceInfo.setIsAdvance(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    KxsUserAdvance advance = kxsUserAdvanceMapper.selectOne(Wrappers.<KxsUserAdvance>lambdaQuery().eq(KxsUserAdvance::getExtField, machineAdvanceInfo.getPosSn()));
                    if(advance != null){
                        machineAdvanceInfo.setAdvanceId(advance.getId());
                    }

                    break;
                case "CancelFlag":
                    machineAdvanceInfo.setIsReturn(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "CancelDate":
                    machineAdvanceInfo.setReturnTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "Status":
                    int status = Integer.parseInt(validFieldDataMap.get(field).toString());
                    machineAdvanceInfo.setIsConfirm(status == -1 ? 2 : status);
                    break;
                case "CreateDate":
                    machineAdvance.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    machineAdvanceInfo.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));

                    break;
                case "UpdateDate":
                    machineAdvance.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    machineAdvanceInfo.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "ApplyDate":
                    machineAdvanceInfo.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;

            }
        }
        if (operation.equals(OperationType.UPDATE)) {
            if(BeanUtil.isNotEmpty(machineAdvance, "entityClass")){
                machineAdvance.setId(id);
                baseMapper.updateById(machineAdvance);
            }
            if(BeanUtil.isNotEmpty(machineAdvanceInfo, "entityClass")){
                machineAdvanceInfo.setId(id);
                machineAdvanceInfo.setOrderId(id);
                kxsMachineAdvanceInfoMapper.updateById(machineAdvanceInfo);
            }
        }
        if (operation.equals(OperationType.INSERT)) {
            KxsMachineAdvance kxsMachineAdvance = baseMapper.selectById(id);
            if (kxsMachineAdvance == null) {
                machineAdvance.setId(id);
                baseMapper.insert(machineAdvance);

                machineAdvanceInfo.setId(id);
                machineAdvanceInfo.setOrderId(id);
                kxsMachineAdvanceInfoMapper.insert(machineAdvanceInfo);

            }
        }
    }
}

