package com.xhkjedu.sstudy.controller.paper;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sstudy.mapper.system.UserMapper;
import com.xhkjedu.sstudy.model.paper.*;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudent;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.service.gradeclass.ClassService;
import com.xhkjedu.sstudy.service.paper.PaperAnalyzeService;
import com.xhkjedu.sstudy.service.paper.PaperService;
import com.xhkjedu.sstudy.service.subjectbook.DirectorService;
import com.xhkjedu.sstudy.service.system.SchoolService;
import com.xhkjedu.sstudy.vo.paper.CheckClassVo;
import com.xhkjedu.sstudy.vo.paper.PaperCAnalyzeVo;
import com.xhkjedu.sstudy.vo.paper.PaperParam;
import com.xhkjedu.sstudy.vo.paperstudent.PaperSearchVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.vo.paper.PaperListVo;
import com.xhkjedu.vo.system.UserVo;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName PaperController
 * Description TODO
 * Author WN
 * Date 2020/6/9 10:07
 **/
@RestController
@RequestMapping("/paper")
public class PaperController {
    @Resource
    private PaperService paperService;
    @Resource
    private PaperAnalyzeService paperAnalyzeService;
    @Resource
    private ClassService classService;
    @Resource
    private SchoolService schoolService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private DirectorService directorService;

    /**
     * 功能描述  答题卡作业--保存
     *
     * @param * @param paper
     * @return com.xhkjedu.vo.ResultVo
     * @author WN
     * @date 2020/6/9
     */
    @PostMapping("/saves")
    @Action("作业管理-增加附件作业")
    public ResultVo saveScantronPaper(@RequestBody TPaper paper) {
        String questionStr = paper.getQuestionStr();
        N_Utils.validation(new Object[]{questionStr, "答题卡json字符串", 1
                , paper.getPaperclass(), "作业类型", 1, paper.getPapername(), "作业名称", 2
                , paper.getSubjectid(), "科目id", 2, paper.getLsbid(), "教材id", 2});
        try {
            List<TPaperScantronQtype> qtypes = JSONObject.parseArray(questionStr, TPaperScantronQtype.class);
            Integer paperid = paperService.saveScantronPaper(paper, qtypes);
            if(paperid == 0){
                return new ResultVo(1, "保存失败");
            }else{
                return new ResultVo(0, "保存成功", paperid);
            }

        } catch (Exception e) {
            return new ResultVo(1, "保存失败");
        }
    }

