package com.qmth.paper.library.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.paper.library.business.bean.result.DictionaryResult;
import com.qmth.paper.library.business.bean.result.EditResult;
import com.qmth.paper.library.business.bean.result.SelectResult;
import com.qmth.paper.library.business.enums.DictionaryEnum;
import com.qmth.paper.library.business.service.*;
import com.qmth.paper.library.common.contant.ApiPrefixConstant;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.entity.BasicSemester;
import com.qmth.paper.library.common.entity.ExamStudent;
import com.qmth.paper.library.common.service.BasicSemesterService;
import com.qmth.paper.library.common.service.SysOrgService;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import com.qmth.paper.library.common.util.ServletUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 查询 前端控制器
 * </p>
 */
@Api(tags = "查询条件Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + ApiPrefixConstant.PREFIX_COMMON)
@Validated
public class ConditionController {

    @Resource
    private BasicExamService basicExamService;
    @Resource
    private BasicSemesterService basicSemesterService;
    @Resource
    private ExamCourseService examCourseService;
    @Resource
    private ExamStudentService basicStudentService;

    @ApiOperation(value = "查询条件-学期")
    @PostMapping("/semester/query")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result querySemester() {
        List<BasicSemester> semesters = basicSemesterService.querySemester();
        List<SelectResult> resultList = semesters.stream().map(m -> {
            SelectResult result = new SelectResult();
            result.setId(String.valueOf(m.getId()));
            result.setCode(m.getCode());
            result.setName(m.getName());
            return result;
        }).collect(Collectors.toList());
        return ResultUtil.ok(resultList);
    }

    @ApiOperation(value = "查询条件-考试")
    @PostMapping("/exam/query")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result queryExam(@ApiParam(value = "学期ID", required = true) @RequestParam(value = "semesterId") Long semesterId) {
        return ResultUtil.ok(basicExamService.listBySemesterId(semesterId));
    }

    @ApiOperation(value = "查询条件-课程")
    @PostMapping("/course/query")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = EditResult.class)})
    public Result queryCourse(@ApiParam(value = "考试ID") @RequestParam(value = "examId", required = false) Long examId) {
        List<SelectResult> courseList = examCourseService.listConditionByExamId(examId);
        return ResultUtil.ok(courseList);
    }

    @ApiOperation(value = "学院/专业/班级级联查询")
    @RequestMapping(value = "/college_major_class/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findDictionaryList(@ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                                     @ApiParam(value = "学院名称") @RequestParam(required = false) String collegeName,
                                     @ApiParam(value = "专业名称") @RequestParam(required = false) String majorName,
                                     @ApiParam(value = "查询字典对象", required = true) @RequestParam DictionaryEnum dictionaryEnum) {
        QueryWrapper<ExamStudent> studentQuery = new QueryWrapper<>();
        studentQuery.lambda().eq(ExamStudent::getExamId, examId);
        if (StringUtils.isNotBlank(collegeName)) {
            studentQuery.lambda().eq(ExamStudent::getCollegeName, collegeName);
        }
        if (StringUtils.isNotBlank(majorName)) {
            studentQuery.lambda().eq(ExamStudent::getMajorName, majorName);
        }
        List<DictionaryResult> dictionaryResultList = new ArrayList<>();
        switch (dictionaryEnum) {
            case COLLEGE:
                dictionaryResultList = basicStudentService.list(studentQuery).stream().filter(m -> StringUtils.isNotBlank(m.getCollegeName())).map(ExamStudent::getCollegeName).distinct().flatMap(e -> {
                    DictionaryResult dictionaryResult = new DictionaryResult();
                    dictionaryResult.setCode(e);
                    dictionaryResult.setName(e);
                    return Stream.of(dictionaryResult);
                }).collect(Collectors.toList());
                break;
            case MAJOR:
                dictionaryResultList = basicStudentService.list(studentQuery).stream().filter(m -> StringUtils.isNotBlank(m.getMajorName())).map(ExamStudent::getMajorName).distinct().flatMap(e -> {
                    DictionaryResult dictionaryResult = new DictionaryResult();
                    dictionaryResult.setCode(e);
                    dictionaryResult.setName(e);
                    return Stream.of(dictionaryResult);
                }).collect(Collectors.toList());
                break;
            case CLAZZ:
                dictionaryResultList = basicStudentService.list(studentQuery).stream().filter(m -> StringUtils.isNotBlank(m.getClassName())).map(ExamStudent::getClassName).distinct().flatMap(e -> {
                    DictionaryResult dictionaryResult = new DictionaryResult();
                    dictionaryResult.setCode(e);
                    dictionaryResult.setName(e);
                    return Stream.of(dictionaryResult);
                }).collect(Collectors.toList());
                break;
        }
        return ResultUtil.ok(dictionaryResultList);
    }
}
