package com.srs.web.controller.comprehensive;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.srs.comprehensive.domain.Dto.CphExport;
import com.srs.comprehensive.domain.SrsCqScore;
import com.srs.comprehensive.domain.SrsMajors;
import com.srs.comprehensive.domain.Vo.*;
import com.srs.comprehensive.service.ISrsCqScoreService;
import com.srs.system.service.ISysPostService;
import com.srs.web.controller.common.RoleBool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.srs.common.annotation.Log;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.enums.BusinessType;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.core.page.TableDataInfo;

/**
 * 学生综合素质评分表Controller
 *
 * @author srs
 * @date 2023-06-29
 */
@Api("学生综合素质评分")
@RestController
@RequestMapping("/system/cqScore")
public class SrsCqScoreController extends BaseController {
    @Autowired
    private ISrsCqScoreService srsCqScoreService;

    @Autowired
    private ISysPostService _postService;

    @GetMapping("/listOwnMajor")
    @ApiOperation(value = "学务查询自己学院的专业")
    public AjaxResult listOwnMajor() {
        boolean isXw = RoleBool.isXw(getUserId(), _postService);
        if (isXw) {
            Long id = getDeptId();
            List<SrsMajors> list = srsCqScoreService.listOwnMajor(id);

            return success(list);
        } else {
            return AjaxResult.error("您无权做这些");
        }
    }

    @GetMapping("/listOwnClass/{id}")
    @ApiOperation(value = "学务根据专业查询自己学院的班级")
    public AjaxResult listOwnClass(@PathVariable("id") Long major) {
        boolean isXw = RoleBool.isXw(getUserId(), _postService);
        if (isXw) {
            CphSearch param = new CphSearch();
            Long dept = getDeptId();
            param.setDeptId(dept);
            param.setMajorId(major);
            List<SrsMajors> list = srsCqScoreService.listOwnClass(param);
            return success(list);
        } else {
            return AjaxResult.error("您无权做这些");
        }
    }

    @GetMapping("/listOwnCollegeAudit")
    @ApiOperation(value = "学务查询自己学院的审核记录")
    public TableDataInfo listOwnCollegeAudit(CphSearch param) {
        boolean isXw = RoleBool.isXw(getUserId(), _postService);
        if (isXw) {
            Long dept = getDeptId();
            param.setDeptId(dept);
            startPage();
            List<CphCollegeLook> list = srsCqScoreService.listOwnCollegeAudit(param);
            return getDataTable(list);
        } else {
            return getDataTable(null);
        }
    }

    @GetMapping("/xwExport")
    @ApiOperation(value = "学务导出成绩")
    public AjaxResult xwExport(CphExport param) {
        boolean hasRole = RoleBool.isXw(getUserId(), _postService);
        if (hasRole) {
            CphExport temp = new CphExport();
            temp.setDeptId(getDeptId());
            temp.setGradeId(param.gradeId);
            temp.setClassId(param.classId);
            temp.setMajorId(param.majorId);
            List<Map<String, Object>> list = srsCqScoreService.exportScore(temp);
            return AjaxResult.success(list);
        } else {
            return AjaxResult.error(401, "您无权做这些");

        }
    }

    @GetMapping("/listOwnCollegeScore")
    @ApiOperation(value = "学务查询自己学院的相关成绩")
    public TableDataInfo listOwnCollegeScore(CphSearch param) {
        boolean isXw = RoleBool.isXw(getUserId(), _postService);
        if (isXw) {
            Long dept = getDeptId();
            param.setDeptId(dept);
            startPage();
            List<CphCollegeLook> list = srsCqScoreService.listOwnCollegeScore(param);
            return getDataTable(list);
        } else {
            return getDataTable(null);
        }
    }

    @GetMapping("/listOwnCollegeStu")
    @ApiOperation(value = "学务查询自己学院的学生")
    public TableDataInfo listOwnCollegeStu(CphSearch param) {
        boolean isXw = RoleBool.isXw(getUserId(), _postService);
        if (isXw) {
            Long dept = getDeptId();
            param.setDeptId(dept);
            startPage();
            List<CphCollegeLook> list = srsCqScoreService.listOwnCollegeStu(param);
            return getDataTable(list);
        } else {
            return getDataTable(null);
        }
    }


    @GetMapping("/rankDis")
    public AjaxResult rankDis(CphSearch search) {
        List<DataScreen> res = srsCqScoreService.rankDis(search);
        return success(res);
    }

    @GetMapping("/countScoreDis/{id}")
    public AjaxResult countScoreDis(@PathVariable("id") Long id) {
        List<DataScreen> res = srsCqScoreService.countScoreDis(id);
        return success(res);
    }

    @GetMapping("/countScoreDetailDis/{id}")
    public AjaxResult countScoreDetailDis(@PathVariable("id") Long id) {
        List<DataScreen> res = srsCqScoreService.countScoreDetailDis(id);
        return success(res);
    }

    @GetMapping("/exportScore")
    public AjaxResult exportScore(CphExport param) {
        List<Map<String, Object>> list = srsCqScoreService.exportScore(param);
        return success(list);
    }

