package com.xjrsoft.module.teacher.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.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.teacher.dto.AddWfHeadTeacherLeaveDto;
import com.xjrsoft.module.teacher.dto.UpdateWfHeadTeacherLeaveDto;
import com.xjrsoft.module.teacher.dto.WfHeadTeacherLeavePageDto;
import com.xjrsoft.module.teacher.entity.WfHeadTeacherLeave;
import com.xjrsoft.module.teacher.service.IWfHeadTeacherLeaveService;
import com.xjrsoft.module.teacher.vo.WfHeadTeacherLeavePageVo;
import com.xjrsoft.module.teacher.vo.WfHeadTeacherLeaveVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
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 javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
* @title: 班主任事项请假
* @Author szs
* @Date: 2023-12-19
* @Version 1.0
*/
@RestController
@RequestMapping("/teacher" + "/wfHeadTeacherLeave")
@Api(value = "/teacher"  + "/wfHeadTeacherLeave",tags = "班主任事项请假代码")
@AllArgsConstructor
public class WfHeadTeacherLeaveController {


    private final IWfHeadTeacherLeaveService wfHeadTeacherLeaveService;

    @GetMapping(value = "/page")
    @ApiOperation(value="班主任事项请假列表(分页)")
    @SaCheckPermission("wfheadteacherleave:detail")
    public RT<PageOutput<WfHeadTeacherLeavePageVo>> page(@Valid WfHeadTeacherLeavePageDto dto){
        IPage<WfHeadTeacherLeavePageVo> page = wfHeadTeacherLeaveService.getListPage(dto);
        PageOutput<WfHeadTeacherLeavePageVo> pageOutput = ConventPage.getPageOutput(page, WfHeadTeacherLeavePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询班主任事项请假信息")
    @SaCheckPermission("wfheadteacherleave:detail")
    public RT<WfHeadTeacherLeaveVo> info(@RequestParam Long id){
        WfHeadTeacherLeave wfHeadTeacherLeave = wfHeadTeacherLeaveService.getById(id);
        if (wfHeadTeacherLeave == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(wfHeadTeacherLeave, WfHeadTeacherLeaveVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增班主任事项请假")
    @SaCheckPermission("wfheadteacherleave:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfHeadTeacherLeaveDto dto){
        WfHeadTeacherLeave wfHeadTeacherLeave = BeanUtil.toBean(dto, WfHeadTeacherLeave.class);
        boolean isSuccess = wfHeadTeacherLeaveService.save(wfHeadTeacherLeave);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改班主任事项请假")
    @SaCheckPermission("wfheadteacherleave:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateWfHeadTeacherLeaveDto dto){

        WfHeadTeacherLeave wfHeadTeacherLeave = BeanUtil.toBean(dto, WfHeadTeacherLeave.class);
        return RT.ok(wfHeadTeacherLeaveService.updateById(wfHeadTeacherLeave));

    }

    @DeleteMapping
    @ApiOperation(value = "删除班主任事项请假")
    @SaCheckPermission("wfheadteacherleave:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(wfHeadTeacherLeaveService.removeBatchByIds(ids));

    }

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

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