package com.kxs.transfer.api.service.sys;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
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.product.api.model.KxsMachine;
import com.kxs.product.api.model.KxsMerchant;
import com.kxs.system.api.model.*;
import com.kxs.transfer.api.mapper.sys.KxsAppModalMapper;
import com.kxs.transfer.api.mapper.sys.KxsSysMsgMapper;
import com.kxs.transfer.api.mapper.sys.KxsSysMsgReadMapper;
import com.kxs.transfer.api.mapper.sys.KxsUserMsgMapper;
import com.kxs.transfer.api.mapper.user.KxsUserMapper;
import com.kxs.transfer.api.model.table.DMLData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 系统消息推送表(KxsSysMsg)表服务接口
 *
 * @author 系统
 * @since 2023-12-15 12:16:32
 */
@Service
@RequiredArgsConstructor
public class KxsSysMsgService extends ServiceImpl<KxsSysMsgMapper, KxsSysMsg> {

    private final KxsUserMsgMapper kxsUserMsgMapper;

    private final KxsAppModalMapper kxsAppModalMapper;

    private final KxsSysMsgReadMapper kxsSysMsgReadMapper;

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

        KxsUserMsg userMsg = new KxsUserMsg();
        userMsg.setId(id);

        OperationType operation = dmlData.getOperation();

        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "UserId":
                    int userId = Integer.parseInt(validFieldDataMap.get(field).toString());
                    userMsg.setUserId(userId);
                    break;
                case "MsgType":
                    userMsg.setMsgType(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "BgPic":
                    if(validFieldDataMap.get(field) != null){
                        userMsg.setBgPic(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "Title":
                    userMsg.setTitle(validFieldDataMap.get(field).toString());
                    break;
                case "Content":
                    if(validFieldDataMap.get(field) != null){
                        userMsg.setContent(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "Url":
                    if(validFieldDataMap.get(field) != null){
                        userMsg.setUrl(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "Summary":
                    if(validFieldDataMap.get(field) != null){
                        userMsg.setSummary(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "ReadFlag":
                    userMsg.setReadFlag(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "CreateDate":
                    userMsg.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    userMsg.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsUserMsg kxsUserMsg = kxsUserMsgMapper.selectById(id);
            if(kxsUserMsg == null){
                kxsUserMsgMapper.insert(userMsg);
            }
        }
        if(operation.equals(OperationType.UPDATE)){

            kxsUserMsgMapper.updateById(userMsg);
        }
    }

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

        KxsSysMsg sysMsg = new KxsSysMsg();
        sysMsg.setId(id);

        OperationType operation = dmlData.getOperation();

        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "MsgType":
                    sysMsg.setMsgType(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "BgPic":
                    if(validFieldDataMap.get(field) != null){
                        sysMsg.setBgPic(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "Title":
                    sysMsg.setTitle(validFieldDataMap.get(field).toString());
                    break;
                case "Content":
                    if(validFieldDataMap.get(field) != null){
                        sysMsg.setContent(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "Url":
                    if(validFieldDataMap.get(field) != null){
                        sysMsg.setUrl(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "Summary":
                    if(validFieldDataMap.get(field) != null){
                        sysMsg.setSummary(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "PushType":
                    sysMsg.setPushType(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "PushTime":
                    if(validFieldDataMap.get(field) != null){
                        sysMsg.setPushTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    }
                    break;
                case "EffectStartDate":
                    if(validFieldDataMap.get(field) != null){
                        sysMsg.setStartTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    }
                    break;
                case "EffectEndDate":
                    if(validFieldDataMap.get(field) != null){
                        sysMsg.setEndTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    }
                    break;
                case "CreateDate":
                    sysMsg.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    sysMsg.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsSysMsg kxsSysMsg = baseMapper.selectById(id);
            if(kxsSysMsg == null){
                baseMapper.insert(sysMsg);
            }
        }
        if(operation.equals(OperationType.UPDATE)){
            baseMapper.updateById(sysMsg);
        }

    }

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

        KxsAppModal appModal = new KxsAppModal();
        appModal.setId(id);

        OperationType operation = dmlData.getOperation();

        if(operation.equals(OperationType.DELETE)){
            baseMapper.deleteById(id);
            return;
        }
        for (String field : changeFieldList) {

            switch (field) {
                case "Status":
                    int status = Integer.parseInt(validFieldDataMap.get(field).toString());
                    if(status == -1){
                        kxsAppModalMapper.deleteById(id);
                        return;
                    }
                    break;
                case "MsgType":
                    appModal.setMsgType(Integer.parseInt(validFieldDataMap.get(field).toString()) - 1);
                    break;
                case "BgPic":
                    if(validFieldDataMap.get(field) != null){
                        appModal.setBgPic(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "Title":
                    appModal.setTitle(validFieldDataMap.get(field).toString());
                    break;
                case "Content":
                    if(validFieldDataMap.get(field) != null){
                        appModal.setContent(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "Url":
                    if(validFieldDataMap.get(field) != null){
                        appModal.setUrl(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "BtnText":
                    if(validFieldDataMap.get(field) != null){
                        appModal.setBtnText(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "TimeByDay":
                    appModal.setTimeByDay(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "IsWeekend":
                    appModal.setIsWeekend(Integer.valueOf(validFieldDataMap.get(field).toString()));
                    break;
                case "UrlParam":
                    if(validFieldDataMap.get(field) != null){
                        appModal.setUrlParam(validFieldDataMap.get(field).toString());
                    }
                    break;
                case "EffectStartDate":
                    if(validFieldDataMap.get(field) != null){
                        appModal.setStartTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    }
                    break;
                case "EffectEndDate":
                    if(validFieldDataMap.get(field) != null){
                        appModal.setEndTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    }
                    break;
                case "CreateDate":
                    appModal.setCreateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
                case "UpdateDate":
                    appModal.setUpdateTime(LocalDateTimeUtil.parse(validFieldDataMap.get(field).toString(), DatePattern.NORM_DATETIME_PATTERN));
                    break;
            }
        }

        if(operation.equals(OperationType.INSERT)){
            KxsAppModal kxsAppModal = kxsAppModalMapper.selectById(id);
            if(kxsAppModal == null){
                kxsAppModalMapper.insert(appModal);
            }
        }
        if(operation.equals(OperationType.UPDATE)){
            kxsAppModalMapper.updateById(appModal);
        }

    }

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

        KxsSysMsgRead sysMsgRead = new KxsSysMsgRead();
        sysMsgRead.setId(id);

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

            switch (field) {
                case "UserId":
                    sysMsgRead.setUserId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
                case "PlacardId":
                    sysMsgRead.setMsgId(Integer.parseInt(validFieldDataMap.get(field).toString()));
                    break;
            }
        }

        if(operation.equals(OperationType.UPDATE)){
            // 更新数据
            kxsSysMsgReadMapper.updateById(sysMsgRead);
        }
        if(operation.equals(OperationType.INSERT)){
            KxsSysMsgRead kxsSysMsgRead = kxsSysMsgReadMapper.selectById(sysMsgRead.getId());
            if (kxsSysMsgRead != null){
                kxsSysMsgReadMapper.updateById(sysMsgRead);
            }else{
                // 插入数据
                kxsSysMsgReadMapper.insert(sysMsgRead);
            }
        }
    }


}

