package com.qmth.distributed.print.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.distributed.print.business.bean.result.PrintPlanBrief;
import com.qmth.distributed.print.business.enums.ExamDetailStatusEnum;
import com.qmth.distributed.print.business.enums.PrintPlanStatusEnum;
import com.qmth.distributed.print.business.mapper.ConditionMapper;
import com.qmth.distributed.print.business.service.BasicExamService;
import com.qmth.distributed.print.business.service.ConditionService;
import com.qmth.distributed.print.business.service.TeachCourseService;
import com.qmth.teachcloud.common.bean.dto.DataPermissionRule;
import com.qmth.teachcloud.common.entity.*;
import com.qmth.teachcloud.common.service.BasicRoleDataPermissionService;
import com.qmth.teachcloud.common.service.BasicSemesterService;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 查询条件 服务实现类
 * </p>
 */
@Service
public class ConditionServiceImpl implements ConditionService {

    @Resource
    BasicSemesterService basicSemesterService;

    @Resource
    BasicExamService basicExamService;

    @Resource
    BasicRoleDataPermissionService basicRoleDataPermissionService;

    @Resource
    TeachCourseService teachCourseService;

    @Resource
    ConditionMapper conditionMapper;

    @Override
    public List<BasicSemester> listSemester(Boolean enable) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        QueryWrapper<BasicSemester> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasicSemester::getSchoolId, schoolId);
        if (enable != null) {
            queryWrapper.lambda().eq(BasicSemester::getEnable, enable);
        }
        queryWrapper.lambda().orderByDesc(BasicSemester::getCreateTime);
        return basicSemesterService.list(queryWrapper);
    }

    @Override
    public List<BasicExam> listExam(Long semesterId, Boolean enable) {
        QueryWrapper<BasicExam> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasicExam::getSemesterId, semesterId);
        if (enable != null) {
            queryWrapper.lambda().eq(BasicExam::getEnable, enable);
        }
        return basicExamService.list(queryWrapper);
    }

    @Override
    public List<BasicCourse> listCourse(Long semesterId, Long examId, Boolean enable, List<String> printPlanIds) {
        Long privilegeId = ServletUtil.getCurrentPrivilegeId();
        String privilegeIdString = privilegeId == null ? null : String.valueOf(privilegeId);

        DataPermissionRule dpr = new DataPermissionRule();
        // 未找到权限菜单，默认为本人权限
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();

        // 入库申请菜单
        if ("40".equals(privilegeIdString)) {
            return conditionMapper.listCourseForApply(semesterId, examId, enable, sysUser.getId());
        }
        // 入库审核菜单
        else if ("41".equals(privilegeIdString)) {
            return conditionMapper.listCourseForAudit(semesterId, examId, enable);
        }
        // 成绩查询
        else if ("487".equals(privilegeIdString)) {
            List<BasicCourse> basicCourses = teachCourseService.findTeachCourseByUserId(sysUser.getId());
            if (basicCourses.isEmpty()) {
                dpr = basicRoleDataPermissionService.findDataPermission(privilegeId);
            }
            return conditionMapper.listCourseForScore(semesterId, examId, basicCourses, dpr);
        } else {
            dpr = basicRoleDataPermissionService.findDataPermission(privilegeId);
            return conditionMapper.listCourse(semesterId, examId, enable, printPlanIds, dpr);
        }
    }

    @Override
    public List<String> listPaperNumber(Long semesterId, Long examId, String courseCode, List<String> printPlanIds) {
        Long privilegeId = ServletUtil.getCurrentPrivilegeId();
        String privilegeIdString = privilegeId == null ? null : String.valueOf(privilegeId);
        DataPermissionRule dpr = basicRoleDataPermissionService.findDataPermission(privilegeId);
        // 未找到权限菜单，默认为本人权限
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();

        // 如果byAssignUser为true时，不能通过层级关系查询
        if (privilegeId == null) {
            dpr.setRequestUserId(sysUser.getId());
            dpr.setOrgIdSet(null);
        } else {
            dpr = basicRoleDataPermissionService.findDataPermission(privilegeId);
        }

        // 入库申请菜单
        if ("40".equals(privilegeIdString)) {
            return conditionMapper.listPaperNumberForApply(semesterId, examId, courseCode, sysUser.getId());
        }
        // 入库审核菜单
        else if ("41".equals(privilegeIdString)) {
            return conditionMapper.listPaperNumberAudit(semesterId, examId, courseCode);
        } else {
            return conditionMapper.listPaperNumber(semesterId, examId, courseCode, printPlanIds, dpr);
        }
    }

    @Override
    public List<PrintPlanBrief> listPrintPlan(Long semesterId, Long examId, String source) {
        Long privilegeId = ServletUtil.getCurrentPrivilegeId();
        DataPermissionRule dpr = new DataPermissionRule();
        // 未找到权限菜单，默认为本人权限
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();

        if ("client".equals(source)) {
            String[] examDetailStatus = new String[]{ExamDetailStatusEnum.NEW.name(), PrintPlanStatusEnum.READY.name()};
            return conditionMapper.listPrintPlanForClient(semesterId, examId, examDetailStatus);
        } else {
            // 如果byAssignUser为true时，不能通过层级关系查询
            if (privilegeId == null) {
                dpr.setRequestUserId(sysUser.getId());
                dpr.setOrgIdSet(null);
            } else {
                dpr = basicRoleDataPermissionService.findDataPermission(privilegeId);
            }
            return conditionMapper.listPrintPlan(semesterId, examId, dpr);
        }
    }

    @Override
    public List<BasicClazz> listClazz(Long semesterId, Long examId, String courseCode) {
        Long privilegeId = ServletUtil.getCurrentPrivilegeId();
        String privilegeIdString = privilegeId == null ? null : String.valueOf(privilegeId);
        DataPermissionRule dpr = basicRoleDataPermissionService.findDataPermission(privilegeId);
        if ("487".equals(privilegeIdString)) {
            return conditionMapper.listClazz(semesterId, examId, courseCode, dpr);
        }
        return null;
    }
}
