package com.xjrsoft.module.textbook.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.textbook.dto.AddWfTextbookRecedeDto;
import com.xjrsoft.module.textbook.dto.UpdateWfTextbookRecedeDto;
import com.xjrsoft.module.textbook.dto.WfTextbookRecedePageDto;
import com.xjrsoft.module.textbook.entity.WfTextbookRecede;
import com.xjrsoft.module.textbook.service.IWfTextbookRecedeService;
import com.xjrsoft.module.textbook.vo.WfTextbookRecedePageVo;
import com.xjrsoft.module.textbook.vo.WfTextbookRecedeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
* @title: 推书申请
* @Author szs
* @Date: 2024-01-03
* @Version 1.0
*/
@RestController
@RequestMapping("/textbook" + "/wfTextbookRecede")
@Api(value = "/textbook"  + "/wfTextbookRecede",tags = "推书申请代码")
@AllArgsConstructor
public class WfTextbookRecedeController {


    private final IWfTextbookRecedeService wfTextbookRecedeService;

    @GetMapping(value = "/page")
    @ApiOperation(value="推书申请列表(分页)")
    @SaCheckPermission("wftextbookrecede:detail")
    public RT<PageOutput<WfTextbookRecedePageVo>> page(@Valid WfTextbookRecedePageDto dto){

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询推书申请信息")
    @SaCheckPermission("wftextbookrecede:detail")
    public RT<WfTextbookRecedeVo> info(@RequestParam Long id){
        WfTextbookRecede wfTextbookRecede = wfTextbookRecedeService.getByIdDeep(id);
        if (wfTextbookRecede == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(wfTextbookRecede, WfTextbookRecedeVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增推书申请")
    @SaCheckPermission("wftextbookrecede:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfTextbookRecedeDto dto){
        WfTextbookRecede wfTextbookRecede = BeanUtil.toBean(dto, WfTextbookRecede.class);
        boolean isSuccess = wfTextbookRecedeService.add(wfTextbookRecede);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改推书申请")
    @SaCheckPermission("wftextbookrecede:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateWfTextbookRecedeDto dto){

        WfTextbookRecede wfTextbookRecede = BeanUtil.toBean(dto, WfTextbookRecede.class);
        return RT.ok(wfTextbookRecedeService.update(wfTextbookRecede));

    }

    @DeleteMapping
    @ApiOperation(value = "删除推书申请")
    @SaCheckPermission("wftextbookrecede:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(wfTextbookRecedeService.delete(ids));

    }

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

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