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.stat.api.model.KxsUserActTrade;
import com.kxs.stat.api.model.KxsUserTrade;
import com.kxs.transfer.api.mapper.stat.KxsUserActTradeMapper;
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;

/**
 * 创客激活统计表(KxsUserActTrade)表服务实现类
 *
 * @author 系统
 * @since 2024-05-07 17:25:06
 */
@Service("kxsUserActTradeService")
public class KxsUserActTradeService extends ServiceImpl<KxsUserActTradeMapper, KxsUserActTrade> {

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

        KxsUserActTrade userActTrade = new KxsUserActTrade();

        if (fieldDataMap.get("UserId").getValue() != null) {
            userActTrade.setUserId(Integer.valueOf(fieldDataMap.get("UserId").getValue().toString()));
        }
        if (fieldDataMap.get("SeoKeyword").getValue() != null) {
            userActTrade.setTradeDate(Integer.valueOf(fieldDataMap.get("SeoKeyword").getValue().toString()));
        }
        if (fieldDataMap.get("TradeMonth").getValue() != null) {
            userActTrade.setTradeMonth(Integer.valueOf(fieldDataMap.get("TradeMonth").getValue().toString()));
        }
        if (fieldDataMap.get("BrandId").getValue() != null) {
            userActTrade.setBrandId(Integer.valueOf(fieldDataMap.get("BrandId").getValue().toString()));
        }
        if (fieldDataMap.get("ActiveBuddyMerStatus").getValue() != null) {
            userActTrade.setActNum(Integer.valueOf(fieldDataMap.get("ActiveBuddyMerStatus").getValue().toString()));
        }
        if (fieldDataMap.get("SeoTitle").getValue() != null) {
            String seoTitle = fieldDataMap.get("SeoTitle").getValue().toString();
            if ("self".equals(seoTitle)) {
                userActTrade.setTotalType(0);
            } else {
                userActTrade.setTotalType(1);
            }
        }
        KxsUserActTrade kxsUserActTrade = baseMapper.selectOne(Wrappers.<KxsUserActTrade>lambdaQuery()
                .eq(KxsUserActTrade::getUserId, userActTrade.getUserId())
                .eq(KxsUserActTrade::getBrandId, userActTrade.getBrandId())
                .eq(KxsUserActTrade::getTotalType, userActTrade.getTotalType())
                .eq(KxsUserActTrade::getTradeMonth, userActTrade.getTradeMonth())
                .eq(KxsUserActTrade::getTradeDate, userActTrade.getTradeDate()));
        if(kxsUserActTrade == null){
            baseMapper.insert(userActTrade);
        }else{
            userActTrade.setId(kxsUserActTrade.getId());
            baseMapper.updateById(userActTrade);
        }
    }
}

