package com.engine.kq.wfset.attendance.manager;

import com.alibaba.fastjson.JSON;
import com.engine.kq.biz.KQAttProcSetComInfo;
import com.engine.kq.enums.KqSplitFlowTypeEnum;
import com.engine.kq.wfset.attendance.dao.HrmAttProcSetDao;
import com.engine.kq.wfset.attendance.domain.HrmAttProcSet;
import com.engine.kq.wfset.attendance.domain.WorkflowBillfield;
import com.engine.kq.wfset.attendance.framework.BaseManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;

/**
 * HrmAttProcSetManager
 * @author wcd
 * @version 1.0 2015-04-22
 */
public class HrmAttProcSetManager extends BaseManager<HrmAttProcSet> {

	private HrmAttProcSetDao dao = null;

	public HrmAttProcSetManager() {
		dao = new HrmAttProcSetDao();
		this.setDao(dao);
	}
	
	public Long save(HrmAttProcSet bean) {
		String id = String.valueOf(bean.getId());
		if(id.equals("0") || id.equals("-1")) id = String.valueOf(this.insert(bean));
		else this.update(bean);
		return Long.valueOf(id);
	}

	public List<HrmAttProcSet> getFieldList(int field006, boolean field005, String id,String usedetail) {
		return dao.getByField006(field006, field005, id,usedetail);
	}

  /**
   *
   * @param field006 变更流程类型，0表示请假，1表示出差，2表示公出，3表示加班，4表示异常，5表示调班，6表示销假
   * @param paramMap 参数拼接sqlwhere
   * @param ef true表示归档流程
   * @param field005 表示考勤流程是否启用
   * @param id 查询kq_ATT_PROC_SET的sql条件
   * @param usedetails 是否使用明细
   * @return
   */
  public Map<String,String> getSQLByField006Map(int field006, Map<String, String> paramMap, boolean ef, boolean field005, String id,String usedetails) {
    Map<String,String> sqlMap = new HashMap<>();
    List<HrmAttProcSet> list = getFieldList(field006, field005, id,usedetails);
    //和E8不一样，这里的tableMap里只能记录一种考勤流程设置，所以size都只能是1
    Map<String, String> tableMap = new HashMap<String, String>();
    String concort = "###" ;

    //补卡/销假流程既有主表也有明细表
    if(field006 == KqSplitFlowTypeEnum.CARD.getFlowtype()
        || field006 == KqSplitFlowTypeEnum.LEAVEBACK.getFlowtype()
        || field006 == KqSplitFlowTypeEnum.PROCESSCHANGE.getFlowtype()){
      getTableMap7(list,tableMap);
    }else{
      getTableMap(list,tableMap);
    }

    StringBuffer sql = new StringBuffer();
    Iterator it = tableMap.entrySet().iterator();
    Iterator paramIt = null;
    String whereSql = ef ? "b.currentNodeType = '3'" : "1 = 1";
    while(it.hasNext()){
      sql = new StringBuffer();
      Map.Entry entry = (Map.Entry) it.next();
      String key = StringUtil.vString(entry.getKey());
      String[] keys = key.split(concort);
      //是否是补卡流程
      String isSpecial = "";
      if(keys.length == 5){
        //是否是补卡流程
        isSpecial =  keys[4] ;
      }
      String wfId =  keys[3] ;
      String usedetail =  keys[2] ;
      String tableDetailName=  keys[1] ;
      String tableName=  keys[0] ;
      boolean isDetail = "1".equalsIgnoreCase(usedetail);
      boolean hasSpecial = "1".equalsIgnoreCase(isSpecial);

      //value是所有的字段 主表明细表要显示的字段
      String value = StringUtil.vString(entry.getValue());
      if(value.endsWith(",")) {
        value = value.substring(0, value.length()-1);
      }
      kqLog.info("getSQLByField006Map:value:"+ value);
      if(value.length() == 0) {
        continue;
      }

//    sql模型：
//      (select backfield  from 主表 t left join 明细表（如果有的话） t1 on t.id = t1.mainid) t
//      left join Workflow_Requestbase b on t.requestid = b.requestid left join HrmResource c
//      on t.detail_resourceId = c.id（这个是和人员表关联，因为resourceId这个字段补卡和其他类型不一样需要特殊处理）
//      left join HrmDepartment d  on c.departmentId = d.id

      sql.append("select t.*,b.requestname,b.status,c.lastname,c.departmentId c_departmentId,d.departmentname,c.subcompanyid1,c.workcode from (select t.requestid,").
      append(isDetail ? " t1.id as detailId," : "t.id as dataId,").append(value).
      append(" from ").append(tableName).append(" t ").
      append(isDetail ? " left join "+ tableDetailName + " t1 on t.id = t1.mainid" : "").append(") t").
      append(" left join Workflow_Requestbase b on t.requestid = b.requestid left join HrmResource c on ").
      append(hasSpecial? " t.resourceId = c.id" :(isDetail ? " t.detail_resourceId = c.id " :" t.resourceId = c.id ")).
      append(" left join HrmDepartment d on c.departmentId = d.id where ").
      append(whereSql).append(" and workflowid="+wfId);

      if(paramMap != null && !paramMap.isEmpty()) {
        paramIt = paramMap.entrySet().iterator();
        while(paramIt.hasNext()){
          entry = (Map.Entry) paramIt.next();
          sql.append(" ").append(StringUtil.vString(entry.getValue()));
        }
      }
      sqlMap.put(key, sql.toString());
    }

    kqLog.info("getSQLByField006Map:"+sqlMap);
    return sqlMap;
  }

