package com.xhkjedu.sexam.controller.report;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sexam.model.exam.EBase;
import com.xhkjedu.sexam.model.report.ERbase;
import com.xhkjedu.sexam.model.reportstu.ERstudent;
import com.xhkjedu.sexam.model.reportstu.ERstudentAction;
import com.xhkjedu.sexam.service.report.EReportSchoolService;
import com.xhkjedu.sexam.utils.ExamUtil;
import com.xhkjedu.sexam.vo.report.ERankParamVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ywx
 * @className ReportSchoolController
 * @description 校级报告
 * @date 2022/8/8 9:37
 **/
@RestController
@RequestMapping("ers")
public class EReportSchoolController {
    @Resource
    private EReportSchoolService eReportSchoolService;

    /**
     * 考试概况
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [rbase]
     * @Author ywx
     * @Date 2022/8/25 9:20
     **/
    @PostMapping("/get_ksgk")
    public ResultVo getKsGk(@RequestBody ERbase rbase) {
        Integer examid = rbase.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = eReportSchoolService.getKsGk(examid);
        return new ResultVo(0, "获取成功", map);
    }

    /**
     * 成绩单
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [stu]
     * @Author ywx
     * @Date 2022/8/25 9:36
     **/
    @PostMapping("/list_cjd")
    public ResultVo listCjd(@RequestBody ERstudent stu) {
        Integer examid = stu.getExamid();
        Integer page = stu.getPage();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Integer showrank = eReportSchoolService.getShowRank(examid);
        stu.setShowrank(showrank);
        if (N_Utils.isEmptyInteger(page)) {
            List<Map> list = eReportSchoolService.listCjd(stu);
            return new ResultVo(0, "获取成功", ExamUtil.listCjdOrder(list, showrank));
        } else {
            List<Map> list = eReportSchoolService.listCjd(stu);
            PageResult pageResult = ExamUtil.getCjdPageResult(stu, list);
            return new ResultVo(0, "获取成功", pageResult);
        }
    }

    /**
     * 成绩分析-班级成绩
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [rank]
     * @Author ywx
     * @Date 2022/9/9 10:05
     **/
    @PostMapping("/cjfx_bjcj")
    public ResultVo listCjFxBjCj(@RequestBody ERankParamVo rank) {
        N_Utils.validation(new Object[]{rank.getExamid(), "考试id", 1, rank.getSubjectid(), "科目id", 2});
        List<Map> list = eReportSchoolService.listCjFxBjCj(rank);
        return new ResultVo(0, "获取成功", list);
    }

    /*
     * 成绩分析-等级统计搜索
     * @Param [rank]
     * @Author ywx
     * @Date 2022/8/29 9:55
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/cjfx_ss")
    public ResultVo listCjFxForSearch(@RequestBody ERankParamVo rank) {
        N_Utils.validation(new Object[]{rank.getExamid(), "考试id", 1, rank.getSubjectid(), "科目id", 2});
        Map map = eReportSchoolService.listCjFxForSearch(rank);
        return new ResultVo(0, "获取成功", map);
    }

    /*
     * 成绩分析-等级统计设置
     * @Param [rank]
     * @Author ywx
     * @Date 2022/8/29 10:07
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/cjfx_sz")
    public ResultVo listCjFxForSet(@RequestBody ERankParamVo rank) {
        N_Utils.validation(new Object[]{rank.getExamid(), "考试id", 1, rank.getCreateid(), "操作人id", 1
                , rank.getSubjectid(), "科目id", 2});
        eReportSchoolService.listCjFxForSet(rank);
        return new ResultVo(0, "设置成功");
    }

    /*
     * 行为分析-班级
     * @Param [rank]
     * @Author ywx
     * @Date 2022/8/29 16:29
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_xw_bj")
    public ResultVo listXwBj(@RequestBody ERstudentAction rank) {
        N_Utils.validation(new Object[]{rank.getExamid(), "考试id", 1, rank.getActiontype(), "类型", 1});
        List<Map> map = eReportSchoolService.listXwBj(rank);
        return new ResultVo(0, "获取成功", map);
    }

    /*
     * 行为分析-学生
     * @Param [rank]
     * @Author ywx
     * @Date 2022/8/29 16:45
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_xw_xs")
    public ResultVo listXwXs(@RequestBody ERstudentAction rank) {
        Integer page = rank.getPage();
        Integer pageSize = rank.getPageSize();
        N_Utils.validation(new Object[]{rank.getExamid(), "考试id", 1, rank.getActiontype(), "类型", 1});
        if(N_Utils.isEmptyInteger(page)){
            List<Map> list = eReportSchoolService.listXwXs(rank);
            return new ResultVo(0, "获取成功", list);
        }else {
            PageHelper.startPage(page, pageSize);
            List<Map> list = eReportSchoolService.listXwXs(rank);
            PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
            return new ResultVo(0, "获取成功", pageResult);
        }
    }

    /*
     * 违纪行为分析-详情
     * @Param [rank]
     * @Author ywx
     * @Date 2022/8/29 16:54
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/xw_xq")
    public ResultVo listWjXq(@RequestBody ERstudentAction rank) {
        Integer page = rank.getPage();
        Integer pageSize = rank.getPageSize();
        N_Utils.validation(new Object[]{rank.getExamid(), "考试id", 1, rank.getActiontype(), "类型", 1
                , page, "显示页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = eReportSchoolService.listWjXq(rank);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /**
     * 等级分类列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [rank]
     * @Author ywx
     * @Date 2022/9/13 17:42
     **/
    @PostMapping("/list_djfl")
    public ResultVo listDjFl(@RequestBody ERankParamVo rank) {
        N_Utils.validation(new Object[]{rank.getExamid(), "考试id", 1, rank.getRanktype(), "类型", 1
                , rank.getSubjectid(), "科目id", 2});
        List<Map> map = eReportSchoolService.listDjFl(rank);
        return new ResultVo(0, "获取成功", map);
    }

