package com.xjrsoft.module.assessment.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerPageDto;
import com.xjrsoft.module.assessment.dto.AssessmentPlanAnswerStudentDto;
import com.xjrsoft.module.assessment.dto.AssessmentPlanSubmitDto;
import com.xjrsoft.module.assessment.dto.AssessmentTemplatePlanPageDto;
import com.xjrsoft.module.assessment.dto.AssessmentTemplatePlanSureDto;
import com.xjrsoft.module.assessment.service.IAssessmentTemplatePlanService;
import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerClassResultVo;
import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerStudentPageVo;
import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerStudentVo;
import com.xjrsoft.module.assessment.vo.AssessmentPlanAnswerTeacherPageVo;
import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanPageVo;
import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanQuestionVo;
import com.xjrsoft.module.assessment.vo.AssessmentTemplatePlanResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 考核计划
* @Author dzx
* @Date: 2024-04-01
* @Version 1.0
*/
@RestController
@RequestMapping("/assessment" + "/assessmentTemplateAnswer")
@Api(value = "/assessment"  + "/assessmentTemplateAnswer",tags = "考核计划答题代码")
@AllArgsConstructor
public class AssessmentPlanAnswerController {


    private final IAssessmentTemplatePlanService planService;

    @GetMapping(value = "/student-page")
    @ApiOperation(value="考核计划答题列表-学生端(分页)")
    @SaCheckPermission("assessmenttemplateplan:detail")
    public RT<PageOutput<AssessmentPlanAnswerStudentPageVo>> studentPage(@Valid AssessmentPlanAnswerPageDto dto){
        dto.setStudentUserId(StpUtil.getLoginIdAsLong());
        Page<AssessmentPlanAnswerStudentPageVo> page = planService.getStudentPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<AssessmentPlanAnswerStudentPageVo> pageOutput = ConventPage.getPageOutput(page, AssessmentPlanAnswerStudentPageVo.class);
        return RT.ok(pageOutput);
    }

//    @GetMapping(value = "/result-page")
//    @ApiOperation(value="考核计划答题列表-学生端(分页)")
//    @SaCheckPermission("assessmenttemplateplan:detail")
//    public RT<PageOutput<AssessmentPlanAnswerStudentPageVo>> resultPage(@Valid AssessmentPlanAnswerPageDto dto){
//        dto.setStudentUserId(StpUtil.getLoginIdAsLong());
//        Page<AssessmentPlanAnswerStudentPageVo> page = planService.getStudentPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
//        PageOutput<AssessmentPlanAnswerStudentPageVo> pageOutput = ConventPage.getPageOutput(page, AssessmentPlanAnswerStudentPageVo.class);
//        return RT.ok(pageOutput);
//    }

    @GetMapping(value = "/teacher-page")
    @ApiOperation(value="考核计划答题列表-教师端(分页)")
    @SaCheckPermission("assessmenttemplateplan:detail")
    public RT<PageOutput<AssessmentPlanAnswerTeacherPageVo>> teacherPage(@Valid AssessmentPlanAnswerPageDto dto){
        dto.setTeacherId(StpUtil.getLoginIdAsLong());
        Page<AssessmentPlanAnswerTeacherPageVo> page = planService.getTeacherPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<AssessmentPlanAnswerTeacherPageVo> pageOutput = ConventPage.getPageOutput(page, AssessmentPlanAnswerTeacherPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/all-class-page")
    @ApiOperation(value="考核计划答题列表-所有班级(分页)")
    @SaCheckPermission("assessmenttemplateplan:detail")
    public RT<PageOutput<AssessmentPlanAnswerTeacherPageVo>> allClassPage(@Valid AssessmentPlanAnswerPageDto dto){
        Page<AssessmentPlanAnswerTeacherPageVo> page = planService.getTeacherPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<AssessmentPlanAnswerTeacherPageVo> pageOutput = ConventPage.getPageOutput(page, AssessmentPlanAnswerTeacherPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/mobile-page")
    @ApiOperation(value="考核计划答题列表-所有计划(分页)")
    @SaCheckPermission("assessmenttemplateplan:detail")
    public RT<List<AssessmentTemplatePlanPageVo>> mobilePage(@Valid AssessmentTemplatePlanPageDto dto){
        List<AssessmentTemplatePlanPageVo> page = planService.getMobilePage(dto);
        return RT.ok(page);
    }

    @GetMapping(value = "/answer-student-list")
    @ApiOperation(value="考核计划答题列表-学生答题情况(分页)")
    @SaCheckPermission("assessmenttemplateplan:detail")
    public RT<List<AssessmentPlanAnswerStudentVo>> answerStudent(@Valid AssessmentPlanAnswerStudentDto dto){
        List<AssessmentPlanAnswerStudentVo> page = planService.getAnswerStudent(dto);
        return RT.ok(page);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询考核计划信息")
    @SaCheckPermission("assessmenttemplateplan:detail")
    public RT<AssessmentTemplatePlanQuestionVo> info(@RequestParam Long id){
        AssessmentTemplatePlanQuestionVo questionVo = planService.getQuestionsById(id);
        if (questionVo == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(questionVo);
    }


    @PostMapping(value="sure")
    @ApiOperation(value = "班主任确认考核")
    @SaCheckPermission("assessmenttemplateplan:add")
    public RT<Boolean> add(@Valid @RequestBody AssessmentTemplatePlanSureDto dto){
        boolean isSuccess = planService.sure(dto);
        return RT.ok(isSuccess);
    }

    @PostMapping(value="save-answer")
    @ApiOperation(value = "提交答题")
    @SaCheckPermission("assessmenttemplateplan:save-answer")
    public RT<Boolean> saveAnswer(@Valid @RequestBody AssessmentPlanSubmitDto dto){
        boolean isSuccess = planService.saveAnswer(dto);
        return RT.ok(isSuccess);
    }

    @GetMapping(value="class-result")
    @ApiOperation(value = "班级答题情况")
    @SaCheckPermission("assessmenttemplateplan:class-result")
    public RT<List<AssessmentPlanAnswerClassResultVo>> classResult(@RequestParam Long id){
        List<AssessmentPlanAnswerClassResultVo> resultList = planService.getClassResult(id);
        return RT.ok(resultList);
    }

    @GetMapping(value="question-result")
    @ApiOperation(value = "每道题的答题情况")
    @SaCheckPermission("assessmenttemplateplan:question-result")
    public RT<AssessmentTemplatePlanResultVo> questionResult(@RequestParam Long id){
        AssessmentTemplatePlanResultVo resultList = planService.getQuestionResult(id);
        return RT.ok(resultList);
    }

    @PostMapping(value="send-msg")
    @ApiOperation(value = "推送微信通知")
    @SaCheckPermission("assessmenttemplateplan:send-msg")
    public RT<Boolean> questionResult(@Valid @RequestBody AssessmentTemplatePlanSureDto dto){
        Boolean sendMsg = planService.sendMsg(dto);
        return RT.ok(sendMsg);
    }
}