package weaver.interfaces.htsc.payment;

import com.engine.htsc.payment.biz.WfToDocBiz;
import com.engine.htsc.payment.util.CommonAPI;
import com.engine.htsc.payment.util.pdf.HtmlToPdfUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

/**
 * k1810012
 * 同步资金业务付款和内部资金调拨
 */
public class UpdatePdf2Eusp implements Action {

  private static final String OLD_SYNC_TEMP = "/app/ecology/filesystem/pwf/zjpdf/";
  /**
   * 签字意见显示长度
   */
  private static final int SING_PAGE_SIZE = 100;
  /**
   * 0关闭签字意见 1 open
   */
  private static final int KEEP_SIGN = 0;
  /**
   * 2html 3pdf
   */
  private static final String GENERATE_DOCTYPE = "3";
  /**
   * 是否开启水印
   */
  private static final boolean IS_OPEN_WATERMARK = false;

  private static final User user = new User();

  public String endUnNumber; //自定义参数

  public String getEndUnNumber() {
    return endUnNumber;
  }

  public void setEndUnNumber(String endUnNumber) {
    this.endUnNumber = endUnNumber;
  }

  private final Log log = LogFactory.getLog(UpdatePdf2Eusp.class);

  @Override
  public String execute(RequestInfo requestInfo) {
    log.info("EUSP调用 开始:endUnNumber" + endUnNumber);
    try {
      String rid = requestInfo.getRequestid();
      RequestManager rm = requestInfo.getRequestManager();
      String tableName = rm.getBillTableName();
      log.info("EUSP调用 参数 rid" + rid);
      RecordSet rs = new RecordSet();
      rs.executeQuery("SELECT LCBH,LCBT FROM " + tableName +
          " WHERE REQUESTID = ? AND AUTOLAUNCHPLATFORMID IS NOT NULL ", rid);
      String lcbh = "";
      String lcbt = "";
      if (rs.next()) {
        lcbh = Util.null2String(rs.getString("LCBH"));
        lcbt = Util.null2String(rs.getString("LCBT"));
      }
      if (StringUtils.isBlank(lcbh) || StringUtils.isBlank(lcbt)) {
        log.info("EUSP调用 无法获取pdf件");
        return SUCCESS;
      }

      log.info(String.format("EUSP调用 同步pdf附件入参: lcbh:%s; ", lcbh));
      String modeid = getPrintModeByEndUnNumber(endUnNumber);
      String path = saveProcessPDF(modeid, rid, lcbh);
      String stream = HtmlToPdfUtil.read2String(path);
      HtmlToPdfUtil.sendPdfToEusp(lcbh, stream);

      log.info("同步pdf成功");
    } catch (Exception e) {
      log.error(e);
    }
    return SUCCESS;
  }

  /**
   * 生成审批单pdf
   *
   * @param printModeId 打印模板
   * @param requestid   流程id
   * @param pdfName     传流程编号,流程标题
   */
  public String saveProcessPDF(String printModeId, String requestid, String pdfName) throws Exception {
    WfToDocBiz wfToDocBiz = new WfToDocBiz(user, SING_PAGE_SIZE, KEEP_SIGN,
        GENERATE_DOCTYPE, printModeId, IS_OPEN_WATERMARK);
    wfToDocBiz.setOnlineHtmlMethod("");
    wfToDocBiz.generatepdfandhtml(requestid, pdfName, OLD_SYNC_TEMP, "save");
    return OLD_SYNC_TEMP + pdfName + "_pdf.pdf";
  }

  public String getPrintModeByEndUnNumber(String endUnNumber) throws Exception {
    String sql = "    select t2.unnumber,t1.modeid from workflow_printset t1\n" +
        "    left join workflow_nodebase t2\n" +
        "    on t1.nodeid = t2.id\n" +
        "    where type =1\n" +
        "    and printenable =1\n" +
        "    and t2.nodename = '办结'\n" +
        "    and t1.isactive = 1\n" +
        "    and t1.orderid = 0\n" +
        "    and t2.unnumber = ?";
    return CommonAPI.querySingleFieldWithPlaceholder(sql, "modeid", endUnNumber);
  }


}
