package nc.ui.wa.ruleex.action;

import java.text.DecimalFormat;

import nc.ui.hr.uif2.model.HRMetaDataDataSource;
import nc.ui.uif2.model.BatchBillTableModel;
import nc.ui.uif2.model.BillManageModel;
import nc.vo.hrwa.waruleex.AggWageformExVO;
import nc.vo.hrwa.waruleex.WageformExVO;
import nc.vo.hrwa.waruleex.WageformdetExVO;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.lang.UFDouble;
import nccloud.commons.lang.ArrayUtils;


/**
 * @author xueliang1
 * 薪资规则的打印数据源，处理精度
 * 根据指定的小数位数指定打印精度
 * */
public class WaRuleDataSource extends HRMetaDataDataSource {

	private static final long serialVersionUID = 2684346227215103111L;

	@Override
	public Object[] getMDObjects() {
		
		   if (getModel() == null)
	        {
	            return null;
	        }
	        
	        if (isSingleData())
	        {
	        	if(getModel().getSelectedData()==null){
	        		return null;
	        	}
	        	AggWageformExVO selectedData=(AggWageformExVO)getModel().getSelectedData();
	        	//获取小数位数
	        	int bitnum=0;
	        	bitnum=((WageformExVO)selectedData.getParentVO()).getIflddecimal();
	        	String formtext="0.";
	        	//构造格式
	        	if(bitnum==0){
	        		formtext="0";
	        	}else{
	        		for(int i=0;i<bitnum;i++){
	        			formtext+="0";
	        		}
	        	}
	        	DecimalFormat dcmFmt = new DecimalFormat(formtext);
	        	CircularlyAccessibleValueObject[] childs=selectedData.getChildrenVO();
	        	//将子表的金额值按照主表的小数 位数进行格式化
	        	if(!ArrayUtils.isEmpty(childs)){
	        		for(CircularlyAccessibleValueObject obj:childs){
	        			if(obj.getAttributeValue(WageformdetExVO.NVALUE)!=null){
	        				obj.setAttributeValue(WageformdetExVO.NVALUE, new UFDouble(dcmFmt.format(obj.getAttributeValue(WageformdetExVO.NVALUE))));
	        			}
	        		}
	        	}
	            return new Object[]{getModel().getSelectedData()};
	        }
	        
	        if (getModel() instanceof BillManageModel)
	        {
	            return ((BillManageModel) getModel()).getData().toArray();
	        }
	        else if (getModel() instanceof BatchBillTableModel)
	        {
	            return ((BatchBillTableModel) getModel()).getRows().toArray();
	        }
	        
	        return new Object[]{getModel().getSelectedData()};
	}

	
	
}
