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.stat.api.model.KxsUserTrade;
import com.kxs.transfer.api.mapper.stat.KxsUserTradeMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.model.table.FieldData;
import org.springframework.stereotype.Service;

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

/**
 * 个人交易统计总表(KxsUserTrade)表服务实现类
 *
 * @author 系统
 * @since 2024-05-07 17:25:06
 */
@Service("kxsUserTradeService")
public class KxsUserTradeService extends ServiceImpl<KxsUserTradeMapper, KxsUserTrade> {

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

        KxsUserTrade userTrade = new KxsUserTrade();

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

        if (fieldDataMap.get("UserId").getValue() != null) {
            userTrade.setUserId(Integer.valueOf(fieldDataMap.get("UserId").getValue().toString()));
        }
        if (fieldDataMap.get("TradeDate").getValue() != null) {
            userTrade.setTradeDate(Integer.valueOf(fieldDataMap.get("TradeDate").getValue().toString()));
        }
        if (fieldDataMap.get("TradeMonth").getValue() != null) {
            userTrade.setTradeMonth(Integer.valueOf(fieldDataMap.get("TradeMonth").getValue().toString()));
        }
        if (fieldDataMap.get("BrandId").getValue() != null) {
            userTrade.setBrandId(Integer.valueOf(fieldDataMap.get("BrandId").getValue().toString()));
        }
        if (fieldDataMap.get("VipFlag").getValue() != null) {
            userTrade.setVipFlag(Integer.valueOf(fieldDataMap.get("VipFlag").getValue().toString()));
        }
        if (fieldDataMap.get("SeoTitle").getValue() != null) {
            String seoTitle = fieldDataMap.get("SeoTitle").getValue().toString();
            if ("self".equals(seoTitle)) {
                userTrade.setTotalType(0);
                userTrade.setHelpDirectTradeAmt(new BigDecimal(fieldDataMap.get("HelpDirectTradeAmt").getValue().toString()));
                userTrade.setHelpDebitTradeAmt(new BigDecimal(fieldDataMap.get("HelpDirectDebitTradeAmt").getValue().toString()));
                userTrade.setProDirectTradeAmt(new BigDecimal(fieldDataMap.get("NotHelpDirectTradeAmt").getValue().toString()));
                userTrade.setProDebitTradeAmt(new BigDecimal(fieldDataMap.get("NotHelpDirectDebitTradeAmt").getValue().toString()));
                userTrade.setProADirectTradeAmt(new BigDecimal(fieldDataMap.get("ProfitDirectTradeAmt").getValue().toString()));
                userTrade.setProADebitTradeAmt(new BigDecimal(fieldDataMap.get("ProfitNonDirectDebitTradeAmt").getValue().toString()));
                userTrade.setProADebitCapTradeAmt(new BigDecimal(fieldDataMap.get("ProfitDirectDebitCapTradeAmt").getValue().toString()));
            } else {
                userTrade.setTotalType(1);
                userTrade.setHelpDirectTradeAmt(new BigDecimal(fieldDataMap.get("HelpNonDirectTradeAmt").getValue().toString()));
                userTrade.setHelpDebitTradeAmt(new BigDecimal(fieldDataMap.get("HelpNonDirectDebitTradeAmt").getValue().toString()));
                userTrade.setProDirectTradeAmt(new BigDecimal(fieldDataMap.get("NotHelpNonDirectTradeAmt").getValue().toString()));
                userTrade.setProDebitTradeAmt(new BigDecimal(fieldDataMap.get("NotHelpNonDirectDebitTradeAmt").getValue().toString()));
                userTrade.setProADirectTradeAmt(new BigDecimal(fieldDataMap.get("ProfitNonDirectTradeAmt").getValue().toString()));
                userTrade.setProADebitTradeAmt(new BigDecimal(fieldDataMap.get("ProfitNonDirectDebitTradeAmt").getValue().toString()));
                userTrade.setProADebitCapTradeAmt(new BigDecimal(fieldDataMap.get("ProfitNonDirectDebitTradeAmt").getValue().toString()));
            }
        }
        if (fieldDataMap.get("QueryCount").getValue() != null) {
            userTrade.setTradeType(Integer.valueOf(fieldDataMap.get("QueryCount").getValue().toString()));
        }
        if (fieldDataMap.get("HelpDirectDebitCapTradeAmt").getValue() != null) {
            userTrade.setHelpDebitCapTradeAmt(new BigDecimal(fieldDataMap.get("HelpDirectDebitCapTradeAmt").getValue().toString()));
        }
        if (fieldDataMap.get("NotHelpDirectDebitCapTradeAmt").getValue() != null) {
            userTrade.setProDebitCapTradeAmt(new BigDecimal(fieldDataMap.get("NotHelpDirectDebitCapTradeAmt").getValue().toString()));
        }
        if (fieldDataMap.get("HelpDirectDebitCapNum").getValue() != null) {
            userTrade.setHelpDebitCapNum(new BigDecimal(fieldDataMap.get("HelpDirectDebitCapNum").getValue().toString()).intValue());
        }
        if (fieldDataMap.get("NotHelpDirectDebitCapNum").getValue() != null) {
            userTrade.setProDebitCapNum(new BigDecimal(fieldDataMap.get("NotHelpDirectDebitCapNum").getValue().toString()).intValue());
        }
        if (fieldDataMap.get("ProfitDirectDebitCapNum").getValue() != null) {
            userTrade.setProADebitCapNum(new BigDecimal(fieldDataMap.get("ProfitDirectDebitCapNum").getValue().toString()).intValue());
        }

