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

/**
 * 助力宝交易额统计(KxsZlbTrade)表服务实现类
 *
 * @author 系统
 * @since 2024-05-13 18:02:06
 */
@Service("kxsZlbTradeService")
public class KxsZlbTradeService extends ServiceImpl<KxsZlbTradeMapper, KxsZlbTrade> {

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

        KxsZlbTrade zlbTrade = new KxsZlbTrade();

        OperationType operation = dmlData.getOperation();

        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }
        if (fieldDataMap.get("UserId").getValue() != null) {
            zlbTrade.setUserId(Integer.valueOf(fieldDataMap.get("UserId").getValue().toString()));
        }
        if (fieldDataMap.get("TradeMonth").getValue() != null) {
            zlbTrade.setTradeMonth(Integer.valueOf(fieldDataMap.get("TradeMonth").getValue().toString()));
        }
        if (fieldDataMap.get("TradeDate").getValue() != null) {
            zlbTrade.setTradeDate(Integer.valueOf(fieldDataMap.get("TradeDate").getValue().toString()));
        }
        if (fieldDataMap.get("SeoTitle").getValue() != null) {
            String seoTitle = fieldDataMap.get("SeoTitle").getValue().toString();
            if ("self".equals(seoTitle)) {
                zlbTrade.setTotalType(0);
            } else {
                zlbTrade.setTotalType(1);
            }
        }
        if (fieldDataMap.get("TradeAmount").getValue() != null) {
            zlbTrade.setTradeAmt(new BigDecimal(fieldDataMap.get("TradeAmount").getValue().toString()));
        }




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

