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.KxsUserNewTrade;
import com.kxs.stat.api.model.KxsZlbTrade;
import com.kxs.transfer.api.mapper.stat.KxsUserNewTradeMapper;
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;

/**
 * 创客新增统计表(KxsUserNewTrade)表服务实现类
 *
 * @author 系统
 * @since 2024-05-16 11:07:42
 */
@Service("kxsUserNewTradeService")
public class KxsUserNewTradeService extends ServiceImpl<KxsUserNewTradeMapper, KxsUserNewTrade> {

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

        KxsUserNewTrade userNewTrade = new KxsUserNewTrade();

        OperationType operation = dmlData.getOperation();

        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }
        if (fieldDataMap.get("UserId").getValue() != null) {
            userNewTrade.setUserId(Integer.valueOf(fieldDataMap.get("UserId").getValue().toString()));
        }
        if (fieldDataMap.get("StatMonth").getValue() != null) {
            userNewTrade.setTradeMonth(Integer.valueOf(fieldDataMap.get("StatMonth").getValue().toString()));
        }
        if (fieldDataMap.get("StatDate").getValue() != null) {
            userNewTrade.setTradeDate(Integer.valueOf(fieldDataMap.get("StatDate").getValue().toString()));
        }
        if (fieldDataMap.get("SeoTitle").getValue() != null) {
            String seoTitle = fieldDataMap.get("SeoTitle").getValue().toString();
            if ("self".equals(seoTitle)) {
                userNewTrade.setTotalType(0);
            } else {
                userNewTrade.setTotalType(1);
            }
        }
        if (fieldDataMap.get("RecUserAuthNum").getValue() != null) {
            userNewTrade.setNewCount(Integer.valueOf(fieldDataMap.get("RecUserAuthNum").getValue().toString()));
        }




        KxsUserNewTrade kxsZlbTrade = baseMapper.selectOne(Wrappers.<KxsUserNewTrade>lambdaQuery()
                .eq(KxsUserNewTrade::getUserId, userNewTrade.getUserId())
                .eq(KxsUserNewTrade::getTotalType, userNewTrade.getTotalType())
                .eq(KxsUserNewTrade::getTradeMonth, userNewTrade.getTradeMonth())
                .eq(KxsUserNewTrade::getTradeDate, userNewTrade.getTradeDate()));
        if(kxsZlbTrade == null){
            baseMapper.insert(userNewTrade);
        }else{
            userNewTrade.setId(kxsZlbTrade.getId());
            baseMapper.updateById(userNewTrade);
        }
    }
}