        KxsUserTrade kxsUserTrade = baseMapper.selectOne(Wrappers.<KxsUserTrade>lambdaQuery()
                .eq(KxsUserTrade::getUserId, userTrade.getUserId())
                .eq(KxsUserTrade::getBrandId, userTrade.getBrandId())
                .eq(KxsUserTrade::getTradeType, userTrade.getTradeType())
                .eq(KxsUserTrade::getTotalType, userTrade.getTotalType())
                .eq(KxsUserTrade::getTradeMonth, userTrade.getTradeMonth())
                .eq(KxsUserTrade::getTradeDate, userTrade.getTradeDate())
                .eq(KxsUserTrade::getVipFlag, userTrade.getVipFlag()));
        if (kxsUserTrade == null) {
            // 插入数据
            baseMapper.insert(userTrade);
        } else {
            userTrade.setId(kxsUserTrade.getId());
            baseMapper.updateById(userTrade);
        }

        //删除线程，避免线程污染
        MonthTableNameHandler.removeData();

    }

    public void change2Data(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());

        KxsUserTrade userTrade = new KxsUserTrade();

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

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

        if (fieldDataMap.get("SeoTitle").getValue() != null) {
            String seoTitle = fieldDataMap.get("SeoTitle").getValue().toString();
            if ("self".equals(seoTitle)) {
                userTrade.setTotalType(0);
            }else{
                userTrade.setTotalType(1);
            }
        }
        if (fieldDataMap.get("QueryCount").getValue() != null) {
            userTrade.setTradeType(Integer.valueOf(fieldDataMap.get("QueryCount").getValue().toString()));
        }
        if (fieldDataMap.get("UserId").getValue() != null) {
            userTrade.setUserId(Integer.valueOf(fieldDataMap.get("UserId").getValue().toString()));
        }
        if (fieldDataMap.get("TradeDate").getValue() != null) {
            userTrade.setTradeDate(Integer.valueOf(fieldDataMap.get("TradeDate").getValue().toString()));
        }
        if (fieldDataMap.get("TradeMonth").getValue() != null) {
            userTrade.setTradeMonth(Integer.valueOf(fieldDataMap.get("TradeMonth").getValue().toString()));
        }
        if (fieldDataMap.get("BrandId").getValue() != null) {
            userTrade.setBrandId(Integer.valueOf(fieldDataMap.get("BrandId").getValue().toString()));
        }
        if (fieldDataMap.get("VipFlag").getValue() != null) {
            userTrade.setVipFlag(Integer.valueOf(fieldDataMap.get("VipFlag").getValue().toString()));
        }
        if (fieldDataMap.get("ProfitTradeAmt").getValue() != null) {
            userTrade.setProBDirectTradeAmt(new BigDecimal(fieldDataMap.get("ProfitTradeAmt").getValue().toString()));
        }
        if (fieldDataMap.get("ProfitDebitTradeAmt").getValue() != null) {
            userTrade.setProBDebitTradeAmt(new BigDecimal(fieldDataMap.get("ProfitDebitTradeAmt").getValue().toString()));
        }
        if (fieldDataMap.get("ProfitDebitCapTradeAmt").getValue() != null) {
            userTrade.setProBDebitCapTradeAmt(new BigDecimal(fieldDataMap.get("ProfitDebitCapTradeAmt").getValue().toString()));
        }
        if (fieldDataMap.get("ProfitDebitCapNum").getValue() != null) {
            userTrade.setProBDebitCapNum(new BigDecimal(fieldDataMap.get("ProfitDebitCapNum").getValue().toString()).intValue());
        }

        KxsUserTrade kxsUserTrade = baseMapper.selectOne(Wrappers.<KxsUserTrade>lambdaQuery()
                .eq(KxsUserTrade::getUserId, userTrade.getUserId())
                .eq(KxsUserTrade::getBrandId, userTrade.getBrandId())
                .eq(KxsUserTrade::getTradeType, userTrade.getTradeType())
                .eq(KxsUserTrade::getTotalType, userTrade.getTotalType())
                .eq(KxsUserTrade::getTradeMonth, userTrade.getTradeMonth())
                .eq(KxsUserTrade::getTradeDate, userTrade.getTradeDate())
                .eq(KxsUserTrade::getVipFlag, userTrade.getVipFlag()));
        if (kxsUserTrade == null) {
            // 插入数据
            baseMapper.insert(userTrade);
        } else {
            userTrade.setId(kxsUserTrade.getId());
            baseMapper.updateById(userTrade);
        }

        //删除线程，避免线程污染
        MonthTableNameHandler.removeData();
    }
}




















