package com.palmnest.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.palmnest.application.CodeConstants;
import com.palmnest.application.ElementConstant;
import com.palmnest.application.GoodsCodeConstants;
import com.palmnest.application.core.util.HttpKit;
import com.palmnest.application.core.util.PropertiesKit;
import com.palmnest.application.dto.IndexMenuDto;
import com.palmnest.application.dto.MenuDto;
import com.palmnest.application.dto.output.RepairSumIndexDto;
import com.palmnest.model.*;
import com.palmnest.model.estateserve.ServiceProjectAllocation;
import com.palmnest.model.estateserve.ServiceTypeInfo;
import com.palmnest.service.*;
import com.palmnest.service.dorm.manager.DormCheckOutManager;
import com.palmnest.service.estateserve.EmigrationManager;
import com.palmnest.service.estateserve.EnterpriseManager;
import com.palmnest.service.estateserve.ServeConfigManager;
import com.palmnest.service.estateserve.release.GoodsReleaseService;
import com.palmnest.service.notice.AppNoticeManager;
import com.palmnest.service.notice.EmailSendManager;
import com.palmnest.service.notice.PhoneMessageManager;
import com.palmnest.service.notice.WxtemplateMessageManager;
import com.palmnest.service.repair.RepairBillService;
import com.palmnest.service.washcar.manager.WashCarOrderManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service("appMenuService")
public class AppMenuServiceImpl implements AppMenuService {


	private       String IMAGE_SERVER    = PropertiesKit.readPropertiesValue("image_server", "wechat.properties");
	public static String wjx_service_url = PropertiesKit.readPropertiesValue("wjx_service_url", "wechat.properties");

	@Autowired
	private RoleManager              roleManager;
	@Autowired
	private AppMenuManager           appMenuManager;
	@Autowired
	private UserManager              userManager;
	@Autowired
	private AppFunctionManager       appFunctionManager;
	@Autowired
	private RepairBillService        repairBillService;
	@Autowired
	private AppNoticeManager         appNoticeManager;
	@Autowired
	private PhoneMessageManager      phoneMessageManager;
	@Autowired
	private EmailSendManager         emailSendManager;
	@Autowired
	private WxtemplateMessageManager wxtemplateMessageManager;
	@Autowired
	private EmigrationManager        emigrationManager;
	@Autowired
	private ElementGroupManager      elementGroupManager;
	@Autowired
	private GoodsReleaseService      goodsReleaseService;
	@Autowired
	private EnterpriseManager        enterpriseManager;
	@Autowired
	private ServeConfigManager       serveConfigManager;
	@Autowired
	private DormCheckOutManager      dormCheckOutManager;
	@Autowired
	private WashCarOrderManager      washCarOrderManager;


