package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.FormalParentsStudent;
import com.ruoyi.common.core.domain.entity.FormalTeacherClass;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.score.ScoreData;
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.RecordLeaveMapper;
import com.ruoyi.system.domain.RecordLeave;
import com.ruoyi.system.service.IRecordLeaveService;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 请假记录信息Service业务层处理
 * 
 * @author boman
 * @date 2023-08-08
 */
@Service
public class RecordLeaveServiceImpl implements IRecordLeaveService 
{
    @Autowired
    private RecordLeaveMapper recordLeaveMapper;

    @Autowired
    private FormalTeacherClassMapper formalTeacherClassMapper;

    @Autowired
    private FormalParentsStudentMapper formalParentsStudentMapper;

    /**
     * 查询请假记录信息
     * 
     * @param id 请假记录信息主键
     * @return 请假记录信息
     */
    @Override
    public RecordLeave selectRecordLeaveById(Long id)
    {
        RecordLeave recordLeave = recordLeaveMapper.selectRecordLeaveById(id);

        //更新为查看
        RecordLeave recordLeavePass = new RecordLeave();
        recordLeavePass.setId(id);
        recordLeavePass.setIsPass("2");
        recordLeaveMapper.updateRecordLeave(recordLeavePass);
        return recordLeave;
    }

    /**
     * 查询请假记录信息列表
     * 
     * @param recordLeave 请假记录信息
     * @return 请假记录信息
     */
    @Override
    public List<RecordLeave> selectRecordLeaveList(RecordLeave recordLeave)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        StringBuilder classId = new StringBuilder("0,");
        Map<String, Object> params = recordLeave.getParams();
        String role = "admin";
        if (params != null && params.size() > 0) {
            role = (String) params.get("role");
        }
        List<RecordLeave> recordLeaveList = new ArrayList<>();
        if ("teacher".equals(role)) {
            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.append(teacherClass.getClassId()).append(",");
                }
            }
            if (StringUtils.isNotBlank(classId.toString())) {
                classId = new StringBuilder(classId.substring(0, classId.length() - 1));
            }
            startPage();
            recordLeave.setClassId(classId.toString());
            recordLeaveList = recordLeaveMapper.selectRecordLeaveList(recordLeave);
        } else if ("parents".equals(role)) {
            FormalParentsStudent formalParentsStudent = new FormalParentsStudent();
            formalParentsStudent.setParentsId(user.getUserId());
            List<FormalParentsStudent> formalParentsStudents = formalParentsStudentMapper.selectFormalParentsStudentList(formalParentsStudent);
            for (FormalParentsStudent parentsStudent : formalParentsStudents) {
                classId.append(parentsStudent.getClassId()).append(",");
            }
            if (StringUtils.isNotBlank(classId.toString())) {
                classId = new StringBuilder(classId.substring(0, classId.length() - 1));
            }
            startPage();
            recordLeave.setClassId(classId.toString());
            recordLeaveList = recordLeaveMapper.selectRecordLeaveList(recordLeave);
        } else if ("admin".equals(role)) {
            startPage();
            recordLeaveList = recordLeaveMapper.selectRecordLeaveList(recordLeave);
        }

        return recordLeaveList;
    }

    /**
     * app请假记录数量通知
     * @param recordLeave
     * @return
     */
    @Override
    public AjaxResult selectRecordLeaveNum(RecordLeave recordLeave) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        StringBuilder classId = new StringBuilder("0,");
        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.append(teacherClass.getClassId()).append(",");
            }
        }
        if (StringUtils.isNotBlank(classId.toString())) {
            classId = new StringBuilder(classId.substring(0, classId.length() - 1));
        }
        recordLeave.setClassId(classId.toString());
        int num =recordLeaveMapper.selectRecordLeaveNum(recordLeave);
        return AjaxResult.success(num);
    }

    /**
     * 新增请假记录信息
     * 
     * @param recordLeave 请假记录信息
     * @return 结果
     */
    @Override
    public int insertRecordLeave(RecordLeave recordLeave)
    {
        recordLeave.setCreateTime(DateUtils.getNowDate());
        return recordLeaveMapper.insertRecordLeave(recordLeave);
    }

    /**
     * 修改请假记录信息
     * 
     * @param recordLeave 请假记录信息
     * @return 结果
     */
    @Override
    public int updateRecordLeave(RecordLeave recordLeave)
    {
        recordLeave.setUpdateTime(DateUtils.getNowDate());
        return recordLeaveMapper.updateRecordLeave(recordLeave);
    }

    /**
     * 批量删除请假记录信息
     * 
     * @param ids 需要删除的请假记录信息主键
     * @return 结果
     */
    @Override
    public int deleteRecordLeaveByIds(Long[] ids)
    {
        return recordLeaveMapper.deleteRecordLeaveByIds(ids);
    }

    /**
     * 删除请假记录信息信息
     * 
     * @param id 请假记录信息主键
     * @return 结果
     */
    @Override
    public int deleteRecordLeaveById(Long id)
    {
        return recordLeaveMapper.deleteRecordLeaveById(id);
    }
}
