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.AddTextbookIssueRecordDto;
import com.xjrsoft.module.textbook.dto.TextbookIssueRecordExportDto;
import com.xjrsoft.module.textbook.dto.TextbookIssueRecordPageDto;
import com.xjrsoft.module.textbook.dto.UpdateTextbookIssueRecordDto;
import com.xjrsoft.module.textbook.entity.TextbookIssueRecord;
import com.xjrsoft.module.textbook.service.ITextbookIssueRecordService;
import com.xjrsoft.module.textbook.vo.TextbookIssueRecordExcelVo;
import com.xjrsoft.module.textbook.vo.TextbookIssueRecordPageVo;
import com.xjrsoft.module.textbook.vo.TextbookIssueRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
* @title: 教材出库记录
* @Author szs
* @Date: 2023-12-27
* @Version 1.0
*/
@RestController
@RequestMapping("/textbook" + "/textbookIssueRecord")
@Api(value = "/textbook"  + "/textbookIssueRecord",tags = "教材出库记录代码")
@AllArgsConstructor
public class TextbookIssueRecordController {


    private final ITextbookIssueRecordService textbookIssueRecordService;

    @Autowired
    private ServletContext servletContext;

    @GetMapping(value = "/page")
    @ApiOperation(value="教材出库记录列表(分页)")
    @SaCheckPermission("textbookissuerecord:detail")
    public RT<PageOutput<TextbookIssueRecordPageVo>> page(@Valid TextbookIssueRecordPageDto dto){

        Page<TextbookIssueRecordPageVo> page = textbookIssueRecordService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<TextbookIssueRecordPageVo> pageOutput = ConventPage.getPageOutput(page, TextbookIssueRecordPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询教材出库记录信息")
    @SaCheckPermission("textbookissuerecord:detail")
    public RT<TextbookIssueRecordVo> info(@RequestParam Long id){
        TextbookIssueRecord textbookIssueRecord = textbookIssueRecordService.getById(id);
        if (textbookIssueRecord == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(textbookIssueRecord, TextbookIssueRecordVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增教材出库记录")
    @SaCheckPermission("textbookissuerecord:add")
    public RT<Boolean> add(@Valid @RequestBody AddTextbookIssueRecordDto dto){
        TextbookIssueRecord textbookIssueRecord = BeanUtil.toBean(dto, TextbookIssueRecord.class);
        boolean isSuccess = textbookIssueRecordService.save(textbookIssueRecord);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改教材出库记录")
    @SaCheckPermission("textbookissuerecord:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateTextbookIssueRecordDto dto){

        TextbookIssueRecord textbookIssueRecord = BeanUtil.toBean(dto, TextbookIssueRecord.class);
        return RT.ok(textbookIssueRecordService.updateById(textbookIssueRecord));

    }

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

    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData() {
        List<TextbookIssueRecordPageVo> customerList = textbookIssueRecordService.getList();
        List<TextbookIssueRecordExcelVo> dataList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        for (TextbookIssueRecordPageVo textbookIssueRecordPageVo : customerList) {
            if(textbookIssueRecordPageVo.getCreateDate() == null){
                continue;
            }
            textbookIssueRecordPageVo.setCreateDateStr(sdf.format(textbookIssueRecordPageVo.getCreateDate()));
            dataList.add(BeanUtil.toBean(textbookIssueRecordPageVo, TextbookIssueRecordExcelVo.class));
        }
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, TextbookIssueRecordExcelVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(dataList);

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

    @PostMapping("/export-query")
    @ApiOperation(value = "指定导出")
    public ResponseEntity<byte[]> exportDataQuery(@Valid @RequestBody TextbookIssueRecordExportDto dto) {
        List<TextbookIssueRecordPageVo> customerList = textbookIssueRecordService.getList(dto);
        List<TextbookIssueRecordExcelVo> dataList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        for (TextbookIssueRecordPageVo textbookIssueRecordPageVo : customerList) {
            if(textbookIssueRecordPageVo.getCreateDate() == null){
                continue;
            }
            textbookIssueRecordPageVo.setCreateDateStr(sdf.format(textbookIssueRecordPageVo.getCreateDate()));
            dataList.add(BeanUtil.toBean(textbookIssueRecordPageVo, TextbookIssueRecordExcelVo.class));
        }
        ByteArrayOutputStream bot = new ByteArrayOutputStream();

//        String projectRoot = servletContext.getRealPath("/");
//        String fileName = projectRoot + "test" + System.currentTimeMillis() + ".xlsx";
//        EasyExcel.write(fileName, TextbookIssueRecordExcelVo.class).sheet("用户表").doWrite(dataList);

        EasyExcel.write(bot, TextbookIssueRecordExcelVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(dataList);

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