    /**
     *功能描述 修改作业信息
     * @author WN
     * @date 2020/6/11
     * @param  * @param paper
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/ups")
    @Action("作业管理-修改附件作业")
    public ResultVo updateScantronPaper(@RequestBody TPaper paper){
        String questionStr = paper.getQuestionStr();
        N_Utils.validation(new Object[]{
                paper.getPaperid(),"作业id",1,
                questionStr,"答题卡json字符串",1
        });
        try {
            List<TPaperScantronQtype> qtypes = JSONObject.parseArray(questionStr,TPaperScantronQtype.class);
            boolean rtn = paperService.updateScantronPaper(paper,qtypes);
            if(rtn){
                return new ResultVo(0,"保存成功");
            }else{
                return new ResultVo(1,"保存失败");
            }
        } catch (Exception e) {
            return new ResultVo(1,"保存失败");
        }
    }

    /**
     * 作业详情
     * @Param [paper]
     * @Author ywx
     * @Date 2020/6/10 14:33
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/details")
    public ResultVo getPaperScantronDetial(@RequestBody TPaper paper){
        N_Utils.validation(new Object[]{paper.getPaperid(),"作业id",1});
        Map psvo = paperService.getPaperScantronDetial(paper.getPaperid());
        return new ResultVo(0,"获取答题卡作业详情成功",psvo);
    }


    /**
     *功能描述 删除作业--答题卡作业、题库作业
     * @author WN
     * @date 2020/6/9
     * @param  * @param paper
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("del")
    @Action("作业管理-删除作业")
    public ResultVo delPaper(@RequestBody TPaper paper){
        Integer result = paperService.delPaper(paper);
        if(result>0){
            return new ResultVo(0,"删除成功");
        }else{
            return new ResultVo(1,"删除失败");
        }
    }

    //保存题库作业
    @PostMapping("/saveq")
    @Action("作业管理-增加题库作业")
    public ResultVo saveQuestionPaper(@RequestBody TPaper paper) {
        String questionStr = paper.getQuestionStr();
        N_Utils.validation(new Object[]{questionStr, "试题json字符串", 1, paper.getPapertype(), "作业分类", 1
                , paper.getPaperclass(), "作业类型", 1, paper.getPapername(), "作业名称", 2
                , paper.getSubjectid(), "科目id", 2, paper.getLsbid(), "教材id", 2});
        List<TPaperQtype> qtypes = JSONObject.parseArray(questionStr, TPaperQtype.class);
        Integer paperid = null;
        try {
            paperid = paperService.saveQuestionPaper(paper, qtypes);
        } catch (Exception e) {
            return new ResultVo(1, "保存失败");
        }
        return new ResultVo(0, "保存成功", paperid);
    }

    /**
     * 修改结构性试题作业
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [paper]
     * @Author ywx
     * @Date 2020/6/28 17:20
     **/
    @PostMapping("/updateq")
    @Action("作业管理-修改题库作业")
    public ResultVo updateQuestionPaper(@RequestBody TPaper paper) {
        String questionStr = paper.getQuestionStr();
        N_Utils.validation(new Object[]{questionStr, "试题json字符串", 1, paper.getPapertype(), "作业分类", 1
                , paper.getPaperclass(), "作业类型", 1, paper.getPapername(), "作业名称", 2});
        List<TPaperQtype> qtypes = JSONObject.parseArray(questionStr, TPaperQtype.class);
        try {
            boolean rtn = paperService.updateQuestionPaper(paper, qtypes);
            if (rtn) {
                return new ResultVo(0, "修改成功");
            } else {
                return new ResultVo(1, "修改失败");
            }
        } catch (Exception e) {
            return new ResultVo(1, "修改失败");
        }

    }

