package com.kingdee.shr.compensation.app.integrate;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.core.util.EmptyUtil;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.shr.compensation.util.integrate.CmpIntegrateSqlUtils;
import com.kingdee.shr.compensation.util.integrate.CmpIntegrateUtilsNew;
import com.kingdee.util.NumericExceptionSubItem;



/**
 * 广以生成9999表
 * @author dengzhouhong
 *
 */
public class BatchSubmitSchemeBillControllerBeanExt extends BatchSubmitShemeBillControllerBean{

	 private static Logger logger = LoggerFactory.getLogger(BatchSubmitSchemeBillControllerBeanExt.class);	 
	
	/**
	 * 提报项目汇总
	 */
	private static String All_TMP_TABLE="CT_COM_SubmitItemSumEntry";
	
	
	 
	@Override
    public IObjectPK save(Context ctx, CoreBaseInfo model) throws BOSException,
    		EASBizException {
		logger.error("into.BatchSubmitSchemeBillControllerBeanExt.save.method...");
		IObjectPK pk = super.save(ctx, model);
		BatchSubmitShemeBillInfo info = (BatchSubmitShemeBillInfo)model;
		String billId = info.getId().toString();
		logger.error("BatchSubmitSchemeBillControllerBeanExt.save.billId:"+billId);  
    	//checkBillExists(ctx, info);
    	//IObjectPK pk =doSaveOrUpdate(ctx, info);
    	String submitSchemeId = info.getSubmitScheme().getId().toString();
		Map map = CmpIntegrateUtilsNew.getBillColumnListAndBillEntryTableName(ctx, submitSchemeId);//。。。
		//动态列的集合
		List colNames = (List)map.get("colNames");
		List colTypes = (List)map.get("colTypes");
		String billEntryTable = map.get("fBillEntryTableName").toString();
		logger.error("BatchSubmitSchemeBillControllerBeanExt.save.billEntryTable:"+billEntryTable);  
		if(!EmptyUtil.isEmpty(billId)&&!EmptyUtil.isEmpty(billId)) {
			//先删除当前总表数据在保存,不考虑更新处理
			/*String sql ="delete "+All_TMP_TABLE+" where fparentid ='"+billId+"'";
			DbUtil.execute(ctx, sql);*/
			  /*String insertCol = CmpIntegrateSqlUtils.assembleInsertSql(ctx, billEntryTable, colNames, info.getId().toString());
			  logger.error("BatchSubmitSchemeBillControllerBeanExt.save.insertCol:"+insertCol);  
			  String str = insertCol.split("values")[0];
			  String fileds = insertCol.substring(str.indexOf("(")+1, str.lastIndexOf(")"));
			  logger.error("BatchSubmitSchemeBillControllerBeanExt.save.fileds:"+fileds);  */
			  
			/*  String fields01Str = CmpIntegrateUtilsNew.getAssembleField1Sql(ctx, billEntryTable, colNames, info.getId().toString());
			  String fileds1 = fields01Str.substring(fields01Str.indexOf("(")+1, fields01Str.lastIndexOf(")"));
			  String fields02Str = CmpIntegrateUtilsNew.getAssembleField2Sql(ctx, billEntryTable, colNames, info.getId().toString());
			  String fileds2 = fields02Str.substring(fields02Str.indexOf("(")+1, fields02Str.lastIndexOf(")"));
			  logger.error("BatchSubmitSchemeBillControllerBeanExt.save.fileds1:"+fileds1); 
			  logger.error("BatchSubmitSchemeBillControllerBeanExt.save.fileds2:"+fileds2); 
			String insertSql="insert  into  "+All_TMP_TABLE+"("+fileds1+")  (select "+fileds2+" from  "+billEntryTable+" where fbillid ='"+info.getId().toString()+"')";
			*/
			
			if(colNames == null){
				logger.error("BatchSubmitSchemeBillControllerBeanExt.save.colNames为空");
				return pk;
			}
			
			//获取该方案的所有项目
			List<Map<String,Object>> sValuelist = CmpIntegrateUtilsNew.getAssembleListByCmp(ctx, billEntryTable, colNames, info.getId().toString());
			
			logger.error("BatchSubmitSchemeBillControllerBeanExt.save.sValuelist。size："+sValuelist.size());
			
			if(sValuelist == null){
				logger.error("BatchSubmitSchemeBillControllerBeanExt.save.sValuelist为空");
				return pk;
			}
			
			for(int i = 0;i<sValuelist.size();i++){
				Map<String,Object> sValue = sValuelist.get(i);
				//更新提报分录的s值
				CmpIntegrateUtilsNew.updateSchemeBillEntryByCmpS(ctx, colNames,sValue);
			}
			/*logger.error("BatchSubmitSchemeBillControllerBeanExt.save.insertSql:"+insertSql);
			//addTempAllTable(ctx, info,All_TMP_TABLE,coll);
			DbUtil.execute(ctx, insertSql);*/
		}
		
    	return pk;
    }
	
}
