package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.core.domain.entity.FormalParentsStudent;
import com.ruoyi.common.core.domain.entity.FormalTeacherClass;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.FormalParentsStudentMapper;
import com.ruoyi.system.mapper.FormalTeacherClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.XiaoyuanNoticeMapper;
import com.ruoyi.system.domain.XiaoyuanNotice;
import com.ruoyi.system.service.IXiaoyuanNoticeService;

import javax.annotation.Resource;

/**
 * 校园新闻Service业务层处理
 *
 * @author boman
 * @date 2023-06-05
 */
@Service
public class XiaoyuanNoticeServiceImpl implements IXiaoyuanNoticeService {
    @Autowired
    private XiaoyuanNoticeMapper xiaoyuanNoticeMapper;
    @Autowired
    private FormalTeacherClassMapper formalTeacherClassMapper;
    @Autowired
    private FormalParentsStudentMapper formalParentsStudentMapper;

    /**
     * 查询校园新闻
     *
     * @param noticeId 校园新闻主键
     * @return 校园新闻
     */
    @Override
    public XiaoyuanNotice selectXiaoyuanNoticeByNoticeId(Integer noticeId) {
        return xiaoyuanNoticeMapper.selectXiaoyuanNoticeByNoticeId(noticeId);
    }

    /**
     * 查询校园新闻列表
     *
     * @param xiaoyuanNotice 校园新闻
     * @return 校园新闻
     */
    @Override
    public List<XiaoyuanNotice> selectXiaoyuanNoticeList(XiaoyuanNotice xiaoyuanNotice) {
        SysUser user = SecurityUtils.getLoginUser().getUser().;
        xiaoyuanNotice.setSenderId(user.getUserId().toString());
        List<SysRole> roles = user.getRoles();
        String classId = "";
        for (SysRole role : roles) {
            if ("teacher".equals(role.getRoleKey())) {
                FormalTeacherClass formalTeacherClass = new FormalTeacherClass();
                formalTeacherClass.setTeacherId(user.getUserId());
                List<FormalTeacherClass> formalTeacherClasses = formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClass);
                if (formalTeacherClasses != null && formalTeacherClasses.size() > 0) {
                    for (FormalTeacherClass teacherClass : formalTeacherClasses) {
                        classId = teacherClass.getClassId() + ",";
                    }
                }
            }
            if ("parents".equals(role.getRoleKey())){
                FormalParentsStudent formalParentsStudent = new FormalParentsStudent();
                formalParentsStudent.setParentsId(user.getUserId());
                List<FormalParentsStudent> formalParentsStudents = formalParentsStudentMapper.selectFormalParentsStudentList(formalParentsStudent);
                for (FormalParentsStudent parentsStudent : formalParentsStudents) {
                    classId = parentsStudent.getClassId() + ",";
                }
            }
        }
        //查询出所有班级id
        classId = classId.substring(0, classId.length() - 1);
        xiaoyuanNotice.setSenderDept(classId);
        return xiaoyuanNoticeMapper.selectXiaoyuanNoticeList(xiaoyuanNotice);
    }

    /**
     * 新增校园新闻
     *
     * @param xiaoyuanNotice 校园新闻
     * @return 结果
     */
    @Override
    public int insertXiaoyuanNotice(XiaoyuanNotice xiaoyuanNotice) {
        xiaoyuanNotice.setCreateTime(DateUtils.getNowDate());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        xiaoyuanNotice.setSenderId(String.valueOf(user.getUserId()));
        xiaoyuanNotice.setSenderName(user.getUserName());
        String senderDept = xiaoyuanNotice.getSenderDept();
        int result = 0;
        if (StringUtils.isNotBlank(senderDept)){
            String[] split = senderDept.split(",");
            for (String deptId : split) {
                xiaoyuanNotice.setSenderDept(deptId);
                result = result + xiaoyuanNoticeMapper.insertXiaoyuanNotice(xiaoyuanNotice)
            }
        }
        return result;
    }

    /**
     * 修改校园新闻
     *
     * @param xiaoyuanNotice 校园新闻
     * @return 结果
     */
    @Override
    public int updateXiaoyuanNotice(XiaoyuanNotice xiaoyuanNotice) {
        xiaoyuanNotice.setUpdateTime(DateUtils.getNowDate());
        return xiaoyuanNoticeMapper.updateXiaoyuanNotice(xiaoyuanNotice);
    }

    /**
     * 批量删除校园新闻
     *
     * @param noticeIds 需要删除的校园新闻主键
     * @return 结果
     */
    @Override
    public int deleteXiaoyuanNoticeByNoticeIds(Integer[] noticeIds) {
        return xiaoyuanNoticeMapper.deleteXiaoyuanNoticeByNoticeIds(noticeIds);
    }

    /**
     * 删除校园新闻信息
     *
     * @param noticeId 校园新闻主键
     * @return 结果
     */
    @Override
    public int deleteXiaoyuanNoticeByNoticeId(Integer noticeId) {
        return xiaoyuanNoticeMapper.deleteXiaoyuanNoticeByNoticeId(noticeId);
    }
}
