package com.koobietech.eas.controller;

import cn.hutool.db.Page;
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.PageData;
import com.koobietech.eas.mbg.model.EasArcTlsScores;
import com.koobietech.eas.service.EasArcTlsScoresService;
import io.swagger.v3.core.util.Json;
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.List;

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

    @Resource
    EasArcTlsScoresService easArcTlsScoresService;

    /**
     * 添加学生成绩信息
     * @param easArcTlsScores
     * @return
     */
    @PostMapping(value = "/addStudentScore")
    @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 = "/updateStudentScore")
    @Operation(summary = "修改学生成绩信息",description = "用于修改学生成绩信息")
    public JsonResult updateStudentScore(@RequestBody EasArcTlsScores easArcTlsScores) {
        int i = easArcTlsScoresService.updateStudentScore(easArcTlsScores);
        if (i > 0){
            return JsonResult.ok();
        }else {
            return JsonResult.fail();
        }
    }

    /**
     * 查询所有学生成绩信息
     * @return
     */
    @GetMapping(value = "/selectAllStudentScores")
    @Operation(summary = "查询所有学生的成绩信息",description = "用于查询所有学生的成绩信息")
    public JsonResult selectAllStudentScores() {
        List<EasArcTlsScores> easArcTlsScores = easArcTlsScoresService.selectAllStudentScores();
        return JsonResult.data(easArcTlsScores);
    }

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

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