package com.koobietech.eas.controller;

import com.github.pagehelper.PageHelper;
import com.koobietech.eas.common.result.JsonPageResult;
import com.koobietech.eas.common.result.JsonResult;
import com.koobietech.eas.common.result.PageDataResult;
import com.koobietech.eas.common.utils.SecurityManager;
import com.koobietech.eas.dao.dto.ScoresDto;
import com.koobietech.eas.mbg.model.EasArcTlsScores;
import com.koobietech.eas.service.EasArcTlsScoresService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

@RestController
@Tag(name = "成绩控制器")
@RequestMapping("/scores")
public class EasArcTlsScoresController {

    @Resource
    EasArcTlsScoresService easArcTlsScoresService;

    /**
     * 添加学生成绩信息
     * @param easArcTlsScores
     * @return
     */
    @PostMapping(value = "/add")
    @Operation(summary = "添加学生成绩信息",description = "用于添加学生成绩信息")
    public JsonResult addStudentScore(@RequestBody EasArcTlsScores easArcTlsScores) {
        int i = easArcTlsScoresService.addStudentScore(easArcTlsScores);
        System.out.println(i);
        return JsonResult.ok("添加成功!");
    }

    /**
     * 修改学生成绩信息
     * @param easArcTlsScores
     * @return
     */
    @PutMapping(value = "/update")
    @Operation(summary = "修改学生成绩信息",description = "用于修改学生成绩信息")
    public JsonResult updateStudentScore(@RequestBody EasArcTlsScores easArcTlsScores) {
        int i = easArcTlsScoresService.updateStudentScore(easArcTlsScores);
        if (i > 0){
            return JsonResult.ok();
        }else {
            return JsonResult.fail();
        }
    }

    /**
     * 条件查询学生成绩信息
     * @param easArcTlsScores
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/query")
    @Operation(summary = "条件查询学生成绩信息",description = "用于条件查询学生成绩信息")
    public JsonPageResult selectScoreByExample(@RequestBody EasArcTlsScores easArcTlsScores,
                                               @RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        PageDataResult pageDataResult = easArcTlsScoresService.selectScoreByExample(easArcTlsScores);
        return JsonPageResult.data(pageDataResult);
    }

    /**
     * 条件查询学生成绩信息
     * @param scoresDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/queryScores")
    @Operation(summary = "综合查询分数接口",description = "综合查询分数接口")
    public JsonPageResult queryStudentScores(@RequestBody ScoresDto scoresDto,
                                               @RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);

        if ( SecurityManager.isStudentUser() ) {
            scoresDto.setStudentId(SecurityManager.getLoginSid().intValue());
        }

        PageDataResult pageDataResult = easArcTlsScoresService.queryStudentScores(scoresDto);
        return JsonPageResult.data(pageDataResult);
    }

    @PostMapping(value = "/import")
    @Operation(summary = "Excel导入学生成绩信息",description = "用于Excel导入学生成绩信息")
    public JsonResult importExcelScores(MultipartFile file) {
        InputStream inputStream = null;
        Boolean ret;
        try {
            inputStream = file.getInputStream();
            ret = easArcTlsScoresService.importExcelScores(inputStream);
        } catch (IOException e) {
            ret = false;
        } finally {
            try {
                if (Objects.nonNull(inputStream) ) {
                    inputStream.close();
                }
            } catch (IOException e) {}
        }
        if (ret == true){
            return JsonResult.ok();
        }else {
            return JsonResult.fail();
        }
    }
}
