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.extension.service.impl.ServiceImpl;
import com.kxs.store.api.model.KxsWarehouseStockLog;
import com.kxs.transfer.api.mapper.store.KxsWarehouseStockLogMapper;
import com.kxs.transfer.api.model.table.DMLData;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽仓库库存变动记录表(KxsWarehouseStockLog)表服务实现类
 *
 * @author 系统
 * @since 2024-01-08 09:46:02
 */
@Service("kxsWarehouseStockLogService")
public class KxsWarehouseStockLogService extends ServiceImpl<KxsWarehouseStockLogMapper, KxsWarehouseStockLog> {

    public void changeData(DMLData dmlData) {

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

        KxsWarehouseStockLog warehouseStockLog = new KxsWarehouseStockLog();
        warehouseStockLog.setId(id);

        OperationType operation = dmlData.getOperation();

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

            switch (field) {
                case "StoreId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    warehouseStockLog.setWarehouseId(userId);
                    break;
                case "BrandId":
                    warehouseStockLog.setBrandId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "OpStoreNum":
                    warehouseStockLog.setChangeNum(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeTotalNum":
                    warehouseStockLog.setBeforeStockTotal(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterTotalNum":
                    warehouseStockLog.setAfterStockTotal(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeLaveNum":
                    warehouseStockLog.setBeforeStockNum(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterLaveNum":
                    warehouseStockLog.setAfterStockNum(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeOutNum":
                    warehouseStockLog.setBeforeDeliveryNum(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterOutNum":
                    warehouseStockLog.setAfterDeliveryNum(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "TransType":
                    warehouseStockLog.setChangeType(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "OpSymbol":
                    String sub = validFieldDataMap.get(field).toString();
                    warehouseStockLog.setKind("+".equals(sub) ? "add" : "sub");
                    break;
                case "CreateDate":
                    warehouseStockLog.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    warehouseStockLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsWarehouseStockLog kxsWarehouseStockLog = baseMapper.selectById(id);
            if(kxsWarehouseStockLog == null){
                baseMapper.insert(warehouseStockLog);
            }
        }
        if(operation.equals(OperationType.UPDATE)){

            baseMapper.updateById(warehouseStockLog);
        }
    }
}

