package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.student.dto.AddBaseStudentGraduateDto;
import com.xjrsoft.module.student.dto.BaseStudentGraduatePageDto;
import com.xjrsoft.module.student.dto.UpdateBaseStudentGraduateDto;
import com.xjrsoft.module.student.entity.BaseStudentGraduate;
import com.xjrsoft.module.student.service.IBaseStudentGraduateService;
import com.xjrsoft.module.student.vo.BaseStudentGraduatePageVo;
import com.xjrsoft.module.student.vo.BaseStudentGraduateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @title: 学生毕业管理
* @Author dzx
* @Date: 2024-01-26
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/baseStudentGraduate")
@Api(value = "/student"  + "/baseStudentGraduate",tags = "学生毕业管理代码")
@AllArgsConstructor
public class BaseStudentGraduateController {


    private final IBaseStudentGraduateService baseStudentGraduateService;

    @GetMapping(value = "/page")
    @ApiOperation(value="学生毕业管理列表(分页)")
    @SaCheckPermission("basestudentgraduate:detail")
    public RT<PageOutput<BaseStudentGraduatePageVo>> page(@Valid BaseStudentGraduatePageDto dto){
        Page<BaseStudentGraduatePageVo> page = baseStudentGraduateService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<BaseStudentGraduatePageVo> pageOutput = ConventPage.getPageOutput(page, BaseStudentGraduatePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询学生毕业管理信息")
    @SaCheckPermission("basestudentgraduate:detail")
    public RT<BaseStudentGraduateVo> info(@RequestParam Long id){
        BaseStudentGraduateVo baseStudentGraduateVo = baseStudentGraduateService.getInfoById(id);
        if (baseStudentGraduateVo == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(baseStudentGraduateVo);
    }


    @PostMapping
    @ApiOperation(value = "毕业登记")
    @SaCheckPermission("basestudentgraduate:add")
    public RT<Boolean> add(@Valid @RequestBody AddBaseStudentGraduateDto dto){
        boolean isSuccess = baseStudentGraduateService.addOne(dto);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改学生毕业管理")
    @SaCheckPermission("basestudentgraduate:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateBaseStudentGraduateDto dto){

        BaseStudentGraduate baseStudentGraduate = BeanUtil.toBean(dto, BaseStudentGraduate.class);
        return RT.ok(baseStudentGraduateService.updateById(baseStudentGraduate));

    }

    @PostMapping("/import")
    @ApiOperation(value = "导入")
    public RT<Boolean> importData(@RequestParam MultipartFile file) throws IOException {
        List<BaseStudentGraduatePageVo> savedDataList = EasyExcel.read(file.getInputStream()).head(BaseStudentGraduatePageVo.class).sheet().doReadSync();
        Boolean result = baseStudentGraduateService.saveBatch(BeanUtil.copyToList(savedDataList, BaseStudentGraduate.class));
        return RT.ok(result);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData(@Valid BaseStudentGraduatePageDto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) {
        List<BaseStudentGraduatePageVo> customerList = isTemplate != null && isTemplate ? new ArrayList<>() : ((PageOutput<BaseStudentGraduatePageVo>) page(dto).getData()).getList();
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, BaseStudentGraduatePageVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);

        return RT.fileStream(bot.toByteArray(), "BaseStudentGraduate" + ExcelTypeEnum.XLSX.getValue());
    }
}