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

import cn.hutool.core.util.ReflectUtil;
import com.engine.core.impl.Service;
import com.engine.htsc.branchpayment.service.impl.BranchPaymentServiceImpl;
import com.engine.htsc.payment.enums.WBEnum;
import com.engine.htsc.payment.service.PaymentCommonService;
import com.engine.htsc.payment.util.BizUtils;
import com.engine.htsc.payment.util.PayUtil;
import com.engine.htsc.payment.util.ZipUtils;
import com.engine.htsc.trip.util.WeaverUtil;
import net.lingala.zip4j.ZipFile;
import weaver.conn.RecordSet;
import weaver.general.Util;
import com.engine.htsc.payment.service.PaymentService;
import com.engine.htsc.payment.util.PayCommonUtil;
import com.engine.htsc.payment.util.ZipUtils;
import com.engine.htsc.subpayment.service.impl.SubPaymentServiceImpl;
import net.lingala.zip4j.ZipFile;
import weaver.conn.RecordSetDataSource;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;

import javax.ws.rs.ext.Provider;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PaymentCommonServiceImpl extends Service implements PaymentCommonService {

  @Override
  public File buildAttachmentsZipFile() throws Exception {
    String parentPath = "/app/ecology/filesystem/temp/zip";
    ZipUtils zipUtils = new ZipUtils();
    PropBean pp = new PropBean();
    String categories = pp.getPropName("AttachmentCategories"); //附件目录名称,逗号分隔
    String[] attachmentsCategories = categories.split(",");
    File targetFile = new File(parentPath + File.separator + "网报流程附件.zip");
    if (targetFile.exists()) {
      targetFile.delete();
    }
    for (String attachmentsCategory : attachmentsCategories) {
      List<File> filesByCategory = zipUtils.getFilesByCategory(attachmentsCategory, parentPath);
      File file = zipUtils.zipFiles(new File(parentPath + File.separator + attachmentsCategory + ".zip"), filesByCategory);
      new ZipFile(targetFile).addFile(file);
    }
    return new File(parentPath + File.separatorChar + "网报流程附件.zip");
  }

  @Override
  public String syncContractInfo(String syncIt, String rids) throws Exception {
    RecordSet rs = new RecordSet();
    rs.writeLog("syncContractInfo,流程编号:requestid:" + rids);
    BizUtils bizUtils = new BizUtils();
    StringBuilder sb = new StringBuilder();
    for (String requestId : rids.split(",")) {
      boolean normalContractFlag = PayUtil.updatePaymentInfoToHuaTai(requestId);
      rs.writeLog("普通合同更新状态flag:" + normalContractFlag);
      sb.append("普通合同更新状态flag:").append(normalContractFlag);
      String tableName = WeaverUtil.getTableNameByRequestId(requestId, rs);
      rs.executeQuery("SELECT LCBH FROM " + tableName + " fm WHERE REQUESTID = ?", requestId);
      String lcbh = "";
      if (rs.next()) {
        lcbh = Util.null2String(rs.getString("LCBH"));
      }
      if (lcbh.contains("总部IT付款") && "true".equals(syncIt)) {
        boolean type = BizUtils.whetherItOrTy(requestId, tableName);
        rs.writeLog("当前流程未it付款, 判断是否是it费用付款,flag=" + type + "; (注:false为it费用付款)");
        if (!type) {
          sb.append(";");
          //执行it流程合同
          rs.writeLog("it合同更新状态开始");
          String workflowTable = bizUtils.getTableNameByRequestId(requestId);
          boolean itContractFlag = PayUtil.updateItPaymentInfoToHT(requestId, workflowTable);
          rs.writeLog("it合同更新状态flag:" + itContractFlag);
          sb.append("it合同更新状态flag:").append(itContractFlag);
        }
      }
    }
    return sb.toString();
  }

  @Override
  public List<Map<String, String>> syncFundAllStatus(String requestMarks) throws Exception {
    BaseBean bb = new BaseBean();
    bb.writeLog(String.format("syncFundAllStatus starting...., requestMarks: %s", requestMarks));
    Map<String, String> unNumberByRequestId = PayCommonUtil.getUnNumberByRequestId(requestMarks);
    String unNumber = unNumberByRequestId.get("UNNUMBER");
    String requestId = unNumberByRequestId.get("REQUESTID");
    if (WBEnum.HEAD.getWfUnNumber().contains(unNumber)) {
      bb.writeLog(String.format("requestId's unnumber: %s, 总部流程,执行" +
          "com.engine.htsc.payment.service.PaymentService.doPayment", unNumber));
      //总部 doPayment入参为建模id,需要转化
      String ufId = PayCommonUtil.trans2Id(requestId);
      List<Map<String, String>> resp = ReflectUtil.invoke(new PaymentServiceImpl(), "doPayment", ufId);
      List<Map<String, String>> tempList = new ArrayList<>();
      resp.forEach(map -> map.forEach((key, value) -> {
        Map<String, String> temp = new HashMap<>();
        temp.put("requestMark", key);
        temp.put("result", value);
        tempList.add(temp);
      }));
      return tempList;
    }

    if (WBEnum.BRANCH.getWfUnNumber().contains(unNumber)) {
      bb.writeLog(String.format("requestId's unnumber: %s, 分支机构流程,执行" +
          "com.engine.htsc.branchpayment.service.impl.BranchPaymentServiceImpl.doPaymentByBatchId", unNumber));
      Map<String, String> map = ReflectUtil.invoke(new BranchPaymentServiceImpl(), "doPaymentByBatchId", requestId);
      List<Map<String, String>> tempList = new ArrayList<>();
      map.forEach((key, value) -> {
        Map<String, String> temp = new HashMap<>();
        temp.put("requestMark", key);
        temp.put("result", value);
        tempList.add(temp);
      });
      return tempList;
    }

    if (WBEnum.SUB.getWfUnNumber().contains(unNumber)) {
      bb.writeLog(String.format("requestId's unnumber: %s, 子公司流程,执行" +
          "com.engine.htsc.subpayment.service.impl.SubPaymentServiceImpl.doPaymentByBatchId", unNumber));
      Map<String, String> map = ReflectUtil.invoke(new SubPaymentServiceImpl(), "doPaymentByBatchId", requestId);
      List<Map<String, String>> tempList = new ArrayList<>();
      map.forEach((key, value) -> {
        Map<String, String> temp = new HashMap<>();
        temp.put("requestMark", key);
        temp.put("result", value);
        tempList.add(temp);
      });
      return tempList;
    }

    throw new Exception(String.format("流程资金表状态更新失败,流程id不存在 %s", requestId));
  }
}
