package com.srs.routine.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.SecurityUtils;
import com.srs.comprehensive.domain.SrsClass;
import com.srs.comprehensive.domain.SrsGrade;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.comprehensive.mapper.SrsClassMapper;
import com.srs.comprehensive.mapper.SrsGradeMapper;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import com.srs.routine.domain.RtClassMeetingClass;
import com.srs.routine.domain.RtClassMettingTheme;
import com.srs.routine.domain.SrsAttachResource;
import com.srs.routine.mapper.RtClassMeetingClassMapper;
import com.srs.routine.mapper.RtClassMettingThemeMapper;
import com.srs.routine.mapper.SrsAttachResourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.routine.mapper.RtClassMettingContentMapper;
import com.srs.routine.domain.RtClassMettingContent;
import com.srs.routine.service.IRtClassMettingContentService;

import static com.srs.common.utils.SecurityUtils.getUsername;

/**
 * 班会内容Service业务层处理
 *
 * @author srs
 * @date 2024-05-18
 */
@Service
public class RtClassMettingContentServiceImpl extends ServiceImpl<RtClassMettingContentMapper,RtClassMettingContent> implements IRtClassMettingContentService {
    @Autowired
    private RtClassMettingContentMapper rtClassMettingContentMapper;

    @Autowired
    private SrsAttachResourceMapper srsAttachResourceMapper;

    @Autowired
    private SrsStudentMapper _stuMapper;

    @Autowired
    private SrsClassMapper _classMapper;

    @Autowired
    public SrsGradeMapper _gradeMapper;

    @Autowired
    private RtClassMettingThemeMapper rtClassMettingThemeMapper;

    @Autowired
    public RtClassMeetingClassMapper rtClassMeetingClassMapper;