    /**
     * 作业详情
     * @Param [paper]
     * @Author ywx
     * @Date 2020/6/10 14:33
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TPaper paper){
        Integer paperid = paper.getPaperid();
        N_Utils.validation(new Object[]{paperid,"作业id",1});
        Map paperVo = paperService.findById(paperid, paper.getSchoolid());
        return new ResultVo(0,"获取详情成功",paperVo);
    }

    /**
     *功能描述 作业分析
     * @author WN
     * @date 2020/6/11
     * @param  * @param paper
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/analyze")
    public ResultVo analyze(@RequestBody TPaper paper) {
        Integer paperid = paper.getPaperid();
        N_Utils.validation(new Object[]{paperid,"作业id",1});
        TPaperAnalyze paperAnalyze = paperAnalyzeService.getAnalyzeByPaperid(paper.getPaperid());
        return new ResultVo(0,"作业分析成功",paperAnalyze);
    }

    /*
     * @Description 作业列表-全部
     * @Param [paper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list")
    public ResultVo list(@RequestBody PaperSearchVo paper) {
        Integer createid = paper.getCreateid();
        Integer page = paper.getPage();
        Integer pageSize = paper.getPageSize();
        N_Utils.validation(new Object[]{createid,"教师id",1,page,"显示页码",1,pageSize,"显示条数",1});
        UserVo userVo = userMapper.getUserInfoForHandleLog(createid);
        if (userVo != null && !userVo.getUsertype().equals(0)) {
            //获取当前教师权限班级
            Map classMap = classService.getQxClassidsByTeacherid(createid, null);
            String bxclassids = classMap.get("bxclassids").toString();//本校权限班级
            paper.setBxclassids(bxclassids);
            paper.setClassids(classMap.get("classids").toString());
        }
        //获取所选章节信息
        if(N_Utils.isNotEmpty(paper.getDirectorid()) && !"0".equals(paper.getDirectorid())){
            TDirector director = directorService.getDirectorById(paper.getDirectorid());
            paper.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                paper.setDirectorall(director.getDirectorall());
            }
        }
        PageHelper.startPage(page,pageSize);
        List<PaperListVo> list = paperService.findAll(paper, userVo);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0,"获取作业列表成功",pageResult);
    }

    /*
     * @Description 作业列表-我发布
     * @Param [paper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_my")
    public ResultVo listMy(@RequestBody PaperSearchVo paper) {
        Integer createid = paper.getCreateid();
        Integer page = paper.getPage();
        Integer pageSize = paper.getPageSize();
        N_Utils.validation(new Object[]{createid,"教师id",1,page,"显示页码",1,pageSize,"显示条数",1});
        UserVo userVo = userMapper.getUserInfoForHandleLog(createid);
        // if (userVo != null && !userVo.getUsertype().equals(0)) {
        //     //获取当前教师权限班级
        //     Map classMap = classService.getQxClassidsByTeacherid(createid, null);
        //     String bxclassids = classMap.get("bxclassids").toString();//本校权限班级
        //     paper.setBxclassids(bxclassids);
        //     paper.setClassids(classMap.get("classids").toString());
        // }

        //获取所选章节信息
        if(N_Utils.isNotEmpty(paper.getDirectorid()) && !"0".equals(paper.getDirectorid())){
            TDirector director = directorService.getDirectorById(paper.getDirectorid());
            paper.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                paper.setDirectorall(director.getDirectorall());
            }
        }

        PageHelper.startPage(page,pageSize);
        List<PaperListVo> list = paperService.findAllMy(paper, userVo);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0,"获取作业列表成功",pageResult);
    }

    //保存批阅方式
    @PostMapping("/set_checktype")
    @Action("作业管理-设置批阅方式")
    public ResultVo setCheckType(@RequestBody PaperParam param) {
        N_Utils.validation(new Object[]{param.getPaperid(),"试卷id",1,param.getChecktype(),"批改方式",1});
        return paperService.setCheckType(param);
    }

    @PostMapping("/check_students")
    public ResultVo checkStudents(@RequestBody TPaperStudent param) {
        Integer paperid = param.getPaperid();
        Integer checkid = param.getCheckid();
        Integer classid = param.getClassid();
        N_Utils.validation(new Object[]{paperid,"作业id",1,classid,"班级id",1});
        CheckClassVo checkVo = paperService.listStudentsByPaperIdCheckId(paperid, checkid, classid);
        return new ResultVo(0,"获取批改学生列表成功",checkVo);
    }

    /**
     * 教师批阅列表
     * @Param [paper]
     * @Author ywx
     * @Date 2020/6/18 14:55
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/correct_list")
    public ResultVo correctList(@RequestBody TPaper paper) {
        Integer checkid = paper.getCreateid();
        Integer page = paper.getPage();
        Integer pageSize = paper.getPageSize();
        N_Utils.validation(new Object[]{checkid,"批阅人id",1,page,"显示页码",1,pageSize,"显示条数",1});
        Integer schoolid = schoolService.getSchoolIdByUserid(checkid);
        PageHelper.startPage(page,pageSize);
        List<PaperListVo> list = paperService.correctList(checkid, schoolid);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0,"获取教师批阅列表成功",pageResult);
    }

    @PostMapping("/paper_file")
    public ResultVo listPaperFile(@RequestBody TPaperFile file){
        N_Utils.validation(new Object[]{file.getPaperid(),"作业id",1});
        List<TPaperFile> list = paperService.listPaperFile(file.getPaperid(),file.getFileclass());
        return new ResultVo(0,"获取作业附件列表成功",list);
    }

    @PostMapping("/commit_analyze")
    public ResultVo commitAnalyze(@RequestBody TPaper paper) {
        Integer papernum = paper.getPapernum();
        Integer createid = paper.getCreateid();
        N_Utils.validation(new Object[]{papernum,"统计次数",1,createid,"教师id",1});
        List<PaperCAnalyzeVo> list = paperAnalyzeService.listCommitAnalyze(papernum, createid);
        return new ResultVo(0,"获取作业提交情况成功",list);
    }

    @PostMapping("/list_sourcepath")
    public ResultVo listSourcePath(@RequestBody TPaper paper) {
        Integer paperid = paper.getPaperid();
        N_Utils.validation(new Object[]{paperid,"作业id",1});
        List<String> list = paperService.listSourcePath(paperid);
        return new ResultVo(0,"获取作业原文件保存地址列表成功",list);
    }

    @PostMapping("/get_papernum")
    public ResultVo getPaperNum(@RequestBody TPaper paper) {
        String subjectid = paper.getSubjectid();
        Integer createid = paper.getCreateid();
        Integer createtime = paper.getCreatetime();
        N_Utils.validation(new Object[]{subjectid,"科目id",2,createid,"用户id",1,createtime,"当前日期时间戳",1});
        String paperNum = paperService.getPaperNum(subjectid, createid, createtime);
        return new ResultVo(0,"获取今天布置作业数量成功",paperNum);
    }

    @PostMapping("/get_paperstate")
    public ResultVo getPaperState(@RequestBody TPaper paper) {
        Integer paperid = paper.getPaperid();
        N_Utils.validation(new Object[]{paperid,"作业id",1});
        Integer paperstate = paperService.getPaperState(paperid);
        return new ResultVo(0,"获取作业状态成功",paperstate);
    }

    @PostMapping("/paper_class")
    public ResultVo paperClass(@RequestBody TPaper paper) {
        Integer paperid = paper.getPaperid();
        Integer schoolid = paper.getSchoolid();
        N_Utils.validation(new Object[]{paperid, "作业id", 1, schoolid, "学校id", 1});
        List<Map> list = paperService.listPaperClass(paperid, paper.getCreateid(), schoolid);
        return new ResultVo(0, "获取批阅班级列表成功", list);
    }

    /**
     * @Description 获取批阅班级数量
     * @Date 2024/9/23 10:28
     * @Author YWX
     * @Param [paper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/check_classnum")
    public ResultVo getCheckClassNum(@RequestBody TPaper paper) {
        Integer paperid = paper.getPaperid();
        N_Utils.validation(new Object[]{paperid, "作业id", 1});
        Integer num = paperService.getCheckClassNum(paperid, paper.getCreateid());
        return new ResultVo(0, "获取批阅班级列表成功", num);
    }

    /*
    * @Description 该方法用于重新生成之前的作业报告
    * @Author WN
    * @Date 2022/10/18 15:34:21
    */
    @GetMapping("/hpa")
    public ResultVo handlePaperAnalyze(Integer paperid) {
        paperService.listPapersForAnalyze(paperid);
        return new ResultVo(0, "成功处理作业分析");
    }

