package com.ruoyi.web.controller.socre;

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

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.score.ScoreData;
import com.ruoyi.system.domain.score.ScoreDataDetails;
import com.ruoyi.system.service.score.IScoreDataService;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 成绩Controller
 * 
 * @author boman
 * @date 2023-07-20
 */
@RestController
@RequestMapping("/score/data")
public class ScoreDataController extends BaseController
{
    @Autowired
    private IScoreDataService scoreDataService;

    /**
     * 查询成绩列表
     */
    @PreAuthorize("@ss.hasPermi('score:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScoreData scoreData)
    {
        List<ScoreData> list = scoreDataService.selectScoreDataList(scoreData);
        return getDataTable(list);
    }

    /**
     * 导出成绩列表
     */
    @PreAuthorize("@ss.hasPermi('score:data:export')")
    @Log(title = "成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScoreData scoreData)
    {
        List<ScoreData> list = scoreDataService.selectScoreDataList(scoreData);
        ExcelUtil<ScoreData> util = new ExcelUtil<ScoreData>(ScoreData.class);
        util.exportExcel(response, list, "成绩数据");
    }

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


    /**
     * 新增成绩
     */
    @PreAuthorize("@ss.hasPermi('score:data:add')")
    @Log(title = "成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(ScoreData scoreData,MultipartFile file)throws Exception
    {
        if (file != null){
            ExcelUtil<ScoreDataDetails> util = new ExcelUtil<ScoreDataDetails>(ScoreDataDetails.class);
            List<ScoreDataDetails> scoreDataDetailsList = util.importExcel(file.getInputStream());
            if (scoreDataDetailsList == null || scoreDataDetailsList.size() < 1){
                return AjaxResult.error("不能导入空数据");
            }
            for (ScoreDataDetails scoreDataDetails : scoreDataDetailsList) {
                int zongfen = scoreDataDetails.getZongfen();
                if (zongfen < 0){
                    return AjaxResult.error("总成绩不能有小于0的数据");
                }
            }
            scoreData.setScoreDataDetailsList(scoreDataDetailsList);
        }
        return toAjax(scoreDataService.insertScoreData(scoreData));
    }
    /**
     * 新增成绩
     */
    @PreAuthorize("@ss.hasPermi('score:data:add')")
    @Log(title = "成绩", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ScoreData scoreData)
    {

        return toAjax(scoreDataService.insertScoreData(scoreData));
    }

    /**
     * 修改成绩
     */
    @PreAuthorize("@ss.hasPermi('score:data:edit')")
    @Log(title = "成绩", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody ScoreData scoreData)
    {
        return toAjax(scoreDataService.updateScoreData(scoreData));
    }

    /**
     * 删除成绩
     */
    @PreAuthorize("@ss.hasPermi('score:data:remove')")
    @Log(title = "成绩", businessType = BusinessType.DELETE)
	@GetMapping("/delete/{scoreIds}")
    public AjaxResult remove(@PathVariable Long[] scoreIds)
    {
        return toAjax(scoreDataService.deleteScoreDataByScoreIds(scoreIds));
    }

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

    /**
     * 后台首页成绩统计分段人数饼图
     */
    @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);
    }

}
