package com.koobietech.eas.controller;


import com.koobietech.eas.common.result.JsonResult;
import com.koobietech.eas.mbg.model.EasArcTlsStudents;
import com.koobietech.eas.service.EasStuProfileService;
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;

/**
 * @author lc
 */
@Tag(name = "学生档案下载模块")
@RestController
public class EasSysStuProfileController {

    @Resource
    private EasStuProfileService easStuProfileService;

    @PostMapping("/add")
    @Operation(summary = "添加学员", description = "添加学员")
    public JsonResult add(@RequestBody EasArcTlsStudents easArcTlsStudents) {
        return JsonResult.bool(easStuProfileService.add(easArcTlsStudents));
    }

    @PostMapping("/del/{id}")
    @Operation(summary = "更新学员信息", description = "更新学员信息")
    public JsonResult delete(@PathVariable int id){
        boolean result = easStuProfileService.delete(id);
        return JsonResult.bool(result);
    }

    @PostMapping("/update")
    @Operation(summary = "更新学员信息", description = "更新学员信息")
    public JsonResult update(@RequestBody EasArcTlsStudents studentDto){
        boolean result = easStuProfileService.update(studentDto);
        return JsonResult.bool(result);
    }
    @PostMapping("/query")
    @Operation(summary = "查询学员信息", description = "查询学员信息")
    public JsonResult query(@RequestBody(required = false) EasArcTlsStudents studentDto,
                            @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        List<EasArcTlsStudents> res = easStuProfileService.query(studentDto, pageNum, pageSize);
        return JsonResult.data(res);
    }

    @PostMapping("/importExcel")
    @Operation(summary = "导入学员信息", description = "导入学员信息")
    public JsonResult importExcel( MultipartFile file ){
        InputStream inputStream = null;
        boolean result = false;
        try {
            inputStream = file.getInputStream();
            result = easStuProfileService.importExcel(inputStream);
        } catch (IOException e) {}finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {}
            }
        }
        return JsonResult.bool(result);
    }
}
