package com.ruoyi.hywz.controller.stamp.tax;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.hywz.entity.AdsMatHyxtStampTaxComputeVoucherD;
import com.ruoyi.hywz.service.IAdsMatHyxtStampTaxComputeVoucherDService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * <p>
 * 【印花税】印花税计算单记账凭证 前端控制器
 * </p>
 *
 * @author mybatisplus-generator
 * @date 2024-07-29 15:59:04
 */
@RestController
@AllArgsConstructor
@RequestMapping("adsMatHyxtStampTaxComputeVoucherD")
@Api(value = "adsMatHyxtStampTaxComputeVoucherD", tags = "【印花税】印花税计算单记账凭证")
public class AdsMatHyxtStampTaxComputeVoucherDController {

    private final IAdsMatHyxtStampTaxComputeVoucherDService adsMatHyxtStampTaxComputeVoucherDService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/queryPage")
    public R queryPage(@RequestParam(defaultValue = "10") Long pageSize,
                       @RequestParam(defaultValue = "1") Long pageNo,
                       @RequestBody(required = false) AdsMatHyxtStampTaxComputeVoucherD param) {
        return R.ok(adsMatHyxtStampTaxComputeVoucherDService.queryPage(pageSize, pageNo, param));
    }

    @ApiOperation(value = "凭证导出")
    @PostMapping("/dataExport")
    public void dataExport(@RequestBody(required = false) AdsMatHyxtStampTaxComputeVoucherD param, HttpServletResponse response) throws IOException {
        InputStream fis = null;
        OutputStream ops = null;

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = java.util.UUID.randomUUID() + ".xlsx";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        String templateFileName = "excelTemplate/stampTax/stampTaxComputeVoucherExport.xlsx";
        fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(templateFileName);
        ops = response.getOutputStream();
        Page<AdsMatHyxtStampTaxComputeVoucherD> pageResult = adsMatHyxtStampTaxComputeVoucherDService.queryPage(100000L, 1L, param);
        List<AdsMatHyxtStampTaxComputeVoucherD> itemList = pageResult.getRecords();
        int i = 0;
        for (AdsMatHyxtStampTaxComputeVoucherD itemD : itemList) {
            i++;
            itemD.setNo(i);
        }

        //填充导出信息
        ExcelWriter excelWriter = EasyExcel.write(ops, AdsMatHyxtStampTaxComputeVoucherD.class).withTemplate(fis).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        excelWriter.fill(itemList, fillConfig, writeSheet);
        excelWriter.finish();
    }

}
