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.conditions.query.LambdaQueryWrapper;
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.common.utils.VoToColumnUtil;
import com.xjrsoft.module.teacher.dto.AddWfTeacherleaveDto;
import com.xjrsoft.module.teacher.dto.UpdateWfTeacherleaveDto;
import com.xjrsoft.module.teacher.dto.WfTeacherleavePageDto;
import com.xjrsoft.module.teacher.entity.WfTeacherleave;
import com.xjrsoft.module.teacher.service.IWfTeacherleaveService;
import com.xjrsoft.module.teacher.vo.WfTeacherleavePageVo;
import com.xjrsoft.module.teacher.vo.WfTeacherleaveVo;
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-20
* @Version 1.0
*/
@RestController
@RequestMapping("/teacher" + "/wfTeacherleave")
@Api(value = "/teacher"  + "/wfTeacherleave",tags = "教职工请假流程代码")
@AllArgsConstructor
public class WfTeacherleaveController {


    private final IWfTeacherleaveService wfTeacherleaveService;

    @GetMapping(value = "/page")
    @ApiOperation(value="教职工请假流程列表(分页)")
    @SaCheckPermission("wfteacherleave:detail")
    public RT<PageOutput<WfTeacherleavePageVo>> page(@Valid WfTeacherleavePageDto dto){

        LambdaQueryWrapper<WfTeacherleave> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(WfTeacherleave::getId)
                .select(WfTeacherleave.class,x -> VoToColumnUtil.fieldsToColumns(WfTeacherleavePageVo.class).contains(x.getProperty()));
        IPage<WfTeacherleave> page = wfTeacherleaveService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<WfTeacherleavePageVo> pageOutput = ConventPage.getPageOutput(page, WfTeacherleavePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询教职工请假流程信息")
    @SaCheckPermission("wfteacherleave:detail")
    public RT<WfTeacherleaveVo> info(@RequestParam Long id){
        WfTeacherleave wfTeacherleave = wfTeacherleaveService.getById(id);
        if (wfTeacherleave == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(wfTeacherleave, WfTeacherleaveVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增教职工请假流程")
    @SaCheckPermission("wfteacherleave:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfTeacherleaveDto dto){
        WfTeacherleave wfTeacherleave = BeanUtil.toBean(dto, WfTeacherleave.class);
        boolean isSuccess = wfTeacherleaveService.save(wfTeacherleave);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改教职工请假流程")
    @SaCheckPermission("wfteacherleave:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateWfTeacherleaveDto dto){

        WfTeacherleave wfTeacherleave = BeanUtil.toBean(dto, WfTeacherleave.class);
        return RT.ok(wfTeacherleaveService.updateById(wfTeacherleave));

    }

    @DeleteMapping
    @ApiOperation(value = "删除教职工请假流程")
    @SaCheckPermission("wfteacherleave:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(wfTeacherleaveService.removeBatchByIds(ids));

    }

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

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