package com.xhkjedu.sstudy.controller.resource;

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.resource.TGuide;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.service.gradeclass.ClassService;
import com.xhkjedu.sstudy.service.resource.GuideService;
import com.xhkjedu.sstudy.service.subjectbook.DirectorService;
import com.xhkjedu.sstudy.vo.paper.AnalyzeSearchVo;
import com.xhkjedu.sstudy.vo.resource.GuideAnalyzeVo;
import com.xhkjedu.sstudy.vo.resource.GuideVo;
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.system.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
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.List;
import java.util.Map;

/**
 * @ClassName GuideController
 * Description TODO 学生预习
 * Author WN
 * Date 2021/10/20 10:38
 **/
@RequestMapping("/guide")
@RestController
@Slf4j
public class GuideController {
    @Resource
    private GuideService guideService;
    @Resource
    private ClassService classService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private DirectorService directorService;

    //教师网页端预习列表
    @PostMapping("/teaweb")
    public ResultVo listGuidesForTeacherWeb(@RequestBody TGuide guide) {
        Integer page = guide.getPage();
        Integer pageSize = guide.getPageSize();
        Integer createid = guide.getCreateid();
        String subjectid = guide.getSubjectid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,createid,"教师id",1
                ,subjectid,"科目",2});
        UserVo userVo = userMapper.getUserInfoForHandleLog(createid);
        if (userVo != null && !userVo.getUsertype().equals(0)) {
            Map<String, String> map = classService.getQxClassidsByTeacherid(createid, null);
            guide.setClassids(map.get("classids"));
            guide.setBxclassids(map.get("bxclassids"));
            if (N_Utils.isEmpty(guide.getSchoolid())) {
                guide.setSchoolid(N_Utils.str2Int(map.get("userschoolid")));
            }
        }

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

        PageHelper.startPage(page, pageSize);
        List<GuideVo> list = guideService.listGuidesForTeacherWeb(guide, userVo);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0,"获取教师预习列表成功",pageResult);
    }
    //教师网页端预习列表
    @PostMapping("/teaweb_my")
    public ResultVo listGuidesForTeacherWebMy(@RequestBody TGuide guide) {
        Integer page = guide.getPage();
        Integer pageSize = guide.getPageSize();
        String subjectid = guide.getSubjectid();
        Integer createid = guide.getCreateid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,createid,"教师id",1
                ,subjectid,"科目",2});

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

        PageHelper.startPage(page, pageSize);
        List<GuideVo> list = guideService.listGuidesForTeacherWebMy(guide);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0,"获取教师预习列表成功",pageResult);
    }

    //教师app预习列表
    @PostMapping("/teaapp")
    public ResultVo listGuidesForTeacherApp(@RequestBody TGuide guide) {
        Integer page = guide.getPage();
        Integer pageSize = guide.getPageSize();
        Integer createid = guide.getCreateid();
        String subjectid = guide.getSubjectid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,createid,"教师id",1
                ,subjectid,"科目",2});
        Map<String, String> map = classService.getQxClassidsByTeacherid(createid, null);
        guide.setClassids(map.get("classids"));
        guide.setBxclassids(map.get("bxclassids"));
        if (N_Utils.isEmpty(guide.getSchoolid())) {
            guide.setSchoolid(N_Utils.str2Int(map.get("userschoolid")));
        }

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

        guide.setPage((page - 1)*pageSize);
        List<Map> list = guideService.listGuidesForTeacherApp(guide);
        return new ResultVo(0,"获取教师预习列表成功",list);
    }

    /*
    * @Description 预习关联权限班级
    * @Author WN
    * @Date 2023/4/21 9:00
    */
    @PostMapping("/list_class")
    public ResultVo listClass(@RequestBody TGuide guide) {
        Integer guideid = guide.getGuideid();
        N_Utils.validation(new Object[]{guideid, "预习id", 1,guide.getCreateid(),"教师id",1
                ,guide.getSchoolid(),"学校id",1});
        Map<String, String> map = classService.getQxClassidsByTeacherid(guide.getCreateid(), guideService.getYearById(guideid));
        guide.setClassids(map.get("classids"));
        guide.setBxclassids(map.get("bxclassids"));
        Map list = guideService.listGuideClassByGuide(guide);
        return new ResultVo(0, "获取预习班级成功", list);
    }
    //学生网页端预习列表
    @PostMapping("/stuweb")
    public ResultVo listGuidesForStudentWeb(@RequestBody TGuide guide) {
        Integer page = guide.getPage();
        Integer pageSize = guide.getPageSize();
        Integer studentid = guide.getStudentid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,studentid,"学生id",1});

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

        PageHelper.startPage(page, pageSize);
        List<GuideVo> list = guideService.listGuidesForStuWeb(guide);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo<>(list));
        return new ResultVo(0,"获取学生预习列表成功",pageResult);
    }
    //学生APP预习列表
    @PostMapping("/stuapp")
    public ResultVo listGuidesForStuApp(@RequestBody TGuide guide) {
        Integer page = guide.getPage();
        Integer pageSize = guide.getPageSize();
        Integer studentid = guide.getStudentid();
        String subjectid = guide.getSubjectid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,studentid,"学生id",1
                ,subjectid,"科目",2});
        //获取所选章节信息
        if(N_Utils.isNotEmpty(guide.getDirectorid()) && !"0".equals(guide.getDirectorid())){
            TDirector director = directorService.getDirectorById(guide.getDirectorid());
            guide.setDirectorLeaf(director.getLeaf());
            //leaf 是否是叶子节点0不是1是
            if(director.getLeaf() == 0){
                //说明不是叶子节点，需要获取其子章节所有信息
                guide.setDirectorall(director.getDirectorall());
            }
        }
        guide.setPage((page - 1)*pageSize);
        List<Map> list = guideService.listGuidesForStuApp(guide);
        return new ResultVo(0,"获取学生预习列表成功",list);
    }

    //详情
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TGuide guide) {
        N_Utils.validation(new Object[]{guide.getGuideid(),"预习id",1});

        GuideVo guideVo = guideService.getGuideByGuideid(guide.getGuideid());
        if(guideVo!=null){
            return new ResultVo(0, "获取预习详情成功",guideVo);
        }else{
            return new ResultVo(1, "预习不存在");
        }
    }



    //保存预习
    @PostMapping("/add")
    @Action("预习管理-增加")
    public ResultVo add(@RequestBody TGuide guide) {
        Integer createid = guide.getCreateid();
        String subjectid = guide.getSubjectid();
        String lsbid = guide.getLsbid();
        N_Utils.validation(new Object[]{createid,"教师id",1,subjectid,"科目",2,lsbid,"教材",2});

        return guideService.save(guide);
    }

    //分享预习
    @PostMapping("/share")
    @Action("预习管理-分享")
    public ResultVo shareGuide(@RequestBody TGuide guide) {
        if (CollectionUtils.isEmpty(guide.getGuideGroups()) && CollectionUtils.isEmpty(guide.getJtrclist())) {
            return new ResultVo(1, "未选择班级");
        }
        String resourceid = guide.getResourceid();
        Integer createid = guide.getCreateid();
        String subjectid = guide.getSubjectid();
        String lsbid = guide.getLsbid();
        N_Utils.validation(new Object[]{resourceid,"资源",2,createid,"教师id",1,subjectid,"科目",2,lsbid,"教材",2});

        return guideService.shareGuide(guide);
    }

    //删除预习
    @PostMapping("/del")
    @Action("预习管理-删除")
    public ResultVo delete(@RequestBody TGuide guide) {
        N_Utils.validation(new Object[]{guide.getGuideid(),"预习id",1});

        guideService.delete(guide.getGuideid());
        return new ResultVo(0, "删除成功");
    }

    //////统计相关//////

    @PostMapping("/preview_analyze")
    public ResultVo previewAnalyze(@RequestBody AnalyzeSearchVo paper) {
        if (paper.getUsertype() == null) return new ResultVo(1,"用户类型不能为空");
        Integer schoolid = paper.getSchoolid();
        Integer gradeid = paper.getGradeid();
        N_Utils.validation(new Object[]{schoolid,"学校id",1,gradeid,"年级id",1});
        List<GuideAnalyzeVo> list = guideService.getPreviewAnalyze(paper);
        return new ResultVo(0,"获取年级下学生预习统计列表成功",list);
    }

    @PostMapping("/preview_list")
    public ResultVo listPreview(@RequestBody AnalyzeSearchVo paper) {
        Integer page = paper.getPage();
        Integer pageSize = paper.getPageSize();
        Integer teacherid = paper.getTeacherid();
        Integer gradeid = paper.getGradeid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,teacherid,"教师id",1
                ,gradeid,"年级id",1,paper.getCreateid(),"创建人id",1});
        //guideService.setYear(paper);
        guideService.setClassIds(paper);
        PageHelper.startPage(page, pageSize);
        List<Map> list = guideService.listPreview(paper);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取年级下教师发布学生预习列表成功",pageResult);
    }

    @PostMapping("/preview_stu")
    public ResultVo previewStu(@RequestBody AnalyzeSearchVo paper){
        if (paper.getUsertype() == null) return new ResultVo(1,"用户类型不能为空");
        Integer schoolid = paper.getSchoolid();
        Integer gradeid = paper.getGradeid();
        Integer page = paper.getPage();
        Integer pageSize = paper.getPageSize();
        N_Utils.validation(new Object[]{schoolid,"学校id",1,gradeid,"年级id",1
                ,page,"显示页码",1,pageSize,"显示条数",1});
        //guideService.setYear(paper);
        guideService.setClassIds(paper);
        PageHelper.startPage(page, pageSize);
        List<Map> list = guideService.listPreviewStuGrade(paper);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取预习统计--按学生列表成功",pageResult);
    }

    @PostMapping("/preview_sdetail")
    public ResultVo listPreviewStuDetail(@RequestBody AnalyzeSearchVo paper) {
        Integer studentid = paper.getStudentid();
        Integer gradeid = paper.getGradeid();
        N_Utils.validation(new Object[]{studentid,"学生id",1,gradeid,"年级id",1
                ,paper.getSchoolid(),"学校id",1});
        if (paper.getUsertype() == null) return new ResultVo(1,"用户类型不能为空");
        //guideService.setYear(paper);
        Map map = guideService.listPreviewStuDetail(paper);
        return new ResultVo(0,"获取预习统计--按学生详情成功",map);
    }

    @PostMapping("/preview_sexport")
    public ResultVo listPaperStuExport(@RequestBody AnalyzeSearchVo paper) {
        if (paper.getUsertype() == null) return new ResultVo(1,"用户类型不能为空");
        Integer schoolid = paper.getSchoolid();
        Integer gradeid = paper.getGradeid();
        N_Utils.validation(new Object[]{schoolid,"学校id",1,gradeid,"年级id",1});
        Map list = guideService.listPreviewStuExport(paper);
        return new ResultVo(0,"获取预习统计--按学生导出列表成功",list);
    }

    /*
     * @Description 预习是否存在
     * @Date 2023/12/4 17:06:45
     * @Author WN
     * @Param [guide]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/isexits")
    public ResultVo isExits(@RequestBody TGuide guide) {
        N_Utils.validation(new Object[]{guide.getGuideid(),"预习id",1});
        return new ResultVo(0, "获取成功",guideService.getGuideidInfo(guide.getGuideid()));
    }
}