  /**
   * 因为getSQLByField006Map我限定了外层循环kq_att_set表，所以这个tableMap的size只能唯一
   * 针对补卡流程，主表明细表都有的情况
   * @param list
   * @param tableMap
   */
  private void getTableMap7(List<HrmAttProcSet> list,Map<String, String> tableMap) {
    String tempSql = "";
    String concort = "###" ;
    int wfId = 0;
    int usedetail = 1;
    //表示是主表带明细表流程
    int isSpecial = 1;
    String tablename = "";
    String detailtablename = "";

    for(HrmAttProcSet bean : list){
      wfId = bean.getField001() > 0 ? bean.getField001() : wfId;
      tablename = bean.getTablename().length() > 0 ? bean.getTablename() :tablename;
      detailtablename = bean.getDetailtablename().length() > 0 ? bean.getDetailtablename() :detailtablename;

      if(StringUtil.isNotNull(bean.getField011())) {
        String alias = "t";

        if(bean.getField011().startsWith("detail_")) {
          //t1表示明细别名
          alias = "t1";
        }else{
          alias = "t";
        }
        String column=(StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp") ? "''"
            :" "+alias+"." + bean.getField010()) + " as " + bean.getField011()+",";

        if(!tempSql.contains(column)){
          tempSql += column;
        }
      }
    }
    String key = tablename + concort+detailtablename+ concort + usedetail+ concort + wfId+ concort + isSpecial ;
    tableMap.put(key, tempSql);

  }
  /**
   * 因为getSQLByField006Map我限定了外层循环kq_att_set表，所以这个tableMap的size只能唯一
   * 针对考勤字段只有主表或者明细表的情况
   * @param list
   * @param tableMap
   */
  private void getTableMap(List<HrmAttProcSet> list,Map<String, String> tableMap) {
    String tempSql = "";
    String concort = "###" ;
    int wfId = 0;
    int usedetail = 0;
    String tablename = "";
    String detailtablename = "";
    for(HrmAttProcSet bean : list){
      kqLog.info("getTableMap:bean:"+ JSON.toJSON(bean));
      usedetail = bean.getUsedetail() > -1 ? bean.getUsedetail() : usedetail;
      boolean isUseDetail = usedetail == 1;

      wfId = bean.getField001() > 0 ? bean.getField001() : wfId;
      tablename = bean.getTablename().length() > 0 ? bean.getTablename() :tablename;
      detailtablename = bean.getDetailtablename().length() > 0 ? bean.getDetailtablename() :detailtablename;
      if(StringUtil.isNotNull(bean.getField011())) {
        if("vacationInfo".equalsIgnoreCase(bean.getField011()) || "detail_vacationInfo".equalsIgnoreCase(bean.getField011())) {
          continue;
        }
        String alias = "t";

        //如果是明细表，那么所有以detail_开头的都记录，其他的都不记录
        if(isUseDetail){
          if(!bean.getField011().startsWith("detail_")) {
            continue;
          }
        }else{
          //主表的话，只要是detail_开头的都不记录
          if(bean.getField011().startsWith("detail_")) {
            continue;
          }
        }
        if(isUseDetail){
          //t1表示明细别名
          alias = "t1";
        }
        String column=(StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp") ? "''"
            :" "+alias+"." + bean.getField010()) + " as " + bean.getField011()+",";

        if(!tempSql.contains(column)){
          tempSql += column;
        }
      }
    }
    String key = tablename + concort+detailtablename+ concort + usedetail+ concort + wfId ;
    kqLog.info("getTableMap:key:"+ key);
    tableMap.put(key, tempSql);
  }

  public void welcomeToSet(int field001) {
		welcomeToSet(field001, 0);
	}
	
	public void welcomeToSet(int field001, int field006) {
		dao.setCustomPage(field001, field006);
	}
	public void welcomeToSet(int field001, int field006,String templetfile,String templetmobilefile,String templetroute) {
		dao.setCustomPage(field001, field006,templetfile,templetmobilefile,templetroute);
	}

