package com.product.admin.service; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Maps; import com.product.admin.config.CmnConst; import com.product.admin.config.SystemCode; import com.product.admin.entity.*; import com.product.admin.service.idel.ISystemMenusService; import com.product.common.lang.DateUtils; import com.product.common.lang.StringUtils; import com.product.core.cache.util.RedisUtil; import com.product.core.config.CoreConst; import com.product.core.dao.BaseDao; import com.product.core.entity.DataTableEntity; import com.product.core.entity.FieldSetEntity; import com.product.core.exception.BaseException; import com.product.core.service.support.AbstractBaseService; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.core.transfer.Transactional; import com.product.core.util.BeanRefUtil; import com.product.core.util.JsonUtil; import com.product.module.sys.entity.SystemUser; import com.product.util.BaseUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; /** * Copyright LX-BASE * * @Title: SystemMenusService * @Project: LX-BASE-SERVER * @Date: 2020-06-09 14:20 * @Author: ZhouJie * @Description: 菜单管理service层 */ @Service public class SystemMenusService extends AbstractBaseService implements ISystemMenusService { @Autowired public BaseDao baseDao; @Autowired public RolesService rolesService; @Autowired CodeService codeService; @Autowired UpdateLoginUserInfoService infoService; @Autowired public RouterService routerService; @Autowired UpdateLoginUserInfoService updateLoginUserInfoService; private static final String MENU_CACHE_KEY = "system-cache:menus"; public void refreshOnlineUserMenu(String function_uuid) { if (!StringUtils.isEmpty(function_uuid)) { //刷新菜单 String sql = " select GROUP_CONCAT(role_uuid) role_uuid from product_sys_function_permission " + " where function_uuid =? "; FieldSetEntity fsro = baseDao.getFieldSetEntityBySQL(sql, new Object[]{function_uuid}, false); if (fsro != null) { String role_uuid = fsro.getString("role_uuid"); if (!StringUtils.isEmpty(role_uuid)) { infoService.updateUserInfoByUpdateRole(role_uuid.split(","), true); } } } } public void initSystemMenu() { initSystemMenu(null, null); } /** * 初始化菜单缓存单条数据 */ public void refreshMenu(String uuid) { initSystemMenu(null, new String[]{uuid}); } /** * 初始化菜单缓存整体 */ public void refreshMenu() { initSystemMenu(); } public synchronized void refreshMenuByRole(String uuid) { routerService.updateRouter(new String[]{uuid}); initSystemMenu(new String[]{uuid}, null); } public synchronized void refreshMenuByRole(String[] uuid) { initSystemMenu(uuid, null); } /** * 初始化菜单缓存 * * @param role_uuid * @param menu_uuid */ private void initSystemMenu(String[] role_uuid, String[] menu_uuid) { StringBuilder sql = new StringBuilder(); String menuFilter = BaseUtil.buildQuestionMarkFilter("a.uuid", menu_uuid, true); //功能菜单 sql.append(" SELECT c.uuid,a.uuid AS menu_uuid,a.menu_icon, a.menu_name, a.tricode, a.tricode_parent, a.menu_color "); sql.append(" , a.is_show, a.sequence, b.uuid AS function_uuid, b.function_name "); sql.append(" , b.table_uuid, c.button_name, c.uuid AS button_uuid, c.button_title, c.route_name "); sql.append(" , e.page_url, e.page_name, e.page_type, NULL AS portal_uuid,b.function_type_uuid function_type "); sql.append(" FROM product_sys_menus a "); sql.append(" JOIN "); sql.append(" product_sys_functions b "); sql.append(" ON "); sql.append(" a.function_uuid = b.uuid AND a.is_show = 1 and b.status_uuid=1 "); if (menu_uuid != null && menu_uuid.length > 0) { sql.append(" AND "); sql.append(menuFilter); } sql.append(" JOIN "); sql.append(" ( "); sql.append(" select "); sql.append(" button.uuid , button_title,button_name,route_name,button.function_uuid "); sql.append(" from "); sql.append(" product_sys_function_buttons button "); sql.append(" join product_sys_function_permission permission on button.status_uuid=1 and is_main=1 and button.client_type_uuid='Web' and concat(',',permission.button_uuid,',') like concat(',%',button.uuid,'%,') "); String roleFilter = ""; if (role_uuid != null && role_uuid.length > 0) { roleFilter = BaseUtil.buildQuestionMarkFilter("role_uuid", role_uuid, true); sql.append(" AND "); sql.append(roleFilter); } sql.append(" GROUP BY button.uuid , button_title,button_name,route_name,button.function_uuid "); sql.append(" ) c "); sql.append(" ON "); sql.append(" c.function_uuid = b.uuid "); sql.append(" JOIN "); sql.append(" product_sys_link d "); sql.append(" ON "); sql.append(" d.line_from = c.uuid AND d.from_type = 0 "); sql.append(" JOIN "); sql.append(" product_sys_mvc_page e "); sql.append(" ON "); sql.append(" e.uuid = d.line_to "); if (role_uuid == null || role_uuid.length <= 0) { sql.append(" UNION ALL "); sql.append(" SELECT null,a.uuid AS menu_uuid, a.menu_icon, a.menu_name, a.tricode, a.tricode_parent, a.menu_color "); sql.append(" , a.is_show, a.sequence, NULL, NULL "); sql.append(" , NULL, NULL, NULL, NULL, NULL "); sql.append(" , NULL, NULL, NULL, f.uuid ,null"); sql.append(" FROM "); sql.append(" product_sys_menus a "); sql.append(" LEFT JOIN "); sql.append(" product_sys_portals f "); sql.append(" ON "); sql.append(" f.portal_menu = a.tricode "); sql.append(" WHERE function_uuid is null or function_uuid='' "); if (menu_uuid != null && menu_uuid.length > 0) { sql.append(" AND "); sql.append(menuFilter); } } DataTableEntity dt = baseDao.listTable(sql.toString(), new Object[]{}); if (!BaseUtil.dataTableIsEmpty(dt)) { sql.setLength(0); sql.append("SELECT button.uuid, role_uuid "); sql.append(" FROM product_sys_function_buttons button "); sql.append(" JOIN product_sys_function_permission permission ON is_main = 1 and button.client_type_uuid='Web' "); sql.append(" AND concat( ',', permission.button_uuid, ',' ) LIKE concat( ',%', button.uuid, '%,' ) "); if (!StringUtils.isEmpty(roleFilter)) { sql.append(" and ").append(roleFilter); } DataTableEntity roleTempDt = baseDao.listTable(sql.toString(), new Object[]{}); if (BaseUtil.dataTableIsEmpty(roleTempDt)) { return; } dt.getFieldSets().forEach((k, v) -> { List fieldSetEntity = roleTempDt.getFieldSetEntity(k); if (fieldSetEntity != null && fieldSetEntity.size() > 0) { String uuid = ""; for (FieldSetEntity setEntity : fieldSetEntity) { if (uuid.length() > 0) { uuid += ","; } uuid += setEntity.getString(CmnConst.ROLE_UUID); } for (FieldSetEntity setEntity : v) { setEntity.setValue(CmnConst.ROLE_UUID, uuid); } } }); } else { if (menu_uuid != null && menu_uuid.length > 0) { //删除菜单 MenuTreeEntity menuTreeEntity = getMenuCache(); menuTreeEntity.removeMenu(menu_uuid); setMenuCache(menuTreeEntity); } return; } MenuTreeEntity menuTreeEntity; if ((role_uuid != null && role_uuid.length > 0) || (menu_uuid != null && menu_uuid.length > 0)) { menuTreeEntity = getMenuCache(); menuTreeEntity.refreshMenus(dt, role_uuid, menu_uuid); } else { // 删除菜单刷新所有 menuTreeEntity = new MenuTreeEntity(dt); } setMenuCache(menuTreeEntity); } /** * 装载菜单缓存 * * @param menuTreeEntity */ public void setMenuCache(MenuTreeEntity menuTreeEntity) { if (menuTreeEntity == null && RedisUtil.exists(MENU_CACHE_KEY)) { RedisUtil.del(MENU_CACHE_KEY); } else if (menuTreeEntity != null) { RedisUtil.set(MENU_CACHE_KEY, menuTreeEntity); } } /** * 获取菜单缓存 * * @return */ public MenuTreeEntity getMenuCache() { if (RedisUtil.exists(MENU_CACHE_KEY)) { return (MenuTreeEntity) RedisUtil.get(MENU_CACHE_KEY); } return null; } public DataTableEntity getMenuFunction(FieldSetEntity fse) { String tricodeParent = fse.getString(CmnConst.TRICODE_PARENT); // if (StringUtils.isEmpty(tricodeParent)) { // return null; // } String[] parentCode = tricodeParent.split(","); StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); sql.append(" a.is_catalog,a.function_uuid,a.uuid,a.menu_name,a.menu_description,a.tricode,a.tricode_parent,a.menu_icon,a.is_show "); if (!StringUtils.isEmpty(tricodeParent)) { sql.append(" ,b.uuid parent_uuid,b.menu_name directory_name "); sql.append(" FROM product_sys_menus a"); sql.append(" JOIN product_sys_menus b "); sql.append(" ON a.tricode_parent=b.tricode and length(a.function_uuid)>0 "); sql.append(" and ").append(BaseUtil.buildQuestionMarkFilter("b.tricode", parentCode.length, true)); } else { sql.append(" FROM product_sys_menus a"); sql.append(" where (a.tricode_parent is null or a.tricode_parent='') and length(a.function_uuid)>0 "); parentCode = null; } sql.append(" order by a.sequence asc "); DataTableEntity dt = baseDao.listTable(sql.toString(), parentCode); baseDao.loadPromptData(dt); return dt; } public DataTableEntity getMenuDirectory() { StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); sql.append(" a.uuid,a.menu_name,a.tricode,a.tricode_parent,a.menu_icon,a.is_catalog,a.sequence "); sql.append(" ,b.uuid parent_uuid "); sql.append(" FROM product_sys_menus a"); sql.append(" LEFT JOIN product_sys_menus b "); sql.append(" ON a.tricode_parent=b.tricode "); sql.append(" where (a.function_uuid is null or a.function_uuid ='')"); sql.append(" order by length(a.tricode),a.sequence asc ,a.tricode "); DataTableEntity dt = baseDao.listTable(sql.toString(), new Object[]{}); return BaseUtil.dataTableToTreeTable(dt, CmnConst.TRICODE, CmnConst.TRICODE_PARENT, null); } public DataTableEntity getMenuChildren(FieldSetEntity fse) { String tricode = fse.getString(CmnConst.TRICODE); if (StringUtils.isEmpty(tricode)) { return null; } DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_SYS_MENUS, "(function_uuid is null or function_uuid ='' ) and tricode like concat(?,'-%')", new Object[]{tricode}, new Object[]{"uuid,menu_name,tricode,tricode_parent,menu_icon,is_catalog"}, CmnConst.SEQUENCE); return BaseUtil.dataTableToTreeTable(dt, CmnConst.TRICODE, CmnConst.TRICODE_PARENT, null); } /** * 新增菜单 * * @param fs * @return * @throws BaseException */ @Transactional public FieldSetEntity addMenu(FieldSetEntity fs) throws BaseException { codeService.createCode(fs, CmnConst.PRODUCT_SYS_MENUS, CmnConst.TRICODE, !StringUtils.isEmpty(fs.getString(CmnConst.TRICODE_PARENT)) ? fs.getString(CmnConst.TRICODE_PARENT) : ""); fs.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id());// 获取登录帐号 fs.setValue(CmnConst.CREATED_UTC_DATETIME, DateUtils.getDateTime());// 创建时间 baseDao.add(fs); return fs; } /** * 修改菜单 * * @param fse * @return * @throws BaseException */ @Transactional public boolean updateMenu(FieldSetEntity fse) throws BaseException { FieldSetEntity fss = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_MENUS, fse.getString(CmnConst.UUID), false); String new_parent_code = fse.getString(CmnConst.TRICODE_PARENT); String parent_code = fss.getString(CmnConst.TRICODE_PARENT); fse.setValue("updated_by", SpringMVCContextHolder.getCurrentUser().getUser_id());// 获取登录帐号 fse.setValue("updated_utc_datetime", DateUtils.getDateTime());// 创建时间 List updateCacheUuid = new ArrayList<>(); if (!StringUtils.isEmpty(new_parent_code) || !StringUtils.isEmpty(parent_code)) { boolean status = false; if ((StringUtils.isEmpty(new_parent_code) && !StringUtils.isEmpty(parent_code)) || (!StringUtils.isEmpty(new_parent_code) && StringUtils.isEmpty(parent_code)) || !parent_code.equals(new_parent_code)) { // 改编码 status = true; } if (status) { codeService.createCode(fse, CmnConst.PRODUCT_SYS_MENUS, CmnConst.TRICODE, !StringUtils.isEmpty(new_parent_code) ? new_parent_code : ""); //当前菜单所有下级 DataTableEntity d = baseDao.listTable("SELECT uuid,tricode,tricode_parent FROM product_sys_menus where tricode_parent like concat(?,'%') ORDER BY tricode", new Object[]{fss.getString(CmnConst.TRICODE)}); if (!BaseUtil.dataTableIsEmpty(d)) { // key = 旧编码 value = 新编码 Map codeMap = Maps.newHashMap(); codeMap.put(fss.getString(CmnConst.TRICODE), fse.getString(CmnConst.TRICODE)); for (int i = 0; i < d.getRows(); i++) { //在map中获取父级的新编码 String newParentCode = codeMap.get(d.getString(i, CmnConst.TRICODE_PARENT)); String fixCode = codeService.createFixCode(CmnConst.PRODUCT_SYS_MENUS, CmnConst.TRICODE, newParentCode); //将新编码放入map中 codeMap.put(d.getString(i, CmnConst.TRICODE), fixCode); d.setFieldValue(i, CmnConst.TRICODE, fixCode); d.setFieldValue(i, CmnConst.TRICODE_PARENT, newParentCode); } if (codeMap.size() > 1) { baseDao.update(d); Object[] uuids = d.getUuids(); updateCacheUuid.addAll(Arrays.asList(uuids)); } } } } // 处理文件删除 if (!StringUtils.isEmpty(fse.getString("del"))) { String[] dels = fss.getString("del").split(","); baseDao.delete("product_sys_attachments", BaseUtil.buildQuestionMarkFilter(CmnConst.UUID, dels.length, true), dels); } //周杰 2020年12月17日 上午09:39 boolean flag = baseDao.update(fse); updateCacheUuid.add(fse.getUUID()); if (flag) { //菜单被隐藏时 隐藏子级菜单 if (!"1".equals(fse.getString("is_show"))) { //菜单是否有效,子菜单同主菜单 String usql = " UPDATE product_sys_menus SET is_show = ? WHERE tricode_parent = ? "; Object args[] = {fse.getString("is_show"), fse.getString(CmnConst.TRICODE)}; baseDao.executeUpdate(usql, args); } //刷新菜单 String sql = " select GROUP_CONCAT(role_uuid) role_uuid from ( select role_uuid from product_sys_function_permission " + " where function_uuid in( select function_uuid from product_sys_menus where tricode like ? ) GROUP BY role_uuid) a "; FieldSetEntity fsro = baseDao.getFieldSetEntityBySQL(sql, new Object[]{fse.getString(CmnConst.TRICODE) + "%"}, false); if (fsro != null) { String role_uuid = fsro.getString("role_uuid"); if (!StringUtils.isEmpty(role_uuid)) { infoService.updateUserInfoByUpdateRole(role_uuid.split(","), true); } } } return flag; } public JSONArray getMenuTopFunction() { SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); String role_uuids = null; if (currentUser.getCurrentStaff() != null && !currentUser.isManager()) { role_uuids = currentUser.getCurrentStaff().getString("role_uuids"); } else if (currentUser.getCurrentManager() != null && currentUser.isManager()) { role_uuids = currentUser.getCurrentManager().getString("role_uuids"); } if (StringUtils.isEmpty(role_uuids)) { //角色uuid不能为空 throw new BaseException(SystemCode.SYSTEM_USER_ROLE_ACQUIRE_FAIL.getValue(), SystemCode.SYSTEM_USER_ROLE_ACQUIRE_FAIL.getText()); } StringBuilder sql = new StringBuilder(); sql.append(" SELECT "); sql.append(" menu_name, "); sql.append(" uuid, "); sql.append(" function_uuid, "); sql.append(" tricode, "); sql.append(" SUBSTRING_INDEX( tricode_parent, '-', 1 ) tricode_parent "); sql.append(" FROM "); sql.append(" `product_sys_menus` a "); sql.append(" WHERE "); sql.append(" ( tricode_parent IS NULL OR tricode_parent = '' ) "); sql.append(" OR ( "); sql.append(" ( tricode_parent IS NOT NULL AND tricode_parent != '' ) "); sql.append(" AND function_uuid IN ( "); sql.append(" SELECT "); sql.append(" b.function_uuid "); sql.append(" FROM "); sql.append(" product_sys_function_permission p "); sql.append(" JOIN product_sys_function_buttons b ON concat( ',', p.button_uuid, ',' ) LIKE concat( '%,', b.uuid, ',%' ) "); sql.append(" AND b.button_category_uuid = 'main' where "); sql.append(BaseUtil.buildQuestionMarkFilter("p.role_uuid", role_uuids.split(",").length, true)); sql.append(" ) "); sql.append(" ) "); sql.append(" ORDER BY "); sql.append(" tricode_parent, "); sql.append(" sequence "); DataTableEntity dt = baseDao.listTable(sql.toString(), role_uuids.split(",")); return BaseUtil.dataTableToTreeData(dt, "tricode", "tricode_parent", null, true); } /** * @Date: 2020-06-10 10:12 * @Author: ZhouJie * @Description: 查询菜单详情 */ public FieldSetEntity findMenu(FieldSetEntity fse) throws BaseException { FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_MENUS, fse.getString(CmnConst.UUID), true); return baseDao.listInternationDataTable(fs, null); } /** * 树型菜单 * * @return */ public Object treeMenu() { SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); String role_uuids = null; if (currentUser.getCurrentStaff() != null && !currentUser.isManager()) { role_uuids = currentUser.getCurrentStaff().getString("role_uuids"); } else if (currentUser.getCurrentManager() != null && currentUser.isManager()) { role_uuids = currentUser.getCurrentManager().getString("role_uuids"); } if (StringUtils.isEmpty(role_uuids)) { //角色uuid不能为空 throw new BaseException(SystemCode.SYSTEM_USER_ROLE_ACQUIRE_FAIL.getValue(), SystemCode.SYSTEM_USER_ROLE_ACQUIRE_FAIL.getText()); } String[] role_uuid = role_uuids.split(","); MenuTreeEntity menuCache = getMenuCache(); if (menuCache != null) { List menuTree = menuCache.getMenuTree(role_uuid, currentUser.getUserType()); if (menuTree == null || menuTree.size() <= 0) { throw new BaseException(SystemCode.SYSTEM_USER_MENU_FAIL.getValue(), SystemCode.SYSTEM_USER_MENU_FAIL.getText()); } return menuTree; } throw new BaseException(SystemCode.SYSTEM_USER_MENU_FAIL.getValue(), SystemCode.SYSTEM_USER_MENU_FAIL.getText()); } public JSONObject getMenu() { SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); if (currentUser != null) { JSONObject object = new JSONObject(); object.put("menus", treeMenu()); object.put("router", routerService.getRouter()); return object; } return null; } public void setMenuCache(JSONArray menus) throws BaseException { SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); if (currentUser != null) { String cache_key = "user:menu:" + currentUser.getUser_id(); RedisUtil.set(cache_key, menus); } } public static void deleteEmptyTree(JSONArray array) { if (array != null && !array.isEmpty()) { for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); if (StringUtils.isEmpty(jsonObject.getString(CmnConst.FUNCTION_UUID))) { deleteEmptyTree(jsonObject.getJSONArray("children")); if (jsonObject.getJSONArray("children") == null || jsonObject.getJSONArray("children").isEmpty()) { array.remove(i); i--; continue; } } } } } /** * @throws BaseException * @Date: 2020-06-10 10:12 * @Author: ZhouJie * @Description: 菜单列表 */ public JSONArray listMenu() throws BaseException { // 2020年12月25日10:26:40 xupengcheng 优化菜单国际化 if (SpringMVCContextHolder.getCurrentUser() != null) { DataTableEntity data = baseDao.listTable(CmnConst.PRODUCT_SYS_MENUS, null, "length(tricode),sequence asc"); if (!BaseUtil.dataTableIsEmpty(data)) { baseDao.listInternationDataTable(data, null); baseDao.loadPromptData(data); return MenusTree(data, null); } return new JSONArray(); } // 用户获取失败 throw new BaseException(SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getValue(), SystemCode.SYSTEM_ACQUIRE_PARAM_FAIL.getText(), this.getClass(), "listMenu"); } public JSONArray MenusTree(DataTableEntity data, String dept_parent) throws BaseException { // 2020年12月25日10:26:40 xupengcheng 优化菜单国际化 JSONArray top_array = new JSONArray(); if (data != null && data.getRows() > 0) { for (int i = 0; i < data.getRows(); i++) { FieldSetEntity fs = data.getFieldSetEntity(i); if (StringUtils.isEmpty(fs.getString(CmnConst.TRICODE_PARENT)) || (!StringUtils.isEmpty(dept_parent) && dept_parent.equals(fs.getString(CmnConst.TRICODE_PARENT)))) { JSONObject menu_top = new JSONObject(); menu_top.put(CmnConst.UUID, fs.getString(CmnConst.UUID)); menu_top.put(CmnConst.MENU_NAME, fs.getString("menu_name")); menu_top.put(CmnConst.TRICODE, fs.getString(CmnConst.TRICODE)); menu_top.put(CmnConst.TRICODE_PARENT, fs.getString(CmnConst.TRICODE_PARENT)); menu_top.put(CmnConst.IS_SHOW, fs.getString(CmnConst.IS_SHOW)); menu_top.put("is_catalog", fs.getString("is_catalog")); menu_top.put(CmnConst.MENU_DESCRIPTION, fs.getString(CmnConst.MENU_DESCRIPTION)); menu_top.put("menu_route_component", fs.getString("menu_route_component")); menu_top.put("function_uuid", fs.getString("function_uuid")); menu_top.put("children", MenusChildrenTree(data, fs.getString(CmnConst.TRICODE))); top_array.add(menu_top); } } } return top_array; } public JSONArray MenusChildrenTree(DataTableEntity data, String tricode_parent) throws BaseException { // 2020年12月25日10:26:40 xupengcheng 优化菜单国际化 JSONArray children_array = new JSONArray(); if (data != null && data.getRows() > 0 && !StringUtils.isEmpty(tricode_parent)) { for (int i = 0; i < data.getRows(); i++) { FieldSetEntity fs = data.getFieldSetEntity(i); if (tricode_parent.equals(fs.getString(CmnConst.TRICODE_PARENT))) { JSONObject menu_children = new JSONObject(); menu_children.put(CmnConst.UUID, fs.getString(CmnConst.UUID)); menu_children.put(CmnConst.TRICODE, fs.getString(CmnConst.TRICODE)); menu_children.put(CmnConst.MENU_NAME, fs.getString("menu_name")); menu_children.put(CmnConst.TRICODE_PARENT, fs.getString(CmnConst.TRICODE_PARENT)); menu_children.put(CmnConst.IS_SHOW, fs.getString(CmnConst.IS_SHOW)); menu_children.put("is_catalog", fs.getString("is_catalog")); menu_children.put(CmnConst.MENU_DESCRIPTION, fs.getString(CmnConst.MENU_DESCRIPTION)); menu_children.put("menu_route_component", fs.getString("menu_route_component")); menu_children.put("function_uuid", fs.getString("function_uuid")); FieldSetEntity fss = data.getFieldSetEntity(i); menu_children.put("children", MenusChildrenTree(data, fss.getString(CmnConst.TRICODE))); children_array.add(menu_children); } } } return children_array; } /*** * 查询顶级模块 * * @param tricode 菜单code * @param org_level_uuid 组织架构uuid * @return */ public DataTableEntity getMenuList(String tricode, String org_level_uuid) throws BaseException { String sql = "select menus.uuid,menus.menu_name,menus.menu_description,menus.tricode,menus.tricode_parent,menus.is_show,menus.sequence,menus.menu_icon,menus.created_by,menus.created_utc_datetime, " + "menus.function_uuid,functions.function_name,modules.product_uuid,products.product_name " + "from product_sys_menus menus " + "left join product_sys_functions functions on menus.function_uuid = functions.uuid " + "left join product_sys_modules modules on modules.uuid = functions.module_uuid " + "left join product_sys_products products on modules.product_uuid = products.uuid where 1=1 "; Object[] object = null; DataTableEntity dataTableEntity; //周杰 2020年12月17日 上午11:16 if (!StringUtils.isEmpty(tricode)) { sql += " and menus.tricode like ? and length(menus.tricode) > " + tricode.length(); object = new Object[]{tricode + "%"}; } //周杰 2020年12月17日 上午11:16 if (!StringUtils.isEmpty(org_level_uuid)) { sql += " and functions.group_uuid = ? "; if (object != null && object.length == 1) { object = new Object[]{tricode + "%", org_level_uuid}; } else { object = new Object[]{org_level_uuid}; } } dataTableEntity = baseDao.listTable(sql, object); return dataTableEntity; } /** * 查询下级菜单(只查一级) * * @param tricode_parent 子级菜单编码 * @param org_level_uuid * @return * @throws BaseException */ public DataTableEntity getChildrenMenuList(String tricode_parent, String org_level_uuid) throws BaseException { String sql = "select menus.uuid,menus.menu_name,menus.menu_description,menus.tricode,menus.tricode_parent,menus.is_show,menus.sequence,menus.menu_icon,menus.created_by,menus.created_utc_datetime, " + "menus.function_uuid,functions.function_name,modules.product_uuid,products.product_name " + "from product_sys_menus menus " + "left join product_sys_functions functions on menus.function_uuid = functions.uuid " + "left join product_sys_modules modules on modules.uuid = functions.module_uuid " + "left join product_sys_products products on modules.product_uuid = products.uuid where menus.tricode_parent=?"; Object[] objects = new Object[]{tricode_parent}; //周杰 2020年12月17日 上午11:16 if (!StringUtils.isEmpty(org_level_uuid)) { sql += " and functions.group_uuid=? "; objects = new Object[]{tricode_parent, org_level_uuid}; } return baseDao.listTable(sql, objects); } // ================后台管理返回菜单树======================start============================================================= /*** * 后台管理封装菜单树结构 * * @param dataTableEntity 需要封装的数据 * @param tricode_parent 上级code * @return * @throws BaseException */ public List encapsulationTreeManger(DataTableEntity dataTableEntity, String tricode_parent) throws BaseException { List fieldSetEntities = dataTableEntity.getData(); List list = new ArrayList<>(); for (FieldSetEntity fieldSetEntity : fieldSetEntities) { MenuTree bean = new MenuTree(); BeanRefUtil.setFieldValue(bean, fieldSetEntity.getValues()); list.add(bean); } return listToTreeManger(list, tricode_parent); } /*** * 获取菜单树结构 * * @param list 数据集合 * @return */ public List listToTreeManger(List list, String tricode_parent) { List treeList = new ArrayList<>(); //周杰 2020年12月17日 上午11:16 if (StringUtils.isEmpty(tricode_parent)) { tricode_parent = "0"; } for (MenuTree tree : list) { if (tree.getTricode_parent().equals(tricode_parent)) { tree.setMrbase_sys_menus(new ArrayList<>()); treeList.add(findChildrenManger(tree, list)); } } return treeList; } /*** * 获取下级菜单树结构 * * @param tree 当前节点数据 * @param list 所有节点数据 * @return */ private static MenuTree findChildrenManger(MenuTree tree, List list) { for (MenuTree node : list) { if (node.getTricode_parent().equals(tree.getTricode())) { if (tree.getMrbase_sys_menus() == null) { tree.setMrbase_sys_menus(new ArrayList<>()); } tree.getMrbase_sys_menus().add(findChildrenManger(node, list)); } } if (tree.getMrbase_sys_menus() == null) { tree.setMrbase_sys_menus(new ArrayList<>()); } return tree; } // ================后台管理返回菜单树======================start============================================================= // ================登录返回菜单============================start============================================================== /** * 根据当前人查询拥有权限的菜单 * * @param user * @return * @throws BaseException */ public List getMenuTreeByuserUuid(SystemUser user) throws BaseException { DataTableEntity dt = this.getMenuCurrentUserListLogin(user); List treeMenuList = this.encapsulationTree(dt); return treeMenuList; } /** * @param user * @return * @throws BaseException */ public DataTableEntity getMenuCurrentUserListLogin(SystemUser user) throws BaseException { DataTableEntity dt = rolesService.functionsByUser(user); if (dt != null) { String param = ""; for (int i = 0; i < dt.getRows(); i++) { String function_uuid = dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID); if (function_uuid == null || function_uuid.equals("")) { continue; } if (i != 0) { param += ",'" + dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID) + "'"; } else { param += "'" + dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID) + "'"; } } if (param.equals("")) { return null; } // 查出关联功能的菜单及父给菜单 String sql1 = "select menus.uuid,menus.menu_name,menus.tricode,menus.tricode_parent,menus.function_uuid,menus.menu_icon " + " from product_sys_menus menus " + " where menus.is_show = 1 " + " and menus.function_uuid in (" + param + ")" + " union all select b.uuid,b.menu_name,b.tricode,b.tricode_parent,b.function_uuid,b.menu_icon from ( " + " (SELECT DISTINCT CASE WHEN length( a.tricode ) > 3 THEN SUBSTRING( a.tricode, 1, 3 ) END AS code1, " + " CASE WHEN length( a.tricode ) > 7 THEN SUBSTRING( a.tricode, 1, 7 )END AS code2 " + " FROM product_sys_menus a" + " WHERE a.is_show = 1 " + " AND a.function_uuid IN ( " + param + " ))as c)" + " right JOIN product_sys_menus b ON ( c.code1 = b.tricode OR c.code2 = b.tricode ) where c.code1 is not null and b.is_show=1"; DataTableEntity dtm = baseDao.listTable(sql1, new Object[]{}); if (dtm.getRows() > 0) { for (int i = 0; i < dtm.getRows(); i++) { FieldSetEntity fss = dtm.getFieldSetEntity(i); FieldSetEntity fsf = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS, "function_uuid=? and is_main=?", new Object[]{fss.getString(CmnConst.FUNCTION_UUID), 1}, false); if (fsf != null) { fss.setValue(CmnConst.BUTTON_NAME, fsf.getString(CmnConst.BUTTON_NAME)); fss.setValue(CmnConst.URL, fsf.getString(CmnConst.URL)); fss.setValue(CmnConst.BUTTON_COMPONENT, fsf.getString(CmnConst.BUTTON_COMPONENT)); } DataTableEntity menu_names = baseDao.listTable(CmnConst.PRODUCT_SYS_LANGUAGE_CONT_VALUES, "field_name=? and data_uuid=?", new String[]{CmnConst.MENU_NAME, fss.getString(CmnConst.UUID)}); if (menu_names.getRows() > 0) { List> menus = new ArrayList>(); for (int j = 0; j < menu_names.getRows(); j++) { Map language_names = new HashMap(); FieldSetEntity menu_name = menu_names.getFieldSetEntity(j); language_names.put(menu_name.getString(CmnConst.LANGUAGE_CODE), menu_name.getString("field_value")); menus.add(language_names); } fss.setValue(CmnConst.LANGUAGE_NAME, menus); } } } return dtm; } return dt; } /** * 根据当前人查询拥有权限的指定客户端菜单 * * @param client_type 客户端类型 * @param user 当前用户 * @return * @throws BaseException */ public List> getMenuTreeByuserUuid(SystemUser user, Object client_type, Object language_code) throws BaseException { DataTableEntity dt = this.getMenuCurrentUserListLogin(user, client_type, language_code); return this.convertMenuInternation(dt); } /** * 菜单国际化转换 * * @param dt * @return */ private List> convertMenuInternation(DataTableEntity dt) throws BaseException { List> menus = new ArrayList>(); // 封装上下级菜单 Map> parent_menu = new HashMap>(); if (dt != null && dt.getRows() > 0) { for (int j = 0; j < dt.getRows(); j++) { FieldSetEntity menu_name = dt.getFieldSetEntity(j); Map menu = JsonUtil.parseJsonFieldSetToMap(menu_name); String pcode = menu_name.getString(CmnConst.TRICODE_PARENT); if (StringUtils.isEmpty(pcode) || parent_menu.get(pcode) == null) { menus.add(menu); } else {// 上级菜单 Map pm = parent_menu.get(pcode); Object subs = pm.get("menus_tree"); List> submenus = null; if (subs == null) { submenus = new ArrayList>(); pm.put("menus_tree", submenus); } else { submenus = (List>) subs; } submenus.add(menu); } parent_menu.put(menu_name.getString(CmnConst.TRICODE), menu); } } return menus; } /** * 查指定客户类型的当前用户的菜单 * * @param user 当前用户 * @param client_type 客户端类型 * @return * @throws BaseException */ public DataTableEntity getMenuCurrentUserListLogin(SystemUser user, Object client_type, Object language_code) throws BaseException { StringBuilder function = new StringBuilder(); Vector fp = new Vector(); String role_uuids = null; if (user.getCurrentStaff() != null) { role_uuids = user.getCurrentStaff().getString("role_uuids"); } else if (user.getCurrentManager() != null) { role_uuids = user.getCurrentManager().getString("role_uuids"); } DataTableEntity mt = new DataTableEntity(); if (!StringUtils.isEmpty(role_uuids)) { function.append( " select function_uuid uuid from product_sys_functions a left join product_sys_function_permission b on a.uuid=b.function_uuid where CONCAT(',',client_type_uuid,',') like ? ") .append(" and b.role_uuid in ("); fp.add("%," + client_type + ",%"); String rs[] = role_uuids.split(","); for (int i = 0; i < rs.length; i++) { fp.add(rs[i]); if (i > 0) function.append(","); function.append("?"); } function.append(")"); } if (function.length() > 0) { DataTableEntity dt = baseDao.listTable(function.toString(), fp.toArray()); if (dt == null) return mt; Object function_uuids[] = dt.getUuids("product_sys_function_permission"); if (function_uuids == null) return mt; StringBuilder s = new StringBuilder(); List v = new ArrayList<>(); for (int i = 0; i < function_uuids.length * 3; i++) { if (i < function_uuids.length) { if (s.length() > 0) s.append(","); s.append("?"); } v.add(function_uuids[i % function_uuids.length]); } if (function_uuids != null) { function = new StringBuilder(); function.append(// "select * from ( " + "select m.uuid,menu_name,menu_icon,menu_color,m.sequence,tricode,tricode_parent,bt.path path ,m.function_uuid,bt.url,bt.button_name,ifnull(bt.button_component,'/') routing_path ") .append(" from product_sys_menus m ") .append(" left join product_sys_function_buttons bt on bt.function_uuid=m.function_uuid and bt.is_main=1 ") .append(" where m.is_show=1 and ( m.function_uuid in (").append(s).append(") ").append(" or m.tricode in (") .append(" select tricode_parent ").append(" from product_sys_menus where function_uuid in (") .append(s).append(") ").append(" union all ") .append(" select SUBSTRING( tricode_parent, 1, 3 ) ").append(" from product_sys_menus ") .append(" where function_uuid in (").append(s) .append(") and LENGTH(tricode_parent) > 6 ) ) order by length(tricode),sequence asc,tricode asc"); mt = baseDao.listTable(function.toString(), v.toArray()); baseDao.listInternationDataTable(mt, CoreConst.CLIENT_TYPE_WEB.equals(client_type) ? null : language_code.toString()); } } return mt; } /** * 返回所有菜单 * * @return * @throws BaseException */ public List getMenuTree() throws BaseException { // 封封菜单树结构 List treeMenuList = this.encapsulationTree(this.getMenuListLogin()); return treeMenuList; } /*** * 帐号登录返回菜单列表 * * @return */ public DataTableEntity getMenuListLogin() throws BaseException { String sql = "select menu.uuid,menu.menu_name,menu.tricode,menu.tricode_parent,menu.function_uuid,menu.menu_icon " + " from product_sys_menus menu where menu.is_show = 1 order by menu.sequence asc"; DataTableEntity dataTableEntity = baseDao.listTable(sql, new Object[]{}); return dataTableEntity; } /*** * 封装菜单树结构 * * @param dataTableEntity 需要封装的数据 * @return * @throws BaseException */ public List encapsulationTree(DataTableEntity dataTableEntity) throws BaseException { if (dataTableEntity == null || dataTableEntity.getRows() <= 0) { return new ArrayList(); } List fieldSetEntities = dataTableEntity.getData(); List list = new ArrayList<>(); for (FieldSetEntity fieldSetEntity : fieldSetEntities) { LoginMenu bean = new LoginMenu(); BeanRefUtil.setFieldValue(bean, fieldSetEntity.getValues()); bean.setLanguage_name((List) fieldSetEntity.getValue(CmnConst.LANGUAGE_NAME)); list.add(bean); } return listToTree(list); } /*** * 获取菜单树结构 * * @param list 数据集合 * @return */ public List listToTree(List list) { List treeList = new ArrayList<>(); for (LoginMenu tree : list) { if (tree.getTricode_parent() == null || "0".equals(tree.getTricode_parent())) { tree.setMenusTree(new ArrayList<>()); treeList.add(findChildren(tree, list)); } } return treeList; } /*** * 获取下级菜单树结构 * * @param tree 当前节点数据 * @param list 所有节点数据 * @return */ private static LoginMenu findChildren(LoginMenu tree, List list) { for (LoginMenu node : list) { if (node.getTricode_parent() != null && node.getTricode_parent().equals(tree.getTricode())) { if (tree.getMenusTree() == null) { tree.setMenusTree(new ArrayList<>()); } tree.getMenusTree().add(findChildren(node, list)); } } if (tree.getMenusTree() == null) { tree.setMenusTree(new ArrayList<>()); } return tree; } // ================登录返回菜单============================strt============================================================== /** * 删除菜单 * * @param fs * @throws BaseException */ @Transactional public boolean deleteMenus(FieldSetEntity fse) throws BaseException { String uuid = fse.getString(CmnConst.UUID); FieldSetEntity fs = baseDao.getFieldSetBySQL( "SELECT uuid,(select 1 from product_sys_menus b where b.tricode like concat(a.tricode,'-%') limit 1) is_chidren from product_sys_menus a where uuid=?", new Object[]{uuid}, false); if (FieldSetEntity.isEmpty(fs)) { return false; } if ("1".equals(fs.getString("is_chidren"))) { //有子级不允许删除 throw new BaseException(SystemCode.DELETE_MENU_FAIL_CHILDREN_EXEIST); } return baseDao.delete("product_sys_menus", new String[]{uuid}); } /** * @Date: 2020-10-27 10:48 * @Author: ZhouJie * @Description: 获取当前登录人的所有菜单 */ public List getUserMenu(SystemUser user) throws BaseException { DataTableEntity dt = this.getMenuUserList(user); List treeMenuList = this.ncapsulationTree(dt); return treeMenuList; } public DataTableEntity getMenuUserList(SystemUser user) throws BaseException { DataTableEntity dt = rolesService.functionsByUser(user); if (dt != null) { String param = ""; for (int i = 0; i < dt.getRows(); i++) { String function_uuid = dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID); if (function_uuid == null || function_uuid.equals("")) { continue; } if (i != 0) { param += ",'" + dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID) + "'"; } else { param += "'" + dt.getFieldSetEntity(i).getString(CmnConst.FUNCTION_UUID) + "'"; } } if (param.equals("")) { return null; } // 查出关联功能的菜单及父给菜单 String sql1 = "select menus.uuid,menus.menu_name,menus.tricode,menus.tricode_parent,menus.function_uuid,menus.menu_icon,menus.menu_color,menus.sequence " + " from product_sys_menus menus " + " where menus.is_show = 1 " + " and menus.function_uuid in (" + param + ")" + " union all " + " select b.uuid,b.menu_name,b.tricode,b.tricode_parent,b.function_uuid,b.menu_icon,b.menu_color,b.sequence from ( " + " (SELECT DISTINCT CASE WHEN length( a.tricode ) > 3 THEN SUBSTRING( a.tricode, 1, 3 ) END AS code1," + " CASE WHEN length( a.tricode ) > 7 THEN SUBSTRING( a.tricode, 1, 7 ) END AS code2 " + " FROM product_sys_menus a" + " WHERE a.is_show = 1 " + " AND a.function_uuid IN ( " + param + " ))as c)" + " right JOIN product_sys_menus b ON ( c.code1 = b.tricode OR c.code2 = b.tricode ) where c.code1 is not null and b.is_show=1"; DataTableEntity dtm = baseDao.listTable(sql1, new Object[]{}); if (dtm.getRows() > 0) { for (int i = 0; i < dtm.getRows(); i++) { FieldSetEntity fss = dtm.getFieldSetEntity(i); FieldSetEntity fsf = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_FUNCTION_BUTTONS, "function_uuid=? and is_main=?", new Object[]{fss.getString(CmnConst.FUNCTION_UUID), 1}, false); if (fsf != null) { fss.setValue("button_component", fsf.getString("button_component")); } DataTableEntity menu_names = baseDao.listTable(CmnConst.PRODUCT_SYS_LANGUAGE_CONT_VALUES, "field_name=? and data_uuid=?", new String[]{CmnConst.MENU_NAME, fss.getString(CmnConst.UUID)}); if (menu_names.getRows() > 0) { List> menus = new ArrayList>(); for (int j = 0; j < menu_names.getRows(); j++) { Map language_names = new HashMap(); FieldSetEntity menu_name = menu_names.getFieldSetEntity(j); language_names.put(menu_name.getString(CmnConst.LANGUAGE_CODE), menu_name.getString("field_value")); menus.add(language_names); } fss.setValue("language_names", menus); } } } return dtm; } return dt; } public List ncapsulationTree(DataTableEntity dataTableEntity) throws BaseException { if (dataTableEntity == null || dataTableEntity.getRows() <= 0) { return new ArrayList(); } List fieldSetEntities = dataTableEntity.getData(); List list = new ArrayList<>(); for (FieldSetEntity fieldSetEntity : fieldSetEntities) { UserMenu bean = new UserMenu(); BeanRefUtil.setFieldValue(bean, fieldSetEntity.getValues()); bean.setLanguage_name((List) fieldSetEntity.getValue("language_names")); list.add(bean); } return listoTree(list); } public List listoTree(List list) { List treeList = new ArrayList<>(); for (UserMenu tree : list) { if (tree.getTricode_parent() == null || "0".equals(tree.getTricode_parent())) { tree.setMenusTree(new ArrayList<>()); treeList.add(findChildrens(tree, list)); } } return treeList; } private static UserMenu findChildrens(UserMenu tree, List list) { for (UserMenu node : list) { if (node.getTricode_parent() != null && node.getTricode_parent().equals(tree.getTricode())) { if (tree.getMenusTree() == null) { tree.setMenusTree(new ArrayList<>()); } tree.getMenusTree().add(findChildrens(node, list)); } } if (tree.getMenusTree() == null) { tree.setMenusTree(new ArrayList<>()); } return tree; } }