	@Override
	public List<MenuDto> findMenuDto(String userName) {
		//获取当前用户角色集合
		User      user       = userManager.getUserByUsername2(userName);
		Set<Role> allRoleSet = user.getRoles();
		Set<Role> roleSet    = new HashSet<Role>();   //可用的角色
		for (Role role : allRoleSet) {
			if (ElementConstant.YESORNO_1.equals(role.getIsenalbed())) {
				roleSet.add(role);
			}
		}
		//用户菜单集合
		Set<AppMenu> parentMenus = new HashSet<AppMenu>();
		Set<AppMenu> usermenus   = new HashSet<AppMenu>();
		if (roleSet != null && roleSet.size() > 0) {
			//获取系统菜单集合(所有)
			List<AppMenu> menus = appMenuManager.getAllAppMenu();
			//用户功能权限集合
			Set<AppFunction> userfuncs = new HashSet<AppFunction>();
			for (Role role : roleSet) {
				List<RoleFunction> funcSet = roleManager.queryRoleFunctionSet(role);
				//Set<RoleFunction> funcSet=role.getRoleFunctions();
				for (RoleFunction roleFunction : funcSet) {
					userfuncs.add(roleFunction.getFunction());
				}
			}

			if (userfuncs.size() > 0) {
				//遍历系统菜单
				for (AppMenu appMenu : menus) {
					if (appMenu.getAppfunction() != null) {
						int functiondId = appMenu.getAppfunction().getId();
						for (AppFunction appFunction : userfuncs) {
							if (functiondId == appFunction.getId()) {
								appFunction = appFunctionManager.getById(appFunction.getId());
								if (appFunction.getIsApp().intValue() == 0) {
									break;
								}
								//父级菜单
								if (appMenu.getParentCode().equals("0")) {
									parentMenus.add(appMenu);
								} else {
									usermenus.add(appMenu);
								}
								break;
							}
						}
					}
				}
			}
		}
		List<MenuDto> dtos = new ArrayList<MenuDto>();
		for (AppMenu appMenu : parentMenus) {

			MenuDto dto = new MenuDto(appMenu.getId(), appMenu.getMenuName(), appMenu.getParentCode(), appMenu.getDescription(),
					appMenu.getOrdernum());
			dto.setIconUrl(IMAGE_SERVER + appMenu.getIconUrl());
			dto.setMenuType(appMenu.getMenuCode());
			Set<MenuDto> childDtos = new HashSet<MenuDto>();
			for (AppMenu menu : usermenus) {
				if (new Integer(menu.getParentCode()).equals(appMenu.getId())) {
					MenuDto chDto = new MenuDto(menu.getId(), menu.getMenuName(), menu.getParentCode(), menu.getDescription(),
							menu.getOrdernum());
					chDto.setIconUrl(IMAGE_SERVER + appMenu.getIconUrl());
					chDto.setMenuCode(menu.getMenuCode());
					childDtos.add(chDto);
				}
			}
			List<MenuDto> childs = new ArrayList<MenuDto>(childDtos);
			Collections.sort(childs, new Comparator<MenuDto>() {

				@Override
				public int compare(MenuDto o1, MenuDto o2) {
					if (o1.getOrdernum() == null || o2.getOrdernum() == null) {
						return 0;
					}
					int i = o1.getOrdernum() - o2.getOrdernum();
					if (i == 0) {
						return o1.getOrdernum() - o2.getOrdernum();
					}
					return i;
				}
			});
			dto.setChildrenMenus(childs);
			dtos.add(dto);
		}
		Collections.sort(dtos, new Comparator<MenuDto>() {

			@Override
			public int compare(MenuDto o1, MenuDto o2) {
				if (o1.getOrdernum() == null || o2.getOrdernum() == null) {
					return 0;
				}
				int i = o1.getOrdernum() - o2.getOrdernum();
				if (i == 0) {
					return o1.getOrdernum() - o2.getOrdernum();
				}
				return i;
			}
		});
		return dtos;
	}

