package com.kxs.transfer.api.service.sys.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.system.api.model.KxsMorning;
import com.kxs.system.api.model.KxsSchoolStudy;
import com.kxs.transfer.api.mapper.sys.KxsSchoolStudyMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.sys.KxsSchoolStudyService;
import org.springframework.stereotype.Service;

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

/**
 * 商学院创客学堂(KxsSchoolStudy)表服务实现类
 *
 * @author 系统
 * @since 2023-12-15 12:16:31
 */
@Service("kxsSchoolStudyService")
public class KxsSchoolStudyServiceImpl extends ServiceImpl<KxsSchoolStudyMapper, KxsSchoolStudy> implements KxsSchoolStudyService {

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

        KxsSchoolStudy schoolStudy = new KxsSchoolStudy();
        schoolStudy.setId(id);

        //操作类型
        OperationType operation = dmlData.getOperation();
        for (String field : changeFieldList) {

            switch (field) {
                case "QueryCount":
                    schoolStudy.setStudyPerson(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "Status":
                    schoolStudy.setDelFlag(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "SeoTitle":
                    schoolStudy.setLecturer(validFieldDataMap.get(field).toString());
                    break;
                case "SeoKeyword":
                    schoolStudy.setMenuId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "Url":
                    schoolStudy.setUrl(validFieldDataMap.get(field).toString());
                    break;
                case "ListPic":
                    schoolStudy.setListPic(validFieldDataMap.get(field).toString());
                    break;
                case "Detail":
                    schoolStudy.setDetail(validFieldDataMap.get(field).toString());
                    break;
                case "Title":
                    schoolStudy.setTitle(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    schoolStudy.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    schoolStudy.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        if(schoolStudy.getDelFlag() != null && schoolStudy.getDelFlag() == -1){
            baseMapper.deleteById(schoolStudy.getId());
            return;
        }
        if(operation.equals(OperationType.UPDATE)){
            // 更新数据
            baseMapper.updateById(schoolStudy);
        }
        if(operation.equals(OperationType.INSERT)){
            KxsSchoolStudy kxsSchoolStudy = baseMapper.selectById(schoolStudy.getId());
            if (kxsSchoolStudy != null){
                baseMapper.updateById(schoolStudy);
            }else{
                // 插入数据
                baseMapper.insert(schoolStudy);
            }
        }
    }
}

