package com.kxs.transfer.api.service.stat;

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.common.mybatis.handler.MonthTableNameHandler;
import com.kxs.product.api.model.KxsMerchant;
import com.kxs.stat.api.model.KxsMerchantTrade;
import com.kxs.transfer.api.mapper.product.KxsMerchantMapper;
import com.kxs.transfer.api.mapper.stat.KxsMerchantTradeMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 商户交易统计总表(KxsMerchantTrade)表服务实现类
 *
 * @author 系统
 * @since 2024-05-07 17:25:06
 */
@Service("kxsMerchantTradeService")
@RequiredArgsConstructor
public class KxsMerchantTradeService extends ServiceImpl<KxsMerchantTradeMapper, KxsMerchantTrade>{

    private final KxsMerchantMapper kxsMerchantMapper;

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

        KxsMerchantTrade merchantTrade = new KxsMerchantTrade();
        String tradeMonth;
        if (fieldDataMap.get("TradeMonth") != null && fieldDataMap.get("TradeMonth").getValue() != null) {
            tradeMonth = fieldDataMap.get("TradeMonth").getValue().toString();
        } else {
            tradeMonth = MonthTableNameHandler.getThisMoth();
        }
        //每个请求线程维护一个month数据，避免多线程数据冲突
        MonthTableNameHandler.setData(tradeMonth);

        OperationType operation = dmlData.getOperation();

        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }
        if (fieldDataMap.get("MerchantId").getValue() != null) {
            merchantTrade.setMerchantId(Integer.valueOf(fieldDataMap.get("MerchantId").getValue().toString()));
        }
        if (fieldDataMap.get("TradeDate").getValue() != null) {
            merchantTrade.setTradeDate(Integer.valueOf(fieldDataMap.get("TradeDate").getValue().toString()));
        }
        if (fieldDataMap.get("TradeMonth").getValue() != null) {
            merchantTrade.setTradeMonth(Integer.valueOf(fieldDataMap.get("TradeMonth").getValue().toString()));
        }
        if (fieldDataMap.get("BrandId").getValue() != null) {
            merchantTrade.setBrandId(Integer.valueOf(fieldDataMap.get("BrandId").getValue().toString()));
        }
        if (fieldDataMap.get("TradeAmount").getValue() != null) {
            merchantTrade.setTradeAmt(new BigDecimal(fieldDataMap.get("TradeAmount").getValue().toString()));
        }

        KxsMerchantTrade kxsUserActTrade = baseMapper.selectOne(Wrappers.<KxsMerchantTrade>lambdaQuery()
                .eq(KxsMerchantTrade::getMerchantId, merchantTrade.getMerchantId())
                .eq(KxsMerchantTrade::getBrandId, merchantTrade.getBrandId())
                .eq(KxsMerchantTrade::getTradeMonth, merchantTrade.getTradeMonth())
                .eq(KxsMerchantTrade::getTradeDate, merchantTrade.getTradeDate()));
        if(kxsUserActTrade == null){
            baseMapper.insert(merchantTrade);
        }else{
            merchantTrade.setId(kxsUserActTrade.getId());
            baseMapper.updateById(merchantTrade);
        }
        //删除线程，避免线程污染
        MonthTableNameHandler.removeData();

        //添加总交易额
        KxsMerchant kxsMerchant = kxsMerchantMapper.selectById(merchantTrade.getMerchantId());
        if(kxsMerchant != null){
            kxsMerchant.setTotalAmt(kxsMerchant.getTotalAmt().add(merchantTrade.getTradeAmt()));
            kxsMerchantMapper.updateById(kxsMerchant);
        }
    }
}

