package com.usky.system.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.system.domain.MceRequestVO;
import com.usky.system.domain.SysNotice;
import com.usky.system.domain.SysUser;
import com.usky.system.mapper.SysNoticeMapper;
import com.usky.system.mapper.SysUserMapper;
import com.usky.system.service.ISysNoticeService;
import com.usky.system.service.MceReceiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

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

    @Autowired
    private MceReceiveService mceReceiveService;

    @Autowired
    private SysUserMapper sysUserMapper;

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

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        notice.setTenantId(SecurityUtils.getTenantId());
        String deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId().toString();
        notice.setDeptId(deptId);
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        notice.setTenantId(SecurityUtils.getTenantId());
        noticeMapper.insertNotice(notice);
        int insertId = notice.getNoticeId();

        String deptId = notice.getDeptId();
        List<Long> deptIds = Arrays.stream(deptId.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<Long> usersIds = sysUserMapper.selectList(Wrappers.lambdaQuery(SysUser.class).select(SysUser::getUserId).in(SysUser::getDeptId, deptIds)).stream().map(SysUser::getUserId).collect(Collectors.toList());

        if (!deptIds.isEmpty()) {
            sendNotice(notice.getNoticeTitle(), insertId, notice.getCreateBy(), usersIds);
        }

        return insertId;
    }

    /**
     * 发送消息
     * @param infoContent 消息内容
     * @param id 通知公告id
     * @param userName 发布人
     * @param userIds 接收人
     */
    private void sendNotice(String infoContent, Integer id, String userName, List<Long> userIds) {

        // JSONObject jsonObject = new JSONObject();
        // jsonObject.put("infoTitle", "通知公告");
        // jsonObject.put("infoContent", infoContent);
        // jsonObject.put("infoType", 1);
        // jsonObject.put("id", id);
        // jsonObject.put("userName", userName);
        // jsonObject.put("userIds", userIds);
        MceRequestVO mceRequestVO = new MceRequestVO();
        mceRequestVO.setInfoTitle("通知公告");
        mceRequestVO.setInfoContent(infoContent);
        mceRequestVO.setInfoType("1");
        mceRequestVO.setId(id);
        mceRequestVO.setUserName(userName);
        mceRequestVO.setUserIds(userIds);
        mceRequestVO.setRealName(SecurityUtils.getLoginUser().getSysUser().getNickName());
        try {
            // mceReceiveService.add(jsonObject.toString());
            mceReceiveService.addMceReceive(mceRequestVO);
        } catch (Exception e) {
            log.error(String.format("公告:%d，消息发送失败:%s", id, e.getMessage()));
        }
    }

    /**
     * 修改公告
     *
     * @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);
    }
}
