package org.dromara.system.controller.score;

import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.AjaxResult;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.score.ScoreData;
import org.dromara.system.domain.score.ScoreDataStudent;
import org.dromara.system.domain.score.bo.ScoreDataBo;
import org.dromara.system.domain.score.vo.ScoreDataDetailVo;
import org.dromara.system.domain.score.vo.ScoreDataStudentVo;
import org.dromara.system.domain.score.vo.ScoreDataVo;
import org.dromara.system.domain.vo.SysUserImportVo;
import org.dromara.system.listener.ScoreDataListener;
import org.dromara.system.listener.ScoreDateUtil;
import org.dromara.system.listener.SysUserImportListener;
import org.dromara.system.service.score.IScoreDataService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 成绩
 *
 * @author boman
 * @date 2023-08-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/score/data")
public class ScoreDataController extends BaseController {

    private final IScoreDataService scoreDataService;

    /**
     * 查询成绩列表
     */
    @SaCheckPermission("score:data:list")
    @GetMapping("/list")
    public TableDataInfo<ScoreDataVo> list(ScoreDataBo bo, PageQuery pageQuery) {
        return scoreDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出成绩列表
     */
    @SaCheckPermission("score:data:export")
    @Log(title = "成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ScoreDataBo bo, HttpServletResponse response) {
        List<ScoreDataVo> list = scoreDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "成绩", ScoreDataVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     */
    @SaCheckPermission("score:data:add")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(ScoreDataBo scoreData,@RequestPart("file") MultipartFile file) throws Exception {

        //获取到处理后的数据
        List<Map<String, String>> maps = ScoreDateUtil.importExcel(file);
        //进行新增操作
        scoreDataService.importData(scoreData,maps);
        return R.ok();
    }

    /**
     * 获取成绩详细信息
     *
     * @param scoreId 主键
     */
    //@SaCheckPermission("score:data:query")
    @GetMapping("/{scoreId}")
    public R<ScoreDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long scoreId) {
        return R.ok(scoreDataService.queryById(scoreId));
    }

    /**
     * 获取成绩详细信息
     */
    @SaCheckPermission("score:data:query")
    @PostMapping("/query")
    public AjaxResult getInfo(@RequestBody ScoreData scoreData)
    {
        return AjaxResult.success(scoreDataService.selectScoreDataByScoreId(scoreData));
    }

    /**
     * 新增成绩
     */
    @SaCheckPermission("score:data:add")
    @Log(title = "成绩", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ScoreDataBo bo) {
        return toAjax(scoreDataService.insertByBo(bo));
    }

    /**
     * 修改成绩
     */
    @SaCheckPermission("score:data:edit")
    @Log(title = "成绩", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ScoreDataBo bo) {
        return toAjax(scoreDataService.updateByBo(bo));
    }

    /**
     * 删除成绩
     *
     * @param scoreIds 主键串
     */
    @SaCheckPermission("score:data:remove")
    @Log(title = "成绩", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{scoreIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] scoreIds) {
        return toAjax(scoreDataService.deleteWithValidByIds(List.of(scoreIds), true));
    }


    /**
     * 后台首页成绩统计排名
     */
    @PostMapping("/indexPaiMing")
    public AjaxResult indexPaiMing(@RequestBody ScoreData scoreData)
    {
        List<ScoreDataStudentVo> scoreDataStudentList = scoreDataService.indexPaiMing(scoreData);
        return AjaxResult.success(scoreDataStudentList);
    }

    /**
     * 后台首页成绩统计分段人数饼图
     */
    @PostMapping("/indexBingTu")
    public AjaxResult indexBingTu(@RequestBody ScoreData scoreData)
    {
        Map<String, Object> map = scoreDataService.indexBingTu(scoreData);
        return AjaxResult.success(map);
    }

    /**
     * 后台首页成绩统计分段人数折线图
     */
    @PostMapping("/indexZheXian")
    public AjaxResult indexZheXian(@RequestBody ScoreData scoreData)
    {
        Map<String, Object> map = scoreDataService.indexZheXian(scoreData);
        return AjaxResult.success(map);
    }

    /**
     * 后台首页成绩统计列表
     */
    @PostMapping("/indexList")
    public TableDataInfo indexList(@RequestBody ScoreData scoreData)
    {
        return TableDataInfo.build(scoreDataService.indexList(scoreData));
    }


    /**
     * 成绩年级统计
     */
    @PostMapping("/gradeRank")
    public TableDataInfo<ScoreDataStudentVo> gradeRank(@RequestBody ScoreData scoreData)
    {
        List<ScoreDataStudentVo> scoreDataStudentVos = scoreDataService.gradeRank(scoreData);
        return TableDataInfo.build(scoreDataStudentVos);
    }

    /**
     * 成绩学科统计
     */
    @PostMapping("/xuekeRank")
    public TableDataInfo<ScoreDataDetailVo> xuekeRank(@RequestBody ScoreDataBo scoreDatabo)
    {
        List<ScoreDataDetailVo> scoreDataDetailVo = scoreDataService.xuekeRank(scoreDatabo);
        return TableDataInfo.build(scoreDataDetailVo);
    }

    /**
     * 班级总分平均分排名统计
     */
    @PostMapping("/classRank")
    public TableDataInfo<ScoreDataVo> classRank(@RequestBody ScoreData scoreData)
    {
        List<ScoreDataVo> scoreDataVos = scoreDataService.classRank(scoreData);
        return TableDataInfo.build(scoreDataVos);
    }

    /**
     * 年级总分平均分排名统计折线
     */
    @PostMapping("/avgRankZheXian")
    public AjaxResult avgRankZheXian(@RequestBody ScoreData scoreData)
    {
        return AjaxResult.success(scoreDataService.classRankZheXian(scoreData));
    }

    /**
     * 年级平均分各班级人数统计
     */
    @PostMapping("/avgRank")
    public TableDataInfo<ScoreDataVo> avgRank(@RequestBody ScoreData scoreData)
    {
        List<ScoreDataVo> scoreDataVos = scoreDataService.avgRank(scoreData);
        return TableDataInfo.build(scoreDataVos);
    }

    /**
     * 年级分数段统计
     */
    @PostMapping("/bingTuRank")
    public AjaxResult bingTuRank(@RequestBody ScoreData scoreData)
    {
        return AjaxResult.success(scoreDataService.bingTuRank(scoreData));
    }
}
