package com.engine.kq.wfset.util;

import com.engine.kq.biz.KQFlowActiontBiz;
import com.engine.kq.biz.KQTravelRulesBiz;
import com.engine.kq.enums.DurationTypeEnum;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.bean.SplitBean;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

public class KQFlowEvectionUtil {
  private KQLog kqLog = new KQLog();


  /**
   * 拆分出差数据 生成splitBeans
   * @param sqlMap
   * @param splitBeans
   * @param datetimeFormatter
   * @param workflowId
   * @param requestId
   * @param rci
   * @return
   * @throws Exception
   */
  public Map<String,String> handleKQEvectionAction(Map<String, String> sqlMap,
      List<SplitBean> splitBeans, DateTimeFormatter datetimeFormatter, int workflowId,
      int requestId, ResourceComInfo rci) throws Exception{

    KQFlowActiontBiz kqFlowActiontBiz = new KQFlowActiontBiz();
    return kqFlowActiontBiz.handleAction(sqlMap,splitBeans, datetimeFormatter, workflowId,requestId, rci,DurationTypeEnum.EVECTION);
  }
  /**
   * 出差流程单独需要赋值的数据
   * @param prefix
   * @param rs1
   * @param splitBean
   */
  public static void bean4Evection(String prefix,RecordSet rs1, SplitBean splitBean) {
    String minimumUnit = Util.null2s(Util.null2String(KQTravelRulesBiz.getMinimumUnit()),"-1");
    String computingMode = Util.null2s(Util.null2String(KQTravelRulesBiz.getComputingMode()),"-1");
    String companion = Util.null2s(rs1.getString(prefix+"companion"), "");
    //设置同行人 这里拆分的时候并没有把同行人也拆分
    splitBean.setCompanion(companion);
    splitBean.setDurationrule(minimumUnit);
    splitBean.setComputingMode(computingMode);
  }

  /**
   * 出差流程特有的陪同人
   * @param companion
   * @param bean
   * @param params
   * @param rci
   * @param formateList
   */
  public void splitEvectionCompanion(String companion, SplitBean bean,
      List<List> params, ResourceComInfo rci, List<String> formateList) {
    String[] companions = companion.split(",");
    if(companions != null && companions.length > 0 ) {
      for (int i = 0; i < companions.length; i++) {
        String compan_resid = companions[i];
        if(compan_resid.length() > 0 && Util.getIntValue(compan_resid) > 0){
          if(bean.getResourceId().equalsIgnoreCase(compan_resid)){
            //陪同人是自己的不要存到中间表里
            continue;
          }
          List<Object> beanParams =  new ArrayList<Object>();
          beanParams.add(bean.getRequestId());
          beanParams.add(bean.getWorkflowId());
          beanParams.add(bean.getDataId());
          beanParams.add(bean.getDetailId());
          beanParams.add(compan_resid);
          beanParams.add(bean.getFromDate());
          beanParams.add(bean.getFromTime());
          beanParams.add(bean.getToDate());
          beanParams.add(bean.getToTime());
          beanParams.add(bean.getNewLeaveType());
          beanParams.add(Util.null2s(bean.getDuration(),"0"));
          beanParams.add(bean.getUsedetail());
          beanParams.add(bean.getDurationrule());
          beanParams.add(bean.getTablenamedb());
          beanParams.add(bean.getFromdatedb());
          beanParams.add(bean.getFromtimedb());
          beanParams.add(bean.getTodatedb());
          beanParams.add(bean.getTotimedb());
          beanParams.add(bean.getDurationDB());
          beanParams.add(bean.getStatus());
          beanParams.add(bean.getBelongDate());
          beanParams.add(bean.getD_Mins());
          beanParams.add(bean.getSerialid());
          beanParams.add(bean.getChangeType());
          beanParams.add(rci.getSubCompanyID(compan_resid));
          beanParams.add(rci.getDepartmentID(compan_resid));
          beanParams.add(rci.getDepartmentID(compan_resid));
          beanParams.add(bean.getCompanion());
          beanParams.add("1");
          String format_1 = compan_resid+"_"+bean.getBelongDate();
          formateList.add(format_1);
          params.add(beanParams);
        }
      }
    }
  }
}
