package com.yeejoin.amos.boot.module.ugp.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yeejoin.amos.boot.biz.common.bo.ReginParams;
import com.yeejoin.amos.boot.module.ugp.api.dto.CompanyDto;
import com.yeejoin.amos.boot.module.ugp.api.dto.MsgNoticeDto;
import com.yeejoin.amos.boot.module.ugp.api.dto.QualityProblemDto;
import com.yeejoin.amos.boot.module.ugp.api.entity.InstallNoticeMsg;
import com.yeejoin.amos.boot.module.ugp.api.entity.RectifyMsg;
import com.yeejoin.amos.boot.module.ugp.api.mapper.InstallNoticeMapper;
import com.yeejoin.amos.boot.module.ugp.api.mapper.InstallNoticeMsgMapper;
import com.yeejoin.amos.boot.module.ugp.api.mapper.RectifyMsgMapper;
import com.yeejoin.amos.boot.module.ugp.api.service.IInstallNoticeMsgService;
import com.yeejoin.amos.boot.module.ugp.api.service.IRectifyMsgService;
import com.yeejoin.amos.boot.module.ugp.api.dto.RectifyMsgDto;
import com.yeejoin.amos.boot.module.ugp.biz.framework.BusinessIdentify;
import org.springframework.beans.factory.annotation.Autowired;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import org.typroject.tyboot.core.rdbms.service.BaseService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 整改短信通知表服务实现类
 *
 * @author system_generator
 * @date 2022-09-22
 */
@Service
public class RectifyMsgServiceImpl extends BaseService<RectifyMsgDto,RectifyMsg,RectifyMsgMapper> implements IRectifyMsgService {

    @Autowired
    IRectifyMsgService iRectifyMsgService;

    @Autowired
    RectifyMsgMapper rectifyMsgMapper;

    @Autowired
    OrgServiceImpl orgService;
    @Autowired
    InstallNoticeMsgMapper installNoticeMsgMapper;


    /**
     * 质量问题整改短信列表查询
     */
    public IPage<RectifyMsgDto> queryForRectifyMsgPage(IPage<RectifyMsgDto> page,String name,String installationUnit) {
        return rectifyMsgMapper.queryForRectifyMsgPage(page,name,installationUnit);
    }

    /**
     * 列表查询 示例
     */
    public List<RectifyMsgDto> queryForRectifyMsgList()   {
        return this.queryForList("" , false);
    }

    /**
     * 消息提醒
     */
    @BusinessIdentify
    public List<MsgNoticeDto> msgNotice(Boolean isRead){

        LambdaQueryWrapper<InstallNoticeMsg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InstallNoticeMsg::getTargetPersonId,orgService.getReginParams().getBusinessInfo().getUserSequenceNbr())
                .orderByDesc(InstallNoticeMsg::getSendTime);
        if(!ValidationUtil.isEmpty(isRead)){
            wrapper.eq(InstallNoticeMsg::getIsRead,isRead);
        }
        LambdaQueryWrapper<RectifyMsg> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(RectifyMsg::getMsgReceiver,orgService.getReginParams().getBusinessInfo().getUserSequenceNbr())
                .orderByDesc(RectifyMsg::getSendTime);
        if(!ValidationUtil.isEmpty(isRead)){
            wrapper1.eq(RectifyMsg::getIsRead,isRead);
        }
        List<RectifyMsg> rectifyMsgList = rectifyMsgMapper.selectList(wrapper1);
        List<InstallNoticeMsg> installNoticeMsgList = installNoticeMsgMapper.selectList(wrapper);
        List<MsgNoticeDto> msgNoticeDtoList = new ArrayList<>();

        if(!ValidationUtil.isEmpty(installNoticeMsgList)) {
            for (InstallNoticeMsg installNoticeMsg : installNoticeMsgList) {
                MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
                String contents = installNoticeMsg.getContent();
                if (!ValidationUtil.isEmpty(contents)) {
                    String form = contents.substring(contents.indexOf('【') + 1, contents.indexOf("】"));
                    String context = contents.substring(contents.indexOf("】") + 1);
                    msgNoticeDto.setType(form);
                    msgNoticeDto.setContent(context);
                }
                msgNoticeDto.setCreateDate(installNoticeMsg.getSendTime());
                msgNoticeDto.setSequenceNbr(installNoticeMsg.getSequenceNbr());
                msgNoticeDtoList.add(msgNoticeDto);
            }
        }
        if(!ValidationUtil.isEmpty(rectifyMsgList)) {
            for (RectifyMsg rectifyMsg : rectifyMsgList) {
                MsgNoticeDto msgNoticeDto = new MsgNoticeDto();
                String contents = rectifyMsg.getContent();
                if (!ValidationUtil.isEmpty(contents)) {
                    msgNoticeDto.setType(contents.substring(contents.indexOf('【') + 1, contents.indexOf("】")));
                    msgNoticeDto.setContent(contents.substring(contents.indexOf("】") + 1));
                }
                msgNoticeDto.setCreateDate(rectifyMsg.getSendTime());
                msgNoticeDto.setSequenceNbr(rectifyMsg.getSequenceNbr());
                msgNoticeDtoList.add(msgNoticeDto);
            }
        }
        return msgNoticeDtoList;
    }

}