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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import com.zjrs.zwnw.psam.blo.PsamDcBLO;
import com.zjrs.zwnw.psam.dto.PsamDcDTO;
import com.zjrs.zwnw.psam.dto.PsamKglDTO;
import com.zjrs.zwnw.psam.entity.RsPsamAzfkxxbMapper;
import com.zjrs.zwnw.sxgl.blo.impl.JcsxqdkBLOimpl;
import org.springframework.stereotype.Service;

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

/**
 *
 * @author liangxiudong
 * @since 2021-04-07
 */
@Service
public class PsamDcBLOImpl extends BLOImpl implements PsamDcBLO {
  @Resource
  private RsPsamAzfkxxbMapper rsPsamAzfkxxbMapper;
  @Override
  public PageResult getList(PsamKglDTO dto, int page, int rows) {
    Page<PsamDcDTO> page1 = new Page<>(page, rows);
    Page<PsamDcDTO> pageResult = (Page) rsPsamAzfkxxbMapper.getList(page1, dto);
    PageResult result = PageUtil.PageResult(pageResult);
    return result;
  }

  @Override
  public List<PsamDcDTO> dcList(PsamKglDTO dto) {
    return rsPsamAzfkxxbMapper.dcList(dto);
  }

  @Override
  public void exportExcel(PsamKglDTO dto, HttpServletResponse res) {
    //列表数据
    List<PsamDcDTO> list=rsPsamAzfkxxbMapper.getListExcel(dto);
    InputStream inputStream = null;
    try {
      inputStream = JcsxqdkBLOimpl.class.getClassLoader()
              .getResourceAsStream("templates" + File.separator + "psam" + File.separator + "psamExport.xlsx");

      String fileName = "psamExport.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();
        }
      }
    }
  }

  @Override
  public void exportTxt(PsamKglDTO dto, HttpServletResponse response) {
    response.setCharacterEncoding("utf-8");
    //设置响应的内容类型
    response.setContentType("text/plain");
    //设置文件的名称和格式
    response.addHeader("Content-Disposition","attachment;filename="+ "商户白名单.txt");
    BufferedOutputStream buff = null;
    ServletOutputStream outStr = null;
    try {
      outStr = response.getOutputStream();
      buff = new BufferedOutputStream(outStr);
      List<PsamDcDTO> list=this.dcList(dto);

      StringBuffer sb=new StringBuffer();
      buff.write(sb.toString().getBytes("UTF-8"));
      buff.flush();
      buff.close();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        buff.close();
        outStr.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}
