package com.hxhq.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.Study;
import com.hxhq.business.domain.StudySubject;
import com.hxhq.business.dto.select.DeptUserTreeDto;
import com.hxhq.business.enums.study.StudyTypeEnum;
import com.hxhq.business.service.IStudyService;
import com.hxhq.business.service.IStudySubjectService;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.core.utils.DateUtils;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.security.utils.SecurityUtils;
import com.hxhq.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.NoticeMapper;
import com.hxhq.business.domain.Notice;
import com.hxhq.business.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 系统通知Service业务层处理
 *
 * @author hxhq
 * @date 2026-01-16
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService
{
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private IStudyService studyService;
    @Autowired
    private IStudySubjectService studySubjectService;

    /**
     * 查询系统通知列表
     *
     * @return 系统通知
     */
    @Override
    public List<Notice> queryList()
    {
        QueryWrapper<Notice> queryWrapper = Wrappers.query();
        queryWrapper.eq("user_id", SecurityUtils.getUserId());
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

    @Override
    public void save(String title, Long userId, String url) {
        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setUserId(userId);
        notice.setUrl(url);
        save(notice);
    }

    @Override
    public void saveBatch(String title, List<Long> userIdList, String url) {
        List<Notice> list = new ArrayList<>();
        for(Long userId : userIdList){
            Notice notice = new Notice();
            notice.setTitle(title);
            notice.setUserId(userId);
            notice.setUrl(url);
            list.add(notice);
        }
        saveBatch(list);
    }

    @Override
    public void saveStudyMethod(Long studyId, Long studySubjectId, Long createUserId) {
        Study study = studyService.getById(studyId);
        if(study==null){
            throw new ServiceException("信息不存在");
        }
        StudySubject studySubject = null;
        if(study.getType().equals(StudyTypeEnum.sy.getValue())){
            studySubject = studySubjectService.getById(studySubjectId);
            if(studySubject==null){
                throw new ServiceException("学科不存在");
            }
        }
        String title = "您参与的【"+study.getName()+"（"+study.getSn()+"）】【"+(study.getType().equals(StudyTypeEnum.sy.getValue())?studySubject.getDeptName():study.getDeptName())+"】有新试验方法，请进入试验的对应学科进行阅读";
        String url = study.getType().equals(StudyTypeEnum.sy.getValue())?("/study/enter/"+studyId+"/syff"):study.getType().equals(StudyTypeEnum.fsy.getValue())?("/nonTrial/enter/"+studyId+"/syff"):"";
        //试验学科内部的人+非试验部门内的人+麻精药不用判断（只需要判断有审核按钮）
        List<Long> userIdList = userMapper.selectStudyMethodUserIdExcludeCreate(studyId, study.getType().equals(StudyTypeEnum.sy.getValue())?studySubjectId:null,study.getType().equals(StudyTypeEnum.fsy.getValue())?study.getDeptId():null,createUserId);
        if(userIdList.size()>0){
            saveBatch(title,userIdList,url);
        }
    }
}
