package com.engine.htsc.trip.web;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.trip.pojo.UnionStroke;
import com.engine.htsc.trip.pojo.UnionTraveler;
import com.engine.htsc.trip.service.TripExcelOutService;
import com.engine.htsc.trip.service.impl.TripExcelOutServiceImpl;
import com.engine.htsc.trip.util.AjaxResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.net.URLEncoder;
import java.util.List;

public class TripExcelOutApiAction {

  private TripExcelOutService getService() {
    return ServiceUtil.getService(TripExcelOutServiceImpl.class);
  }

  @GET
  @Path("/travel")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<String> exportTravelStuff(
          @QueryParam("startTime") @DefaultValue("2021-01-01") String startTime,
          @QueryParam("endTime") String endTime,
          @Context HttpServletRequest request,
          @Context HttpServletResponse response) {
    try {
      String sheetOneSql = "\n" +
              "select t1.LCBH,t2.REQUESTNAME,t1.NGR,t1.NGRQ,\n" +
              "       t1.cclxzxx,\n" +
              "       t1.TXXMJC,t1.TXXMBH,t1.xmjc,\n" +
              "       t1.KHBM,t1.KHMC,\n" +
              "       t1.CCQK,\n" +
              "       t1.XDFHSJ,\n" +
              "\n" +
              "       t3.CCRGH,\n" +
              "       t3.XM,\n" +
              "       t3.BM,t3.BMFZR,\n" +
              "       t3.FYCDBM,t3.FYCDBMFZR,\n" +
              "       t3.GZD,t3.ZJ,t3.BXD\n" +
              "\n" +
              "from FORMTABLE_MAIN_58 t1\n" +
              "left join WORKFLOW_REQUESTBASE t2\n" +
              "on t1.REQUESTID=t2.REQUESTID\n" +
              "left join FORMTABLE_MAIN_58_DT2 t3\n" +
              "on t1.ID=t3.MAINID\n" +
              "where t1.LCBH is not null and t1.CCQK is not null and t1.CCLXZXX is not null\n" +
              "and to_date(t1.NGRQ,'yyyy-MM-dd HH24:MI:SS')\n" +
              "between to_date('" + startTime + "','yyyy-MM-dd') and to_date('" + endTime + "','yyyy-MM-dd')";


      String sheetTSql = "select t1.LCBH,t2.REQUESTNAME,t1.NGR,t1.NGRQ,\n" +
              "       t1.cclxzxx,\n" +
              "       t1.TXXMJC,t1.TXXMBH,t1.xmjc,\n" +
              "       t1.KHBM,t1.KHMC,\n" +
              "       t1.CCQK,\n" +
              "       t1.XDFHSJ,\n" +
              "\n" +
              "       t3.SFBC,t3.CFDGN,t3.MDDGN,t3.CFDHW,t3.MDDHW,t3.CFSJ,t3.FHSJ\n" +
              "\n" +
              "from FORMTABLE_MAIN_58 t1\n" +
              "left join WORKFLOW_REQUESTBASE t2\n" +
              "on t1.REQUESTID=t2.REQUESTID\n" +
              "left join FORMTABLE_MAIN_58_DT3 t3\n" +
              "on t1.ID=t3.MAINID\n" +
              "where t1.LCBH is not null and t1.CCQK is not null and t1.CCLXZXX is not null and\n" +
              "to_date(t1.NGRQ,'yyyy-MM-dd HH24:MI:SS')\n" +
              "between to_date('" + startTime + "','yyyy-MM-dd') and to_date('" + endTime + "','yyyy-MM-dd')";

      List<UnionTraveler> travelerData = getService().getTravelerData(sheetOneSql);
      List<UnionStroke> strokeData = getService().getStrokeData(sheetTSql);

      String excelName = "联合出差流程数据导出";
      // 指定下载的文件名--设置响应头
//      response.setContentType("application/vnd.ms-excel");
//      response.setCharacterEncoding("utf-8");
      // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//      excelName = URLEncoder.encode(excelName, "UTF-8");
//      response.setHeader("Content-disposition", "attachment;filename=" + excelName + ".xlsx");

      //这里 需要指定写用哪个class去写
      String fileName = "/app/excel/" + excelName + ".xlsx";
      ExcelWriter excelWriter = EasyExcel.write(fileName).build();
      WriteSheet t = EasyExcel.writerSheet(0, "出差人").build();
      WriteSheet s = EasyExcel.writerSheet(1, "行程").build();
      excelWriter.write(travelerData, t).write(strokeData, s);
      excelWriter.finish();

      return AjaxResult.ok("success");
    } catch (Exception e) {
      return AjaxResult.error(e.getMessage());
    }
  }


}