	public boolean isFreezeNode(int wfId, int nodeType) {
		return dao.getFreezeNodeType(wfId) == nodeType;
	}
	
	//只在创建节点做冻结判断
	public boolean isFreezeNodeId(int wfId, int nodeid) {
		boolean hasFreezeNodeId = dao.hasFreezeNodeId(wfId);
		boolean isStartNodeId = dao.getWFStartNodeId(wfId) == nodeid;
		return isStartNodeId && hasFreezeNodeId;
	}
	
	public String[] getFieldList(int field001, int field002) {
    Map<String,Object> result = getFieldList(0, field001, field002);
		return (String[])result.get("fields");
	}
	
	private Map<String, List> getResultMap(int type, int field001, int field002) {
		Map<String, List> map = new HashMap<String, List>();
		WorkflowBillfieldManager manager = new WorkflowBillfieldManager();
		List<WorkflowBillfield> fList = manager.find("[map]billid:"+field002);
    List<WorkflowBillfield> fListDetail = manager.find("[map]billid:"+field002+";viewtype:1");

    List<HrmAttProcSet> list = getFieldList(type, true, "select id from kq_ATT_PROC_SET where field001 = "+field001+" and field002 = "+field002);
		map.put("fList", fList);
		map.put("fListDetail", fListDetail);
    map.put("list", list);
		return map;
	}

  public List<HrmAttProcSet> getFieldList(int field006, boolean field005, String id) {
    return dao.getByField006(field006, field005, id,"");
  }
  /**
   *
   * @param type 变更流程类型，0表示请假，1表示出差，2表示公出，3表示加班，4表示异常，5表示调班，6表示销假，7表示补卡，8表示考勤变更流程
   * @param field001 流程id
   * @param field002 表单id
   * @return
   */
	public Map<String,Object> getFieldList(int type, int field001, int field002) {
		Map<String, List> map = getResultMap(type, field001, field002);
		List<WorkflowBillfield> fList = (List<WorkflowBillfield>)map.get("fList");
    List<WorkflowBillfield> fListDetail = (List<WorkflowBillfield>)map.get("fListDetail");
		List<HrmAttProcSet> list = (List<HrmAttProcSet>)map.get("list");
    Map<String,Object> result = new HashMap<>();
		if(fList == null || list == null) {
		  return result;
    }

//    变更流程类型，0表示请假，1表示出差，2表示公出，3表示加班，4表示异常，5表示调班，6表示销假，7表示补卡，8表示考勤变更流程
		switch(type) {
		case 0:
			result = getFieldList0(fList, fListDetail,list);
			break;
    case 1:
      result = getFieldList1(fList, fListDetail,list);
      break;
    case 2:
      result = getFieldList2(fList, fListDetail,list);
      break;
		case 3:
			result = getFieldList3(fList, fListDetail,list);
			break;
    case 4:
      result = getFieldList4(fList, fListDetail,list);
      break;
    case 5:
      result = getFieldList5(fList, fListDetail,list);
      break;
    case 6:
      result = getFieldList6(fList, fListDetail,list);
      break;
    case 7:
      result = getFieldList7(fList, fListDetail,list);
      break;
    case 8:
      result = getFieldList8(fList, fListDetail,list);
      break;
		}
    KQAttProcSetComInfo kqAttProcSetComInfo = new KQAttProcSetComInfo();
		String attid = kqAttProcSetComInfo.getAttid(field001+"");
		if(result != null){
		  result.put("attid", attid);
    }
		return result;
	}
	
	private static final String FIELD = "field";
	
