package com.muyu.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.system.domain.SysNotice;
import com.muyu.system.domain.resp.NoticeResp;
import com.muyu.system.domain.resp.NoticeUserResp;
import com.muyu.system.mapper.SysNoticeMapper;
import com.muyu.system.service.SysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author muyu
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements SysNoticeService {
    @Autowired
    private SysNoticeMapper noticeMapper;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     *
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById (Long noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     *
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList (SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice    公告信息
     * @param userIdSet
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNotice (SysNotice notice, HashSet<Integer> userIdSet) {
        // 新增通知信息并返回的公告ID
        int n = noticeMapper.insertNotice(notice);
        if (n>0){
            // 为用户公告表插入数据
            int[] userIds = userIdSet.stream().mapToInt(Integer::intValue).toArray();
            noticeMapper.insertNoticeUser(notice.getNoticeId(), userIds);
        }
        return n;
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     *
     * @return 结果
     */
    @Override
    public int updateNotice (SysNotice notice) {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     *
     * @return 结果
     */
    @Override
    public int deleteNoticeById (Long noticeId) {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     *
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds (Long[] noticeIds) {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    @Override
    public List<NoticeUserResp> selNoticeByUserId(Long userId) {
        return noticeMapper.selNoticeByUserId(userId);
    }

    @Override
    public NoticeUserResp findNoticeByNoticeIdAndUserId(Long noticeId, int sta, Long userId) {
        int n = 1;
        // 如果未阅读
        if (sta == 0){
            // 修改阅读状态
            n = noticeMapper.updateNoticeSta(noticeId,userId);
        }
        // 根据公告id查询
        return n>0?noticeMapper.findNoticeByNoticeIdAndUserId(noticeId, userId):null;
    }

    @Override
    public NoticeResp findNoticeByNoticeId(Long noticeId) {
        return noticeMapper.findNoticeByNoticeId(noticeId);
    }

    @Override
    public List<Integer> selUserByDeptIds(ArrayList<Integer> deptIds) {
        return noticeMapper.selUserByDeptIds(deptIds);
    }

    @Override
    public List<Integer> findUserIdsAll() {
        return noticeMapper.findUserIdsAll();
    }


}