    /**
     * @Description 已结束考试列表
     * @Param [paramsVo]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/9/20 16:52
     **/
    @PostMapping("/ends")
    public ResultVo listEndExams(@RequestBody EBase eBase) {
        Integer page = eBase.getPage();
        Integer pageSize = eBase.getPageSize();
        N_Utils.validation(new Object[]{eBase.getSchoolid(), "学校id", 1
                , page, "显示页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = eReportSchoolService.listEndExams(eBase);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0, "获取成功", pageResult);
    }

    /**
     * pdf报告
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [rank]
     * @Author ywx
     * @Date 2022/10/9 14:45
     **/
    @PostMapping("/pdf")
    public ResultVo toPdf(@RequestBody ERankParamVo rank) {
        Integer examid = rank.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = new HashMap();
        Map ksgk = eReportSchoolService.getPdfKsGk(examid);
        map.put("ksgk", ksgk);//考试概况
        List<Map> cjfxList = eReportSchoolService.listCjFxBjCj(rank);
        Map<String, List<Map>> cjfx = cjfxList.stream().collect(Collectors.groupingBy(c -> c.get("subjectname").toString()));
        map.put("cjfx", cjfx);//成绩分析
        List<Map> subjects = (List<Map>) ksgk.get("subjects");
        Map<String, String> subjectMap = subjects.stream().collect(Collectors.toMap(s -> s.get("subjectid").toString(),
                s -> s.get("subjectname").toString()));
        List<Map> djfx = eReportSchoolService.listCjFxByExamId(examid);//考试班级等级分析
        for (Map rc : djfx) {
            rc.put("subjectname", subjectMap.get(rc.get("subjectid").toString()));
        }
        map.put("djfx", djfx);//成绩分析-等级分析
        List<Map> xwList = eReportSchoolService.listXwBjByExamId(examid);
        List<Map> wjxw = xwList.stream().filter(x -> x.get("actiontype").toString().equals("1")).collect(Collectors.toList());
        map.put("wjfx", wjxw);//违纪行为分析
        List<Map> yxsj = xwList.stream().filter(x -> x.get("actiontype").toString().equals("2")).collect(Collectors.toList());
        map.put("yxsj", yxsj);//优秀试卷分析
        List<Map> wjsj = xwList.stream().filter(x -> x.get("actiontype").toString().equals("3")).collect(Collectors.toList());
        map.put("wjsj", wjsj);//违纪试卷分析
        return new ResultVo(0, "获取成功", map);
    }

    /**
     * @Description 获取考试PDF报告基础信息
     * @Param [rank]
     * @Author ywx
     * @Date 2023/3/2 15:38
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/pdf_info")
    public ResultVo getPdfInfo(@RequestBody ERankParamVo rank) {
        Integer examid = rank.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = eReportSchoolService.getPdfInfo(examid);
        return new ResultVo(0, "获取成功", map);
    }

    /*
     * 获取考试基本信息（科目、班级）
     * @Param [rbase]
     * @Author ywx
     * @Date 2022/11/21 11:05
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/exam")
    public ResultVo getExamSubjectClass(@RequestBody ERbase rbase) {
        Integer examid = rbase.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = eReportSchoolService.getExamSubjectClass(examid);
        return new ResultVo(0, "获取成功", map);
    }

    /*
    * @Description 取pdf报告状态和校级报告地址
    * @Author WN
    * @Date 2023/2/28 16:44
    */
    @PostMapping("/rstate")
    public ResultVo getExamReportstate(@RequestBody ERbase rbase) {
        Integer examid = rbase.getExamid();
        N_Utils.validation(new Object[]{examid, "考试id", 1});
        Map map = eReportSchoolService.getReportSchool(examid);
        return new ResultVo(0, "获取成功", map);
    }

}