	private Map<String,Object> getFieldList0(List<WorkflowBillfield> fList,List<WorkflowBillfield> fListDetail, List<HrmAttProcSet> list) {
    Map<String,Object> fieldMap = new HashMap<>();
		String resourceId = "";
		String departmentId = "";
		String newLeaveType = "";
		String fromDate = "";
		String fromTime = "";
		String toDate = "";
		String toTime = "";
		String duration = "";
		String vacationInfo = "";
    String usedetailstr = "";
    String detailtablename = "";
    String tablename = "";
    Long attid = 0L;
    int formid = 0;
    List<WorkflowBillfield> forfList = fList;
		for(HrmAttProcSet bean : list) {
		  int usedetail = bean.getUsedetail();
      formid = bean.getField002();
      if(usedetail == 1){
        forfList = fListDetail;
        usedetailstr = ""+usedetail;
        detailtablename = bean.getDetailtablename();
      }else{
        tablename = bean.getTablename();
      }
      String detailPre = "";
      if(usedetail == 1){
        detailPre = "detail_";
      }
			for(WorkflowBillfield fieldBean : forfList) {
        if(usedetail == 1){
          String fieldbean_detailtable = fieldBean.getDetailtable();
          if(fieldbean_detailtable.length() > 0 && detailtablename.length() >0){
            if(!fieldbean_detailtable.equalsIgnoreCase(detailtablename)){
              continue;
            }
          }
        }
				if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) || 
						StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
				  continue;
        }
        if(bean.getField011().equalsIgnoreCase(detailPre+"resourceId")) {
          resourceId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"departmentId")) {
          departmentId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"newLeaveType")) {
          newLeaveType = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"fromDate")) {
          fromDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"fromTime")) {
          fromTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"toDate")) {
          toDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"toTime")) {
          toTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"duration")) {
          duration = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"vacationInfo")) {
          vacationInfo = FIELD + fieldBean.getId();
          break;
        }
			}
      attid = bean.getId();
		}
		String[] result = new String[9];
		result[0] = resourceId;
		result[1] = departmentId;
		result[2] = newLeaveType;
		result[3] = fromDate;
		result[4] = fromTime;
		result[5] = toDate;
		result[6] = toTime;
		result[7] = duration;
		result[8] = vacationInfo;
		fieldMap.put("fields", result);
		fieldMap.put("usedetail", usedetailstr);
		fieldMap.put("detailtablename", detailtablename);
		if(detailtablename.length() > 0){
		  int detailIndex = getDetail_index(formid+"", detailtablename);
      fieldMap.put("detailIndex", detailIndex);
    }
		fieldMap.put("tablename", tablename);
    fieldMap.put("attid", attid);

		return fieldMap;
	}

  private Map<String,Object> getFieldList1(List<WorkflowBillfield> fList,List<WorkflowBillfield> fListDetail, List<HrmAttProcSet> list) {
    Map<String,Object> fieldMap = new HashMap<>();

    String resourceId = "";
    String departmentId = "";
    String fromDate = "";
    String fromTime = "";
    String toDate = "";
    String toTime = "";
    String duration = "";
    String companion = "";
    String usedetailstr = "";
    String detailtablename = "";
    String tablename = "";
    Long attid = 0L;
    int formid = 0;
    List<WorkflowBillfield> forfList = fList;
    for(HrmAttProcSet bean : list) {
      int usedetail = bean.getUsedetail();
      formid = bean.getField002();
      if(usedetail == 1){
        forfList = fListDetail;
        usedetailstr = ""+usedetail;
        detailtablename = bean.getDetailtablename();
      }else{
        tablename = bean.getTablename();
      }
      String detailPre = "";
      if(usedetail == 1){
        detailPre = "detail_";
      }
      for(WorkflowBillfield fieldBean : forfList) {
        if(usedetail == 1){
          String fieldbean_detailtable = fieldBean.getDetailtable();
          if(fieldbean_detailtable.length() > 0 && detailtablename.length() >0){
            if(!fieldbean_detailtable.equalsIgnoreCase(detailtablename)){
              continue;
            }
          }
        }
        if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
            StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
          continue;
        }
        if(bean.getField011().equalsIgnoreCase(detailPre+"resourceId")) {
          resourceId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"departmentId")) {
          departmentId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"fromDate")) {
          fromDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"fromTime")) {
          fromTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"toDate")) {
          toDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"toTime")) {
          toTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"duration")) {
          duration = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"companion")) {
          companion = FIELD + fieldBean.getId();
          break;
        }
      }
      attid = bean.getId();
    }
    String[] result = new String[8];
    result[0] = resourceId;
    result[1] = departmentId;
    result[2] = fromDate;
    result[3] = fromTime;
    result[4] = toDate;
    result[5] = toTime;
    result[6] = duration;
    result[7] = companion;
    fieldMap.put("fields", result);
    fieldMap.put("usedetail", usedetailstr);
    fieldMap.put("detailtablename", detailtablename);
    fieldMap.put("tablename", tablename);
    fieldMap.put("attid", attid);
    if(detailtablename.length() > 0){
      int detailIndex = getDetail_index(formid+"", detailtablename);
      fieldMap.put("detailIndex", detailIndex);
    }
    return fieldMap;
  }

  private Map<String,Object> getFieldList2(List<WorkflowBillfield> fList,List<WorkflowBillfield> fListDetail, List<HrmAttProcSet> list) {
    Map<String,Object> fieldMap = new HashMap<>();

    String resourceId = "";
    String departmentId = "";
    String fromDate = "";
    String fromTime = "";
    String toDate = "";
    String toTime = "";
    String duration = "";
    String usedetailstr = "";
    String detailtablename = "";
    String tablename = "";
    Long attid = 0L;
    int formid = 0;
    List<WorkflowBillfield> forfList = fList;
    for(HrmAttProcSet bean : list) {
      int usedetail = bean.getUsedetail();
      formid = bean.getField002();
      if(usedetail == 1){
        forfList = fListDetail;
        usedetailstr = ""+usedetail;
        detailtablename = bean.getDetailtablename();
      }else{
        tablename = bean.getTablename();
      }
      String detailPre = "";
      if(usedetail == 1){
        detailPre = "detail_";
      }
      for(WorkflowBillfield fieldBean : forfList) {
        if(usedetail == 1){
          String fieldbean_detailtable = fieldBean.getDetailtable();
          if(fieldbean_detailtable.length() > 0 && detailtablename.length() >0){
            if(!fieldbean_detailtable.equalsIgnoreCase(detailtablename)){
              continue;
            }
          }
        }
        if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
            StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
          continue;
        }
        if(bean.getField011().equalsIgnoreCase(detailPre+"resourceId")) {
          resourceId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"departmentId")) {
          departmentId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"fromDate")) {
          fromDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"fromTime")) {
          fromTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"toDate")) {
          toDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"toTime")) {
          toTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"duration")) {
          duration = FIELD + fieldBean.getId();
          break;
        }
      }
      attid = bean.getId();
    }
    String[] result = new String[7];
    result[0] = resourceId;
    result[1] = departmentId;
    result[2] = fromDate;
    result[3] = fromTime;
    result[4] = toDate;
    result[5] = toTime;
    result[6] = duration;
    fieldMap.put("fields", result);
    fieldMap.put("usedetail", usedetailstr);
    fieldMap.put("detailtablename", detailtablename);
    if(detailtablename.length() > 0){
      int detailIndex = getDetail_index(formid+"", detailtablename);
      fieldMap.put("detailIndex", detailIndex);
    }
    fieldMap.put("tablename", tablename);
    fieldMap.put("attid", attid);
    return fieldMap;
  }

	private Map<String,Object> getFieldList3(List<WorkflowBillfield> fList,List<WorkflowBillfield> fListDetail, List<HrmAttProcSet> list) {
    Map<String,Object> fieldMap = new HashMap<>();

    String resourceId = "";
		String departmentId = "";
		String fromDate = "";
		String fromTime = "";
		String toDate = "";
		String toTime = "";
		String duration = "";
		String overtime_type = "";
    String usedetailstr = "";
    String detailtablename = "";
    String tablename = "";
    Long attid = 0L;
    int formid = 0;
    List<WorkflowBillfield> forfList  = fList;
		for(HrmAttProcSet bean : list) {
      int usedetail = bean.getUsedetail();
      formid = bean.getField002();
      if(usedetail == 1){
        forfList = fListDetail;
        usedetailstr = ""+usedetail;
        detailtablename = bean.getDetailtablename();
      }else{
        tablename = bean.getTablename();
      }
      String detailPre = "";
      if(usedetail == 1){
        detailPre = "detail_";
      }
			for(WorkflowBillfield fieldBean : forfList) {
        if(usedetail == 1){
          String fieldbean_detailtable = fieldBean.getDetailtable();
          if(fieldbean_detailtable.length() > 0 && detailtablename.length() >0){
            if(!fieldbean_detailtable.equalsIgnoreCase(detailtablename)){
              continue;
            }
          }
        }
				if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
						StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
				  continue;
        }
				if(bean.getField011().equalsIgnoreCase(detailPre+"resourceId")) {
					resourceId = FIELD + fieldBean.getId();
					break;
				} else if(bean.getField011().equalsIgnoreCase(detailPre+"departmentId")) {
					departmentId = FIELD + fieldBean.getId();
					break;
				} else if(bean.getField011().equalsIgnoreCase(detailPre+"fromDate")) {
          fromDate = FIELD + fieldBean.getId();
					break;
				} else if(bean.getField011().equalsIgnoreCase(detailPre+"fromTime")) {
          fromTime = FIELD + fieldBean.getId();
					break;
				} else if(bean.getField011().equalsIgnoreCase(detailPre+"toDate")) {
          toDate = FIELD + fieldBean.getId();
					break;
				} else if(bean.getField011().equalsIgnoreCase(detailPre+"toTime")) {
          toTime = FIELD + fieldBean.getId();
					break;
				} else if(bean.getField011().equalsIgnoreCase(detailPre+"duration")) {
          duration = FIELD + fieldBean.getId();
					break;
				} else if(bean.getField011().equalsIgnoreCase(detailPre+"overtime_type")) {
          overtime_type = FIELD + fieldBean.getId();
          break;
        }
			}
      attid = bean.getId();
		}
		String[] result = new String[8];
		result[0] = resourceId;
		result[1] = departmentId;
		result[2] = fromDate;
		result[3] = fromTime;
		result[4] = toDate;
		result[5] = toTime;
		result[6] = duration;
		result[7] = overtime_type;
    fieldMap.put("fields", result);
    fieldMap.put("usedetail", usedetailstr);
    fieldMap.put("detailtablename", detailtablename);
    if(detailtablename.length() > 0){
      int detailIndex = getDetail_index(formid+"", detailtablename);
      fieldMap.put("detailIndex", detailIndex);
    }
    fieldMap.put("tablename", tablename);
    fieldMap.put("attid", attid);
		return fieldMap;
	}

  private Map<String,Object> getFieldList4(List<WorkflowBillfield> fList,List<WorkflowBillfield> fListDetail, List<HrmAttProcSet> list) {
    Map<String,Object> fieldMap = new HashMap<>();

    String resourceId = "";
    String departmentId = "";
    String fromDate = "";
    String fromTime = "";
    String toDate = "";
    String toTime = "";
    String duration = "";
    String usedetailstr = "";
    String detailtablename = "";
    Long attid = 0L;
    List<WorkflowBillfield> forfList = fList;
    for(HrmAttProcSet bean : list) {
      int usedetail = bean.getUsedetail();
      if(usedetail == 1){
        forfList = fListDetail;
        usedetailstr = ""+usedetail;
        detailtablename = bean.getDetailtablename();
      }
      String detailPre = "";
      if(usedetail == 1){
        detailPre = "detail_";
      }
      for(WorkflowBillfield fieldBean : forfList) {
        if(usedetail == 1){
          String fieldbean_detailtable = fieldBean.getDetailtable();
          if(fieldbean_detailtable.length() > 0 && detailtablename.length() >0){
            if(!fieldbean_detailtable.equalsIgnoreCase(detailtablename)){
              continue;
            }
          }
        }
        if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
            StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
          continue;
        }
        if(bean.getField011().equalsIgnoreCase(detailPre+"resourceId")) {
          resourceId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"departmentId")) {
          departmentId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"fromDate")) {
          fromDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"fromTime")) {
          fromTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"toDate")) {
          toDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"toTime")) {
          toTime = FIELD + fieldBean.getId();
          break;
        }
      }
      attid = bean.getId();
    }
    String[] result = new String[6];
    result[0] = resourceId;
    result[1] = departmentId;
    result[2] = fromDate;
    result[3] = fromTime;
    result[4] = toDate;
    result[5] = toTime;
    fieldMap.put("fields", result);
    fieldMap.put("usedetail", usedetailstr);
    fieldMap.put("detailtablename", detailtablename);
    fieldMap.put("attid", attid);
    return fieldMap;
  }

  private Map<String,Object> getFieldList5(List<WorkflowBillfield> fList,List<WorkflowBillfield> fListDetail, List<HrmAttProcSet> list) {
    Map<String,Object> fieldMap = new HashMap<>();

    String resourceId = "";
    String fromDate = "";
    String toDate = "";
    String shift = "";
    String usedetailstr = "";
    String detailtablename = "";
    Long attid = 0L;
    int formid = 0;
    for(HrmAttProcSet bean : list) {
      //TODO 现在排班流程只做明细
      formid = bean.getField002();
      String detailPre = "detail_";
      int usedetail = 1;
      usedetailstr = "1";
      detailtablename = bean.getDetailtablename();

      for(WorkflowBillfield fieldBean : fListDetail) {
        if(usedetail == 1){
          String fieldbean_detailtable = fieldBean.getDetailtable();
          if(fieldbean_detailtable.length() > 0 && detailtablename.length() >0){
            if(!fieldbean_detailtable.equalsIgnoreCase(detailtablename)){
              continue;
            }
          }
        }
        if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
            StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
          continue;
        }
        if(bean.getField011().equalsIgnoreCase(detailPre+"resourceId")) {
          resourceId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"fromDate")) {
          fromDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"toDate")) {
          toDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase(detailPre+"shift")) {
          shift = FIELD + fieldBean.getId();
          break;
        }
      }
      attid = bean.getId();
    }
    String[] result = new String[4];
    result[0] = resourceId;
    result[1] = fromDate;
    result[2] = toDate;
    result[3] = shift;
    fieldMap.put("fields", result);
    fieldMap.put("usedetail", usedetailstr);
    fieldMap.put("detailtablename", detailtablename);
    fieldMap.put("attid", attid);
    if(detailtablename.length() > 0){
      int detailIndex = getDetail_index(formid+"", detailtablename);
      fieldMap.put("detailIndex", detailIndex);
    }
    return fieldMap;
  }

  private Map<String,Object> getFieldList6(List<WorkflowBillfield> fList,List<WorkflowBillfield> fListDetail, List<HrmAttProcSet> list) {
    Map<String,Object> fieldMap = new HashMap<>();

    String resourceId = "";
    String leaverequestid = "";
    String detail_leavefromDate = "";
    String detail_leavefromTime = "";
    String detail_leavetoDate = "";
    String detail_leavetoTime = "";
    String detail_leaveduration = "";

    String detail_newLeaveType = "";

    String detail_fromDate = "";
    String detail_fromTime = "";
    String detail_toDate = "";
    String detail_toTime = "";
    String detail_duration = "";
    String usedetailstr = "";
    String detailtablename = "";
    Long attid = 0L;
    int formid = 0;

    for(HrmAttProcSet bean : list) {
      formid = bean.getField002();
      if(bean.getDetailtablename().length() > 0){
        detailtablename = bean.getDetailtablename();
      }
      for(WorkflowBillfield fieldBean : fList) {
        if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
            StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
          continue;
        }
        if(bean.getField011().equalsIgnoreCase("resourceId")) {
          resourceId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("leaverequestid")) {
          leaverequestid = FIELD + fieldBean.getId();
          break;
        }
      }
      for(WorkflowBillfield fieldBean : fListDetail) {
        String fieldbean_detailtable = fieldBean.getDetailtable();
        if(fieldbean_detailtable.length() > 0 && detailtablename.length() >0){
          if(!fieldbean_detailtable.equalsIgnoreCase(detailtablename)){
            continue;
          }
        }
        if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
            StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
          continue;
        }
        if(bean.getField011().equalsIgnoreCase("detail_leavefromDate")) {
          detail_leavefromDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_leavefromTime")) {
          detail_leavefromTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_leavetoDate")) {
          detail_leavetoDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_leavetoTime")) {
          detail_leavetoTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_newLeaveType")) {
          detail_newLeaveType = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_leaveduration")) {
          detail_leaveduration = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_fromDate")) {
          detail_fromDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_fromTime")) {
          detail_fromTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_toDate")) {
          detail_toDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_toTime")) {
          detail_toTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_duration")) {
          detail_duration = FIELD + fieldBean.getId();
          break;
        }
      }
      attid = bean.getId();
    }

    String[] result = new String[13];
    result[0] = resourceId;
    result[1] = leaverequestid;
    result[2] = detail_leavefromDate;
    result[3] = detail_leavefromTime;
    result[4] = detail_leavetoDate;
    result[5] = detail_leavetoTime;
    result[6] = detail_newLeaveType;
    result[7] = detail_leaveduration;
    result[8] = detail_fromDate;
    result[9] = detail_fromTime;
    result[10] = detail_toDate;
    result[11] = detail_toTime;
    result[12] = detail_duration;

    fieldMap.put("fields", result);
    fieldMap.put("usedetail", usedetailstr);
    fieldMap.put("detailtablename", detailtablename);
    if(detailtablename.length() > 0){
      int detailIndex = getDetail_index(formid+"", detailtablename);
      fieldMap.put("detailIndex", detailIndex);
    }
    fieldMap.put("attid", attid);
    return fieldMap;
  }

  private Map<String,Object> getFieldList7(List<WorkflowBillfield> fList,List<WorkflowBillfield> fListDetail, List<HrmAttProcSet> list) {
    Map<String,Object> fieldMap = new HashMap<>();

    String resourceId = "";
    String fromDate = "";
    String toDate = "";
    String detail_scheduletime = "";
    String detail_atteStatus = "";
    String detail_signtype = "";
    String detail_signdate = "";
    String detail_signtime = "";
    String detailtablename = "";
    Long attid = 0L;
    int formid = 0;

    for(HrmAttProcSet bean : list) {
      formid = bean.getField002();
      if(bean.getDetailtablename().length() > 0){
        detailtablename = bean.getDetailtablename();
      }
      for(WorkflowBillfield fieldBean : fList) {
        if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
            StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
          continue;
        }
        if(bean.getField011().equalsIgnoreCase("resourceId")) {
          resourceId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("fromDate")) {
          fromDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("toDate")) {
          toDate = FIELD + fieldBean.getId();
          break;
        }
      }
      for(WorkflowBillfield fieldBean : fListDetail) {
        String fieldbean_detailtable = fieldBean.getDetailtable();
        if(fieldbean_detailtable.length() > 0 && detailtablename.length() >0){
          if(!fieldbean_detailtable.equalsIgnoreCase(detailtablename)){
            continue;
          }
        }
        if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
            StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
          continue;
        }
        if(bean.getField011().equalsIgnoreCase("detail_scheduletime")) {
          detail_scheduletime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_atteStatus")) {
          detail_atteStatus = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_signtype")) {
          detail_signtype = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_signdate")) {
          detail_signdate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_signtime")) {
          detail_signtime = FIELD + fieldBean.getId();
          break;
        }
      }
      attid = bean.getId();
    }

    String[] result = new String[8];
    result[0] = resourceId;
    result[1] = fromDate;
    result[2] = toDate;
    result[3] = detail_scheduletime;
    result[4] = detail_atteStatus;
    result[5] = detail_signtype;
    result[6] = detail_signdate;
    result[7] = detail_signtime;
    fieldMap.put("fields", result);
    fieldMap.put("detailtablename", detailtablename);
    if(detailtablename.length() > 0){
      int detailIndex = getDetail_index(formid+"", detailtablename);
      fieldMap.put("detailIndex", detailIndex);
    }
    fieldMap.put("attid", attid);
    return fieldMap;
  }

  private Map<String,Object> getFieldList8(List<WorkflowBillfield> fList,List<WorkflowBillfield> fListDetail, List<HrmAttProcSet> list) {
    Map<String,Object> fieldMap = new HashMap<>();

    String resourceId = "";
    String changerequestid = "";
    String changetype = "";
    String detail_attendancefromDate = "";
    String detail_attendancefromTime = "";
    String detail_attendancetoDate = "";
    String detail_attendancetoTime = "";
    String detail_attendanceduration = "";

    String detail_fromDate = "";
    String detail_fromTime = "";
    String detail_toDate = "";
    String detail_toTime = "";
    String detail_duration = "";
    String usedetailstr = "";
    String detailtablename = "";
    Long attid = 0L;
    int formid = 0;

    for(HrmAttProcSet bean : list) {
      formid = bean.getField002();
      if(bean.getDetailtablename().length() > 0){
        detailtablename = bean.getDetailtablename();
      }
      for(WorkflowBillfield fieldBean : fList) {
        if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
            StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
          continue;
        }
        if(bean.getField011().equalsIgnoreCase("resourceId")) {
          resourceId = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("changerequestid")) {
          changerequestid = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("changetype")) {
          changetype = FIELD + fieldBean.getId();
          break;
        }
      }
      for(WorkflowBillfield fieldBean : fListDetail) {
        String fieldbean_detailtable = fieldBean.getDetailtable();
        if(fieldbean_detailtable.length() > 0 && detailtablename.length() >0){
          if(!fieldbean_detailtable.equalsIgnoreCase(detailtablename)){
            continue;
          }
        }
        if(!fieldBean.getFieldname().equalsIgnoreCase(bean.getField010()) ||
            StringUtil.isNull(bean.getField010()) || bean.getField010().endsWith("_temp")) {
          continue;
        }
        if(bean.getField011().equalsIgnoreCase("detail_attendancefromDate")) {
          detail_attendancefromDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_attendancefromTime")) {
          detail_attendancefromTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_attendancetoDate")) {
          detail_attendancetoDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_attendancetoTime")) {
          detail_attendancetoTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_attendanceduration")) {
          detail_attendanceduration = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_fromDate")) {
          detail_fromDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_fromTime")) {
          detail_fromTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_toDate")) {
          detail_toDate = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_toTime")) {
          detail_toTime = FIELD + fieldBean.getId();
          break;
        } else if(bean.getField011().equalsIgnoreCase("detail_duration")) {
          detail_duration = FIELD + fieldBean.getId();
          break;
        }
      }
      attid = bean.getId();
    }

    String[] result = new String[13];
    result[0] = resourceId;
    result[1] = changerequestid;
    result[2] = changetype;
    result[3] = detail_attendancefromDate;
    result[4] = detail_attendancefromTime;
    result[5] = detail_attendancetoDate;
    result[6] = detail_attendancetoTime;
    result[7] = detail_attendanceduration;
    result[8] = detail_fromDate;
    result[9] = detail_fromTime;
    result[10] = detail_toDate;
    result[11] = detail_toTime;
    result[12] = detail_duration;

    fieldMap.put("fields", result);
    fieldMap.put("usedetail", usedetailstr);
    fieldMap.put("detailtablename", detailtablename);
    if(detailtablename.length() > 0){
      int detailIndex = getDetail_index(formid+"", detailtablename);
      fieldMap.put("detailIndex", detailIndex);
    }
    fieldMap.put("attid", attid);
    return fieldMap;
  }

  /**
   * 获取明细下标 WfForm.getDetailAllRowIndexStr("detail_*")这个*就是这个下标
   * @param formid
   * @param tablename
   * @return
   */
  public int getDetail_index(String formid,String tablename){
	  int index = 1;
    RecordSet rs = new RecordSet();
	  String sql = "select * from workflow_billdetailtable  where billid = ? order by orderid";
	  rs.executeQuery(sql,formid);
	  while (rs.next()){
	    String detailname = rs.getString("tablename");
	    if(tablename.equalsIgnoreCase(detailname)){
	      return index;
      }
	    index++;
    }
	  return index;
  }
}
