package com.shlanbao.tzsc.pms.sys.wctMenu.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.shlanbao.tzsc.utils.tools.BeanConvertor; import com.shlanbao.tzsc.utils.tools.FillUserInfoUtil; import com.shlanbao.tzsc.utils.tools.LogAnno; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.shlanbao.tzsc.base.dao.WCTMenuDaoI; import com.shlanbao.tzsc.base.mapping.WCTMenu; import com.shlanbao.tzsc.base.model.Json; import com.shlanbao.tzsc.base.model.Tree; import com.shlanbao.tzsc.base.service.BaseService; import com.shlanbao.tzsc.pms.sys.wctMenu.service.WctMenuServiceI; import com.shlanbao.tzsc.utils.tools.StringUtil; import javax.servlet.http.HttpServletRequest; @Service public class WctMenuServiceImpl extends BaseService implements WctMenuServiceI { @Autowired private WCTMenuDaoI wctMenuDaoI; @Autowired private HttpServletRequest request; /** * 加载wct菜单.顶部菜单UPID=top 左侧主菜单UPID=root 左侧子菜单 UPID=数字 *

Description:

* @author shisihai * @date 2016下午4:53:17 */ @Override public List loadTroubleTree(String id) { List wctMenuTree=new ArrayList<>(); try{ Tree topTree=null;//顶部菜单 Tree leftTree=null;//左侧主菜单 Object params=null; List topResult=null;//顶部 List leftResult=null;//左侧 List lowResult=null;//左侧子菜单 //初始化时,加载顶部菜单 if(!StringUtil.notEmpty(id)){ params="top"; String hql="from WCTMenu o where o.upId=? order by o.grade asc"; topResult=wctMenuDaoI.query(hql, params); //加载左侧主菜单 for (WCTMenu topMenu : topResult) { topTree=setTreeVal(topMenu); params=topMenu.getModul(); hql="from WCTMenu o where o.modul=? and o.upId=? order by o.grade asc"; leftResult=wctMenuDaoI.query(hql, params,"root"); List leftTrees=new ArrayList<>(); //加载左侧子菜单 for (WCTMenu leftMenu : leftResult) { leftTree=setTreeVal(leftMenu); hql="from WCTMenu o where o.modul=? and o.upId=? order by o.grade asc"; lowResult=wctMenuDaoI.query(hql, params,leftMenu.getId()); leftTree.setChildren(setChildTrees(lowResult)); leftTrees.add(leftTree); } topTree.setChildren(leftTrees); wctMenuTree.add(topTree); } } }catch (Exception e) { e.printStackTrace(); } return wctMenuTree; } /** * 将wct菜单封装tree *

Description:

* @author shisihai * @date 2016上午8:38:55 */ private Tree setTreeVal(WCTMenu menu){ Tree tree=new Tree(); Map attrs=new HashMap<>(); tree.setId(menu.getId()); tree.setSeq((long)menu.getGrade()); tree.setPid(menu.getUpId()); tree.setText(menu.getMenu_title()); //自定义属性 attrs.put("menuUrl", menu.getMenu_url()); attrs.put("modul", menu.getModul()); attrs.put("imgPath", menu.getImgPath()); attrs.put("menuType", menu.getType()); attrs.put("del", menu.getDel()); attrs.put("attr1", menu.getAttr1()); attrs.put("attr2", menu.getAttr2()); attrs.put("attr3", menu.getAttr3()); attrs.put("attr4", menu.getAttr4()); tree.setAttributes(attrs); return tree; } private List setChildTrees(List menus){ List trees=new ArrayList<>(); if(menus!=null && menus.size()>0){ for (WCTMenu menu : menus) { trees.add(setTreeVal(menu)); } } return trees; } /** * 根据id查询wct菜单 *

Description:

* @author shisihai * @date 2016下午1:31:17 */ @Override public WCTMenu queryMenuById(String id) { return wctMenuDaoI.findById(WCTMenu.class, id); } /** * 修改WCT菜单 *

Description:

* @author shisihai * @date 2016下午1:36:18 */ @LogAnno(operateType = "修改WCT菜单") @Override public Json editWctMenu(WCTMenu menu) { Json json=new Json(); try { WCTMenu wctMenu = wctMenuDaoI.findById(WCTMenu.class, menu.getId()); BeanConvertor.copyProperties(menu,wctMenu); FillUserInfoUtil.fillUpdateUserInfo(wctMenu,request); wctMenuDaoI.saveOrUpdate(wctMenu); json.setSuccess(true); json.setMsg("修改菜单成功!"); } catch (Exception e) { e.printStackTrace(); json.setMsg("修改菜单失败!"); } return json; } /** * 新增菜单 *

Description:

* @author shisihai * @date 2016下午1:40:59 */ @LogAnno(operateType = "新增菜单") @Override public Json addWctMenu(WCTMenu menu) { Json json=new Json(); try { WCTMenu m=wctMenuDaoI.findById(WCTMenu.class, menu.getId()); if(m!=null){ json.setMsg("新增菜单失败!id重复!"); return json; } FillUserInfoUtil.fillCreateUserInfo(menu,request); wctMenuDaoI.save(menu); json.setSuccess(true); json.setMsg("新增菜单成功!"); } catch (Exception e) { e.printStackTrace(); json.setMsg("新增菜单失败!"); } return json; } }