package cn.ezhizao.project.business.service.impl;

import cn.ezhizao.common.utils.MyHandler;
import cn.ezhizao.project.business.domain.BizOutsourcedOrder;
import cn.ezhizao.project.business.domain.BizOutsourcedOrderDetail;
import cn.ezhizao.project.business.domain.BizReturnReceipt;
import cn.ezhizao.project.business.domain.BizReturnReceiptDetail;
import cn.ezhizao.project.business.mapper.BizOutsourcedOrderMapper;
import cn.ezhizao.project.business.mapper.BizReturnReceiptMapper;
import cn.ezhizao.project.business.service.IBizOutsourcedOrderService;
import cn.ezhizao.project.business.service.IBizReturnReceiptService;
import cn.ezhizao.project.system.domain.SysUser;
import cn.hutool.core.io.IoUtil;
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.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 收回单主表
 *
 * @author ezhizao
 * date    2024-05-05
 */
@Service
public class BizReturnReceiptServiceImpl extends ServiceImpl<BizReturnReceiptMapper, BizReturnReceipt> implements IBizReturnReceiptService
{
    @Resource
    private BizReturnReceiptMapper bizReturnReceiptMapper;
    @Resource
    private BizOutsourcedOrderMapper bizOutsourcedOrderMapper;
    @Value("${file.excelTemplate.url}")
    private  String templatePath;

    /**
     * 查询外协单主表
     *
     * @param bizReturnReceipt 外协单主表
     * @return 外协单主表
     */
    @Override
    public List<BizReturnReceipt> getList(BizReturnReceipt bizReturnReceipt)
    {
        return bizReturnReceiptMapper.getList(bizReturnReceipt);
    }

    /**
     * 物理删除
     * @param bizReturnReceipt 待删除
     * @return 删除结果
     */
    @Override
    public int physicalDelete(BizReturnReceipt bizReturnReceipt){ return bizReturnReceiptMapper.physicalDelete(bizReturnReceipt); };
    @Override
    public void exportTemplateProvinceAreaData(HttpServletResponse response, BizReturnReceipt bizReturnReceipt) throws IOException {
        List<BizReturnReceiptDetail> result = bizReturnReceipt.getDetails();
        String template = "outsourcedReturn";
        String templateFilePath =  templatePath + template + ".xlsx";
        //本地导出
        //excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
        //流输出
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
                .withTemplate(templateFilePath)
                .build();

        WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new MyHandler()).build();

        //配置多组数据填充完后，需要换行，防止覆盖模板中的单组数据模板
        FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
        // 直接写入list数据
        excelWriter.fill(result, fillConfig, writeSheet);
        // 写入自定义的表头
        Map<String, Object> map = new HashMap<>();
        map.put("formCode",bizReturnReceipt.getFormCode());
        map.put("supplierName",bizReturnReceipt.getSupplierName());
        map.put("supplierCode",bizReturnReceipt.getSupplierCode());
        map.put("formDate",bizReturnReceipt.getFormDate());
        excelWriter.fill(map, writeSheet);
        excelWriter.finish();

        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        response.setHeader("Content-Disposition", "attachment;filename=test.xlsx");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public List<BizReturnReceipt> getAllList() {
        return bizReturnReceiptMapper.getAllList();
    }
}
