package com.dev.swms.server.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.ParseException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.dev.swms.server.service.sequence.BussinessCodeManager;
import com.dev.energy.server.config.UniConfig;
import com.dev.energy.server.config.globalparam.GlobalParam;
import com.dev.energy.server.exception.BusinessException;

public class GlobalParamUtils implements ApplicationContextAware {
	private static ApplicationContext ac;
	
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		this.ac = arg0;
	}

	/**
     * 按照配置的全局变量的数字格式格式化数字
     * decimal 保留的小数位数(0－不保留、1－保留1位、以此类推)
     * round 保留的小数位数时是否四舍五入(Y－是、N－否)
     * @param   number   a <code>double</code>.
     */
	public static double formatDecimalByType(double number, String type) {
		if (ac.containsBean("uniConfig")) {
			UniConfig uniConfig = (UniConfig) ac.getBean("uniConfig");
			int decimal = uniConfig.getGlobalParam(type).getInt();
			String round = uniConfig.getGlobalParam("round").getValue();
			String cResult = "";
			
			DecimalFormat df = null;
			if (round.equals("Y")) {
				df = new DecimalFormat("#."+StringUtils.repeat("0", decimal));
			} else {
				df = new DecimalFormat("#."+StringUtils.repeat("0", decimal+1));
			}	
			cResult = df.format(number);
			if (cResult.indexOf(".") != -1) {
				cResult = cResult.substring(0, cResult.indexOf(".")+decimal+1 > cResult.length() ? cResult.length():cResult.indexOf(".")+decimal+1);
				if (".".equals(cResult)) {
					cResult = "0";
				}
			}
			try {
				return df.parse(cResult).doubleValue();
			} catch (ParseException e) {
				throw new BusinessException("number.format.error");
			}
		} else {
			throw new BusinessException("system.error");
		}
	}

    /**
     * 从配置的全局变量中取得布尔值
     * @param beanId 全局参数bean的ID值
     * @return boolean 全局参数bean对应的value值
     */
    public static boolean getGloableBooleanValue(String beanId){
    	if (ac.containsBean("uniConfig")) {
            UniConfig uniConfig = (UniConfig) ac.getBean("uniConfig");
            GlobalParam globalParam = uniConfig.getGlobalParam(beanId);
            if(globalParam!=null && globalParam.getString()!=null && globalParam.getString().equals("Y")){
                return true;
            }
        }
        return false;
    }

    /**
     * 从配置的全局变量中取得字符串值
     * @param beanId 全局参数bean的ID值
     * @return String 全局参数bean对应的value值
     */
    public static String getGloableStringValue(String beanId){
    	if (ac.containsBean("uniConfig")) {
            UniConfig uniConfig = (UniConfig) ac.getBean("uniConfig");
            GlobalParam globalParam = uniConfig.getGlobalParam(beanId);
            if(globalParam != null && globalParam.getString() != null){
                return globalParam.getString();
            }
        }
        return "";
    }
    
    public static String generateCodeByType(String type) {
    	String result = "";
    	if (ac.containsBean("swmsBussinessCodeManager")) {
    		BussinessCodeManager codeManager = (BussinessCodeManager) ac.getBean("swmsBussinessCodeManager");
    		try {
        		Method method = codeManager.getClass().getMethod(type, new Class[0]);
				result = (String) method.invoke(codeManager, new Object[0]);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
    	}
    	return result;
    }
}