    /*
    * @Description 处理第一次（上方法）未处理靶向作业
    * @Author WN
    * @Date 2023/5/31 15:15:03
    */
    @GetMapping("/hpa2")
    public ResultVo handlePaperAnalyze2(Integer paperid) {
        paperService.listPapersForAnalyze2(paperid);
        return new ResultVo(0, "成功处理作业分析");
    }

    //获取作业中听力文件
    @PostMapping("/hfile")
    public ResultVo getPaperHearfile(@RequestBody TPaper paper) {
        Integer paperid = paper.getPaperid();
        N_Utils.validation(new Object[]{paperid, "作业id", 1});
        String hearfile = paperService.getHearfileByPaperid(paperid);
        return new ResultVo(0, "成功获取作业听力文件", hearfile);
    }

    /*
    * @Description 获取批阅方式页面内容
    * @Author WN
    * @Date 2023/4/14 11:25
    */
    @PostMapping("/pcheckt")
    public ResultVo getPaperCheckTypeInfo(@RequestBody PaperParam param) {
        N_Utils.validation(new Object[]{param.getPaperid(), "作业id", 1,param.getSchoolid(),"学校id",1});
        Map rtnMap = paperService.getPaperCheckClassStu(param);
        return new ResultVo(0, "成功获取作业教师和学生", rtnMap);
    }

    /**
     * @Description 修改补交状态
     * @Date 2024/4/1 12:45
     * @Author YWX
     * @Param [paper]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/update_ac")
    public ResultVo updateAfterCommit(@RequestBody TPaper paper) {
        N_Utils.validation(new Object[]{paper.getPaperid(), "作业id", 1});
        paperService.updateAfterCommit(paper);
        return new ResultVo(0, "修改成功！");
    }

}
