package com.xhkjedu.sstudy.controller.paperstudent;

import com.xhkjedu.annotation.Action;
import com.xhkjedu.constant.PlatformConstant;
import com.xhkjedu.sstudy.model.paperstudent.TPaperClass;
import com.xhkjedu.sstudy.service.paperstudent.PaperClassService;
import com.xhkjedu.sstudy.vo.paperstudent.PaperGroupParam;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @ClassName PaperGroupController
 * Description 作业--班级分组
 * Author WN
 * Date 2020/6/9 11:01
 **/
@RestController
@RequestMapping("/pg")
public class PaperClassController {
    @Autowired
    private PaperClassService paperClassService;
    @Resource
    private HttpServletRequest request;

    //发布作业（兼容靶向作业）
    @PostMapping("/save2")
    @Action("作业管理-发布作业")
    public ResultVo save2(@RequestBody PaperGroupParam paperGroupParam){
        N_Utils.validation(new Object[]{paperGroupParam.getPaperid(),"作业id",1,
                paperGroupParam.getShowtype(),"公布答案",1,
                paperGroupParam.getSelclass(),"班级",1,
                paperGroupParam.getTimetype(),"时间类型",1,
                paperGroupParam.getCommittype(),"提交形式",2});
        return paperClassService.savePaperClassStu(paperGroupParam);
    }


    @PostMapping("/paper_student_list")
    public ResultVo listPaperStudent(@RequestBody TPaperClass group) {
        Integer paperid = group.getPaperid();
        Integer schoolid = group.getSchoolid();
        N_Utils.validation(new Object[]{paperid, "作业id", 1, schoolid, "学校id", 1});
        List<Map> list = paperClassService.listPaperStudent(paperid, schoolid);
        Integer checkid = request.getIntHeader(PlatformConstant.XH_USER_ID);
        Integer checknum = 0;//可批阅人数
        if (N_Utils.isTrueInteger(checkid)) {
            checknum = paperClassService.getCheckNum(paperid, checkid);
        }
        Map map = new HashMap();
        map.put("students", list);
        map.put("checknum", checknum);
        return new ResultVo(0, "获取参与作业学生列表成功", map);
    }

    //作业发布对象
    @PostMapping("/list_object")
    public ResultVo listObject(@RequestBody TPaperClass group) {
        Integer paperid = group.getPaperid();
        Integer createid = group.getCreateid();
        Integer papertype = group.getPapertype();
        Integer schoolid = group.getSchoolid();
        N_Utils.validation(new Object[]{paperid, "试卷id", 1, createid, "发布人id", 1
                , papertype, "作业类型", 1, schoolid, "学校id", 1});
        Map list = paperClassService.listObject(paperid, createid, papertype, schoolid);
        if(N_Utils.isEmpty(list.get("classes"))){
            return new ResultVo(1,"发布对象为空，请联系管理员设置班级权限");
        }else{
            return new ResultVo(0, "获取发布对象成功", list);
        }
    }

    /**
     * @Description 作业发布对象-分组学生列表
     * @Param [group]
     * @Author ywx
     * @Date 2023/4/14 11:34
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_gs")
    public ResultVo listGroupStudent(@RequestBody TPaperClass group) {
        Integer paperid = group.getPaperid();
        Integer papertype = group.getPapertype();
        Integer groupid = group.getGroupid();
        N_Utils.validation(new Object[]{paperid, "试卷id", 1, papertype, "作业类型", 1
                , groupid, "分组id", 1});
        List<Map> list = paperClassService.listGroupStudent(paperid, papertype, groupid);
        return new ResultVo(0, "获取发布对象成功", list);
    }
}
