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

import com.aliyun.dts.subscribe.clients.record.OperationType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.store.api.model.KxsActTotal;
import com.kxs.transfer.api.mapper.store.KxsActTotalMapper;
import com.kxs.transfer.api.model.table.DMLData;
import org.springframework.stereotype.Service;

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

/**
 * 客小爽仓库激活统计表(KxsActTotal)表服务实现类
 *
 * @author 系统
 * @since 2024-04-16 15:35:17
 */
@Service("kxsActTotalService")
public class KxsActTotalService extends ServiceImpl<KxsActTotalMapper, KxsActTotal>  {

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

        KxsActTotal actTotal = new KxsActTotal();
        actTotal.setId(id);

        OperationType operation = dmlData.getOperation();

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

            switch (field) {
                case "StoreId":
                    actTotal.setWarehouseId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "BrandId":
                    actTotal.setBrandId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "ActivateNum":
                    actTotal.setTotalNum(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "TradeMonth":
                    actTotal.setTotalMonth(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "TradeDate":
                    actTotal.setTotalDate(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsActTotal kxsActTotal = baseMapper.selectById(id);
            if(kxsActTotal == null){
                baseMapper.insert(actTotal);
            }
        }
        if(operation.equals(OperationType.UPDATE)){
            baseMapper.updateById(actTotal);
        }
    }
}