    @Override
    public AjaxResult listStuFdyClass(){
        try{
            List<SrsClass> res = new ArrayList<SrsClass>();
            QueryWrapper<SrsStudent> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.eq("stu_no", getUsername());
            studentQueryWrapper.last("limit 1");
            SrsStudent student=_stuMapper.selectOne(studentQueryWrapper);
            if(student==null){
                throw new Exception("未找到该学生信息");
            }
            Long classId=student.getClassId();
            if(classId==null){
                throw new Exception("该学生未分配班级");
            }
            SrsClass srsClass=_classMapper.selectSrsClassByClassId(classId);
            if(srsClass==null){
                throw new Exception("未找到该班级信息");
            }
            Long teacherId=srsClass.getTeacherId();
            if(teacherId==null){
                throw new Exception("该班级未分配教师");
            }
            QueryWrapper<SrsClass> classQueryWrapper = new QueryWrapper<>();
            classQueryWrapper.eq("teacher_id", teacherId);
            List<SrsClass> classList=_classMapper.selectList(classQueryWrapper);
            for(SrsClass x:classList){
                if(x.getGradeId() != null){
                    Long gradeId=x.getGradeId();
                    SrsGrade srsGrade=_gradeMapper.selectSrsGradeByGradeId(gradeId);
                    if(srsGrade!=null){
                        if(Objects.equals(srsGrade.gradeStatus, "1") || Objects.equals(srsGrade.gradeStatus, "3")){
                            res.add(x);
                        }
                    }
                }
            }
            if(classList.size() == 0){
                throw new Exception("该教师未分配班级");
            }else{
                res = classList;
            }

            return AjaxResult.success(res);
        }catch(Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }


    public AjaxResult listStuByClassId(Long classId){
        try{
            QueryWrapper<SrsStudent> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("class_id",classId);
            List<SrsStudent> list=_stuMapper.selectList(queryWrapper);
            return AjaxResult.success(list);
        }catch(Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 查询班会内容
     *
     * @param crId 班会内容主键
     * @return 班会内容
     */
    @Override
    public RtClassMettingContent selectRtClassMettingContentByCrId(Long crId) {
        RtClassMettingContent scm=rtClassMettingContentMapper.selectRtClassMettingContentByCrId(crId);
        //按照编号获取附件数据，并放到内容里面返回。
        SrsAttachResource sar=new SrsAttachResource();
        sar.setResMainNo(scm.getCrNo());
        sar.setResType("bhwj02");
        List<SrsAttachResource> list=srsAttachResourceMapper.selectSrsAttachResourceList(sar);
        scm.setAttachResourceList(list);
        return scm;
    }

    /**
     * 查询班会内容列表
     * @param rtClassMettingContent 班会内容
     * @return 班会内容
     */
    @Override
    public List<RtClassMettingContent> selectRtClassMettingContentList(RtClassMettingContent rtClassMettingContent) {
        List<RtClassMettingContent> list=rtClassMettingContentMapper.selectRtClassMettingContentList(rtClassMettingContent);
        for (RtClassMettingContent rc:list) {
            SrsAttachResource sar=new SrsAttachResource();
            sar.setResMainNo(rc.getCrNo());
            sar.setResType("bhwj02");
            List<SrsAttachResource> listr=srsAttachResourceMapper.selectSrsAttachResourceList(sar);
            rc.setAttachResourceList(listr);
        }
        return list;
    }

    /**
     * 新增班会内容
     * @param rtClassMettingContent 班会内容
     * @return 结果
     */
    @Override
    public AjaxResult insertRtClassMettingContent(RtClassMettingContent rtClassMettingContent) {
        try{
            QueryWrapper<RtClassMettingTheme> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cm_no", rtClassMettingContent.getCrCmNo());
            RtClassMettingTheme rmt = rtClassMettingThemeMapper.selectOne(queryWrapper);

            if(rmt == null){
                throw new Exception("未找到该班会主题");
            }

            QueryWrapper<SrsClass> classQueryWrapper = new QueryWrapper<>();
            classQueryWrapper.eq("class_id", rtClassMettingContent.getCrClassNo());
            SrsClass srsClass = _classMapper.selectOne(classQueryWrapper);

            if(srsClass == null){
                throw new Exception("未找到该班级信息");
            }

            QueryWrapper<RtClassMeetingClass> meetingClassQueryWrapper = new QueryWrapper<>();
            meetingClassQueryWrapper.eq("cmt_id",rmt.getCmId());
            meetingClassQueryWrapper.eq("class_id", srsClass.getClassId());
            RtClassMeetingClass rcm = rtClassMeetingClassMapper.selectOne(meetingClassQueryWrapper);
            if(rcm == null){
                throw new Exception( srsClass.getClassName()+"不需要开设此班会");
            }

            QueryWrapper<RtClassMettingContent> contentQueryWrapper = new QueryWrapper<>();
            contentQueryWrapper.eq("cr_cm_no", rmt.getCmNo());
            contentQueryWrapper.eq("cr_class_no", srsClass.getClassId());
            List<RtClassMettingContent> contentList = rtClassMettingContentMapper.selectList(contentQueryWrapper);
            if(!contentList.isEmpty()){
                throw new Exception(srsClass.getClassName()+"已开过班会");
            }


            rtClassMettingContent.setCrNo(UUID.randomUUID().toString());//设置唯一编号
            rtClassMettingContent.setCreateTime(DateUtils.getNowDate());
            //rtClassMettingContent.setCrTeacherNo();
            for (SrsAttachResource atta:rtClassMettingContent.getAttachResourceList()) {
                atta.setDelFlag("0");
                atta.setResNo(UUID.randomUUID().toString());
                atta.setResMainNo(rtClassMettingContent.getCrNo());
                atta.setResType("bhwj02");
                atta.setCreateTime(DateUtils.getNowDate());
                atta.setStatus("0");
                srsAttachResourceMapper.insertSrsAttachResource(atta);
            }
            int res = rtClassMettingContentMapper.insertRtClassMettingContent(rtClassMettingContent);
            if(res == 0){
                throw new Exception(srsClass.getClassName()+"新增班会内容失败");
            }
            return AjaxResult.success();
        }catch(Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改班会内容
     * @param rtClassMettingContent 班会内容
     * @return 结果
     */
    @Override
    public int updateRtClassMettingContent(RtClassMettingContent rtClassMettingContent) {
        rtClassMettingContent.setUpdateTime(DateUtils.getNowDate());
        //先删除附件，再新增附件
        srsAttachResourceMapper.deleteSrsAttachResourceByResManNo(rtClassMettingContent.getCrNo());
        for (SrsAttachResource atta:rtClassMettingContent.getAttachResourceList()) {
            atta.setDelFlag("0");
            atta.setResNo(UUID.randomUUID().toString());
            atta.setResMainNo(rtClassMettingContent.getCrNo());
            atta.setResType("bhwj02");
            atta.setCreateTime(DateUtils.getNowDate());
            atta.setStatus("0");
            srsAttachResourceMapper.insertSrsAttachResource(atta);
        }
        return rtClassMettingContentMapper.updateRtClassMettingContent(rtClassMettingContent);
    }

    /**
     * 批量删除班会内容
     * @param crIds 需要删除的班会内容主键
     * @return 结果
     */
    @Override
    public int deleteRtClassMettingContentByCrIds(Long[] crIds) {
        return rtClassMettingContentMapper.deleteRtClassMettingContentByCrIds(crIds);
    }

    /**
     * 删除班会内容信息
     * @param crId 班会内容主键
     * @return 结果
     */
    @Override
    public int deleteRtClassMettingContentByCrId(Long crId) {
        return rtClassMettingContentMapper.deleteRtClassMettingContentByCrId(crId);
    }
}
