package com.kxs.transfer.api.service.user.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.aliyun.dts.subscribe.clients.record.OperationType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.transfer.api.mapper.user.KxsLeaderMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.user.KxsLeaderService;
import com.kxs.user.api.model.KxsLeader;
import org.springframework.stereotype.Service;

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

/**
 * 盟主表(KxsLeader)表服务实现类
 *
 * @author 系统
 * @since 2024-02-22 15:39:26
 */
@Service("kxsLeaderService")
public class KxsLeaderServiceImpl extends ServiceImpl<KxsLeaderMapper, KxsLeader> implements KxsLeaderService {

    @Override
    public void changeData(DMLData dmlData) {
        //修改的字段值
        Map<String, Object> validFieldDataMap = dmlData.getValidFieldDataMap();
        //修改的字段
        List<String> changeFieldList = dmlData.getChangeFieldList();
        //操作的主键
        int id = Integer.parseInt(dmlData.getId());

        KxsLeader leader = new KxsLeader();
        leader.setId(id);

        //操作类型
        OperationType operation = dmlData.getOperation();
        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(leader.getId());
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    leader.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "LeaderLevel":
                    int leaderLevel = Integer.parseInt(validFieldDataMap.get(field).toString());
                    leader.setLeaderType(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    leader.setLeaderLevel(leaderLevel == 1 ? 5 : 7);
                    break;
                case "LastBuyDate":
                    leader.setLastBuyDate(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "ExpiredDate":
                    leader.setExpiredDate(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "CreateDate":
                    leader.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    leader.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.UPDATE)){
            // 更新数据
            baseMapper.updateById(leader);
        }
        if(operation.equals(OperationType.INSERT)){
            if(id != leader.getUserId()){
                return;
            }
            KxsLeader kxsLeader = baseMapper.selectById(leader.getId());
            if (kxsLeader != null){
                baseMapper.updateById(leader);
            }else{
                // 插入数据
                baseMapper.insert(leader);
            }
        }
    }
}

