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

import java.util.List;
import java.util.Map;

/**
 * 创客广电注销统计表(KxsUserLogoutTrade)表服务实现类
 *
 * @author 系统
 * @since 2024-05-13 11:35:06
 */
@Service("kxsUserLogoutTradeService")
public class KxsUserLogoutTradeService extends ServiceImpl<KxsUserLogoutTradeMapper, KxsUserLogoutTrade>  {

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

        KxsUserLogoutTrade logoutTrade = new KxsUserLogoutTrade();

        OperationType operation = dmlData.getOperation();

        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }
        if (fieldDataMap.get("UserId").getValue() != null) {
            logoutTrade.setUserId(Integer.valueOf(fieldDataMap.get("UserId").getValue().toString()));
        }
        if (fieldDataMap.get("Kind").getValue() != null) {
            logoutTrade.setTotalType(Integer.valueOf(fieldDataMap.get("Kind").getValue().toString()));
        }
        if (fieldDataMap.get("ActCount").getValue() != null) {
            logoutTrade.setLogoutNum(Integer.valueOf(fieldDataMap.get("ActCount").getValue().toString()));
        }


        KxsUserLogoutTrade kxsUserLogoutTrade = baseMapper.selectOne(Wrappers.<KxsUserLogoutTrade>lambdaQuery()
                .eq(KxsUserLogoutTrade::getUserId, logoutTrade.getUserId())
                .eq(KxsUserLogoutTrade::getTotalType, logoutTrade.getTotalType()));
        if(kxsUserLogoutTrade == null){
            baseMapper.insert(logoutTrade);
        }else{
            logoutTrade.setId(kxsUserLogoutTrade.getId());
            baseMapper.updateById(logoutTrade);
        }
    }
}

