package com.palmnest.service.estateserve.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.palmnest.application.dto.OutTreeDTO;
import com.palmnest.model.AppFunction;
import com.palmnest.model.AppMenu;
import com.palmnest.model.estateserve.ServiceFunctionRelation;
import com.palmnest.model.estateserve.ServiceTypeInfo;
import com.palmnest.service.AppFunctionManager;
import com.palmnest.service.AppMenuManager;
import com.palmnest.service.estateserve.ServeConfigManager;
import com.palmnest.service.estateserve.ServeConfigService;
import com.palmnest.service.impl.GenericManagerImpl;

/**
 * @Author HXT
 * @Description TODO
 * @Date 2018/8/13 19:48
 **/
@Service("serveConfigService")
public class ServeConfigServiceImpl extends GenericManagerImpl<ServiceFunctionRelation, Long> implements ServeConfigService {

	@Autowired
	private AppFunctionManager functionManager;
	@Autowired
	private AppMenuManager     appMenuManager;
	@Autowired
	private ServeConfigManager serveConfigManager;
	@Autowired
	private AppFunctionManager appFunctionManager;

	@Override
	public void saveUpdate(Integer serverId, Integer port, String[] funcIds) {
		serveConfigManager.saveUpdate(serverId, port, funcIds);
	}

	@Override
	public List<OutTreeDTO> getFunctionTreeById(Integer serverId, Integer port) {
		List<OutTreeDTO> funcTrees = new ArrayList<OutTreeDTO>();//功能树
		//根据端口获取功能列表
		List<AppFunction> funcList = functionManager.getAll(port);
		//功能服务对应关系
		List<ServiceFunctionRelation> serList = serveConfigManager.getFunctionById(serverId, port);

		//初始化功能树
		for (AppFunction appFunction : funcList) {
			OutTreeDTO obj = new OutTreeDTO();
			obj.setId(appFunction.getId().toString());
			obj.setName(appFunction.getFunctionName());
			obj.setpId(appFunction.getParentcode());
			obj.setOperateStr(appFunction.getUrl());

			for (ServiceFunctionRelation relation : serList) {
				if (appFunction.getId() == relation.getFunctionId().intValue()) {
					obj.setChecked(true);
				}
			}
			funcTrees.add(obj);
		}

		return funcTrees;
	}

	@Override
	public List<OutTreeDTO> getMenuTreeById(String funIds) {

		List<OutTreeDTO> menuTrees = new ArrayList<OutTreeDTO>();//菜单树
		List<AppMenu> menuList = appMenuManager.getAll();//所有菜单列表
		List<AppMenu> menus = getMenuByFunid(funIds);//指定功能对应的mean列表

		//初始化菜单树
		for (AppMenu menu : menuList) {

			OutTreeDTO obj = new OutTreeDTO();

			obj.setId(menu.getTreeCode());      //当前节点code
			obj.setName(menu.getMenuName());    //节点名称
			obj.setpId(menu.getParentCode());   //父节点code

			if (menus != null && menus.size() > 0) {
				for (AppMenu m : menus) {
					if (menu.getId().intValue() == m.getId().intValue()) {
						obj.setChecked(true);
					}
				}
			}

			menuTrees.add(obj);
		}
		return menuTrees;
	}

	/**
	 * 根据功能id查询菜单列表
	 *
	 * @param funIds
	 * @return
	 */
	@Override
	public List<AppMenu> getMenuByFunid(String funIds) {
		List<AppMenu> list = null;
		if (StringUtils.isNotBlank(funIds)) {
			//如果结尾有逗号 则去掉
			if (funIds.endsWith(",")) {
				funIds = funIds.substring(0, funIds.length() - 1);
			}
			list = appMenuManager.getMenuByFunid(funIds);
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getServiceList() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<ServiceTypeInfo> serveList = serveConfigManager.getServiceList();

		//添加基础服务包,id设为-1
		Map<String, Object> basicService = new HashMap<String, Object>();
		basicService.put("id", -1);
		basicService.put("name", "基础服务");
		list.add(basicService);

		for (ServiceTypeInfo info : serveList) {
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("id", info.getId());
			m.put("name", info.getServiceName());

			list.add(m);
		}
		return list;
	}

	/**
	 * 获取基础服务对应的功能列表
	 *
	 * @return
	 */
	@Override
	public List<AppFunction> getServiceFunctionList(Integer serverId, Integer port) {
		List<AppFunction> functions = new ArrayList<AppFunction>();
		//查询基础功能服务对应关系 基础服务id为-1
		List<ServiceFunctionRelation> serList = serveConfigManager.getFunctionById(serverId, port);
		for (ServiceFunctionRelation ser : serList) {
			functions.add(appFunctionManager.getById((ser.getFunctionId())));
		}
		return functions;
	}

	/**
	 * 根据项目id查询对应功能列表
	 *
	 * @param projectId 项目id
	 * @param port      哪个端 0-PC 1-APP null-全部端
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getProjectFunctionList(Integer projectId, Integer port) {

		//查询基础服务功能 -1:基础服务 null:全部端查询
		List<ServiceFunctionRelation> list = serveConfigManager.getFunctionById(-1, port);
		//查询当前项目拥有的已上线的服务所对应的功能集合
		List<Map<String, Object>> serviceList = serveConfigManager.getFunctionListByProjectId(projectId);

		//查询基础服务对应服务列表
		for (ServiceFunctionRelation relation : list) {
			Integer funID = relation.getFunctionId();
			AppFunction appFunction = appFunctionManager.getById(funID);

			if (appFunction != null) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", funID);
				map.put("function_name", appFunction.getFunctionName());
				serviceList.add(map);
			}
		}

		return serviceList;
	}
}
