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.KxsWarehouseLimitLog;
import com.kxs.store.api.model.KxsWarehouseLimitSmallLog;
import com.kxs.transfer.api.mapper.store.KxsWarehouseLimitLogMapper;
import com.kxs.transfer.api.mapper.store.KxsWarehouseLimitSmallLogMapper;
import com.kxs.transfer.api.model.table.DMLData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽仓库额度变动记录(KxsWarehouseLimitLog)表服务实现类
 *
 * @author 系统
 * @since 2024-04-16 14:50:16
 */
@Service("kxsWarehouseLimitLogService")
@RequiredArgsConstructor
public class KxsWarehouseLimitLogService extends ServiceImpl<KxsWarehouseLimitLogMapper, KxsWarehouseLimitLog> {

    private final KxsWarehouseLimitSmallLogMapper kxsWarehouseLimitSmallLogMapper;
    public void changeData(DMLData dmlData) {


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

        KxsWarehouseLimitLog warehouseLimitLog = new KxsWarehouseLimitLog();
        warehouseLimitLog.setId(id);

        OperationType operation = dmlData.getOperation();

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

            switch (field) {
                case "UserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    warehouseLimitLog.setUserId(userId);
                    break;
                case "UseAmount":
                    warehouseLimitLog.setUseAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeAmount":
                    warehouseLimitLog.setBeforeAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterAmount":
                    warehouseLimitLog.setAfterAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AmountType":
                    warehouseLimitLog.setChangeType(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "QueryCount":
                    warehouseLimitLog.setOrderId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Sort":
                    warehouseLimitLog.setLimitType(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "PayMode":
                    warehouseLimitLog.setPayMode(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "OperateType":
                    int kind = Integer.parseInt(validFieldDataMap.get(field).toString());
                    warehouseLimitLog.setKind(kind == 0 ? "sub" : "add");
                    break;
                case "CreateDate":
                    warehouseLimitLog.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    warehouseLimitLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsWarehouseLimitLog kxsWarehouseLimitLog = baseMapper.selectById(id);
            if(kxsWarehouseLimitLog == null){
                baseMapper.insert(warehouseLimitLog);
            }
        }
        if(operation.equals(OperationType.UPDATE)){

            baseMapper.updateById(warehouseLimitLog);
        }
    }

    public void changeSmallData(DMLData dmlData) {


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

        KxsWarehouseLimitSmallLog warehouseLimitLog = new KxsWarehouseLimitSmallLog();
        warehouseLimitLog.setId(id);

        OperationType operation = dmlData.getOperation();

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

            switch (field) {
                case "UserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    warehouseLimitLog.setUserId(userId);
                    break;
                case "UseAmount":
                    warehouseLimitLog.setUseAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "BeforeAmount":
                    warehouseLimitLog.setBeforeAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AfterAmount":
                    warehouseLimitLog.setAfterAmount(new BigDecimal(validFieldDataMap.get(field).toString()));
                    break;
                case "AmountType":
                    warehouseLimitLog.setChangeType(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "QueryCount":
                    warehouseLimitLog.setOrderId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Sort":
                    warehouseLimitLog.setLimitType(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "PayMode":
                    warehouseLimitLog.setPayMode(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "OperateType":
                    int kind = Integer.parseInt(validFieldDataMap.get(field).toString());
                    warehouseLimitLog.setKind(kind == 0 ? "sub" : "add");
                    break;
                case "CreateDate":
                    warehouseLimitLog.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    warehouseLimitLog.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsWarehouseLimitSmallLog kxsWarehouseLimitLog = kxsWarehouseLimitSmallLogMapper.selectById(id);
            if(kxsWarehouseLimitLog == null){
                kxsWarehouseLimitSmallLogMapper.insert(warehouseLimitLog);
            }
        }
        if(operation.equals(OperationType.UPDATE)){

            kxsWarehouseLimitSmallLogMapper.updateById(warehouseLimitLog);
        }
    }
}

