package nccloud.pubimpl.platform.pub;


import java.util.List;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.pubitf.initgroup.InitGroupQuery;
import nccloud.commons.lang.StringUtils;
import nccloud.pubitf.platform.pub.IAppCodeToService;

public class AppCodeToServiceImpl implements IAppCodeToService {

	private BaseDAO dao;
	
	private static String[] pubModuleUrls = new String[] {
		"nccloud/ampub","nccloud/fipub","nccloud/tmpub","nccloud/mapub"
	};
	
	@Override
	public String getServiceByAppCode(String appCode, String dataSource,String url) throws Exception {
		String service = null;
		// 参数合法性校验
		if (StringUtils.isEmpty(appCode)) {
			return null;
		}
		InvocationInfoProxy.getInstance().setUserDataSource(dataSource);
		String pk_group = InvocationInfoProxy.getInstance().getGroupId();
		List<AppCodeToServiceVO> appCodeVos = this.queryServiceByAppCode(appCode);
		if ((appCodeVos != null) && (appCodeVos.size() > 0)) {
			service = appCodeVos.get(0).getService();
			if(url!=null) {
				for(String pubModuleUrl:pubModuleUrls) {
					if(url.contains(pubModuleUrl)) {
						service=pubModuleUrl.split("/")[1];
					}
				}
			}
			
			// 需要查询当前的启用和license情况
			if ("ampub".equals(service)) {
				// aim如果启用，则转发到am-manage
				if (InitGroupQuery.isEnabled(pk_group, "4510")) {
					appCodeVos.get(0).setService("aim");
				} else {
					appCodeVos.get(0).setService("fa");
				}

			} else if ("fipub".equals(service)) {
				// gl如果启用，则转发到gl
				if (InitGroupQuery.isEnabled(pk_group, "2002")) {
					appCodeVos.get(0).setService("gl");
				} else if (InitGroupQuery.isEnabled(pk_group, "2006")||InitGroupQuery.isEnabled(pk_group, "2008")){
					appCodeVos.get(0).setService("arap");
				}else{
					appCodeVos.get(0).setService("resa");
				}
			} else if ("tmpub".equals(service)) {
				//
				if (InitGroupQuery.isEnabled(pk_group, "3607")) {
					appCodeVos.get(0).setService("cmp");
				} else {
					appCodeVos.get(0).setService("cdmc");
				}
			} else if ("mapub".equals(service)) {// 3830是cm的模块，mapub是cm和resa共用
				// cm如果启用，则转发到cm
				if (InitGroupQuery.isEnabled(pk_group, "3830")) {
					appCodeVos.get(0).setService("cm");
				} else {
					appCodeVos.get(0).setService("resa");
				}

			}
			service = appCodeVos.get(0).getService();
		}

		return service;
	}

	private BaseDAO getBaseDAO() {
		if (this.dao == null) {
			this.dao = new BaseDAO();
		}
		return this.dao;
	}

	/**
	 *
	 * @param appCode
	 * @return
	 */
	private List<AppCodeToServiceVO> queryServiceByAppCode(String appCode) throws DAOException {
		// 查询源应用编码
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(
				"select appres.code as code,sys.devmodule as service from sm_appregister appres inner join DAP_DAPSYSTEM sys on sys.moduleid = appres.own_module  and scopeid is null "
						+ (!StringUtils.isEmpty(appCode) ? " and appres.code = '" + appCode + "' " : " "));
		sqlBuilder.append(" UNION all ");
		sqlBuilder.append("select code as code,scopeid as service from sm_appregister where scopeid is not  NULL "
				+ (!StringUtils.isEmpty(appCode) ? " and code = '" + appCode + "' " : " "));
		sqlBuilder.append(" order by code DESC;");

		List<AppCodeToServiceVO> vos = (List<AppCodeToServiceVO>) this.getBaseDAO().executeQuery(sqlBuilder.toString(),
				new BeanListProcessor(AppCodeToServiceVO.class));
		return vos;
	}

}
