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

/**
 * 广电话费统计表(KxsUserGdTrade)表服务实现类
 *
 * @author 系统
 * @since 2024-05-13 15:21:35
 */
@Service("kxsUserGdTradeService")
public class KxsUserGdTradeService extends ServiceImpl<KxsUserGdTradeMapper, KxsUserGdTrade> {

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

        KxsUserGdTrade userGdTrade = new KxsUserGdTrade();

        OperationType operation = dmlData.getOperation();

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



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

