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.KxsUserReRealMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.user.api.model.KxsUserReReal;
import org.springframework.stereotype.Service;

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

/**
 * 创客重新实名开放表(KxsUserReReal)表服务实现类
 *
 * @author 系统
 * @since 2024-06-05 14:02:55
 */
@Service("kxsUserReRealService")
public class KxsUserReRealService extends ServiceImpl<KxsUserReRealMapper, KxsUserReReal> {

    public void changeData(DMLData dmlData) {

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

        KxsUserReReal userReReal = new KxsUserReReal();
        userReReal.setId(id);
        //操作类型
        OperationType operation = dmlData.getOperation();
        if(operation.equals(OperationType.DELETE)){
            // 更新数据
            baseMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    userReReal.setUserId(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Remark":
                    userReReal.setRemark(validFieldDataMap.get(field).toString());
                    break;
                case "RealName":
                    userReReal.setUsername(validFieldDataMap.get(field).toString());
                    break;
                case "MakerCode":
                    userReReal.setUserCode(validFieldDataMap.get(field).toString());
                    break;
                case "Operator":
                    userReReal.setCreateBy(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    userReReal.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    userReReal.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.UPDATE)){

            // 更新数据
            baseMapper.updateById(userReReal);
        }
        if(operation.equals(OperationType.INSERT)){

            KxsUserReReal kxsUserReReal = baseMapper.selectById(id);
            if (kxsUserReReal != null){
                baseMapper.updateById(userReReal);
            }else{
                // 插入数据
                baseMapper.insert(userReReal);
            }
        }
    }
}