    @GetMapping("/listStuFive/{stuNo}")
    public TableDataInfo listStuFive(@PathVariable("stuNo") String stuNo) {
        boolean isXw = RoleBool.isHigh(getUserId(), _postService);
        if (isXw) {
            startPage();
            List<CphStuScoreType> list = srsCqScoreService.listOwnFive(stuNo);
            return getDataTable(list);
        } else {
            return getDataTable(new ArrayList<>());
        }
    }


    @GetMapping("/listOwnFive")
    public TableDataInfo listOwnFive() {
        startPage();
        String stuNo = getUsername();
        List<CphStuScoreType> list = srsCqScoreService.listOwnFive(stuNo);
        return getDataTable(list);
    }

    /**
     * 查询学生综合素质评分表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CphSearch cphSearch) {

        startPage();
        List<SrsCqScore> list = srsCqScoreService.selectSrsCqScoreList(cphSearch);
        return getDataTable(list);

    }
//    /**
//     * 查询汇总
//     */
//    @GetMapping("/lists")
//    public AjaxResult lists(int id)
//    {
//        List<Map<String, Object>> summary = srsCqScoreService.getComprehensiveSummary(id);
//        return AjaxResult.success(summary);
//    }


    /**
     * 生成综合评价总成绩
     */
    @GetMapping("/generate")
    public AjaxResult generate(int id) {
        try {
            srsCqScoreService.generateComprehensiveSummary(id);
            return AjaxResult.success("成功");

        } catch (Exception e) {
            System.out.println(e);
            return AjaxResult.error("失败");
        }

    }

    @GetMapping("/getOwn")
    public AjaxResult getOwn() {
        String stuNo = getUsername();
        return success(srsCqScoreService.getOwn(stuNo));
    }


    /**
     * 导出学生综合素质评分表列表
     */
    @Log(title = "学生综合素质评分表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CphSearch cphSearch) {
        List<SrsCqScore> list = srsCqScoreService.selectSrsCqScoreList(cphSearch);
        ExcelUtil<SrsCqScore> util = new ExcelUtil<SrsCqScore>(SrsCqScore.class);
        util.exportExcel(response, list, "学生综合素质评分表数据");
    }

    /**
     * 获取学生综合素质评分表详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(srsCqScoreService.selectSrsCqScoreById(id));
    }

    /**
     * 新增学生综合素质评分表
     */
    @Log(title = "学生综合素质评分表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrsCqScore srsCqScore) {
        return toAjax(srsCqScoreService.insertSrsCqScore(srsCqScore));
    }

    /**
     * 修改学生综合素质评分表
     */
    @Log(title = "学生综合素质评分表", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody SrsCqScore srsCqScore) {
        return toAjax(srsCqScoreService.updateSrsCqScore(srsCqScore));
    }

    /**
     * 删除学生综合素质评分表
     */
    @Log(title = "学生综合素质评分表", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(srsCqScoreService.deleteSrsCqScoreByIds(ids));
    }

    @ApiOperation("查询当前用户综合评价成绩")
    @PostMapping("/getCqScore")
    public AjaxResult getCqScore() {
        List<SrsUserCqScore> srsCqScores = srsCqScoreService.selectSrsCqStuId();
        return AjaxResult.success(srsCqScores);
    }

    @ApiOperation(value = "查询综合评价得分分布情况", notes =
            "请求示例:<br>"
                    + "{<br>"
                    + "&nbsp;&nbsp;\"params\": {<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"classId\": 0, // 班级id<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"deptId\": 0, // 学院id<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"gradeId\": 0, // 年级id<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"stuYearId\": 0, // 学年id(必填)<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"majorId\": 0 // 专业id<br>"
                    + "&nbsp;&nbsp;}<br>"
                    + "}")
    @PostMapping("/getScoreDistribution")
    public AjaxResult getScoreDistribution(@RequestBody SrsCqScore srsCqScore) {
        return srsCqScoreService.getScoreDistribution(srsCqScore);
    }

    @PostMapping("/comprehensiveRanking")
    @ApiOperation(value = "综合成绩排名", notes =
            "请求示例:<br>"
                    + "{<br>"
                    + "&nbsp;&nbsp;\"params\": {<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"sortType\": 0, // 排序(必填) &nbsp;0倒序 &nbsp; 1正序<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"classId\": 0, // 班级id<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"deptId\": 0, // 学院id<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"gradeId\": 0, // 年级id<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"stuYearId\": 0, // 学年id(必填)<br>"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;\"majorId\": 0 // 专业id<br>"
                    + "&nbsp;&nbsp;}<br>"
                    + "}")
    public TableDataInfo comprehensiveRanking(@RequestBody SrsCqScore srsCqScore) {
        startPage();
        List<SrsCeScoreClassDeptGradeMajorsStudentStuYear> list = srsCqScoreService.comprehensiveRanking(srsCqScore);
        return getDataTable(list);
    }

    @GetMapping("/listCphScore")
    public TableDataInfo listCphScore(CphSearch search) {
        startPage();
        List<CphTotalScoreInfo> list = srsCqScoreService.listCphScore(search);
        return getDataTable(list);
    }

}
