461a750a16d005dba2f83467b6379dd50a75a852.svn-base 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /***************************************************
  2. * Copyright 2014-7-23, -sinosoft.com.cn All rights reserved.
  3. *【创建日期】 : 2014-7-23
  4. *【创建人】 : 徐杨
  5. *【类名】:SysCustomMenuService.java
  6. *【功能】:本类是菜单角色自定义服务实现类
  7. *【修改日志】
  8. * --------------------------------------------------
  9. * VERSION DATE 修改人 修改内容
  10. * --------------------------------------------------
  11. * 1.0 2014-7-23
  12. * --------------------------------------------------
  13. **************************************************/
  14. package com.sinosoft.lz.system.menu.service;
  15. import java.io.UnsupportedEncodingException;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.ArrayList;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import javax.ws.rs.POST;
  29. import javax.ws.rs.Path;
  30. import javax.ws.rs.ProduceMime;
  31. import net.sf.json.JSONArray;
  32. import org.apache.log4j.Logger;
  33. import com.formaction.Utils;
  34. import com.formaction.dao.OperateAble;
  35. import com.formaction.dao.OperateImpl;
  36. import com.formaction.vo.Msg;
  37. import com.google.gson.Gson;
  38. import com.persistence.DbConnection;
  39. import com.persistence.service.assitant.DataObject;
  40. import com.persistence.service.assitant.Field;
  41. import com.persistence.service.assitant.generator.IdentityGenerator;
  42. import com.persistence.service.exception.PersistenceException;
  43. import com.persistence.service.exception.TransformTypeException;
  44. import com.sinosoft.common.excel.JsonPluginsUtil;
  45. import com.sinosoft.lz.system.menu.dao.ISysCustomMenuDao;
  46. import com.sinosoft.lz.system.menu.dao.SysCustomMenuDaoImpl;
  47. import com.sinosoft.lz.system.menu.vo.CustomTreeNodeVO;
  48. import com.sinosoft.lz.system.menu.vo.CustomTreeVO;
  49. import com.sinosoft.lz.system.menu.vo.IndexMenu;
  50. import com.sinosoft.lz.system.menu.vo.SysMenu;
  51. import com.sinosoft.lz.system.positiontab.service.PositionTabService;
  52. import com.sinosoft.lz.system.util.JsonUtil;
  53. import com.sinosoft.lz.system.util.tree.TreeNode;
  54. @Path("/SysCustomMenuService/")
  55. public class SysCustomMenuService {
  56. private final static Logger log = Logger.getLogger(SysCustomMenuService.class);
  57. private List showMenuList = new ArrayList();
  58. @ProduceMime("application/json")
  59. @POST
  60. @Path("/getShowMenuInfoByRoleIds/")
  61. public String getShowMenuInfoByRoleIds(String params){
  62. String roleids = Utils.getParameter("roleids", params) == null ?
  63. "" : Utils.getParameter("roleids", params);
  64. log.info("角色IDS-----------:"+roleids);
  65. String[] rids = null;
  66. if(roleids!=null&&!"".equals(roleids)){
  67. rids = roleids.split(",");
  68. String ins = "";
  69. for(int i=0;i<rids.length;i++){
  70. ins += "?,";
  71. }
  72. ins = ins.substring(0, ins.lastIndexOf(","));
  73. StringBuffer sql =new StringBuffer();
  74. sql.append(" select distinct m.fd_objectid,m.menu_id,m.menu_name,m.menu_url,m.parent_menu_id,m.menu_info,m.sortno,m.menu_statu,m.is_leaf,m.in_control,m.is_del,m.mod_date,m.menu_img");
  75. sql.append(" from sys_menu m , sys_role_right_rel r where m.menu_id = r.menu_id and r.role_id in ( ");
  76. sql.append(ins+" ) and m.is_del='0'");
  77. sql.append(" order by m.sortno");
  78. log.info(sql.toString());
  79. DbConnection dbConn = new DbConnection();
  80. Connection conn = null;
  81. PreparedStatement pstm = null;
  82. ResultSet rs = null;
  83. List<SysMenu> menuList = new ArrayList<SysMenu>();
  84. try{
  85. conn = dbConn.getConnection();
  86. pstm = conn.prepareStatement(sql.toString());
  87. for(int i=0;i<rids.length;i++){
  88. //ins += "?,";
  89. pstm.setString(i+1, rids[i]);
  90. }
  91. rs = pstm.executeQuery();
  92. while(rs.next()){
  93. SysMenu menu = new SysMenu();
  94. menu.setFd_objectid(rs.getString("fd_objectid"));
  95. menu.setMenu_id(rs.getString("menu_id"));
  96. menu.setMenu_name(rs.getString("menu_name"));
  97. menu.setMenu_url(rs.getString("menu_url"));
  98. menu.setParent_menu_id(rs.getString("parent_menu_id"));
  99. menu.setMenu_info(rs.getString("menu_info"));
  100. menu.setSortno(rs.getString("sortno"));
  101. menu.setMenu_statu(rs.getString("menu_statu"));
  102. menu.setIs_leaf(rs.getString("is_leaf"));
  103. menu.setIn_control(rs.getString("in_control"));
  104. menu.setIs_del(rs.getString("is_del"));
  105. menu.setMod_date(rs.getString("mod_date"));
  106. menu.setMenu_img(rs.getString("menu_img"));
  107. menuList.add(menu);
  108. }
  109. return menuList2Json(menuList);
  110. }catch(Exception e){
  111. log.error("查询菜单错误"+e.getMessage());
  112. }finally{
  113. try {
  114. rs.close();
  115. pstm.close();
  116. conn.close();
  117. } catch (SQLException e) {
  118. // TODO Auto-generated catch block
  119. log.error("关闭数据库连接错误:"+e.getMessage());
  120. }
  121. }
  122. }
  123. return "";
  124. //return "[{\"name\":\"sy\",\"submenu\":[{\"name\":\"应急组织机构管理\",\"submenu\":[{\"name\":\"应急组织结构维护\"},{\"name\":\"应急组织结构查询\"}]},{\"name\":\"应急制度规范文件管理\",\"submenu\":[{\"name\":\"应急制度规范文件维护\"},{\"name\":\"应急制度规范文件查询\"}]},{\"name\":\"应急预案管理\",\"submenu\":[{\"name\":\"预案模板管理\"},{\"name\":\"应急预案维护\"},{\"name\":\"应急预案查询\"}]},{\"name\":\"应急预案管理\",\"submenu\":[{\"name\":\"预案模板管理\"},{\"name\":\"应急预案维护\"},{\"name\":\"应急预案查询\"}]}]},{\"name\":\"xtgn\",\"submenu\":[{\"name\":\"应急组织机构管理\",\"submenu\":[{\"name\":\"应急组织结构维护\"},{\"name\":\"应急组织结构查询\"}]},{\"name\":\"应急制度规范文件管理\",\"submenu\":[{\"name\":\"应急制度规范文件维护\"},{\"name\":\"应急制度规范文件查询\"}]},{\"name\":\"应急预案管理\",\"submenu\":[{\"name\":\"预案模板管理\"},{\"name\":\"应急预案维护\"},{\"name\":\"应急预案查询\"}]},{\"name\":\"应急预案管理\",\"submenu\":[{\"name\":\"预案模板管理\"},{\"name\":\"应急预案维护\"},{\"name\":\"应急预案查询\"}]}]},{\"name\":\"xtgl\",\"submenu\":[{\"name\":\"应急组织机构管理\",\"submenu\":[{\"name\":\"应急组织结构维护\"},{\"name\":\"应急组织结构查询\"}]},{\"name\":\"应急制度规范文件管理\",\"submenu\":[{\"name\":\"应急制度规范文件维护\"},{\"name\":\"应急制度规范文件查询\"}]},{\"name\":\"应急预案管理\",\"submenu\":[{\"name\":\"预案模板管理\"},{\"name\":\"应急预案维护\"},{\"name\":\"应急预案查询\"}]},{\"name\":\"应急预案管理\",\"submenu\":[{\"name\":\"预案模板管理\"},{\"name\":\"应急预案维护\"},{\"name\":\"应急预案查询\"}]}]},{\"name\":\"yb\"},{\"name\":\"db\"}]";
  125. }
  126. @ProduceMime("application/json")
  127. @POST
  128. @Path("/getIndexMenuByRoleIds/")
  129. public String getIndexMenuByRoleIds(String params){
  130. String roleids = Utils.getParameter("roleids", params) == null ?
  131. "" : Utils.getParameter("roleids", params);
  132. log.info("角色IDS-----------:"+roleids);
  133. if(roleids!=null&&!"".equals(roleids)){
  134. SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl();
  135. List<Map<String,Object>> mList = new ArrayList<Map<String,Object>>();
  136. Map<String,Object> rcMap = new HashMap<String,Object>();
  137. rcMap.put("display","日常管理");
  138. List<SysMenu> rcList = mDao.getIndexMenu("3", roleids);
  139. rcMap.put("value",rcList);
  140. mList.add(rcMap);
  141. Map<String,Object> czMap = new HashMap<String,Object>();
  142. czMap.put("display","应急处置");
  143. List<SysMenu> czList = mDao.getIndexMenu("4", roleids);
  144. czMap.put("value",czList);
  145. mList.add(czMap);
  146. Map<String,Object> bgdMap = new HashMap<String,Object>();
  147. bgdMap.put("display","保供电");
  148. List<SysMenu> bgdList = mDao.getIndexMenu("5", roleids);
  149. bgdMap.put("value",bgdList);
  150. mList.add(bgdMap);
  151. Map<String,Object> sysMap = new HashMap<String,Object>();
  152. sysMap.put("display","系统管理");
  153. List<SysMenu> sysList = mDao.getIndexMenu("9", roleids);
  154. sysMap.put("value",sysList);
  155. mList.add(sysMap);
  156. String menuStr = JsonPluginsUtil.listToJson(mList);
  157. log.info(menuStr);
  158. return menuStr;
  159. }
  160. return JsonUtil.string2json("");
  161. }
  162. @ProduceMime("application/json")
  163. @POST
  164. @Path("/queryIndexMenu/")
  165. public String queryIndexMenu(String params){
  166. String roleids = Utils.getParameter("roleids", params) == null ?
  167. "" : Utils.getParameter("roleids", params);
  168. String menuName = Utils.getParameter("menuName", params) == null ?
  169. "" : Utils.getParameter("menuName", params);
  170. log.info("角色IDS-----------:"+roleids);
  171. if(roleids!=null&&!"".equals(roleids)){
  172. SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl();
  173. List<Map<String,Object>> mList = new ArrayList<Map<String,Object>>();
  174. List<SysMenu> spList = mDao.queryIndexMenu(menuName, roleids);
  175. List<SysMenu> sbList = mDao.queryIndexSubMenu(menuName, roleids);
  176. for(int i=0;i<spList.size();i++){
  177. SysMenu spMenu = spList.get(i);
  178. Map<String,Object> rcMap = new HashMap<String,Object>();
  179. rcMap.put("display",spMenu.getMenu_name());
  180. List<SysMenu> rcList = new ArrayList<SysMenu>();
  181. for(int j=0;j<sbList.size();j++){
  182. SysMenu sbMenu = sbList.get(j);
  183. if(sbMenu.getParent_menu_id().equals(spMenu.getMenu_id())){
  184. rcList.add(sbMenu);
  185. }
  186. }
  187. rcMap.put("value",rcList);
  188. mList.add(rcMap);
  189. }
  190. String menuStr = JsonPluginsUtil.listToJson(mList);
  191. log.info(menuStr);
  192. return menuStr;
  193. }
  194. return "";
  195. }
  196. /**
  197. * 根据url查询菜单信息;若有菜单信息,继续根据menu_id查询页签信息
  198. * @param params
  199. * @return
  200. */
  201. @SuppressWarnings("resource")
  202. @ProduceMime("html/text")
  203. @POST
  204. @Path("/getMenuInfoByUrl/")
  205. public String getMenuInfoByUrl(String params){
  206. String frmsrc = Utils.getParameter("frmsrc", params) == null ?
  207. "" : Utils.getParameter("frmsrc", params);
  208. String userId = Utils.getParameter("userId", params) == null ? "" : Utils
  209. .getParameter("userId", params);
  210. frmsrc = frmsrc.trim();
  211. String sql = "select MENU_IMG MENU_IMG,MENU_ID,MENU_NAME from SYS_MENU where TRIM(BOTH ' ' FROM menu_url)=? and is_del='0' ";
  212. DbConnection dbConn = new DbConnection();
  213. Connection conn = null;
  214. PreparedStatement pstm = null;
  215. ResultSet rs = null;
  216. StringBuffer sb = new StringBuffer();
  217. sb.append("[{");
  218. try{
  219. conn = dbConn.getConnection();
  220. pstm = conn.prepareStatement(sql);
  221. pstm.setString(1, frmsrc);
  222. rs = pstm.executeQuery();
  223. String imgClass = "";
  224. String menu_id = "";
  225. String menu_name = "";
  226. while(rs.next()){
  227. imgClass = rs.getString("MENU_IMG");
  228. menu_id = rs.getString("MENU_ID");
  229. menu_name = rs.getString("MENU_NAME");
  230. }
  231. if("".equals(menu_id)){//如果在sys_menu(菜单主表)中没有数据,判断是否是带有页签的页面,故再去页签表EMC_AM_YQSJCLB去查数据
  232. String sql1 = "select MENU_IMG MENU_IMG,MENU_ID,MENU_NAME from SYS_MENU where menu_id=(select YO_CDID from EMC_AM_YQSJCLB where TRIM(BOTH ' ' FROM YQ_SRC)=?)";
  233. pstm = conn.prepareStatement(sql1);
  234. pstm.setString(1, frmsrc);
  235. rs = pstm.executeQuery();
  236. while(rs.next()){
  237. imgClass = rs.getString("MENU_IMG");
  238. menu_id = rs.getString("MENU_ID");
  239. menu_name = rs.getString("MENU_NAME");
  240. }
  241. }
  242. sb.append("\"menuData\":{\"imgClass\":\""+imgClass+"\",\"menu_id\":\""+menu_id+"\",\"menu_name\":\""+menu_name+"\"}}");
  243. if(!"".equals(menu_id)){
  244. PositionTabService pt = new PositionTabService();
  245. params = "menuId="+menu_id+"&userId="+userId+"&frmsrc="+frmsrc;
  246. String tabData = pt.getTabById(params);
  247. sb.append(",{\"tabData\":"+tabData+"}");
  248. }
  249. sb.append("]");
  250. return sb.toString();
  251. }catch(Exception e){
  252. log.error("查询图标错误"+e.getMessage());
  253. }finally{
  254. try {
  255. rs.close();
  256. pstm.close();
  257. conn.close();
  258. } catch (SQLException e) {
  259. // TODO Auto-generated catch block
  260. log.error("关闭数据库连接错误:"+e.getMessage());
  261. }
  262. }
  263. return "";
  264. }
  265. public String menuList2Json(List menuList){
  266. TreeNode root = new TreeNode("0", "0", "root", "root","","");
  267. TreeNode treeNode = getTreeNode(root,menuList);
  268. JSONArray obj = JSONArray.fromObject(root.getSubmenu());;
  269. log.info(obj.toString());
  270. return obj.toString();
  271. }
  272. public TreeNode getTreeNode(TreeNode node,List menuList){
  273. for(int i=0;i<menuList.size();i++){
  274. SysMenu menu = (SysMenu)menuList.get(i);
  275. if(menu.getParent_menu_id().equals(node.getId())){
  276. TreeNode tNode = new TreeNode(menu.getMenu_id(),menu.getParent_menu_id(),menu.getMenu_name(),menu.getMenu_url(),menu.getSortno(),menu.getMenu_img());
  277. TreeNode cNode = getTreeNode(tNode,menuList); //递归添加节点
  278. node.add(cNode);
  279. }
  280. }
  281. return node;
  282. }
  283. /**
  284. * 新增工作台菜单
  285. * 若该用户该菜单工作台菜单表中已存在,只是被移除,则将移除状态改成使用
  286. * 即state=0为使用 state=1为移除
  287. * @param params
  288. * @return
  289. */
  290. @POST
  291. @ProduceMime("application/json")
  292. @Path("/addIndexMenu/")
  293. public String addIndexMenu(String params){
  294. String result = JsonUtil.string2json("");
  295. SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl();
  296. String menu_id = Utils.getParameter("menu_id", params) == null ? "" : Utils.getParameter("menu_id", params);
  297. String user_id = Utils.getParameter("user_id", params) == null ? "" : Utils.getParameter("user_id", params);
  298. if(checkAddMenu(user_id,menu_id)){
  299. List<IndexMenu> list = mDao.queryIndexMenu(user_id);
  300. result = JsonPluginsUtil.listToJson(list);
  301. }else{
  302. String menu_name = Utils.getParameter("menu_name", params) == null ? "" : Utils.getParameter("menu_name", params);
  303. String menu_url = Utils.getParameter("menu_url", params) == null ? "" : Utils.getParameter("menu_url", params);
  304. String menu_img_url = Utils.getParameter("menu_img_url", params) == null ? "" : Utils.getParameter("menu_img_url", params);
  305. //String state = Utils.getParameter("state", params) == null ? "" : Utils.getParameter("state", params);
  306. String sort = Utils.getParameter("sort", params) == null ? "" : Utils.getParameter("sort", params);
  307. //封装一条数据
  308. IndexMenu indexMenu = new IndexMenu();
  309. indexMenu.setFD_OBJECTID(IdentityGenerator.getIdentityGenerator().gerenalIdentity(394));
  310. indexMenu.setBUSINESS_URL(menu_url);
  311. indexMenu.setIMAGE_URL(menu_img_url);
  312. indexMenu.setIS_DEL("0");
  313. indexMenu.setSTATE("0");
  314. indexMenu.setCLICK_NUM(0);
  315. indexMenu.setTITLE_NAME(menu_name);
  316. indexMenu.setSORT(sort);
  317. indexMenu.setUSER_ID(user_id);
  318. Date date = new Date();
  319. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  320. indexMenu.setUPDATEDAE(sdf.format(date));
  321. indexMenu.setMENU_ID(menu_id);
  322. result = mDao.addIndexMenu(indexMenu);
  323. if("ok".equals(result)){
  324. List<IndexMenu> list = mDao.queryIndexMenu(user_id);
  325. result = JsonPluginsUtil.listToJson(list);
  326. }
  327. }
  328. return result;
  329. }
  330. /**
  331. * 用户工作台菜单使用频率计数
  332. * @param params
  333. * @return
  334. */
  335. @POST
  336. @ProduceMime("html/text")
  337. @Path("/setUserClickNum/")
  338. public String setUserClickNum(String params){
  339. String result = "error";
  340. SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl();
  341. String menu_id = Utils.getParameter("menu_id", params) == null ? "" : Utils.getParameter("menu_id", params);
  342. String user_id = Utils.getParameter("user_id", params) == null ? "" : Utils.getParameter("user_id", params);
  343. result = mDao.setUserClickNum(menu_id,user_id);
  344. log.info(user_id+"===改变=====菜单:"+menu_id+"==========="+result);
  345. return result;
  346. }
  347. /**
  348. * 删除工作台菜单
  349. * state=0为使用 state=1为移除
  350. * @param params
  351. * @return
  352. */
  353. @POST
  354. @ProduceMime("html/text")
  355. @Path("/deleteIndexMenu/")
  356. public String deleteIndexMenu(String params){
  357. String result = "error";
  358. String fd_id = Utils.getParameter("fd_id", params) == null ? "" : Utils.getParameter("fd_id", params);
  359. SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl();
  360. result = mDao.deleteAddMenu(fd_id);
  361. return result;
  362. }
  363. /**
  364. * 检查工作台菜单表中是否已存在该菜单,若存在则将其状态state改成1
  365. * 即使用状态
  366. * @param menu_id
  367. * @param user_id
  368. * @return
  369. */
  370. private boolean checkAddMenu(String user_id, String menu_id) {
  371. boolean flag = false;
  372. int result = 0;
  373. SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl();
  374. result = mDao.checkAddMenu(user_id,menu_id);
  375. if(result==1){
  376. flag = true;
  377. }
  378. return flag;
  379. }
  380. /**
  381. * 查询个人偏好菜单
  382. * @param params
  383. * @return
  384. */
  385. @POST
  386. @ProduceMime("application/json")
  387. @Path("/queryIndexMenuByroleId/")
  388. public String queryIndexMenuByroleId(String params){
  389. String user_id = Utils.getParameter("user_id", params) == null ?
  390. "" : Utils.getParameter("user_id", params);
  391. String moduleStr = "{\"state\":\"ok\",\"length\":0,\"resultData\":\"[]\"}";
  392. if(user_id!=null&&!"".equals(user_id)){
  393. SysCustomMenuDaoImpl mDao = new SysCustomMenuDaoImpl();
  394. List<IndexMenu> result = mDao.queryIndexMenu(user_id);
  395. int len = result.size();
  396. String resultStr = JsonPluginsUtil.listToJson(result);
  397. moduleStr = "{\"state\":\"ok\",\"length\":"+len+",\"resultData\":"+resultStr+"}";
  398. }
  399. log.info(user_id+"==工作台菜单查询==:"+moduleStr);
  400. return moduleStr;
  401. }
  402. /**
  403. * 功能:根据角色查询出功能菜单树的信息
  404. * @param roleid 角色编号
  405. */
  406. @SuppressWarnings("unchecked")
  407. @POST
  408. @ProduceMime("application/json")
  409. @Path("/getMenuTreeByRoleId/")
  410. public String getMenuTreeByRoleId(String params){
  411. //获取角色id
  412. String roleid = Utils.getParameter("roleid", params) == null ? "" : Utils.getParameter("roleid", params);
  413. String isAdmin = Utils.getParameter("isAdmin", params) == null ? "" : Utils.getParameter("isAdmin", params);
  414. ISysCustomMenuDao dao = new SysCustomMenuDaoImpl();
  415. //如果角色值为ADMIN_ROLEID常量的值,则表明是管理员角色,查出所有权限
  416. List<Map<String, String>> list = null;
  417. //查询左边树数据
  418. if("1".equals(isAdmin)){
  419. list = dao.loadMenuInfo();
  420. log.info("-- "+JSONArray.fromObject(list).toString());
  421. return JSONArray.fromObject(list).toString().toLowerCase();
  422. }else{
  423. list = dao.loadMenuInfoByRoleId(roleid);
  424. //处理数据
  425. Set set = processData(list);
  426. log.info("--- "+JSONArray.fromObject(set).toString());
  427. return JSONArray.fromObject(set).toString().toLowerCase();
  428. }
  429. }
  430. /**
  431. * 功能:根据角色查询出自定义菜单显示树的信息
  432. * @param roleid 角色编号
  433. */
  434. @SuppressWarnings("unchecked")
  435. @POST
  436. @ProduceMime("application/json")
  437. @Path("/getCustomTreeByRoleId/")
  438. public String getCustomTreeByRoleId(String params){
  439. //获取角色id
  440. String roleid = Utils.getParameter("roleid", params) == null ?
  441. "" : Utils.getParameter("roleid", params);
  442. log.info("----- "+roleid);
  443. ISysCustomMenuDao dao = new SysCustomMenuDaoImpl();
  444. List<Map<String, String>> list = null;
  445. //查询右边树数据
  446. list = dao.loadCustomMenuInfoByRoleId(roleid);
  447. log.info(JSONArray.fromObject(list).toString());
  448. return JSONArray.fromObject(list).toString().toLowerCase();
  449. }
  450. /**
  451. * 功能:根据角色复制菜单权限
  452. * @param params
  453. */
  454. @POST
  455. @ProduceMime("application/json")
  456. @Path("/grantMenu")
  457. public Msg grantMenu(String params){
  458. Gson gson = new Gson();
  459. ISysCustomMenuDao dao = new SysCustomMenuDaoImpl();
  460. OperateAble persistence = new OperateImpl();
  461. DataObject dataObject = null;
  462. Msg msg = null;
  463. log.info("params--- "+params);
  464. try {
  465. params = java.net.URLDecoder.decode(params, "utf-8");
  466. CustomTreeVO customTree = gson.fromJson(params, CustomTreeVO.class);
  467. //保存数据之前,先删除数据
  468. dao.clearCustomMenuByRoleId(customTree.getRoleid());
  469. //模型方式保存数据
  470. for(CustomTreeNodeVO ct : customTree.getNodes()){
  471. dataObject = new DataObject();
  472. /* dataObject.setClassid(892);
  473. //为保证SYS_CUSTOM_MENU表中的id和pid的值不与实际功能菜单中的id和pid的值冲突,故要做规则处理
  474. dataObject.addAttribute(new Field("id", ct.getId()));
  475. dataObject.addAttribute(new Field("pid", "null".equals(ct.getPid()) ? "10" : ct.getPid()));
  476. dataObject.addAttribute(new Field("roleid", customTree.getRoleid()));
  477. dataObject.addAttribute(new Field("name", "null".equals(ct.getName()) ? "" : ct.getName()));
  478. dataObject.addAttribute(new Field("sort", "null".equals(ct.getSort()) ? "" : ct.getSort()));
  479. dataObject.addAttribute(new Field("type", "null".equals(ct.getType()) ? "" : ct.getType()));
  480. dataObject.addAttribute(new Field("handler", "null".equals(ct.getHandler()) ? "" : ct.getHandler()));
  481. //如果handler和type值都为”1“, 则表明该菜单项是可显示的功能菜单,所有要关联实际功能菜单的id值
  482. if("1".equals(ct.getHandler()) && "1".equals(ct.getType())){
  483. dataObject.addAttribute(new Field("menuid", ct.getId()));
  484. }*/
  485. dataObject.setClassid(886);
  486. //为保证SYS_CUSTOM_MENU表中的id和pid的值不与实际功能菜单中的id和pid的值冲突,故要做规则处理
  487. // dataObject.addAttribute(new Field("FD_OBJECTID", customTree.getRoleid()+ct.getId()));
  488. dataObject.addAttribute(new Field("ROLE_ID", customTree.getRoleid()));
  489. dataObject.addAttribute(new Field("MENU_ID", ct.getId()));
  490. dataObject.addAttribute(new Field("IS_DEL", "0"));
  491. log.info("-------------");
  492. msg = persistence.createDataObject(dataObject);
  493. }
  494. } catch (UnsupportedEncodingException e) {
  495. log.error(e);
  496. } catch (TransformTypeException e) {
  497. log.error(e);
  498. } catch (PersistenceException e) {
  499. log.error(e);
  500. }
  501. return msg;
  502. }
  503. /**
  504. * 功能:根据角色获取显示菜单数据
  505. * @return
  506. */
  507. @POST
  508. @ProduceMime("application/json")
  509. @Path("/getShowMenuInfoByRoleId")
  510. public String getShowMenuInfoByRoleId(String params){
  511. String roleid = Utils.getParameter("roleid", params) == null ?
  512. "" : Utils.getParameter("roleid", params);
  513. String nodeId = Utils.getParameter("nodeid", params) == null ?
  514. "10" : Utils.getParameter("nodeid", params);
  515. ISysCustomMenuDao dao = new SysCustomMenuDaoImpl();
  516. List retList = new ArrayList();
  517. //把菜单数据全部读到内存中
  518. showMenuList = dao.loadShowMenuInfoByRoleId(roleid);
  519. retList = findChildren(nodeId);
  520. log.info(JSONArray.fromObject(retList).toString().toLowerCase());
  521. return JSONArray.fromObject(retList).toString();
  522. };
  523. /**
  524. * 功能:根据权限过滤功能菜单
  525. * @param list 源数据集
  526. * @return Set 结果数据集
  527. */
  528. private Set processData(List<Map<String, String>> list) {
  529. Set<Map<String, String>> set = new HashSet<Map<String, String>>();
  530. if(list != null){
  531. for(int i = 0, len = list.size(); i < len; i++){
  532. Map<String, String> entry = list.get(i);
  533. String value = entry.get("menuid");
  534. if(value == null) continue;
  535. if("".equals(value)) continue;
  536. if("null".equals(value)) continue;
  537. set.add(entry);
  538. this.findParent(set, list, entry);
  539. }
  540. }
  541. return set;
  542. }
  543. /**
  544. * 功能:递归查询父节点
  545. * @param list
  546. * @param srcList
  547. * @param value
  548. */
  549. private void findParent(Set<Map<String, String>> list,
  550. List<Map<String, String>> srcList,
  551. Map<String, String> value){
  552. for(int i = 0, len = srcList.size(); i < len; i++){
  553. Map<String, String> entry = srcList.get(i);
  554. if(value.get("pid").equals(entry.get("id"))){
  555. list.add(entry);
  556. this.findParent(list, srcList, entry);
  557. }
  558. }
  559. }
  560. /***
  561. * 递归查询显示菜单的树形结构数据
  562. * 算法思路:先把所有的数据查询出来并放到内存中,再进行递归查询
  563. * @param nodeId 当前节点的id
  564. * @return
  565. */
  566. private List findChildren(String nodeId){
  567. List list = new ArrayList();
  568. if(showMenuList.size() > 0){
  569. for(int i = 0, len = showMenuList.size(); i < len; i++){
  570. Map entry = (Map)showMenuList.get(i);
  571. //获取pid
  572. String pid = (String)entry.get("PID");
  573. //获取id
  574. String id = (String)entry.get("ID");
  575. //如果该条数据等于pid,则加入集合当中
  576. if(nodeId.equals(pid)){
  577. entry.put("leaf", (this.isLeaf(id)));
  578. entry.put("children", findChildren(id));
  579. list.add(entry);
  580. }
  581. }
  582. }
  583. return list;
  584. }
  585. private boolean isLeaf(String nodeId){
  586. boolean flag = true;
  587. int n = 0;
  588. for(int i = 0, len = showMenuList.size(); i < len; i++){
  589. Map entry = (Map)showMenuList.get(i);
  590. //获取pid
  591. String pid = (String)entry.get("PID");
  592. //如果该条数据等于pid,则加入集合当中
  593. if(pid.equals(nodeId)){
  594. n++;
  595. }
  596. }
  597. if (n > 0) {
  598. flag = false;
  599. }
  600. return flag;
  601. }
  602. }