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.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.textbook.dto.AddTextbookWarehouseRecordDto;
import com.xjrsoft.module.textbook.dto.TextbookWarehouseRecordExportDto;
import com.xjrsoft.module.textbook.dto.TextbookWarehouseRecordPageDto;
import com.xjrsoft.module.textbook.dto.UpdateTextbookWarehouseRecordDto;
import com.xjrsoft.module.textbook.entity.TextbookWarehouseRecord;
import com.xjrsoft.module.textbook.service.ITextbookWarehouseRecordService;
import com.xjrsoft.module.textbook.vo.TextbookWarehouseRecordExcelVo;
import com.xjrsoft.module.textbook.vo.TextbookWarehouseRecordPageVo;
import com.xjrsoft.module.textbook.vo.TextbookWarehouseRecordVo;
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.List;

/**
* @title: 教材入库
* @Author szs
* @Date: 2023-12-26
* @Version 1.0
*/
@RestController
@RequestMapping("/textbook" + "/textbookWarehouseRecord")
@Api(value = "/textbook"  + "/textbookWarehouseRecord",tags = "教材入库代码")
@AllArgsConstructor
public class TextbookWarehouseRecordController {


    private final ITextbookWarehouseRecordService textbookClassWarehouseService;

    @GetMapping(value = "/page")
    @ApiOperation(value="教材入库列表(分页)")
    @SaCheckPermission("textbookclasswarehouse:detail")
    public RT<PageOutput<TextbookWarehouseRecordPageVo>> page(@Valid TextbookWarehouseRecordPageDto dto){
        Page<TextbookWarehouseRecordPageVo> page = textbookClassWarehouseService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<TextbookWarehouseRecordPageVo> pageOutput = ConventPage.getPageOutput(page, TextbookWarehouseRecordPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询教材入库信息")
    @SaCheckPermission("textbookclasswarehouse:detail")
    public RT<TextbookWarehouseRecordVo> info(@RequestParam Long id){
        TextbookWarehouseRecord textbookWarehouseRecord = textbookClassWarehouseService.getById(id);
        if (textbookWarehouseRecord == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(textbookWarehouseRecord, TextbookWarehouseRecordVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增教材入库")
    @SaCheckPermission("textbookclasswarehouse:add")
    public RT<Boolean> add(@Valid @RequestBody AddTextbookWarehouseRecordDto dto){
        TextbookWarehouseRecord textbookWarehouseRecord = BeanUtil.toBean(dto, TextbookWarehouseRecord.class);
        boolean isSuccess = textbookClassWarehouseService.save(textbookWarehouseRecord);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改教材入库")
    @SaCheckPermission("textbookclasswarehouse:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateTextbookWarehouseRecordDto dto){

        TextbookWarehouseRecord textbookWarehouseRecord = BeanUtil.toBean(dto, TextbookWarehouseRecord.class);
        return RT.ok(textbookClassWarehouseService.updateById(textbookWarehouseRecord));

    }

    @DeleteMapping
    @ApiOperation(value = "删除教材入库")
    @SaCheckPermission("textbookclasswarehouse:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(textbookClassWarehouseService.removeBatchByIds(ids));

    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData() {
        List<TextbookWarehouseRecordPageVo> customerList = textbookClassWarehouseService.getList();
        List<TextbookWarehouseRecordExcelVo> dataList = BeanUtil.copyToList(customerList, TextbookWarehouseRecordExcelVo.class);
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, TextbookWarehouseRecordExcelVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(dataList);

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

    @PostMapping("/export-query")
    @ApiOperation(value = "指定导出")
    public ResponseEntity<byte[]> exportDataQuery(@Valid @RequestBody TextbookWarehouseRecordExportDto dto) {
        List<TextbookWarehouseRecordPageVo> customerList = textbookClassWarehouseService.getList(dto);
        List<TextbookWarehouseRecordExcelVo> dataList = BeanUtil.copyToList(customerList, TextbookWarehouseRecordExcelVo.class);
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, TextbookWarehouseRecordExcelVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(dataList);

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