package com.engine.htsc.mpsq.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.engine.core.impl.Service;
import com.engine.htsc.mpsq.ExcelListener;
import com.engine.htsc.mpsq.MpsqEntity;
import com.engine.htsc.mpsq.service.MpsqService;
import com.engine.htsc.payment.pojo.HTPaymentCostDetail;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.io.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.conn.RecordSet;
import weaver.conn.aop.RecordSetAop;
import weaver.file.ImageFileManager;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MpsqServiceImpl extends Service implements MpsqService {
    @Override
    public List<MpsqEntity> getDetailByExcel(Map<String, Object> params) {
        JAVATools.SetCmdwritelog("-------名片申请Excel解析文件参数:-------" + params.toString());
        ArrayList<MpsqEntity> result = new ArrayList<>();
        List<MpsqEntity> list = null;
        RecordSet rs = new RecordSet();
        String fileIds = (String) params.get("fileIds");
        if(StringUtils.isNotBlank(fileIds)){
            if(fileIds.contains(",")){
                String[] split = fileIds.split(",");
//                for (String fileId :split) {
                String fileId =split[split.length-1] ;
                if(StringUtils.isNotBlank(fileId)){
                    String sql = "select * from docimagefile where docid = " + fileId;
                    rs.execute(sql);
                    if(rs.next()){
                        InputStream inputStream = ImageFileManager.getInputStreamById(Integer.parseInt(rs.getString("imagefileid")));
                        list = EasyExcel.read(inputStream, MpsqEntity.class, new ExcelListener()).sheet().doReadSync();
//                        result.addAll(list);
                        JAVATools.SetCmdwritelog("list的长度"+list.size());
                        JAVATools.SetCmdwritelog("list的内容"+list.toString());
                    }
                }


//                }
            }else{
                String sql = "select * from docimagefile where docid = " + fileIds;
                rs.execute(sql);
                if(rs.next()){
                    InputStream inputStream = ImageFileManager.getInputStreamById(Integer.parseInt(rs.getString("imagefileid")));
                    list = EasyExcel.read(inputStream, MpsqEntity.class, new ExcelListener()).sheet().doReadSync();
                    JAVATools.SetCmdwritelog("list的长度"+list.size());
                    JAVATools.SetCmdwritelog("list的内容"+list.toString());
                }

            }
        }


        return list;
    }

    @Override
    public String getExportExcel(Map<String, Object> map, HttpServletResponse response) {
//        public void getExportExcel(Map<String, Object> map) {
        OutputStream outputStream = null;
        String path=null;
        String requestid = (String)map.get("requestid");
        JAVATools.writelog("名片申请导出 ids="+requestid);
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        if(StringUtils.isNotBlank(requestid)){
            String mainMpsqTableName = pb.getPropName("mainMpsqTableName");
            String mainSql = "select * from " + mainMpsqTableName + " where requestid = " + requestid;
            JAVATools.writelog("名片申请主表sql="+mainSql);
            rs.execute(mainSql);
            rs.next();
            String dtMpsqTableName = pb.getPropName("dtMpsqTableName");
            String sql = "select * from "+dtMpsqTableName+" where mainid = " + rs.getString("id");
            JAVATools.writelog("名片申请明细表sql="+sql);
            rs.execute(sql);
            ArrayList<MpsqEntity> list = new ArrayList<>();
            int i = 1;
            while(rs.next()){
                MpsqEntity entity = new MpsqEntity();
                entity.setXh(i++);
                entity.setXm(rs.getString("xm"));
                entity.setYwm(rs.getString("ywm"));
                entity.setBm(rs.getString("bm"));
                entity.setBmywm(rs.getString("bmywm"));
                entity.setZw(rs.getString("zw"));
                entity.setZwywm(rs.getString("zwywm"));
                entity.setLxdz(rs.getString("lxdz"));
                entity.setLxdzyw(rs.getString("lxdzyw"));
                entity.setGddh(rs.getString("gddh"));
                entity.setSj(rs.getString("sj"));
                entity.setCz(rs.getString("cz"));
                entity.setEmail(rs.getString("email"));
                entity.setYs(rs.getString("ys"));
                list.add(entity);
            }
            JAVATools.writelog("名片申请list="+list.toString());
            ExcelWriter excelWriter = null;
            try {
//                outputStream = response.getOutputStream();
                Date date = new Date();
                String dataForm = new SimpleDateFormat("yyyyMMdd").format(date);
                path="/app/ecology/filesystem/mpsq/"+dataForm;
// 如果不存在,创建文件夹
                File f = new File(path);
                if(!f.exists()){
                    f.mkdirs();
                }
                path = path+"/"+System.currentTimeMillis()+"mpsq.xlsx";
                outputStream = new FileOutputStream(path);
                excelWriter = EasyExcel.write(outputStream, MpsqEntity.class).build();
                WriteSheet writeSheet = EasyExcel.writerSheet("名片申请").build();
                excelWriter.write(list, writeSheet);
                JAVATools.writelog("名片申请导出完成");
                outputStream.flush();
//                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 千万别忘记finish 会帮忙关闭流
                if (excelWriter != null) {
                    excelWriter.finish();
                }

            }

        }
        return path;
    }

    @Override
    public boolean deleteFile(String path) {
        boolean flag = false;
        JAVATools.writelog("名片删除文件路径"+path);
        File file = new File(path);
        if(file.isFile() && file.exists()){
            file.delete();
            JAVATools.writelog("名片删除文件完成");
            return true;
        }
        return false;
    }
}
