package nc.ui.hr.baod_orders.view;


import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import nc.bs.framework.common.NCLocator;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.hr.utils.OutputDialogUtil;
import nc.hr.utils.SQLHelper;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.ui.pub.ButtonObject;
import nc.ui.pub.ToftPanel;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.UIDialog;
import nc.ui.pub.beans.UILabel;
import nc.ui.pub.beans.UIPanel;
import nc.ui.pub.beans.UIRefPane;
import nc.ui.pub.beans.UITablePane;
import nc.ui.pub.beans.ValueChangedEvent;
import nc.ui.pub.beans.ValueChangedListener;
import nc.ui.resa.refmodel.CostCenterTreeRefModel;
import nc.vo.fipub.exception.ExceptionHandler;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.data.ValueUtils;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.MathTool;
import nc.vo.uap.rbac.profile.FunctionPermProfileManager;
import nc.vo.uap.rbac.profile.IFunctionPermProfile;

/**
 * 公积金分摊表
 * @author YaoYu
 *
 */
public class AccumulationFundAllocationUI extends ToftPanel {
	private IUAPQueryBS iuap = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());
	private ButtonObject bt_query = new ButtonObject("查询", "", 2, "bt_query");
	private ButtonObject bt_insert = new ButtonObject("表八新增", "", 2, "bt_insert");
	private ButtonObject bt_update = new ButtonObject("修改", "", 2, "bt_update");
	private ButtonObject bt_output = new ButtonObject("导出", "", 2, "bt_output");
	private ButtonObject bt_payableBill = new ButtonObject("生成应付单", "", 2, "bt_payableBill");
	private JProgressBar pb;
	private UITablePane tablepanel = null;
	private UIPanel ivjUIPanel0 = null;
	private UIPanel ivjUIPanel2 = null;
	private UIPanel ivjUIPanel4 = null;
	private UIPanel ivjUIPanel45 = null;
	private UILabel ivjOrgLabel = null;
	private UILabel ivjperiodLabel = null;
	private UIRefPane ivjcostcpanel = null;
	private UIRefPane ivjOrgPanel = null;
	private UIRefPane ivjperiodpanel = null;
	private UITablePane ivjTablePanel = null;
	private String[] subheading = {"序号","员工编号","姓名","缴费基数","个人部分","公司部分","备注"};
	private String orgCode;//查询组织
	private String period;//查询期间
	private Map<String, Map<String, UFDouble>> beforePsgMoney = null; //处理前金额
	private Map<String, Map<String, UFDouble>> afterPsgMoney = null; //处理后金额
	Map<String,Map<String,String>> middleTableData = null;//中间表数据
	public AccumulationFundAllocationUI() {
		initialize();
		tablepanel = getTablePanel();
	}
	
	

	@Override
	public String getTitle() {
		return "公积金分摊表";
	}

	public void initialize() {
		setLayout(new BorderLayout());
		add(getUIPanel0(), BorderLayout.NORTH);
		add(getUIPanel2(), BorderLayout.CENTER);
		add(getUIPanel4(), BorderLayout.SOUTH);
		setButtons(new ButtonObject[] { this.bt_query,this.bt_insert,this.bt_update,this.bt_payableBill,this.bt_output});//添加按钮
	}
	private UILabel getOrgLabel() {
		if (this.ivjOrgLabel == null) {
			this.ivjOrgLabel = new UILabel();
			this.ivjOrgLabel.setName("pkorg");
			this.ivjOrgLabel.setText("财务组织");
		}
		return this.ivjOrgLabel;
	}
	
	private UIRefPane getOrgPanel() {
		if (this.ivjOrgPanel == null) {
			//节点编码
			String nodeCode = "60H20104";
			this.ivjOrgPanel = new UIRefPane();
			this.ivjOrgPanel.setName("pkorg");
			this.ivjOrgPanel.setRefNodeName("财务组织");
			//权限组织
			String[] pkorgs = FunctionPermProfileManager.getInstance().getProfile(WorkbenchEnvironment.getInstance().getLoginUser().getUser_code()).getFuncSubInfo(nodeCode).getFuncPermissionPkorgs();
			this.ivjOrgPanel.getRefModel().setFilterPks(pkorgs);

			this.ivjOrgPanel.setBounds(new Rectangle(100, 20));
			this.ivjOrgPanel.setPreferredSize(new Dimension(170, 20));
			//this.ivjOrgPanel.setMultiSelectedEnabled(true);
				this.ivjOrgPanel.addValueChangedListener(new ValueChangedListener() {
				public void valueChanged(ValueChangedEvent e) {
					AccumulationFundAllocationUI.this.initialize();
				}
			});
		}
		return this.ivjOrgPanel;
	}
	
	private UILabel getperiodclabel() {
		if (this.ivjperiodLabel == null) {
			this.ivjperiodLabel = new UILabel();
			this.ivjperiodLabel.setName("period");
			this.ivjperiodLabel.setText("年月");
		}
		return this.ivjperiodLabel;
	}
	
	private UIRefPane getperiodpanel() {
		if (this.ivjperiodpanel == null) {
			this.ivjperiodpanel = new UIRefPane();
			this.ivjperiodpanel.setName("period");
			this.ivjperiodpanel.setRefNodeName("会计期间档案");
			this.ivjperiodpanel.setBounds(new Rectangle(100, 20));
			this.ivjperiodpanel.setPreferredSize(new Dimension(170, 20));
			this.ivjperiodpanel.addValueChangedListener(new ValueChangedListener() {	
				public void valueChanged(ValueChangedEvent e) {
					AccumulationFundAllocationUI.this.initialize();
				}
			});
		}
		return this.ivjperiodpanel;
	}
	
	private UIPanel getUIPanel0() {
		if (this.ivjUIPanel0 == null) {
			try {
				this.ivjUIPanel0 = new UIPanel();
				this.ivjUIPanel0.setLayout(new BorderLayout());
				UIPanel sel = new UIPanel();
				sel.setLayout(new FlowLayout());
				sel.add(getOrgLabel());
				sel.add(getOrgPanel());
				sel.add(getperiodclabel());
				sel.add(getperiodpanel());
				this.ivjUIPanel0.add(sel, BorderLayout.WEST);
			} catch (Throwable ivjExc) {
				handleException(ivjExc);
			}
		}
		return this.ivjUIPanel0;
	}
	
	private UIPanel getUIPanel4() {
		if (this.ivjUIPanel4 == null) {
			this.ivjUIPanel4 = new UIPanel();
			this.ivjUIPanel4.setLayout(new BorderLayout());
			this.ivjUIPanel4.add(getUILabel45(), BorderLayout.CENTER);
		}
		return this.ivjUIPanel4;
	}
	
	private UIPanel getUILabel45() {
		if (this.ivjUIPanel45 == null) {
			try {
				this.ivjUIPanel45 = new UIPanel();
				this.ivjUIPanel45.setName("UIPanel3");
				this.ivjUIPanel45.setPreferredSize(new Dimension(0, 10));
				this.ivjUIPanel45.setLayout(new BorderLayout());
				this.ivjUIPanel45.add(getprogressbar(), BorderLayout.EAST);
			} catch (Throwable ivjExc) {
				handleException(ivjExc);
			}
		}
		return this.ivjUIPanel45;
	}
	
	private void handleException(Throwable e) {
		ExceptionHandler.consume(e);
	}
	
	private JProgressBar getprogressbar() {
		if (this.pb == null) {
			this.pb = new JProgressBar();
			this.pb.setIndeterminate(false);
		}
		return this.pb;
	}
	
	private UIPanel getUIPanel2() {
		if (this.ivjUIPanel2 == null) {
			this.ivjUIPanel2 = new UIPanel();
			this.ivjUIPanel2.setLayout(new BorderLayout());
			this.ivjUIPanel2.setPreferredSize(new Dimension(0, 500));
			this.ivjUIPanel2.add(getTablePanel(), BorderLayout.CENTER);
		}
		return this.ivjUIPanel2;
	}
	
	private UITablePane getTablePanel() {
		if (this.ivjTablePanel == null) {
			try {
				this.ivjTablePanel = new UITablePane();
				this.ivjTablePanel.setName("TablePanel");
				this.ivjTablePanel.setLayout(new CardLayout());
			} catch (Throwable ivjExc) {
				
			}
		}
		return this.ivjTablePanel;
	}
	
	private Vector<Object> convertovector(Object[] objArray) {
		if (objArray == null) {
			return null;
		}
		Vector<Object> v = new Vector<Object>(objArray.length);
		for (int i = 0; i < objArray.length; i++) {
			v.addElement(objArray[i]);
		}
		return v;
	}

	@Override
	public void onButtonClicked(ButtonObject bt) {
		try {
			//停止编辑;
			getOrgPanel().stopEditing();
			getperiodpanel().stopEditing();
			orgCode = getOrgPanel().getRefModel().getRefCodeValue();
			Vector periodData = getperiodpanel().getRefModel().getSelectedData();
			if (periodData != null && periodData.size() > 0) {
				Vector v = (Vector) periodData.get(0);
				period = v.get(0).toString();
			}
			if(!"bt_insert".equals(bt.getCode()) && (orgCode == null || period == null)) {
				ExceptionUtils.wrappBusinessException("财务组织、年月必须选择，请选择后再操作！");
			}
			if("bt_query".equals(bt.getCode())) {
				tableProcessing();
			}else if ("bt_insert".equals(bt.getCode())){
				insert();
			}else if ("bt_update".equals(bt.getCode())){
				update(getTablePanel());
			}else if("bt_output".equals(bt.getCode())){
				onoutput();
			}else {
				MessageDialog.showHintDlg(this, "提示", "生成应付单");
			}
		} catch (Exception e) {
			MessageDialog.showHintDlg(this, "提示", e.getMessage());
		}
	}
	
	/**
	 * 表格渲染
	 * @param orgCode
	 * @param period
	 * @throws Exception
	 */
	private void tableProcessing() throws Exception {
		//初始化
		beforePsgMoney = new HashMap<String, Map<String, UFDouble>>();
		afterPsgMoney = new HashMap<String, Map<String, UFDouble>>();
		middleTableData = new HashMap<String,Map<String,String>>();
		queryMiddleTable();
		String[] m_sTitle = {null,null,null,null,null,null,null,null,null};
		List<Object[]> dataList = new ArrayList<Object[]>();
		for (int i = 0; i < 11; i++) {  
            Object[] row = new Object[0];
            dataList.add(row);  
        }
		Map<String, Map<String,UFDouble>> tableMap = new HashMap<String, Map<String,UFDouble>>();
		List<Map<String,String>> tableOne = queryTableOne(orgCode, period);
		moneyDispose("表一：由个人承担", tableOne);
		List<Map<String,String>> tableTwo = queryTableTwo(orgCode, period);
		moneyDispose("表二：由外部公司承担", tableTwo);
		List<Map<String,String>> tableThree = queryTableThree(orgCode, period);
		moneyDispose("表三：由部门承担或全额公司承担", tableThree);
		List<Map<String,String>> tableFour = queryTableFour(orgCode, period);
		moneyDispose("表四：个人部分已在集团内其他公司扣除", tableFour);
		List<Map<String,String>> tableFive = queryTableFive(orgCode, period);
		moneyDispose("表五：月薪工资表中当月扣当月缴纳", tableFive);
		List<Map<String,String>> tableSix = queryTableSix(orgCode, period);
		moneyDispose("表六：实际已缴当月工资个人部分未扣", tableSix);
		List<Map<String,String>> tableSeven = queryTableSeven(orgCode, period);
		moneyDispose("表七：实际未缴当月工资个人部分已扣", tableSeven);
		List<Map<String,String>> TableEight = queryTableEight(orgCode, period);
		moneyDispose("表八：实际集团内其他公司已缴当月工资个人部分已扣", TableEight);
		tableMap.put("tableOne", addTableToList("1-", dataList, "表一：由个人承担", tableOne));
		tableMap.put("tableTwo", addTableToList("2-", dataList, "表二：由外部公司承担", tableTwo));
		tableMap.put("tableThree", addTableToList("3-", dataList, "表三：由部门承担或全额公司承担", tableThree));
		tableMap.put("tableFour", addTableToList("4-", dataList, "表四：个人部分已在集团内其他公司扣除", tableFour));
		tableMap.put("tableFive", addTableToList("5-", dataList, "表五：月薪工资表中当月扣当月缴纳", tableFive));
		tableMap.put("tableSix", addTableToList("6-", dataList, "表六：实际已缴当月工资个人部分未扣", tableSix));
		tableMap.put("tableSeven", addTableToList("7-", dataList, "表七：实际未缴当月工资个人部分已扣", tableSeven));
	    tableMap.put("TableEight",addTableToList("8-", dataList, "表八：实际集团内其他公司已缴当月工资个人部分已扣", TableEight));		
		UFDouble tableOneStaff = tableMap.get("tableOne").get("staff");//个人承担个人部分
		UFDouble tableOneOrg = tableMap.get("tableOne").get("org");//个人承担公司部分
		UFDouble tableTwoStaff = tableMap.get("tableTwo").get("staff");//外部公司承担个人部分
		UFDouble tableTwoOrg = tableMap.get("tableTwo").get("org");//外部公司承担公司部分
		UFDouble tableThreeStaff = tableMap.get("tableThree").get("staff");//部门承担或全额公司承担个人部分
		UFDouble tableThreeOrg = tableMap.get("tableThree").get("org");//部门承担或全额公司承担公司部分
		UFDouble tableFourStaff = tableMap.get("tableFour").get("staff");//个人部分已在集团内其他公司扣除个人部分
		UFDouble tableFourOrg = tableMap.get("tableFour").get("org");//个人部分已在集团内其他公司扣除公司部分
		UFDouble tableFiveStaff = tableMap.get("tableFive").get("staff");//月薪工资表中当月扣当月缴纳个人部分
		UFDouble tableFiveOrg = tableMap.get("tableFive").get("org");//月薪工资表中当月扣当月缴纳公司部分
		UFDouble tableSixStaff = tableMap.get("tableSix").get("staff");//实际已缴当月工资个人部分未扣个人部分
		UFDouble tableSixOrg = tableMap.get("tableSix").get("org");//实际已缴当月工资个人部分未扣公司部分
		UFDouble tableSevenStaff = tableMap.get("tableSeven").get("staff");//实际未缴当月工资个人部分已扣个人部分
		UFDouble tableSevenOrg = tableMap.get("tableSeven").get("org");//实际未缴当月工资个人部分已扣公司部分
		
		UFDouble  queryTableNine = nvl(queryTableNine(orgCode, period), 2);//个人工资表扣缴数
		Map<String,String> TableTenMap =  queryTableTen(orgCode, period);
		UFDouble TableTenStaff = nvl(TableTenMap.get("so_staff_sum"), 2);//个人实际缴纳数
		UFDouble TableTenOrg =  nvl(TableTenMap.get("so_org_sum"), 2);//公司实际缴纳数
		//社保账单实际交纳数 = 个人实际缴纳数+公司实际缴纳数
		UFDouble actualSum = TableTenStaff.add(TableTenOrg);
		//社保分摊表金额 = 个人承担个人、公司部分+外部公司承担个人、公司部分+部门承担或全额公司承担个人、公司部分+
		//		个人部分已在集团内其他公司扣除个人、公司部分+月薪工资表中当月扣当月缴纳个人、公司部分+实际已缴当月工资个人部分未扣个人、公司部分  
		UFDouble specialfillSum = tableOneStaff.add(tableOneOrg).add(tableTwoStaff).add(tableTwoOrg).add(tableThreeStaff).add(tableThreeOrg)
				.add(tableFourStaff).add(tableFourOrg).add(tableFiveStaff).add(tableFiveOrg).add(tableSixStaff).add(tableSixOrg);
		//个人工资表扣缴数分摊表金额 = 月薪工资表中当月扣当月缴纳个人部分 +实际未缴当月工资个人部分已扣个人部分 + 实际集团内其他公司已缴当月工资个人部分已扣个人部分
		UFDouble staffPaymentSum = tableFiveStaff.add(tableSevenStaff);
		//个人实际缴纳数分摊表金额 = 个人承担个人部分 + 外部公司承担个人部分 +部门承担或全额公司承担个人部分 +  个人部分已在集团内其他公司扣除个人部分
		//		+月薪工资表中当月扣当月缴纳个人部分 + 实际已缴当月工资个人部分未扣个人部分
		UFDouble staffActualSum = tableOneStaff.add(tableTwoStaff).add(tableThreeStaff).add(tableFourStaff).add(tableFiveStaff).add(tableSixStaff);
		//公司实际缴纳数分摊表金额 = 个人承担公司部分 + 外部公司承担公司部分 + 部门承担或全额公司承担公司部分 +  个人部分已在集团内其他公司扣除公司部分 +
		//		月薪工资表中当月扣当月缴纳公司部分 + 实际已缴当月工资个人部分未扣公司部分 + 实际未缴当月工资个人部分已扣公司部分
		UFDouble orgActualSum = tableOneOrg.add(tableTwoOrg).add(tableThreeOrg).add(tableFourOrg).add(tableFiveOrg).add(tableSixOrg).add(tableSevenOrg);
		dataList.set(0, new Object[] {"汇总信息"});
		dataList.set(1, new Object[] {"社保账单实际交纳数", actualSum, "社保分摊表金额", specialfillSum});
	    dataList.set(2, new Object[] {"个人承担", "公司部分", tableOneOrg, "外部公司承担", "公司部分", tableTwoOrg, "部门承担或全额公司承担", "公司部分", tableThreeOrg});
	    dataList.set(3, new Object[] {"个人承担", "个人部分", tableOneStaff, "外部公司承担", "个人部分", tableTwoStaff, "部门承担或全额公司承担", "个人部分", tableThreeStaff});
	    dataList.set(4, new Object[] {"个人部分已在集团内其他公司扣除  ", "公司部分", tableFourOrg, "月薪工资表中当月扣当月缴纳", "公司部分", tableFiveOrg, "实际已缴当月工资个人部分未扣", "公司部分", tableSixOrg});
	    dataList.set(5, new Object[] {"个人部分已在集团内其他公司扣除  ", "个人部分", tableFourStaff, "月薪工资表中当月扣当月缴纳", "个人部分", tableFiveStaff, "实际已缴当月工资个人部分未扣", "个人部分", tableSixStaff});
	    dataList.set(6, new Object[] {"实际未缴当月工资个人部分已扣  ", "公司部分", tableSevenOrg, "实际集团内其他公司已缴当月工资个人部分已扣", "公司部分", null});
	    dataList.set(7, new Object[] {"实际未缴当月工资个人部分已扣  ", "个人部分", tableSevenStaff, "实际集团内其他公司已缴当月工资个人部分已扣", "公司部分", null});
	    dataList.set(8, new Object[] {"个人", "工资表扣缴数", queryTableNine, "分摊表金额", staffPaymentSum, "差异", queryTableNine.sub(staffPaymentSum)});
	    dataList.set(9, new Object[] {"个人", "实际缴纳数", TableTenStaff, "分摊表金额", staffActualSum, "差异", TableTenStaff.sub(staffActualSum)});
	    dataList.set(10, new Object[] {"公司", "实际缴纳数", TableTenOrg, "分摊表金额", orgActualSum, "差异", TableTenOrg.sub(orgActualSum)});
	    Object[][] dataArray = new Object[dataList.size()][m_sTitle.length];
		for (int i = 0; i < dataList.size(); i++) {
            Object[] rowData = dataList.get(i);
            dataArray[i] = rowData;
        }
		DefaultTableModel model = new DefaultTableModel(dataArray, m_sTitle)
		{
			//所有单元格不允许编辑
	        @Override
	        public boolean isCellEditable(int row, int column) {
	        	return false;
	        }
	    };
	    tablepanel.getTable().setModel(model);
	    tablepanel.getTable().setColumnWidth(new int[] {300,150,150,280,150,150,190,190,160});
	    
	    TableCellRenderer renderer = new DefaultTableCellRenderer() {
	        @Override  
	        public Component getTableCellRendererComponent(JTable table, Object value,  
	                boolean isSelected, boolean hasFocus, int row, int column) {  
	            // 调用父类的getTableCellRendererComponent方法  
	            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);  
	            //从十三行开始，第七列值变成白色,其他列值为黑色
	            if (row >= 13 && column == 7) {
		            // 如果单元格被选中，则设置值和背景颜色相同，未被选中时设置成白色
		            if (isSelected) {
		                setForeground(Color.decode("#F9E2D0")); 
		            } else {  
		                setForeground(Color.white);  
		            }
	            }else {
	            	setForeground(Color.BLACK);
	            }
	            

	            // 返回渲染的组件  
	            return this;  
	        }  
	    }; 
	    tablepanel.getTable().getColumnModel().getColumn(7).setCellRenderer(renderer);
	}
	
	
	/**
	 * 根据员工号统计个人、公司部分金额
	 * @param ListMap
	 */
	private void moneyDispose(String title, List<Map<String,String>> ListMap) {
		for (int i = 0; i < ListMap.size(); i++) {
			Map<String, UFDouble> mapMoney = new HashMap<String, UFDouble>();
			Map<String,String> map = ListMap.get(i);
			String clerkcode = map.get("clerkcode");
			String clerkname = map.get("clerkname");
			String orgName = map.get("orgname");
			String staffMoney = null;
			String orgMoney = null;
			//orgName可能存在为空的情况,拼接的时候会把null拼接到新的字符串，使用三目运算处理改问题
			String combinedField = title + (orgName == null ? "" : orgName) + clerkcode + clerkname;
			
			//取中间表数据
			Map<String,String> middleTableMap = middleTableData.get(combinedField);
			//middleTableMap为空表示中间表为空，使用原始数据，不为空使用中间表数据
			if(middleTableMap != null) {
				staffMoney = MathTool.nvl(ValueUtils.getUFDouble(middleTableMap.get("staffsum"))).toString();
				orgMoney = MathTool.nvl(ValueUtils.getUFDouble(middleTableMap.get("orgsum"))).toString();
			}else {
				staffMoney = MathTool.nvl(ValueUtils.getUFDouble(map.get("staffsum"))).toString();
				orgMoney = MathTool.nvl(ValueUtils.getUFDouble(map.get("orgsum"))).toString();
			}
			int staffMoneyLength = staffMoney.substring(staffMoney.indexOf(".") +1).length();//个人部分小数位数
			int orgMoneyLength = orgMoney.substring(orgMoney.indexOf(".") +1).length();//公司部分小数位数
			//个人部分或者公司部分小数位数大于2位需要进行金额处理
			if(staffMoneyLength > 2 || orgMoneyLength > 2) {
				UFDouble beforePsgStaffMoney = new UFDouble(staffMoney);
				UFDouble beforePsgOrgMoney = new UFDouble(orgMoney);
				//个人部分或者公司部分存在0.0000情况,个人部分或者公司部分其中一个不等于0需要进行金额处理
				if(!MathTool.equals(beforePsgStaffMoney, null) || !MathTool.equals(beforePsgOrgMoney, null)) {
					//如果当前clerkcode在map中有数据将两个金额相加
					if(beforePsgMoney.get(clerkcode) != null) {
						mapMoney = beforePsgMoney.get(clerkcode);
						beforePsgStaffMoney = beforePsgStaffMoney.add(mapMoney.get("beforePsgStaffMoney"));
						beforePsgOrgMoney = beforePsgOrgMoney.add(mapMoney.get("beforePsgOrgMoney"));
					}
					mapMoney.put("beforePsgStaffMoney", beforePsgStaffMoney);
					mapMoney.put("beforePsgOrgMoney", beforePsgOrgMoney);
					beforePsgMoney.put(clerkcode, mapMoney);
				} 
				
			}
		}
	}
	
	/**
	 * 数据处理，表格渲染
	 * @param dataList
	 * @param title
	 * @param ListMap
	 */
	private Map<String,UFDouble> addTableToList(String str,List<Object[]> dataList, String title, List<Map<String,String>> ListMap) {
		dataList.add(new Object[]{title});  
	    dataList.add(subheading);
	    Map<String,UFDouble> MapMoney = new HashMap<String,UFDouble>();
	    //合计部分
	    UFDouble staffTotal = new UFDouble(0.00, 2);
	    UFDouble orgTotal = new UFDouble(0.00, 2);
	    //小计部分
	    UFDouble staffSubtotal = new UFDouble(0.00, 2);
	    UFDouble orgSubtotal = new UFDouble(0.00, 2);
	    for (int i = 0; i < ListMap.size(); i++) {
			Map<String,String> map = ListMap.get(i);
			Map<String, UFDouble> mapMoney = new HashMap<String, UFDouble>();
			String clerkcode = map.get("clerkcode");
			String clerkname = map.get("clerkname");
			String orgName = map.get("orgname");
			String basicssum = MathTool.nvl(ValueUtils.getUFDouble(map.get("basicssum"))).toString();
			String staffsum = MathTool.nvl(ValueUtils.getUFDouble(map.get("staffsum"))).toString();
			String orgsum = MathTool.nvl(ValueUtils.getUFDouble(map.get("orgsum"))).toString();
			String mark = map.get("mark");
			//orgName可能存在为空的情况,拼接的时候会把null拼接到新的字符串，使用三目运算处理改问题
			String combinedField = title + (orgName == null ? "" : orgName) + clerkcode + clerkname;
			UFDouble basicsMoney = new UFDouble(0.00, 2);
			UFDouble staffMoney = new UFDouble(0.00, 2);
			UFDouble orgMoney = new UFDouble(0.00, 2);
			//取中间表数据
			Map<String,String> middleTableMap = middleTableData.get(combinedField);
			//middleTableMap不为空使用中间表数据
			if(middleTableMap != null ) {
				basicssum = MathTool.nvl(ValueUtils.getUFDouble(middleTableMap.get("basicssum"))).toString();
				staffsum = MathTool.nvl(ValueUtils.getUFDouble(middleTableMap.get("staffsum"))).toString();
				orgsum = MathTool.nvl(ValueUtils.getUFDouble(middleTableMap.get("orgsum"))).toString();
				mark = middleTableMap.get("mark");
			}
			basicsMoney = nvl(basicssum, 2);
			staffMoney = nvl(staffsum, 2);
			orgMoney= nvl(orgsum, 2);
			int staffsumLength = staffsum.substring(staffsum.indexOf(".") + 1).length();//个人部分小数位数
			int orgsumLength = orgsum.substring(orgsum.indexOf(".") + 1).length();//公司部分小数位数
			//个人部分或者公司部分小数位数大于2位需要进行金额处理
			if(staffsumLength > 2 || orgsumLength > 2) {
				UFDouble afterPsgStaffMoney = staffMoney;
				UFDouble afterPsgOrgMoney = orgMoney;
				//个人部分或者公司部分存在0.0000情况,个人部分或者公司部分其中一个不等于0需要进行金额处理
				if(!MathTool.equals(afterPsgStaffMoney, null) || !MathTool.equals(afterPsgOrgMoney, null)) {
					//如果当前员工编号在map中有数据将两个金额相加
					if(afterPsgMoney.get(clerkcode) != null) {
						mapMoney = afterPsgMoney.get(clerkcode);
						afterPsgStaffMoney = afterPsgStaffMoney.add(mapMoney.get("afterPsgStaffMoney"));
						afterPsgOrgMoney = afterPsgOrgMoney.add(mapMoney.get("afterPsgOrgMoney"));					
					}
					//取处理前个人部分金额
					UFDouble beforePsgStaffMoney = beforePsgMoney.get(clerkcode).get("beforePsgStaffMoney");
					//取处理前公司部分金额
					UFDouble beforePsgOrgMoney = beforePsgMoney.get(clerkcode).get("beforePsgOrgMoney");
					//如果处理后个人部分金额大于取处理前个人部分金额，处理后个人部分行金额减去两个值的差
					if(MathTool.greaterThan(afterPsgStaffMoney, beforePsgStaffMoney)) {
						staffMoney = staffMoney.sub(afterPsgStaffMoney.sub(beforePsgStaffMoney).setScale(2, UFDouble.ROUND_HALF_UP));
					}
					//如果处理后公司部分金额大于取处理前公司部分金额，处理后公司部分行金额减去两个值的差
					if(MathTool.greaterThan(afterPsgOrgMoney, beforePsgOrgMoney)) {
						orgMoney = orgMoney.sub(afterPsgOrgMoney.sub(beforePsgOrgMoney).setScale(2, UFDouble.ROUND_HALF_UP));
					}				
					mapMoney.put("afterPsgStaffMoney", afterPsgStaffMoney);
					mapMoney.put("afterPsgOrgMoney", afterPsgOrgMoney);
					afterPsgMoney.put(clerkcode, mapMoney);
				}
			}
			
			//统计合计
			staffTotal = staffTotal.add(staffMoney);
			orgTotal = orgTotal.add(orgMoney);			
			if(orgName != null) {
				//组织不为空输出组织小标题和小计
				if(i == 0) {
					//输出组织小标题
					dataList.add(new Object[] {orgName});
				}else if(!orgName.equals(ListMap.get(i-1).get("orgname"))){
					//如果当前行组织不等于上一行组织输组织小标题
					//输出组织小标题
					dataList.add(new Object[] {orgName});
				}			
				//输出行数据
				dataList.add(new Object[] {i+1, clerkcode, clerkname, basicsMoney, staffMoney, orgMoney, mark, str+orgName});
				//统计小计
				staffSubtotal = staffSubtotal.add(staffMoney);
				orgSubtotal = orgSubtotal.add(orgMoney);
				if(i+1 == ListMap.size()) {
					//第一行和最后一行输出小计
					//输出小计
					dataList.add(new Object[] {"小计", null, null, null, staffSubtotal, orgSubtotal});
					staffSubtotal = new UFDouble(0.00, 2);
					orgSubtotal = new UFDouble(0.00, 2);
				}else if(!orgName.equals(ListMap.get(i+1).get("orgname"))){
					//如果当前行组织不等于下一行组织输出小计
					//输出小计
					dataList.add(new Object[] {"小计", null, null, null, staffSubtotal, orgSubtotal});
					staffSubtotal = new UFDouble(0.00, 2);
					orgSubtotal = new UFDouble(0.00, 2);
				}
			}else {
				//输出行数据
				dataList.add(new Object[] {i+1, clerkcode, clerkname, basicsMoney, staffMoney, orgMoney, mark, str});
			}
	    }
	    //输出合计
	    dataList.add(new Object[] {"合计", null, null, null, staffTotal, orgTotal});
	    MapMoney.put("staff", staffTotal);
	    MapMoney.put("org", orgTotal);
	    return MapMoney;
	}
	
	
	/**
	 * 查询个人承担
	 * @param period
	 * @param orgcode
	 * @return
	 * @throws Exception
	 */
	private List<Map<String,String>> queryTableOne(String orgCode, String period) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT clerkcode,clerkname,endowment_base AS basicssum,sum_per_cost AS orgsum,mark");
		sql.append(" FROM");
		sql.append(" (SELECT ROWNUM AS seq, t.CLERKCODE,T1.CLERKNAME,t.ym,sum( nvl( t.VALUE, 0 ) ) AS sum_per_cost,t1.ENDOWMENT_BASE,t1.mark");
		sql.append(" from NC6337.v_data_xz_gjjshare t");
		sql.append(" left join NC6337.V_SUM_TB_HR_SOCIAL_SECURITY t1 on t1.CLERKCODE=t.CLERKCODE and t1.PAY_MONTH||'-01'=t.ym");
		sql.append(" where");
		sql.append(" t.key = '社保公司税后扣款' and t.VALUE <> 0 and t.ym='"+period+"'||'-01' and t1.ORGCODE='"+orgCode+"' and t1.ENDOWMENT_BASE is not null");
		sql.append(" group by ROWNUM,t.CLERKCODE, T1.CLERKNAME,t.ym,t1.ENDOWMENT_BASE,t1.mark)");
		return (List<Map<String, String>>) iuap.executeQuery(sql.toString(), new MapListProcessor());
	}
		
	/**
	 * 查询外部公司承担
	 * @param period
	 * @param orgcode
	 * @return
	 * @throws Exception
	 */
	private List<Map<String,String>> queryTableTwo(String orgCode, String period) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT clerkcode,clerkname,endowment_base AS basicssum,so_staff_sum AS staffsum,so_org_sum AS orgsum,mark");
		sql.append(" FROM");
		sql.append(" (SELECT ROWNUM,t.*");
		sql.append(" FROM nc6337.V_SUM_TB_HR_SOCIAL_SECURITY t");
		sql.append(" WHERE");
		sql.append(" t.out_org IS NOT NULL AND t.PAY_MONTH = '"+period+"' AND t.ORGCODE = '"+orgCode+"'");
		sql.append(" ORDER BY ROWNUM)");
		return (List<Map<String, String>>) iuap.executeQuery(sql.toString(), new MapListProcessor());
	}
		
	/**
	 * 查询由部门承担或全额公司承担
	 * @param period
	 * @param orgcode
	 * @return
	 * @throws Exception
	 */
	private List<Map<String,String>> queryTableThree(String orgCode, String period) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT clerkcode,clerkname,endowment_base AS basicssum,so_staff_sum AS staffsum,so_org_sum AS orgsum");
		sql.append(" FROM");
		sql.append(" (SELECT ROWNUM,t2.clerkcode,t2.clerkname,t2.endowment_base,t2.so_staff_sum,t2.so_org_sum");
		sql.append(" FROM");
		sql.append(" (SELECT");
		sql.append(" T.Endowment_Base,T.CLERKCODE,T.CLERKNAME,T.so_org_sum,T.so_staff_sum,T.ORGCODE,T.PAY_MONTH");
		sql.append(" FROM");
		sql.append(" NC6337.V_SUM_TB_HR_SOCIAL_SECURITY T");
		sql.append(" WHERE");
		sql.append(" T.PK_POST IN ( SELECT T3.PK_POST FROM NC6337.om_post t3 WHERE T3.postname = '残保金人员' )");
		sql.append(" UNION ALL SELECT");
		sql.append(" t1.Endowment_Base,t1.CLERKCODE,t1.CLERKNAME,t1.so_org_sum,t1.so_staff_sum,T1.ORGCODE,t1.PAY_MONTH ");
		sql.append(" FROM");
		sql.append(" NC6337.V_SUM_TB_HR_SOCIAL_SECURITY_D t1");
		sql.append(" WHERE");
		sql.append(" t1.MARK LIKE '%表三%'");
		sql.append(" ) t2");
		sql.append(" WHERE");
		sql.append(" t2.PAY_MONTH = '"+period+"' AND t2.ORGCODE = '"+orgCode+"'");
		sql.append(" ORDER BY ROWNUM)");
		return (List<Map<String, String>>) iuap.executeQuery(sql.toString(), new MapListProcessor());
	}
	
	/**
	 * 查询个人部分已在集团内其他公司扣除
	 * @param period
	 * @param orgcode
	 * @return
	 * @throws Exception
	 */
	private List<Map<String,String>> queryTableFour(String orgCode, String period) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT salocation as orgname,clerkcode,clerkname,endowment_base AS basicssum,so_staff_sum AS staffsum,so_org_sum AS orgsum,mark");
		sql.append(" FROM");
		sql.append(" (SELECT ROWNUM AS seq,t11.*");
		sql.append(" FROM");
		sql.append(" (SELECT");
		sql.append(" t.CLERKCODE,T1.CLERKNAME,t.ym,decode( t.c_23, '青岛天华易境建筑设计有限公司', 0, sum( nvl( t.VALUE, 0 ) ) ) AS sum_per_cost,");
		sql.append(" t.c_23 AS salocation,decode( t.c_23, '青岛天华易境建筑设计有限公司', t1.so_org_sum + t1.so_staff_sum, t1.so_org_sum ) AS so_org_sum,");
		sql.append(" t1.so_staff_sum,t1.ENDOWMENT_BASE,T1.MARK");
		sql.append(" FROM");
		sql.append(" NC6337.v_data_xz_gjjshare t");
		sql.append(" LEFT JOIN NC6337.V_SUM_TB_HR_SOCIAL_SECURITY t1 ON t1.CLERKCODE = t.CLERKCODE AND t1.PAY_MONTH || '-01' = t.ym");
		sql.append(" WHERE t.key = '社保个人合计'  AND t.VALUE <> 0 AND t.ym = '"+period+"' || '-01' AND t1.ORGCODE = '"+orgCode+"' AND t1.PAY_SOS_ORG <> t.c_23");
		sql.append(" GROUP BY ROWNUM,t.CLERKCODE,T1.CLERKNAME,t.ym,t1.ENDOWMENT_BASE,t1.so_org_sum,t1.so_staff_sum,t.c_23,T1.MARK");
		sql.append(" ORDER BY t.c_23");
		sql.append(" ) t11");
		sql.append(" ORDER BY ROWNUM)");
		return (List<Map<String, String>>) iuap.executeQuery(sql.toString(), new MapListProcessor());
	}
	
	/**
	 * 查询月薪工资表中当月扣当月缴纳
	 * @param period
	 * @param orgcode
	 * @return
	 * @throws Exception
	 */
	private List<Map<String,String>> queryTableFive(String orgCode, String period) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("select costorgname as orgname,clerkcode,clerkname,endowment_base AS basicssum, so_staff_sum as staffsum,so_org_sum AS orgsum,mark ");
		sql.append(" FROM");
		sql.append(" ( SELECT t.CLERKCODE,T1.CLERKNAME,t.ym,sum( nvl( t.VALUE, 0 ) ) AS sum_per_cost,T13.Name AS costorgname,t.c_23 AS salocation,");
		sql.append(" t1.so_org_sum - nvl( t15.sum_per_cost, 0 ) - nvl( t16.SO_ORG_SUM, 0 ) AS so_org_sum,t1.ENDOWMENT_BASE,t1.so_staff_sum,t1.mark");
		sql.append(" FROM");
		sql.append(" NC6337.v_data_xz_gjjshare t");
		sql.append(" LEFT JOIN NC6337.V_SUM_TB_HR_SOCIAL_SECURITY t1 ON t1.CLERKCODE = t.CLERKCODE AND t1.PAY_MONTH || '-01' = t.ym");
		sql.append(" LEFT JOIN ( SELECT t12.CLERK_CODE, t12.NC_PK_POST FROM nc6337.monthly_position_gjj t12 WHERE t12.month = to_char( last_day( to_date( '"+period+"', 'yyyy-mm' ) ), 'yyyy-mm-dd' )");
		sql.append(" ) T12 ON t12.CLERK_CODE = t.CLERKCODE");
		sql.append(" LEFT JOIN nc6337.om_post t14 ON t14.pk_post = t12.NC_PK_POST");
		sql.append(" LEFT JOIN nc6337.org_corp T13 ON T13.Pk_Corp = t14.pk_org");
		sql.append(" LEFT JOIN nc6337.person_afford_sbgjj t15 ON t15.CLERKCODE = t.CLERKCODE AND t.YM = t15.ym");
		sql.append(" LEFT JOIN nc6337.DEPT_AFFORD_SBGJJ t16 ON t16.CLERKCODE = t.CLERKCODE AND t.YM = t16.PAY_MONTH WHERE");
		sql.append(" t.key IN ( '社保个人合计', '费用调整社保', '社保个人扣款', '费用调整社保T' ) ");
		sql.append(" AND t.VALUE <> 0 AND t.ym = '"+period+"' || '-01' AND t1.ORGCODE = '"+orgCode+"' AND t1.PAY_SOS_ORG = t.c_23");
		sql.append(" AND t.CLERKCODE NOT IN ( SELECT t11.WORKERCODE FROM nc6337.v_hr_allocationratio_gjjshareF t11 WHERE t11.WA_PERIOD = '"+period+"' || '-01' ) ");
		sql.append(" GROUP BY t.CLERKCODE,T1.CLERKNAME,t.ym,t1.ENDOWMENT_BASE,t1.so_org_sum - nvl( t15.sum_per_cost, 0 ) - nvl( t16.SO_ORG_SUM, 0 ),");
		sql.append(" t1.so_staff_sum,T13.Name,t.c_23,t1.mark");
		sql.append(" UNION ALL SELECT");
		sql.append(" t.WORKERCODE AS CLERKCODE,t1.CLERKNAME,t.WA_PERIOD AS ym,0 AS sum_per_cost,T6.NAME AS costorgname,'上海天华' AS salocation,");
		sql.append(" round( nvl( t1.so_org_sum, 0 ) * nvl( t.perc, 0 ), 3 ) AS so_org_sum,");
		sql.append(" round( nvl( t1.so_staff_sum, 0 ) * nvl( t.perc, 0 ), 3 ) AS so_staff_sum,t1.Endowment_Base,t1.mark");
		sql.append(" FROM");
		sql.append(" nc6337.v_hr_allocationratio_gjjshareF t");
		sql.append(" LEFT JOIN nc6337.V_SUM_TB_HR_SOCIAL_SECURITY t1 ON t1.CLERKCODE = t.WORKERCODE AND t1.PAY_MONTH || '-01' = t.WA_PERIOD");
		sql.append(" LEFT JOIN ( SELECT DISTINCT T5.CODE, T5.NAME, t5.pk_corp FROM nc6337.ORG_CORP T5 ) T6 ON T6.pk_corp = T.COST_ORG");
		sql.append(" WHERE");
		sql.append(" t.WA_PERIOD = '"+period+"' || '-01' AND t1.CLERKNAME IS NOT NULL AND t1.ORGCODE = '"+orgCode+"'");
		sql.append(" UNION ALL SELECT");
		sql.append(" t1.CLERKCODE,t1.CLERKNAME,t1.PAY_MONTH || '-01' AS ym,0 AS sum_per_cost,T1.PAY_SOS_ORG AS costorgname,'上海天华' AS salocation,");
		sql.append(" decode( t1.mark, '资质人员', t1.so_org_sum + t1.so_staff_sum, t1.so_org_sum ) AS so_org_sum,0 AS so_staff_sum,");
		sql.append(" t1.Endowment_Base,t1.mark");
		sql.append(" FROM");
		sql.append(" NC6337.V_SUM_TB_HR_SOCIAL_SECURITY_D t1");
		sql.append(" WHERE");
		sql.append(" t1.MARK IN ( '资质人员', '上海公司承担' ) AND t1.PAY_MONTH = '"+period+"'AND t1.ORGCODE = '"+orgCode+"'");
		sql.append(" )ORDER BY costorgname");
		return (List<Map<String,String>>) iuap.executeQuery(sql.toString(), new MapListProcessor());
	}
	
	/**
	 * 查询实际已缴纳当月工资个人部分未扣
	 * @param period
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	private List<Map<String,String>> queryTableSix(String orgCode, String period) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT orgname,clerkcode,clerkname,endowment_base AS basicssum,so_staff_sum as staffsum,so_org_sum AS orgsum,mark");
		sql.append(" FROM");
		sql.append(" (SELECT t1.CLERKCODE,t1.CLERKNAME,t1.Endowment_Base,t1.so_org_sum,t1.so_staff_sum,t1.mark,T13.Name AS orgname");
		sql.append(" FROM");
		sql.append(" nc6337.V_SUM_TB_HR_SOCIAL_SECURITY t1");
		sql.append(" LEFT JOIN (SELECT t12.CLERK_CODE,t12.NC_PK_POST FROM nc6337.monthly_position_gjj t12 WHERE");
		sql.append(" t12.month = to_char( last_day( to_date( '"+period+"', 'yyyy-mm' ) ), 'yyyy-mm-dd' )");
		sql.append(" ) T12 ON t12.CLERK_CODE = t1.CLERKCODE");
		sql.append(" LEFT JOIN nc6337.om_post t14 ON t14.pk_post = t12.NC_PK_POST");
		sql.append(" LEFT JOIN nc6337.org_corp T13 ON T13.Pk_Corp = t14.pk_org");
		sql.append(" WHERE");
		sql.append(" t1.CLERKCODE NOT IN (SELECT t2.CLERKCODE FROM(SELECT t.CLERKCODE,sum( nvl( t.VALUE, 0 ) ) AS sumVALUE");
		sql.append(" FROM NC6337.v_data_xz_gjjshare t WHERE t.key IN ( '社保个人合计', '社保个人扣款' ) AND t.ym = '"+period+"' || '-01'");
		sql.append(" GROUP BY t.CLERKCODE ");
		sql.append(" ) t2 WHERE t2.sumVALUE > 0)");
		sql.append(" AND T1.PK_POST NOT IN ( SELECT T3.PK_POST FROM NC6337.om_post t3 WHERE T3.postname IN ( '残保金人员', '资质人员' ) )");
		sql.append(" AND t1.out_org IS NULL AND t1.Endowment_Base <> 0 AND t1.PAY_MONTH = '"+period+"' AND t1.ORGCODE = '"+orgCode+"'");
		sql.append(" UNION ALL SELECT");
		sql.append(" t1.CLERKCODE,t1.CLERKNAME,t1.Endowment_Base,t1.so_org_sum,t1.so_staff_sum,t1.mark,T13.Name AS orgname");
		sql.append(" FROM nc6337.V_SUM_TB_HR_SOCIAL_SECURITY_D t1 LEFT JOIN (SELECT t12.CLERK_CODE,t12.NC_PK_POST FROM");
		sql.append(" nc6337.monthly_position_gjj t12");
		sql.append(" WHERE");
		sql.append(" t12.month = to_char( last_day( to_date( '"+period+"', 'yyyy-mm' ) ), 'yyyy-mm-dd' ) ");
		sql.append(" ) T12 ON t12.CLERK_CODE = t1.CLERKCODE");
		sql.append(" LEFT JOIN nc6337.om_post t14 ON t14.pk_post = t12.NC_PK_POST");
		sql.append(" LEFT JOIN nc6337.org_corp T13 ON T13.Pk_Corp = t14.pk_org");
		sql.append(" WHERE");
		sql.append(" t1.mark LIKE '%表六%' ");
		sql.append(" AND t1.PAY_MONTH = '"+period+"' AND t1.ORGCODE = '"+orgCode+"'");
		sql.append(" )ORDER BY orgname");
		return (List<Map<String,String>>) iuap.executeQuery(sql.toString(), new MapListProcessor());
	}
	
	
	
	/**
	 * 查询实际未缴当月工资个人部分已扣
	 * @param period
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	private List<Map<String,String>> queryTableSeven(String orgCode, String period) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT orgname,clerkcode,clerkname,endowment_base AS basicssum,so_staff_sum as staffsum,so_org_sum AS orgsum,mark");
		sql.append(" FROM");
		sql.append(" (SELECT t1.CLERKCODE,t1.CLERKNAME,t1.Endowment_Base,( t16.sumvalue - t1.so_staff_sum ) AS so_staff_sum,");
		sql.append(" 0 AS so_org_sum,T13.Name AS orgname,t1.mark");
		sql.append(" FROM");
		sql.append(" nc6337.V_SUM_TB_HR_SOCIAL_SECURITY t1");
		sql.append(" LEFT JOIN (SELECT t12.CLERK_CODE,t12.NC_PK_POST FROM nc6337.monthly_position_gjj t12 WHERE");
		sql.append(" t12.month = to_char( last_day( to_date( '"+period+"', 'yyyy-mm' ) ), 'yyyy-mm-dd' )");
		sql.append(" ) T12 ON t12.CLERK_CODE = t1.CLERKCODE");
		sql.append(" LEFT JOIN nc6337.om_post t14 ON t14.pk_post = t12.NC_PK_POST");
		sql.append(" LEFT JOIN nc6337.org_corp T13 ON T13.Pk_Corp = t14.pk_org");
		sql.append(" LEFT JOIN nc6337.person_deduction_sbgjj t16 ON t16.CLERKCODE = t1.CLERKCODE AND t1.pay_month || '-01' = t16.ym");
		sql.append(" WHERE");
		sql.append(" t1.CLERKCODE IN (SELECT t2.CLERKCODE FROM (SELECT t.CLERKCODE,sum( nvl( t.VALUE, 0 ) ) AS sumVALUE FROM");
		sql.append(" NC6337.v_data_xz_gjjshare t WHERE t.key IN ( '社保个人扣款', '社保个人合计' ) AND t.ym = '"+period+"' || '-01'");
		sql.append(" GROUP BY t.CLERKCODE");
		sql.append(" ) t2 WHERE t2.sumVALUE > 0)");
		sql.append(" AND T1.PK_POST NOT IN ( SELECT T3.PK_POST FROM NC6337.om_post t3 WHERE T3.postname IN ( '残保金人员', '资质人员' ) )");
		sql.append(" AND t1.out_org IS NULL AND t1.Endowment_Base <> 0 AND ( t16.sumvalue - t1.so_staff_sum ) > 0");
		sql.append(" AND t1.PAY_MONTH = '"+period+"' AND t1.ORGCODE = '"+orgCode+"'");
		sql.append(" UNION ALL SELECT");
		sql.append(" t1.CLERKCODE,t1.CLERKNAME,t1.Endowment_Base,t1.so_staff_sum,t1.so_org_sum,T13.Name AS orgname,t1.mark");
		sql.append(" FROM");
		sql.append(" nc6337.V_SUM_TB_HR_SOCIAL_SECURITY_D t1");
		sql.append(" LEFT JOIN (SELECT t12.CLERK_CODE,t12.NC_PK_POST FROM nc6337.monthly_position_gjj t12 WHERE");
		sql.append(" t12.month = to_char( last_day( to_date( '"+period+"', 'yyyy-mm' ) ), 'yyyy-mm-dd' ) ");
		sql.append(" ) T12 ON t12.CLERK_CODE = t1.CLERKCODE");
		sql.append(" LEFT JOIN nc6337.om_post t14 ON t14.pk_post = t12.NC_PK_POST");
		sql.append(" LEFT JOIN nc6337.org_corp T13 ON T13.Pk_Corp = t14.pk_org");
		sql.append(" WHERE");
		sql.append(" t1.mark LIKE '%表七%'");
		sql.append(" AND t1.PAY_MONTH = '"+period+"' AND t1.ORGCODE = '"+orgCode+"'");
		sql.append(" )ORDER BY orgname");
		return (List<Map<String,String>>) iuap.executeQuery(sql.toString(), new MapListProcessor());
	}
	
	/**
	 * 查询工资表扣缴数
	 * @param period
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	private Object queryTableNine(String orgCode, String period) throws Exception{	
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT");
		sql.append(" sum( nvl( t.VALUE, 0 ) ) AS sum_per_cost");
		sql.append(" FROM");
		sql.append(" NC6337.v_data_xz_gjjshare t");
		sql.append(" WHERE");
		sql.append(" t.key IN ( '社保个人合计', '费用调整社保', '社保个人扣款', '费用调整社保T' ) AND t.ym = '"+period+"' || '-01'");
		sql.append(" AND t.c_23 = ( SELECT t1.name FROM nc6337.org_corp t1 WHERE t1.code = '"+orgCode+"' )");
		return (Object) iuap.executeQuery(sql.toString(), new ColumnProcessor());
	}
	
	/**
	 * 查询实际缴纳数
	 * @param period
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	private Map<String,String> queryTableTen(String orgCode, String period) throws Exception{	
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT");
		sql.append(" *");
		sql.append(" FROM");
		sql.append(" NC6337.V_SUM_TB_HR_SOCIAL_SECURITY_D T");
		sql.append(" WHERE");
		sql.append(" T.CLERKNAME = '总数' AND T.PAY_MONTH = '"+period+"' AND T.ORGCODE = '"+orgCode+"'");
		Map<String,String> map = (Map<String, String>) iuap.executeQuery(sql.toString(), new MapProcessor());
		if(map == null) {
			map = new HashMap<String,String>();
			map.put("so_staff_sum", null);
			map.put("so_org_sum", null);
		}
		return map;
	}
	
	/**
	 * 查询中间表数据并处理
	 * @return
	 * @throws Exception
	 */
	private void queryMiddleTable() throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT");
		sql.append(" combined_field,basicssum,staffsum,orgsum,mark from accumulation_fund");
		sql.append(" WHERE ");
		sql.append(" tablename = '社保分摊表' AND combined_field NOT LIKE '表八：实际集团内其他公司已缴当月工资个人部分已扣%'");
		sql.append(" AND orgcode = '" + orgCode + "' AND period = '" + period + "'");
		List<Map<String,String>> listMap = (List<Map<String, String>>) iuap.executeQuery(sql.toString(), new MapListProcessor());
		for (int i = 0; i < listMap.size(); i++) {
			Map<String,String> map = listMap.get(i);
			middleTableData.put(map.get("combined_field"), map);
		}
	}
	
	/**
	 * 查询实际集团内其他公司已缴当月工资个人部分已扣
	 * @param period
	 * @param orgCode
	 * @return
	 * @throws Exception
	 */
	private List<Map<String,String>> queryTableEight(String orgCode, String period) throws Exception{
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT");
		sql.append(" contributoryorg AS orgname,clerkcode,clerkname,basicssum,staffsum,orgsum,mark");
		sql.append(" FROM");
		sql.append(" accumulation_fund");
		sql.append(" WHERE");
		sql.append(" tablename = '公积金分摊表' AND combined_field LIKE '表八：实际集团内其他公司已缴当月工资个人部分已扣%'");
		sql.append("  AND orgcode = '" + orgCode + "' AND period = '" + period + "'");
		sql.append(" ORDER BY CONTRIBUTORYORG");
		return (List<Map<String,String>>) iuap.executeQuery(sql.toString(), new MapListProcessor());
	}
	
	/**
	 * 将Object转成UFDouble，设置保留几位小数
	 * @param obj
	 * @param power
	 * @return
	 */
	public UFDouble nvl(Object obj, int power){
		return MathTool.nvl(ValueUtils.getUFDouble(obj)).setScale(2, UFDouble.ROUND_HALF_UP);
	}
	
	/**
	 * 获取修改数据打开修改界面
	 * @param table 修改数据
	 */
	private void update(UITablePane table) {
		try {
			List<Object[]> selectedData = new ArrayList<Object[]>();
			int rows[] = table.getTable().getSelectedRows();
			if(rows.length == 0) {
				MessageDialog.showHintDlg(this, "提示", "请选择需要修改的数据！");
				return;
			}else {
				for (int i = 0; i < rows.length; i++) {
					//小标题加上组织和表组合为8位
					Object[] rowData = new Object[8];
					for (int j = 0; j < 8; j++) {
						Object obj  = table.getTable().getValueAt(rows[i], 7);
						if(obj == null) {
							MessageDialog.showHintDlg(this, "提示", "请选择正确的行修改！");
							return;
						}
						rowData[j] = table.getTable().getValueAt(rows[i], j);
					}
					selectedData.add(rowData);
				}
			}
			//0表示有实际修改，而不是点击弹框后关闭了
			 if(new SocialInsuranceAllUpdateDialog(selectedData, orgCode, period).showModal() == 0) {
				//调用报表查询刷新数据
				tableProcessing();
				MessageDialog.showHintDlg(this, "提示", "修改成功！");
			 }
		} catch (Exception e) {
			MessageDialog.showHintDlg(this, "提示", e.getMessage());
		}
	}
	
	/**
	 * 导出
	 */
	private void onoutput() {
		OutputDialogUtil outdig = new OutputDialogUtil(this, getTablePanel(), "公积金分摊表");
		if (outdig.showModal() == 2) {
			return;
		}
	}
	
	/**
	 * 打开表八：实际集团内其他公司已缴当月工资个人部分已扣新增页面
	 */
	private void insert() {
		 try {
			//0表示有实际新增，而不是点击弹框后关闭了
			if(new AccumulationFundAllInsertDiaLog().showModal() == 0) {
				MessageDialog.showHintDlg(this, "提示", "新增成功！");
			 }
		} catch (Exception e) {
			MessageDialog.showHintDlg(this, "提示", e.getMessage());
		}
	}
}