	@Override
	public List<IndexMenuDto> findIndexMenus(String userName, boolean isAll, String orgIds, String token) {
		//获取当前用户一级菜单集合
		Set<AppMenu>       parentMenus = getMenus(userName, true, null);
		List<IndexMenuDto> dtos        = new ArrayList<IndexMenuDto>();//非灰度菜单

		List<IndexMenuDto> grayDtos = new ArrayList<IndexMenuDto>();//灰度菜单
		for (AppMenu appMenu : parentMenus) {
			IndexMenuDto dto = new IndexMenuDto(appMenu.getId(), appMenu.getMenuName(), appMenu.getOrdernum(), appMenu.getMenuCode());
			dto.setIconUrl(IMAGE_SERVER + appMenu.getIconUrl());
			dto.setIsGray(0);
			dto.setMsgCount(0);
			//设置首页消息总数
			//公告通知
			if (CodeConstants.APP_MENU_NOTICE.equals(appMenu.getMenuCode())) {
				Integer appNoticeTotal    = appNoticeManager.getTotal(orgIds);
				Integer wxMessageTotal    = wxtemplateMessageManager.getTotal(orgIds);
				Integer phoneMessageTotal = phoneMessageManager.getTotal(orgIds);
				Integer emailMessageTotal = emailSendManager.getTotal(orgIds);
				int     noticeCount       = 0;
				if (appNoticeTotal != null) {
					noticeCount += appNoticeTotal;
				}
				if (wxMessageTotal != null) {
					noticeCount += wxMessageTotal;
				}
				if (phoneMessageTotal != null) {
					noticeCount += phoneMessageTotal;
				}
				if (emailMessageTotal != null) {
					noticeCount += emailMessageTotal;
				}
				dto.setMsgCount(noticeCount);
			}


			//工程维修
			if (CodeConstants.APP_MENU_REPAIR.equals(appMenu.getMenuCode())) {
				RepairSumIndexDto repairSum = repairBillService.findBy(userName);
				log.info("findIndexMenus------------repairSum:" + JSON.toJSONString(repairSum).toString());
				int repairCount = 0;
//				if(repairSum.getInProgressTotal() != null) {
//					repairCount += repairSum.getInProgressTotal();
//				}
//				if(repairSum.getIsSingleTotal() != null) {
//					repairCount += repairSum.getIsSingleTotal();
//				}
//				if(repairSum.getPendingTotal() != null) {
//					repairCount += repairSum.getPendingTotal();
//				}
				if (repairSum.getManageTotal() != null) {
					repairCount += repairSum.getManageTotal();
				}
				dto.setMsgCount(repairCount);
				log.info("findIndexMenus------------repairCount:" + repairCount);
			}


			User user = userManager.getUserByUsername2(userName);
//			EmigSumIndexDto emigrationIndex = emigrationManager.findIndex(user.getId()+"",orgIds);
			//企业入驻 state 1-入驻 2-迁出
			if (CodeConstants.APP_MENU_ENTERPRISES.equals(appMenu.getMenuCode())) {
				int count = enterpriseManager.getEnterprisePendingNum(1, orgIds);
				dto.setMsgCount(count);
//				dto.setMsgCount(emigrationIndex.getEnterIndex());
			}


			/*if(CodeConstants.APP_MENU_ENTERPRISES_OUT.equals(appMenu.getMenuCode())) {
				dto.setMsgCount(emigrationIndex.getEmigIndex());
			}*/
			//企业迁出
			if (CodeConstants.APP_MENU_ENTERPRISES_OUT.equals(appMenu.getMenuCode())) {
				int count = enterpriseManager.getEnterprisePendingNum(2, orgIds);
				dto.setMsgCount(count);
//				Map<String, Object> params=new HashMap<String, Object>();

//				//放行类型
//				String releaseType="279";
//				List<Element> releaseTypeGroup = elementGroupManager.getElementsByGroupName("releaseTypeGroup");
//				for(Element e : releaseTypeGroup) {
//					if(StringUtils.isNotEmpty(e.getItemcode()) && GoodsCodeConstants.RELEASETYPE_J.equals(e.getItemcode())){
//						releaseType=e.getId().toString();
//					}
//				}
//				
//				params.put("releaseType", releaseType);//用数据字典 家电类
//				params.put("companyHeadUser", "companyHeadUser");//企业审批过的
//				params.put("releaseStatus", GoodsCodeConstants.ST_AUDITING);//状态：审批中
//				params.put("orgIds", orgIds);//项目id
//				Integer num=goodsReleaseService.findCountBy(params);
//				dto.setMsgCount(emigrationIndex.getEmigIndex());
			}

			//电子放行条
			if (CodeConstants.APP_MENU_OBJECT_RELEASE.equals(appMenu.getMenuCode())) {
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("releaseStatus", GoodsCodeConstants.ST_AUDITING);//状态：审批中
				params.put("orgIds", orgIds);//项目id
				Integer num = goodsReleaseService.findCountBy(params);
				dto.setMsgCount(num);
			}

			//宿管
			if (CodeConstants.APP_MENU_DORM_MANAGE.equals(appMenu.getMenuCode())) {
				//待审核1
				Integer num = dormCheckOutManager.getDormCheckOutIndex("1", orgIds);
				dto.setMsgCount(num);
			}

			//投诉表扬
			if (CodeConstants.APP_MENU_TSBY.equals(appMenu.getMenuCode())) {

				Integer commentAmount   = 0;
				boolean isHaveTsbyReply = getUserIsHaveTsbyReplyByAppUserId(user.getId());
				if (isHaveTsbyReply) {
					try {
						commentAmount = getWjxCommentAmount(token);
					} catch (Exception e) {
						log.info("投诉表扬异常 :" + e.getMessage());
					}

				}
				dto.setMsgCount(commentAmount);
			}


			try {
				//洗车
				if (CodeConstants.APP_MENU_CARWASH.equals(appMenu.getMenuCode())) {
					//获取洗车人当前用户的未取钥匙的单据(洗车小哥)
					//获取排队中的以及洗车人员是该用户的未取钥匙的单据数量(管理员)
					String res    = washCarOrderManager.isPower(CodeConstants.CARWASH_SERVICE, user.getId().toString());
					String manRes = washCarOrderManager.isPower(CodeConstants.CARWASH_MANAGER, user.getId().toString());
					String userId = "";
					if (user != null) {
						userId = user.getId().toString();
					}
					int count = 0;
					if ("HAVE".equals(res)) {//洗车权限
						if ("HAVE".equals(manRes)) {//洗车管理权限
							count = count + washCarOrderManager.getWashByUserId(userId, orgIds);
							count = count + washCarOrderManager.getWashByManager(orgIds);
						} else {
							count = count + washCarOrderManager.getWashByUserId(userId, orgIds);
						}
					}
					dto.setMsgCount(count);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}


			//灰度相关设置
			//根据功能去查询服务信息
			ServiceTypeInfo serviceTypeInfo = serveConfigManager.getServiceTypeInfoByFuncId(appMenu.getAppfunction().getId() + "");
			//根据服务id和项目id查询项目配置的服务信息
			if (serviceTypeInfo != null) {
				ServiceProjectAllocation serviceOrgInfo = serveConfigManager.getServiceOrgInfo(serviceTypeInfo.getId(), orgIds);
				if (serviceOrgInfo != null && "2".equals(serviceOrgInfo.getStatus())) {
					//如果是灰度
					dto.setIconUrl(IMAGE_SERVER + serviceTypeInfo.getGrayAppServiceIcon());//灰度图标
					dto.setIsGray(1);//灰度
					dto.setGrayUrl(serviceTypeInfo.getGrayUrl());//灰度跳转链接

					grayDtos.add(dto);
				} else {
					dtos.add(dto);
				}
			}
		}

		Collections.sort(dtos, new Comparator<IndexMenuDto>() {

			@Override
			public int compare(IndexMenuDto o1, IndexMenuDto o2) {
				if (o1.getOrdernum() == null || o2.getOrdernum() == null) {
					return 0;
				}
				int i = o1.getOrdernum() - o2.getOrdernum();
				if (i == 0) {
					return o1.getOrdernum() - o2.getOrdernum();
				}
				return i;
			}
		});

		Collections.sort(grayDtos, new Comparator<IndexMenuDto>() {

			@Override
			public int compare(IndexMenuDto o1, IndexMenuDto o2) {
				if (o1.getOrdernum() == null || o2.getOrdernum() == null) {
					return 0;
				}
				int i = o1.getOrdernum() - o2.getOrdernum();
				if (i == 0) {
					return o1.getOrdernum() - o2.getOrdernum();
				}
				return i;
			}
		});

		dtos.addAll(grayDtos);

		//如果不是查询所有
		if (!isAll && dtos.size() > 8) {
			dtos = dtos.subList(0, 7);
			IndexMenuDto dto = new IndexMenuDto(null, "全部", 1000, "APP_MENU_ALL");
			dto.setIconUrl(IMAGE_SERVER + "/app/image/super/index_all.png");
			dto.setIsGray(0);
			dtos.add(dto);
		}

		return dtos;
	}


	private boolean getUserIsHaveTsbyReplyByAppUserId(Long appId) {

		boolean isHaveTsbyReply = false;
		User    userTsbyaudit   = userManager.findByFunctionAndUser(CodeConstants.TSBYAUDIT_PERMISSION, appId);
		if (null != userTsbyaudit) {
			isHaveTsbyReply = true;
		} else {
			isHaveTsbyReply = false;
		}

		return isHaveTsbyReply;

	}

	private Integer getWjxCommentAmount(String token) {

		Integer             commentAmount      = 0;
		Map<String, String> oauthLoginResponse = null;
		String              retMsg             = "";
		JSONObject          json               = new JSONObject();
		json.put("client", "");
		String jsonMapStr = JSONObject.toJSONString(json);
		//String token ="FD5A0AEC737CF9B850A8766CE0CFAF8FA12D9774BAA09447";
		retMsg = HttpKit.HttpClientPost(jsonMapStr, token, wjx_service_url + "/web/comment/getCommentAmount");
		log.info("getWjxCommentAmount retMsg:" + retMsg);

		oauthLoginResponse = JSONObject.parseObject(retMsg, new TypeReference<Map<String, String>>() {
		});

		if (oauthLoginResponse.get("code").equals("200")) {

			String dateStr = oauthLoginResponse.get("data");

			Map<String, Integer> dataOj = JSONObject.parseObject(dateStr, new TypeReference<Map<String, Integer>>() {
			});
			commentAmount = dataOj.get("commentAmount");

		}
		return commentAmount;


	}

	/**
	 * 获取用户权限下一级菜单
	 *
	 * @param userName
	 * @return
	 */
	private Set<AppMenu> getMenus(String userName, boolean isParent, String parentCode) {
		User      user       = userManager.getUserByUsername2(userName);
		Set<Role> allRoleSet = user.getRoles();
		Set<Role> roleSet    = new HashSet<Role>();   //可用的角色
		for (Role role : allRoleSet) {
			if (ElementConstant.YESORNO_1.equals(role.getIsenalbed())) {
				roleSet.add(role);
			}
		}
		//用户菜单集合
		Set<AppMenu> parentMenus = new HashSet<AppMenu>();
		Set<AppMenu> usermenus   = new HashSet<AppMenu>();
		if (roleSet != null && roleSet.size() > 0) {
			//获取系统菜单集合(所有)
			List<AppMenu> menus = appMenuManager.getAllAppMenu();
			//用户功能权限集合
			Set<AppFunction> userfuncs = new HashSet<AppFunction>();
			for (Role role : roleSet) {
				List<RoleFunction> funcSet = roleManager.queryRoleFunctionSet(role);
				for (RoleFunction roleFunction : funcSet) {
					userfuncs.add(roleFunction.getFunction());
				}
			}

			if (userfuncs.size() > 0) {
				//遍历系统菜单
				for (AppMenu appMenu : menus) {
					if (appMenu.getAppfunction() != null) {
						int functiondId = appMenu.getAppfunction().getId();
						for (AppFunction appFunction : userfuncs) {
							if (functiondId == appFunction.getId()) {
								appFunction = appFunctionManager.getById(appFunction.getId());
								if (appFunction.getIsApp().intValue() == 0) {
									break;
								}
								//父级菜单
								if (appMenu.getParentCode().equals("0")) {
									parentMenus.add(appMenu);

								} else if (!isParent && parentCode != null && appMenu.getParentCode().equals(parentCode)) {
									usermenus.add(appMenu);
								}
								break;
							}
						}
					}
				}
			}
		}
		if (isParent) {
			return parentMenus;
		} else {
			return usermenus;
		}
	}

	@Override
	public List<IndexMenuDto> findMenusByParent(Integer parentId, User user, String orgIds, String token) throws Exception {
		//获取全部
		if (parentId == null) {
			return this.findCategoryMenu(user, orgIds, token);
		}
		//获取当前用户一级菜单集合
		Set<AppMenu>       userMenus = getMenus(user.getUsername(), false, parentId + "");
		List<IndexMenuDto> dtos      = new ArrayList<IndexMenuDto>();
		//此处判断用户是否具有企业迁出处理权限,如果有才加载子菜单
		String isCustService = enterpriseManager.isHavePower(user.getId() + "", Integer.parseInt(CodeConstants.EMIGAPPROVE_PERMISSION));
		for (AppMenu appMenu : userMenus) {
			if ("500".equals(appMenu.getMenuCode()) && !"1".equals(isCustService)) {
				continue;
			}
			IndexMenuDto dto = new IndexMenuDto(appMenu.getId(), appMenu.getMenuName(), appMenu.getOrdernum(), appMenu.getMenuCode());
			dto.setIconUrl(IMAGE_SERVER + appMenu.getIconUrl());
			dto.setIsGray(0);

			dtos.add(dto);
		}
		Collections.sort(dtos, new Comparator<IndexMenuDto>() {

			@Override
			public int compare(IndexMenuDto o1, IndexMenuDto o2) {
				if (o1.getOrdernum() == null || o2.getOrdernum() == null) {
					return 0;
				}
				int i = o1.getOrdernum() - o2.getOrdernum();
				if (i == 0) {
					return o1.getOrdernum() - o2.getOrdernum();
				}
				return i;
			}
		});

		return dtos;
	}

	@Override
	public List<IndexMenuDto> findCategoryMenu(User user, String orgIds, String token) throws Exception {

		List<IndexMenuDto> finalList = new ArrayList<IndexMenuDto>();//父级菜单

		List<Element> categoryList = elementGroupManager.getElementsByGroupName("serviceCategory");//服务分类列表
		Set<AppMenu>  parentMenus  = getMenus(user.getUsername(), true, null);//获取当前用户的一级目录


		for (Element element : categoryList) {
			IndexMenuDto pdto = new IndexMenuDto();
			pdto.setMenuName(element.getItemname());
			pdto.setMenuCode(element.getItemcode());

			List<IndexMenuDto> list = new ArrayList<IndexMenuDto>();
			for (AppMenu appMenu : parentMenus) {

				//根据功能去查询服务信息
				ServiceTypeInfo serviceTypeInfo = serveConfigManager.getServiceTypeInfoByFuncId(appMenu.getAppfunction().getId() + "");

				if (serviceTypeInfo != null) {
					//根据服务id和项目id查询项目配置的服务信息
					ServiceProjectAllocation serviceOrgInfo = serveConfigManager.getServiceOrgInfo(serviceTypeInfo.getId(), orgIds);
					IndexMenuDto             dto            = new IndexMenuDto(appMenu.getId(), appMenu.getMenuName(), appMenu.getOrdernum(), appMenu.getMenuCode());
					dto.setIconUrl(IMAGE_SERVER + appMenu.getIconUrl());
					dto.setIsGray(0);
					dto.setMsgCount(0);
					if (serviceOrgInfo != null) {
						if ("2".equals(serviceOrgInfo.getStatus())) {//如果是灰度
							dto.setIconUrl(IMAGE_SERVER + serviceTypeInfo.getGrayAppServiceIcon());//灰度图标
							dto.setIsGray(1);//灰度
							dto.setGrayUrl(serviceTypeInfo.getGrayUrl());//灰度跳转链接
						}
					}
					String serviceCategory = serviceTypeInfo.getServiceCategory();
					if (StringUtils.isNotBlank(serviceCategory) && serviceCategory.equals(element.getItemcode())) {

						//设置首页消息总数
						//公告通知
						if (CodeConstants.APP_MENU_NOTICE.equals(appMenu.getMenuCode())) {
							Integer appNoticeTotal    = appNoticeManager.getTotal(orgIds);
							Integer wxMessageTotal    = wxtemplateMessageManager.getTotal(orgIds);
							Integer phoneMessageTotal = phoneMessageManager.getTotal(orgIds);
							Integer emailMessageTotal = emailSendManager.getTotal(orgIds);
							int     noticeCount       = 0;
							if (appNoticeTotal != null) {
								noticeCount += appNoticeTotal;
							}
							if (wxMessageTotal != null) {
								noticeCount += wxMessageTotal;
							}
							if (phoneMessageTotal != null) {
								noticeCount += phoneMessageTotal;
							}
							if (emailMessageTotal != null) {
								noticeCount += emailMessageTotal;
							}
							dto.setMsgCount(noticeCount);
						}


						//工程维修
						if (CodeConstants.APP_MENU_REPAIR.equals(appMenu.getMenuCode())) {
							RepairSumIndexDto repairSum   = repairBillService.findBy(user.getUsername());
							int               repairCount = 0;
//							if(repairSum.getInProgressTotal() != null) {
//								repairCount += repairSum.getInProgressTotal();
//							}
//							if(repairSum.getIsSingleTotal() != null) {
//								repairCount += repairSum.getIsSingleTotal();
//							}
//							if(repairSum.getPendingTotal() != null) {
//								repairCount += repairSum.getPendingTotal();
//							}
							if (repairSum.getManageTotal() != null) {
								repairCount += repairSum.getManageTotal();
							}
							dto.setMsgCount(repairCount);
						}

//						EmigSumIndexDto emigrationIndex = emigrationManager.findIndex(user.getId()+"",orgIds);

						//企业入驻  state-状态（1企业入驻2企业迁出）
						if (CodeConstants.APP_MENU_ENTERPRISES.equals(appMenu.getMenuCode())) {
							int count = enterpriseManager.getEnterprisePendingNum(1, orgIds);
							dto.setMsgCount(count);
						}

						//企业迁出
						if (CodeConstants.APP_MENU_ENTERPRISES_OUT.equals(appMenu.getMenuCode())) {
							int count = enterpriseManager.getEnterprisePendingNum(2, orgIds);
							dto.setMsgCount(count);
						}

						//宿管
						if (CodeConstants.APP_MENU_DORM_MANAGE.equals(appMenu.getMenuCode())) {
							//待审核1
							Integer num = dormCheckOutManager.getDormCheckOutIndex("1", orgIds);
							dto.setMsgCount(num);
						}

						//投诉表扬
						if (CodeConstants.APP_MENU_TSBY.equals(appMenu.getMenuCode())) {

							Integer commentAmount   = 0;
							boolean isHaveTsbyReply = getUserIsHaveTsbyReplyByAppUserId(user.getId());
							if (isHaveTsbyReply) {
								try {
									commentAmount = getWjxCommentAmount(token);
								} catch (Exception e) {
									log.info("投诉表扬异常 :" + e.getMessage());
								}

							}
							dto.setMsgCount(commentAmount);
						}

						//洗车
						if (CodeConstants.APP_MENU_CARWASH.equals(appMenu.getMenuCode())) {
							//获取洗车人当前用户的未取钥匙的单据(洗车小哥)
							//获取排队中的以及洗车人员是该用户的未取钥匙的单据数量(管理员)
							String res    = washCarOrderManager.isPower(CodeConstants.CARWASH_SERVICE, user.getId().toString());
							String manRes = washCarOrderManager.isPower(CodeConstants.CARWASH_MANAGER, user.getId().toString());
							String userId = "";
							if (user != null) {
								userId = user.getId().toString();
							}
							int count = 0;
							if ("HAVE".equals(res)) {//洗车权限
								if ("HAVE".equals(manRes)) {//洗车管理权限
									count = count + washCarOrderManager.getWashByUserId(userId, orgIds);
									count = count + washCarOrderManager.getWashByManager(orgIds);
								} else {
									count = count + washCarOrderManager.getWashByUserId(userId, orgIds);
								}
							}
							dto.setMsgCount(count);
						}

						//电子放行条
						if (CodeConstants.APP_MENU_OBJECT_RELEASE.equals(appMenu.getMenuCode())) {
							Map<String, Object> params = new HashMap<String, Object>();
							params.put("releaseStatus", GoodsCodeConstants.ST_AUDITING);//状态：审批中
							params.put("orgIds", orgIds);//项目id
							Integer num = goodsReleaseService.findCountBy(params);
							dto.setMsgCount(num);
						}

						list.add(dto);

					}
				}

			}

			//根据ordernum排序
			Collections.sort(list, new Comparator<IndexMenuDto>() {

				@Override
				public int compare(IndexMenuDto o1, IndexMenuDto o2) {
					if (o1.getOrdernum() == null || o2.getOrdernum() == null) {
						return 0;
					}
					int i = o1.getOrdernum() - o2.getOrdernum();
					if (i == 0) {
						return o1.getOrdernum() - o2.getOrdernum();
					}
					return i;
				}
			});

			pdto.setChildList(list);
			if (list != null && list.size() > 0) {//如果分类集合不为空则进行添加
				finalList.add(pdto);
			}
		}


		return finalList;
	}

}
