package com.zjrs.zwnw.sxgl.blo.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import com.zjrs.zwnw.sxgl.blo.YwltjBLO;
import com.zjrs.zwnw.sxgl.dto.YwltjQueryDTO;
import com.zjrs.zwnw.sxgl.dto.YwltjResultDTO;
import com.zjrs.zwnw.sxgl.entity.SxtjMapper;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenjiebo
 * @date 2022/12/12 17:02
 **/
@Service
public class YwltjBLOImpl implements YwltjBLO {

    @Resource
    SxtjMapper sxtjMapper;

    @Override
    public PageResult queryYwltjList(YwltjQueryDTO dto) {
        Page<YwltjResultDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<YwltjResultDTO> pageResult = (Page) sxtjMapper.queryYwltjList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void exportYwltjList(YwltjQueryDTO dto, HttpServletResponse res) {
        List<YwltjResultDTO> result = sxtjMapper.exportYwltjList(dto);
        InputStream inputStream = null;
        int sumRow = 3;
        int ywl = 0;
        int fcmy = 0;
        int my = 0;
        int jbmy = 0;
        double hpl = 0.0;
        int bmy = 0;
        int fcbmy = 0;
        double cpl = 0.0;
        //离散数据
        Map<String,Object> sumRows = new HashMap();
        String hplname = hpl+"%";
        String cplname = cpl+"%";
        String msg = "申请日期：%s至%s";
        String start = StringUtils.isNotBlank(dto.getSqrqc()) ? dto.getSqrqc():" ";
        String end = StringUtils.isNotBlank(dto.getSqrqz()) ? dto.getSqrqz():" ";
        msg = String.format(msg,start,end);
        sumRows.put("ywl",ywl);
        sumRows.put("query",msg);
        sumRows.put("total","合计");
        try {
            inputStream = YwltjBLOImpl.class.getClassLoader()
                    .getResourceAsStream("templates" + File.separator + "sxgl" + File.separator + "ywltj.xlsx");
            
            String fileName = "业务量统计导出.xlsx";
            res.setContentType("application/vnd.ms-excel;charset=utf-8");
            res.setHeader("Content-disposition", "attachment;filename=" + fileName);
            res.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
