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.ArchivesDto;
import com.koobietech.eas.dao.dto.EasArcTlsStudentsDto;
import com.koobietech.eas.mbg.model.EasArcArchives;
import com.koobietech.eas.mbg.model.EasArcTlsStudents;
import com.koobietech.eas.mbg.model.EasSysStudent;
import com.koobietech.eas.service.EasStuProfileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
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
@RequestMapping("/student")
public class EasSysStudentController {

    @Resource
    private EasStuProfileService easStuProfileService;

    @PostMapping("/add")
    @Operation(summary = "添加学员", description = "添加学员")
    public JsonResult add(@RequestBody EasArcTlsStudentsDto easArcTlsStudentsDto) {
        EasArcTlsStudents easArcTlsStudents = new EasArcTlsStudents();
        BeanUtils.copyProperties(easArcTlsStudentsDto, 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("/upload")
    @Operation(summary = "上传档案", description = "上传档案")
    public JsonResult upload(MultipartFile file,
                             @RequestParam String fileType,
                             @RequestParam String studentNumber){
        ArchivesDto upload = easStuProfileService.upload(file, studentNumber, fileType);
        return JsonResult.data(upload.getPath());
    }

    @PostMapping("/addArchives")
    @Operation(summary = "添加学员档案", description = "添加学员档案")
    public JsonResult addArchives(@RequestBody EasArcArchives arc){
        boolean res =  easStuProfileService.addArchives(arc);
        return JsonResult.bool(res);
    }

    @DeleteMapping("/delArchives/{id}")
    @Operation(summary = "删除档案", description = "删除档案")
    public JsonResult addArchives(@PathVariable Integer id){
        boolean res =  easStuProfileService.delArchives(id);
        return JsonResult.bool(res);
    }

    @PostMapping("/downloadArchives")
    @Operation(summary = "下载学员档案", description = "下载学员档案")
    public JsonResult downloadArchives(@RequestParam(required = true) String studentNumber){
        if ( !StringUtils.hasText(studentNumber) ||  "".equals(studentNumber) ) {
            return JsonResult.fail("学生学号不能为空");
        }
        if ( studentNumber.isBlank() || studentNumber.isEmpty() )  {
            return JsonResult.fail("学生学号格式错误");
        }
        // 下载
        String res = easStuProfileService.downloadArchives(studentNumber);
        return JsonResult.data(res);
    }

    @PostMapping("/query")
    @Operation(summary = "查询学员信息", description = "查询学员信息")
    public JsonPageResult query(@RequestBody(required = false) EasArcTlsStudents studentDto,
                                @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        PageDataResult res = easStuProfileService.query(studentDto);
        return JsonPageResult.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);
    }

    @GetMapping("/getAll")
    @Operation(summary = "查询所有学员信息", description = "查询所有学员信息")
    public JsonResult getAll(){
        List<EasSysStudent> res = easStuProfileService.getAll();
        return JsonResult.data(res);
    }

    @GetMapping("/getStudentByKeyword")
    @Operation(summary = "查询所有学员信息", description = "查询所有学员信息根据关键词")
    public JsonResult getStudentByKeyword(@RequestParam(value = "", required = true) String keyword){
        //限制学员搜索功能
        if ( SecurityManager.isStudentUser() ) {
            keyword = SecurityManager.getLoginUserName();
        }
        List<EasSysStudent> res = easStuProfileService.getStudentByKeyword(keyword);
        return JsonResult.data(res);
    }

}
