 package com.kingdee.shr.compensation.service;
 
 import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.MetaDataLoaderFactory;
import com.kingdee.bos.metadata.MetaDataPK;
import com.kingdee.bos.metadata.entity.EntityObjectInfo;
import com.kingdee.eas.base.core.util.EmptyUtil;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.compensation.CalSchemeInfo;
import com.kingdee.shr.compensation.app.formula.SalaryCalFunction;
import com.kingdee.shr.compensation.app.formula.data.Tools;
import com.kingdee.shr.compensation.app.integrate.CalSubmitItemCollection;
import com.kingdee.shr.compensation.app.integrate.CalSubmitItemDataType;
import com.kingdee.shr.compensation.app.integrate.CalSubmitItemFactory;
import com.kingdee.shr.compensation.app.integrate.CalSubmitItemInfo;
import com.kingdee.shr.compensation.app.integrate.DateTypeEnum;
import com.kingdee.shr.compensation.app.integrate.SubmitElementCollection;
import com.kingdee.shr.compensation.app.integrate.SubmitElementFactory;
import com.kingdee.shr.compensation.app.integrate.SubmitElementInfo;
import com.kingdee.shr.compensation.util.CmpDateUtil;
import com.kingdee.util.StringUtils;
 
 public class CalFunctionIntegrateServiceEx extends SalaryCalFunction
 {
	
	 private static Logger logger = Logger.getLogger("com.kingdee.shr.compensation.service.CalFunctionIntegrateServiceEx");
   public double getTransferSalaryData(Map paramMap, HashMap tempMap, Map<String, BigDecimal> currencyMap, CalSchemeInfo calScheme, String mainTableRowFilter, String calSubmitScheme, String calSubmitItem, String company, double needSubOrg, String beginDate, String endDate,String hrNum)
   {
     String key = new StringBuilder().append("TransferSalaryEx:").append(calScheme.getNumber()).append("-").append(calSubmitScheme).append("-").append(calSubmitItem).append("-").append(company).append("-").append(needSubOrg).append(beginDate).append("-").append(endDate).toString();
     String relyKey = new StringBuilder().append("TransferSalaryEx-").append(calScheme.getNumber()).append("-").append(calSubmitScheme).append("-").append(calSubmitItem).append("-").append(company).append("-").append(needSubOrg).append(beginDate).append("-").append(endDate).append("-").append("Rely").toString();
     Map salaryMap = new HashMap();
 
     String calItem = (String)paramMap.get("CALITEM");
//     logger.error("tempMap.get(key) ====="+tempMap.get(key) );
     if (tempMap.get(key) == null) {
	       String personFilter = getPersonFilterSql(calScheme, company, needSubOrg, beginDate, endDate, mainTableRowFilter);
	       logger.error("personFilter====="+personFilter);
	       Map methodParamMap = new HashMap();
	       methodParamMap.put("calSubmitScheme", calSubmitScheme);
	       methodParamMap.put("calSubmitItem", calSubmitItem);
	       methodParamMap.put("personFilter", personFilter);
	       methodParamMap.put("key", key);
	       methodParamMap.put("relyKey", relyKey);
	       methodParamMap.put("isNumber", Boolean.valueOf(true));
	       methodParamMap.put("isCycleScheme", Boolean.valueOf(false));
	       combineTransSalaryData(tempMap, currencyMap, methodParamMap,hrNum);
//	       logger.error("tempMap=="+tempMap);
     }
     String personId = (String)paramMap.get("T_HR_SCMPCALTABLE_FPERSONID");
     
     logger.error("key=="+key);
     logger.error("personId=="+personId);
     salaryMap = (Map)tempMap.get(key);
     logger.error("salaryMap=="+salaryMap);
     putSubmitDataToParam(paramMap, tempMap, calItem, relyKey);
     logger.error("salaryMap.containsKey(personId)="+salaryMap.containsKey(personId));
     logger.error("((salaryMap.containsKey(personId)) ? EmptyUtil.isEmpty(salaryMap.get(personId))?0.0D:((BigDecimal)salaryMap.get(personId)).doubleValue() : 0.0D)=="+((salaryMap.containsKey(personId)) ? EmptyUtil.isEmpty(salaryMap.get(personId))?0.0D:((BigDecimal)salaryMap.get(personId)).doubleValue() : 0.0D));
     return ((salaryMap.containsKey(personId)) ? EmptyUtil.isEmpty(salaryMap.get(personId))?0.0D:((BigDecimal)salaryMap.get(personId)).doubleValue() : 0.0D);
   }
 
 
   private String getPersonFilterSql(CalSchemeInfo calScheme, String company, double needSubOrg, String beginDate, String endDate, String mainTableRowFilter)
   {
     String calschemeid = calScheme.getId().toString();
     int calTime = calScheme.getCalTime();
     int periodYear = calScheme.getPeriodYear();
     int periodMonth = calScheme.getPeriodMonth();
     boolean haveSubOrg = 1.0D == needSubOrg;
     String salaryPeriod = new StringBuilder().append(periodYear).append("-").append(periodMonth).append("-").append(calTime).toString();
     String beginDateSql = new StringBuilder().append("{ts '").append(beginDate).append("'}").toString(); String endDateSql = new StringBuilder().append("{ts '").append(endDate).append("'}").toString();
 
     StringBuilder str = new StringBuilder();
     str.append(" select FPersonID from T_HR_SCmpCalTable where FcalSchemeID = '").append(calschemeid).append("' ");
     str.append(" and FperiodYear = ").append(periodYear);
     str.append(" and FperiodMonth = ").append(periodMonth);
     str.append(" and FcalTime = ").append(calTime);
     if (!(StringUtils.isEmpty(mainTableRowFilter))) {
       str.append(" and ").append(mainTableRowFilter);
     }
     String personSql = str.toString();
 
     String adminOrgSql = getAdminOrgIdFilterSql(company, haveSubOrg);
 
     str = new StringBuilder();
     str.append(" WHERE ").append(adminOrgSql);
     str.append(new StringBuilder().append(" AND FPERSONID  IN ( ").append(personSql).append(" )").toString());
     str.append(" AND FEFFECTDATE >= ").append(beginDateSql);
     str.append(" AND FEFFECTDATE <= ").append(endDateSql);
     str.append(" AND FCALSTATE = '03' ");
     str.append(" AND FSALARYPERIOD ='").append(salaryPeriod).append("'");
     return str.toString();
   }
 
   private void combineTransSalaryData(HashMap tempMap, Map<String, BigDecimal> currencyMap, Map<String, Object> methodParamMap,String hrNum)
   {
     String calSubmitScheme = (String)methodParamMap.get("calSubmitScheme");
     String calSubmitItem = (String)methodParamMap.get("calSubmitItem");
     String personFilter = (String)methodParamMap.get("personFilter");
     String key = (String)methodParamMap.get("key");
     String relyKey = (String)methodParamMap.get("relyKey");
     Boolean isNumber = (Boolean)methodParamMap.get("isNumber");
     Boolean isCycleScheme = (Boolean)methodParamMap.get("isCycleScheme");
 
     
     
     Boolean isDate = (Boolean)methodParamMap.get("isDate");
     Boolean dateTypeToDay = (Boolean)methodParamMap.get("dateTypeToDay");
 
     Context ctx = Tools.getInstance().getCtx();
 
     Map salaryMap = new HashMap();
 
     Map relyMap = new HashMap();
 
     List relyList = new ArrayList();
 
     String calItem = (String)this.paramMap.get("CALITEM");
 
     //取所有提报单对应的临时表
     ArrayList schemeTableNameList = initSubmitSchemeTable(ctx, tempMap, calSubmitItem);
     if (EmptyUtil.isEmpty(schemeTableNameList)) {
       tempMap.put(key, salaryMap);
       return;
     }
 
     CalSubmitItemInfo itemInfo = initSubmitItem(ctx, tempMap, calSubmitItem);
     if (null == itemInfo) {
       tempMap.put(key, salaryMap);
       return;
     }
     String itemSn = new StringBuilder().append("S").append(itemInfo.getFieldSn()).toString();
     String itemDatatTypeValue = itemInfo.getDataType().getValue();
     DecimalFormat df = new DecimalFormat((null == itemInfo.getDataDot()) ? "0" : itemInfo.getDataDot().toString());
     DateTypeEnum dateTypeEnum = itemInfo.getDateType();
     if ((itemDatatTypeValue != null) && ("2".equals(itemDatatTypeValue))) {
       isDate = Boolean.valueOf(true);
       if ((dateTypeEnum != null) && (10 == dateTypeEnum.getValue())) {
         dateTypeToDay = Boolean.valueOf(true);
       }
 
     }
 
     Set schemeItemSet = initSchemeItem(ctx, tempMap, calSubmitItem);
 
     if (!(schemeItemSet.contains(new StringBuilder().append(itemInfo.getFieldSn()).append("").toString()))) {
       tempMap.put(key, salaryMap);
       return;
     }
     //循环拼接 union all  提报临时表  按提报项目查询金额
     StringBuilder str = new StringBuilder();
     str.append("/*dialect*/ select t.fbillid,t.FPERSONID,t.FSALARYPERIOD,t.FID,t.FCURRENCYID, ").append("t."+itemSn).append("  from (");
     for (int i = 0; i < schemeTableNameList.size(); i++) {
    	 str= str.append(" SELECT fbillid,FPERSONID,FSALARYPERIOD,FID,FCURRENCYID,").append(itemSn);
    	 str=str.append(" FROM ").append(schemeTableNameList.get(i)).append(" t ");
    	
    	 str.append(personFilter);
    	
         if (isCycleScheme.booleanValue()) {
        	 str= str.append(" ORDER BY t.FTransYear desc,t.FTransMonth desc, t.FTransTime desc,t.FPERSONID, t.FEFFECTDATE DESC, t.FCREATETIME DESC ");
         }
         else if (!(isNumber.booleanValue())) {
        	 str= str.append(" ORDER BY t.FPERSONID, t.FEFFECTDATE DESC, t.sFCREATETIME DESC ");
         }
     
    	 if(schemeTableNameList.size()>1&&i!=schemeTableNameList.size()-1) {
    		 str= str.append(" union all");
    	 }
	  }
     
     str.append(") as t ");
     str=str.append(" left join  T_HR_SBatchSubmitShemeBill   t1  on t.fbillid = t1.fid ");
	 str=str.append(" left join  T_ORG_BaseUnit   t2  on t1.FHrOrgUnitID = t2.fid ");
	 str.append(" where  nvl(t2.fnumber,'##') ='"+hrNum+"' ");
     logger.error("temp_table_sql ===== "+str.toString());
     SimpleDateFormat yearMonthFormat = new SimpleDateFormat("yyyy-MM");
     SimpleDateFormat yearMonthDateFormat = new SimpleDateFormat("yyyy-MM-dd");
     try
     {
       logger.error("sql===="+str.toString());
       IRowSet rowSet = DbUtil.executeQuery(ctx, str.toString());
 
       String moneyStr = "";
 
       while (rowSet.next()) {
         String personId = rowSet.getString("FPersonID");
         logger.error("isCycleScheme ===== "+isCycleScheme);
         Map personRelyMap = new HashMap();
         personRelyMap.put("SUBMITITEMID", null);
         personRelyMap.put("SUBMITITEMTABLE", null);
         if (isCycleScheme.booleanValue()) {
           if (isNumber.booleanValue()) {
        	   
             String currencyId = rowSet.getString("FCURRENCYID");
             BigDecimal money = rowSet.getBigDecimal(itemSn);
             personRelyMap.put("SUBMITMONEY", df.format(money.doubleValue()));
 
             BigDecimal rate = (null == currencyMap.get(currencyId)) ? new BigDecimal(1) : (BigDecimal)currencyMap.get(currencyId);
             if (salaryMap.containsKey(personId)) {
               continue;
             }
             salaryMap.put(personId, money.multiply(rate));
           }
           else
           {
             if ((isDate != null) && (isDate.booleanValue())) {
               java.sql.Date date = rowSet.getDate(itemSn);
               if (date != null) {
                 java.util.Date utilDate = new java.util.Date(date.getTime());
                 if ((dateTypeToDay != null) && (dateTypeToDay.booleanValue()))
                   moneyStr = yearMonthDateFormat.format(utilDate);
                 else
                   moneyStr = yearMonthFormat.format(utilDate);
               }
               else {
                 moneyStr = "";
               }
             } else {
               moneyStr = (null == rowSet.getString(itemSn)) ? "" : rowSet.getString(itemSn);
 
               if ("0".equals(itemDatatTypeValue)) {
                 BigDecimal money = rowSet.getBigDecimal(itemSn);
                 if (money == null) {
                   moneyStr = "";
                 } else {
                   boolean isSQLServerDB = "MS SQL Server".equalsIgnoreCase(ContextUtil.getDbType(ctx));
                   if (isSQLServerDB)
                     moneyStr = df.format(money.doubleValue());
                   else {
                     moneyStr = (null == rowSet.getString(itemSn)) ? "" : rowSet.getString(itemSn);
                   }
                 }
               }
             }
             personRelyMap.put("SUBMITMONEY", moneyStr);
             if (salaryMap.containsKey(personId)) {
               continue;
             }
             salaryMap.put(personId, moneyStr);
           }
 
         }
         else if (isNumber.booleanValue()) {
           String currencyId = rowSet.getString("FCURRENCYID");
           BigDecimal money = EmptyUtil.isEmpty(rowSet.getBigDecimal(itemSn))?BigDecimal.ZERO:rowSet.getBigDecimal(itemSn);
           logger.error("money===="+money);
           personRelyMap.put("SUBMITMONEY", df.format(money.doubleValue()));
           BigDecimal rate = (null == currencyMap.get(currencyId)) ? new BigDecimal(1) : (BigDecimal)currencyMap.get(currencyId);
           BigDecimal sum = (null == salaryMap.get(personId)) ? money.multiply(rate) : ((BigDecimal)salaryMap.get(personId)).add(money.multiply(rate));
           salaryMap.put(personId, sum);
          
         } else {
           if ((isDate != null) && (isDate.booleanValue())) {
             java.sql.Date date = rowSet.getDate(itemSn);
             if (date != null) {
               java.util.Date utilDate = new java.util.Date(date.getTime());
               if ((dateTypeToDay != null) && (dateTypeToDay.booleanValue()))
                 moneyStr = yearMonthDateFormat.format(utilDate);
               else
                 moneyStr = yearMonthFormat.format(utilDate);
             }
             else {
               moneyStr = "";
             }
           } else {
             moneyStr = (null == rowSet.getString(itemSn)) ? "" : rowSet.getString(itemSn);
 
             if ("0".equals(itemDatatTypeValue)) {
               BigDecimal money = rowSet.getBigDecimal(itemSn);
               if (money == null) {
                 moneyStr = "";
               } else {
                 boolean isSQLServerDB = "MS SQL Server".equalsIgnoreCase(ContextUtil.getDbType(ctx));
                 if (isSQLServerDB)
                   moneyStr = df.format(money.doubleValue());
                 else {
                   moneyStr = (null == rowSet.getString(itemSn)) ? "" : rowSet.getString(itemSn);
                 }
               }
             }
           }
           personRelyMap.put("SUBMITMONEY", moneyStr);
           if (salaryMap.containsKey(personId)) {
             continue;
           }
           salaryMap.put(personId, moneyStr);
         }
 
         personRelyMap.put("SUBMITTABLE", schemeTableNameList.get(0));
         personRelyMap.put("SUBMITTABLEID", rowSet.getString("FID"));
         personRelyMap.put("CALITEM", calItem);
         personRelyMap.put("SUBMITNUMBER", calSubmitItem);
         personRelyMap.put("ISBASIC", Integer.valueOf(0));
 
         if (relyMap.containsKey(personId))
           relyList = (List)relyMap.get(personId);
         else {
           relyList = new ArrayList();
         }
         relyList.add(personRelyMap);
         relyMap.put(personId, relyList);
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
     tempMap.put(key, salaryMap);
     tempMap.put(relyKey, relyMap);
   }
 
   private void putSubmitDataToParam(Map paramMap, HashMap tempMap, String calItem, String relyKey)
   {
     String personId = (String)paramMap.get("T_HR_SCMPCALTABLE_FPERSONID");
 
     List relyList = new ArrayList();
 
     Map relyMap = (Map)tempMap.get(relyKey);
     if ((null == relyMap) || (!(relyMap.containsKey(personId)))) {
       return;
     }
 
     if (paramMap.containsKey("SUBMITRELY")) {
       Map paramRelayMap = (Map)paramMap.get("SUBMITRELY");
       if (paramRelayMap.containsKey(calItem)) {
         relyList = (List)paramRelayMap.get(calItem);
         relyList.addAll((Collection)relyMap.get(personId));
       } else {
         paramRelayMap.put(calItem, relyMap.get(personId));
       }
     } else {
       Map paramRelayMap = new HashMap();
       paramRelayMap.put(calItem, relyMap.get(personId));
       paramMap.put("SUBMITRELY", paramRelayMap);
     }
   }
 
   public String getAdminOrgIdFilterSql(String companyNum, boolean haveSubOrg)
   {
     Context ctx = Tools.getInstance().getCtx();
     StringBuffer sqlBuffer = new StringBuffer();
 
     String queryOrg = new StringBuilder().append("SELECT FLONGNUMBER,FID FROM T_ORG_ADMIN WHERE FNUMBER = '").append(companyNum).append("'").toString();
     String companyLongNumber = ""; String adminOrgUnitId = "";
     try
     {
       IRowSet executeQuery = DbUtil.executeQuery(ctx, queryOrg);
       while (executeQuery.next()) {
         companyLongNumber = executeQuery.getString("fLongNumber");
         adminOrgUnitId = executeQuery.getString("fid");
       }
       if (StringUtils.isEmpty(companyLongNumber)) {
         return null;
       }
       if (haveSubOrg)
         sqlBuffer.append(new StringBuilder().append(" FADMINORGUNITID IN (SELECT FID FROM T_ORG_ADMIN WHERE FLONGNUMBER LIKE '").append(companyLongNumber).append("%')").toString());
       else {
         sqlBuffer.append(new StringBuilder().append(" FADMINORGUNITID IN ('").append(adminOrgUnitId).append("')").toString());
       }
     }
     catch (BOSException e)
     {
       e.printStackTrace();
     } catch (SQLException e) {
       e.printStackTrace();
     }
     return sqlBuffer.toString();
   }
 
   public static List<String> getCalShemeTableName(Context ctx, String calSubmitSchemeNum, String calSubmitItemNum)
   {
     List list = new ArrayList();
     String sql = new StringBuilder().append("Select n.FCalShemeTableName, m.fieldSn  from  T_HR_SCalSubmitScheme e  inner join T_HR_SCalShemeTableRelation n on n.FSubmitSchemeID = e.fid   inner join T_HR_SCalSubmitSchemeItem sm on sm.FCalSubmitSchemeID = e.fid   inner join T_HR_SCalSubmitItem m on m.fid = sm.FCalSubmitItemID  where e.FNumber = '").append(calSubmitSchemeNum).append("' and m.FNumber =  '").append(calSubmitItemNum).append("'  ").toString();
     try
     {
       IRowSet rs = DbUtil.executeQuery(ctx, sql);
       while (rs.next())
       {
         list.add(rs.getString("FCalShemeTableName"));
         list.add(new StringBuilder().append("S").append(rs.getString("fieldSn")).toString());
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
     return list;
   }
 
   public ArrayList initSubmitSchemeTable(Context ctx, HashMap tempMap, String schemeNumber)
   {
     String key = "CALFUNCTIONINTEGRATESERVICE_SCHEMETABLE_EXT_"+schemeNumber;
     ArrayList list = new ArrayList<String>();
     Map schemeMap = new HashMap();
     if (null == tempMap.get(key)) {
       StringBuilder str = new StringBuilder();
       str.append(" SELECT REL.FCALSHEMETABLENAME PROPERTYTABLE, REL.FBILLENTRYTABLENAME DATATABLE, SCHEME.FNUMBER ");
       str.append(" FROM T_HR_SCALSHEMETABLERELATION REL ");
       str.append(" INNER JOIN T_HR_SCALSUBMITSCHEME SCHEME ON REL.FSUBMITSCHEMEID = SCHEME.FID ");
       str.append(" INNER JOIN T_HR_SCALSUBMITSCHEMEITEM SCHEMEITEM ON SCHEME.FID = SCHEMEITEM.FCALSUBMITSCHEMEID ");
       str.append(" INNER JOIN T_HR_SCALSUBMITITEM ITEM ON ITEM.FID = SCHEMEITEM.FCALSUBMITITEMID ");
       str.append(" where ITEM.fnumber ='"+schemeNumber+"' ");
        logger.error("initSubmitSchemeTable====="+str.toString());
      
       try {
         IRowSet rowSet = DbUtil.executeQuery(ctx, str.toString());
         while (rowSet.next()) {
           schemeMap.put(rowSet.getString("FNUMBER"), rowSet.getString("DATATABLE"));
         	list.add(rowSet.getString("DATATABLE"));
         }
       }
       catch (Exception e) {
         e.printStackTrace();
       }
       tempMap.put(key, schemeMap);
     } else {
       schemeMap = (Map)tempMap.get(key);
     }
     return list;
   }
 
   public SubmitElementInfo initElementItem(Context ctx, HashMap tempMap, String elementItemNum)
   {
     String key = "INITELEMENTITEM_SERVICE_SUBMITITEM";
 
     Map itemMap = new HashMap();
     if (null == tempMap.get(key)) {
       SubmitElementCollection itemColl = null;
       try {
         itemColl = SubmitElementFactory.getLocalInstance(ctx).getSubmitElementCollection("select id, number, name ");
       } catch (BOSException e) {
         e.printStackTrace();
       }
       for (int i = 0; i < itemColl.size(); ++i) {
         SubmitElementInfo submitElementInfo = itemColl.get(i);
         itemMap.put(submitElementInfo.getNumber(), submitElementInfo);
       }
       tempMap.put(key, itemMap);
     } else {
       itemMap = (Map)tempMap.get(key);
     }
     SubmitElementInfo submitElementInfo = (SubmitElementInfo)itemMap.get(elementItemNum);
     if (null == submitElementInfo)
       return null;
     return ((SubmitElementInfo)itemMap.get(elementItemNum));
   }
 
   public CalSubmitItemInfo initSubmitItem(Context ctx, HashMap tempMap, String itemNumber)
   {
     String key = "CALFUNCTIONINTEGRATESERVICE_SUBMITITEM_EXT_"+itemNumber;
     String basicItemKey = new StringBuilder().append("CALFUNCTIONINTEGRATESERVICE_SUBMITITEM_").append(itemNumber).toString();
 
     Map itemMap = new HashMap();
     if (null == tempMap.get(key)) {
       CalSubmitItemCollection itemColl = null;
       try {
         itemColl = CalSubmitItemFactory.getLocalInstance(ctx).getCalSubmitItemCollection("select id, fieldSn, number, dateType, dataType,dataDot,filterField.entityName ");
       } catch (BOSException e) {
         e.printStackTrace();
       }
       for (int i = 0; i < itemColl.size(); ++i) {
         CalSubmitItemInfo calSubmitItemInfo = itemColl.get(i);
         itemMap.put(calSubmitItemInfo.getNumber(), calSubmitItemInfo);
       }
       tempMap.put(key, itemMap);
     } else {
       itemMap = (Map)tempMap.get(key);
     }
 
     CalSubmitItemInfo calSubmitItemInfo = (CalSubmitItemInfo)itemMap.get(itemNumber);
     if (null == calSubmitItemInfo)
       return null;
     if ((calSubmitItemInfo.getDataType().equals(CalSubmitItemDataType.F7)) && (null == tempMap.get(basicItemKey))) {
       Map basicItemMap = new HashMap();
       String entityName = calSubmitItemInfo.getFilterField().getEntity();
       MetaDataPK meataDataPk = MetaDataPK.create(entityName);
       EntityObjectInfo entityInfo = MetaDataLoaderFactory.getRemoteMetaDataLoader().getEntity(meataDataPk);
       String tableName = entityInfo.getTable().getName();
       String sql = new StringBuilder().append("SELECT FID, FNUMBER FROM ").append(tableName).toString();
       try {
         IRowSet rowSet = DbUtil.executeQuery(ctx, sql);
         while (rowSet.next()) {
           basicItemMap.put(rowSet.getString("FNUMBER"), new StringBuilder().append(rowSet.getString("FID")).append("###").append(rowSet.getString("FNUMBER")).toString());
         }
 
         basicItemMap.put("TABLENAME", tableName);
       } catch (Exception e) {
         e.printStackTrace();
       }
       tempMap.put(basicItemKey, basicItemMap);
     }
 
     return ((CalSubmitItemInfo)itemMap.get(itemNumber));
   }
 
   public Set<String> initSchemeItem(Context ctx, HashMap tempMap, String schemeNumber)
   {
     String key = "CALFUNCTIONINTEGRATESERVICE_SCHEMEITEM";
 
     Set itemSet = new HashSet();
     Map<String, Set<String>> schemeItemMap = new HashMap<String, Set<String>>();
     if (null == tempMap.get(key)) {
       StringBuilder str = new StringBuilder();
       str.append(" SELECT ITEM.FNUMBER, ITEM.FIELDSN ");
       str.append(" FROM T_HR_SCALSUBMITSCHEME SCHEME ");
       str.append(" INNER JOIN T_HR_SCALSUBMITSCHEMEITEM SCHEMEITEM ON SCHEME.FID = SCHEMEITEM.FCALSUBMITSCHEMEID ");
       str.append(" INNER JOIN T_HR_SCALSUBMITITEM ITEM ON ITEM.FID = SCHEMEITEM.FCALSUBMITITEMID ");
       try {
         IRowSet rowSet = DbUtil.executeQuery(ctx, str.toString());
         while (rowSet.next()) {
           String number = rowSet.getString("FNUMBER");
           if (schemeItemMap.containsKey(number)) {
             ((Set)schemeItemMap.get(number)).add(rowSet.getString("FIELDSN"));
           }
           itemSet = new HashSet();
           itemSet.add(rowSet.getString("FIELDSN"));
           schemeItemMap.put(number, itemSet);
         }
       }
       catch (Exception e) {
         e.printStackTrace();
       }
     } else {
       schemeItemMap = (Map)tempMap.get(key);
     }
     return schemeItemMap.containsKey(schemeNumber) ? schemeItemMap.get(schemeNumber) : new HashSet<String>();
   }
 
 }