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.KxsMorningLog;
import com.kxs.transfer.api.mapper.sys.KxsMorningLogMapper;
import com.kxs.transfer.api.mapper.sys.KxsMorningMapper;
import com.kxs.transfer.api.model.table.DMLData;
import com.kxs.transfer.api.service.sys.KxsMorningService;
import com.kxs.user.api.model.KxsLeader;
import com.kxs.user.api.model.KxsPartner;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 商学院晨会表(KxsMorning)表服务实现类
 *
 * @author 系统
 * @since 2023-12-15 12:16:31
 */
@Service("kxsMorningService")
@RequiredArgsConstructor
public class KxsMorningServiceImpl extends ServiceImpl<KxsMorningMapper, KxsMorning> implements KxsMorningService {

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

        KxsMorning morning = new KxsMorning();
        morning.setId(id);

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

            switch (field) {
                case "StudyPerson":
                    morning.setStudyPerson(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "Status":
                    morning.setDelFlag(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "IsBanner":
                    morning.setIsBanner(Boolean.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "Title":
                    morning.setTitle(validFieldDataMap.get(field).toString());
                    break;
                case "Photo":
                    morning.setPhoto(validFieldDataMap.get(field).toString());
                    break;
                case "Lecturer":
                    morning.setLecturer(validFieldDataMap.get(field).toString());
                    break;
                case "SeoDescription":
                    morning.setUrl(validFieldDataMap.get(field).toString());
                    break;
                case "Url":
                    morning.setAppLink(validFieldDataMap.get(field).toString());
                    break;
                case "SeoKeyword":
                    morning.setPageLink(validFieldDataMap.get(field).toString());
                    break;
                case "BannerPhoto":
                    morning.setBannerPhoto(validFieldDataMap.get(field).toString());
                    break;
                case "CreateDate":
                    morning.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    morning.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }
        if(morning.getDelFlag() != null && morning.getDelFlag() == -1){
            baseMapper.deleteById(morning.getId());
            return;
        }
        if(operation.equals(OperationType.UPDATE)){
            // 更新数据
            baseMapper.updateById(morning);
        }
        if(operation.equals(OperationType.INSERT)){
            KxsMorning kxsMorning = baseMapper.selectById(morning.getId());
            if (kxsMorning != null){
                baseMapper.updateById(morning);
            }else{
                // 插入数据
                baseMapper.insert(morning);
            }
        }
    }

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

        KxsMorningLog morningLog = new KxsMorningLog();
        morningLog.setId(id);

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

            switch (field) {
                case "UserId":
                    morningLog.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "MeetId":
                    morningLog.setMorningId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
            }
        }

        if(operation.equals(OperationType.UPDATE)){
            // 更新数据
            kxsMorningLogMapper.updateById(morningLog);
        }
        if(operation.equals(OperationType.INSERT)){
            KxsMorningLog kxsMorningLog = kxsMorningLogMapper.selectById(morningLog.getId());
            if (kxsMorningLog != null){
                kxsMorningLogMapper.updateById(morningLog);
            }else{
                // 插入数据
                kxsMorningLogMapper.insert(morningLog);
            }
        }
    }
}

