package com.diagbot.facade;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.dto.GetNewsNoticeInfoByIdDTO;
import com.diagbot.dto.NewsNoticePageDTO;
import com.diagbot.dto.SysDictionaryInfoDTO;
import com.diagbot.entity.MedNewsNotice;
import com.diagbot.entity.SysUser;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.enums.NewsNoticeStatusEnum;
import com.diagbot.service.impl.MedNewsNoticeServiceImpl;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.GetNewsNoticeInfoByIdVO;
import com.diagbot.vo.NewsNoticePageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @Description:消息通知处理
 * @Author: songxl
 * @time: 2022/4/12
 */
@Component
public class MedNewsNoticeFacade extends MedNewsNoticeServiceImpl {
    @Autowired
    private SysUserFacade sysUserFacade;
    @Autowired
    private SysDictionaryFacade sysDictionaryFacade;

    /**
     * 消息通知--获取未读消息数量
     *
     * @param
     * @Return java.util.Map<java.lang.String, java.lang.Integer>
     */
    public Map<String, Integer> getNewsCount() {
        Map<String, Integer> out = new HashMap<>();
        out.put("count", this.count(new QueryWrapper<MedNewsNotice>()
                .eq("status", "0")
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("receiver", SysUserUtils.getCurrentPrincipleID())));
        return out;
    }

    /**
     *  消息通知--获取未读消息数量
     * @param newsNoticePageVO
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.diagbot.dto.NewsNoticePageDTO>
     */
    public IPage<NewsNoticePageDTO> newsNoticePage(NewsNoticePageVO newsNoticePageVO) {
        newsNoticePageVO.setPrinciple(SysUserUtils.getCurrentPrincipleID());
        return this.getBaseMapper().newsNoticePage(newsNoticePageVO);
    }

    /**
     * 消息通知--查看消息通知详情
     *
     * @param getNewsNoticeInfoByIdVO
     * @Return com.diagbot.dto.GetNewsNoticeInfoByIdDTO
     */
    public GetNewsNoticeInfoByIdDTO getNewsNoticeInfoById(GetNewsNoticeInfoByIdVO getNewsNoticeInfoByIdVO) {
        //更新未读消息状态
        if (NewsNoticeStatusEnum.NEWS_NOT_READ.getKey().equals(getNewsNoticeInfoByIdVO.getStatus())) {
            MedNewsNotice medNewsNotice = new MedNewsNotice();
            BeanUtils.copyProperties(getNewsNoticeInfoByIdVO, medNewsNotice);
            medNewsNotice.setStatus(NewsNoticeStatusEnum.NEWS_READ.getKey());
            this.updateById(medNewsNotice);
        }
        MedNewsNotice medNewsNotice = this.getById(getNewsNoticeInfoByIdVO.getId());
        GetNewsNoticeInfoByIdDTO out = new GetNewsNoticeInfoByIdDTO();
        BeanUtils.copyProperties(medNewsNotice, out);
        //默认发送者为系统 0
        if ("0".equals(medNewsNotice.getSender())) {
            out.setSenderName("系统");
        } else {
            SysUser user = sysUserFacade.getById(medNewsNotice.getSender());
            out.setSenderName(user == null ? "未知用户" : user.getLinkman());
        }
        return out;
    }

    public Map<String,List<SysDictionaryInfoDTO>> getNewsDropDownBox() {
        Map<Long, List<SysDictionaryInfoDTO>> dicMap = sysDictionaryFacade.getDictionary(); // 获取字典信息
        Map<String,List<SysDictionaryInfoDTO>> outBox = new HashMap<>();
        outBox.put("newsType",dicMap.get(46l));
        outBox.put("newsStatus",dicMap.get(47l));
        return outBox;
    }
}
