|  |  |  | 
|---|
|  |  |  | package com.moral.api.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.moral.api.entity.ManageMenu; | 
|---|
|  |  |  | import com.moral.api.entity.ManageRole; | 
|---|
|  |  |  | import com.moral.api.mapper.ManageRoleMapper; | 
|---|
|  |  |  | import com.moral.api.mapper.ManageRoleMenuMapper; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.moral.api.entity.*; | 
|---|
|  |  |  | import com.moral.api.mapper.*; | 
|---|
|  |  |  | import com.moral.api.service.ManageRoleService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.api.util.LogUtils; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.ResponseCodeEnum; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.util.StringUtils; | 
|---|
|  |  |  | import org.springframework.web.context.request.RequestContextHolder; | 
|---|
|  |  |  | import org.springframework.web.context.request.ServletRequestAttributes; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import java.text.SimpleDateFormat; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * <p> | 
|---|
|  |  |  | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | @Autowired(required = false) | 
|---|
|  |  |  | private ManageRoleMapper manageRoleMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | @Autowired(required = false) | 
|---|
|  |  |  | private ManageMenuMapper manageMenuMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired(required = false) | 
|---|
|  |  |  | private ManageRoleMenuMapper manageRoleMenuMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired(required = false) | 
|---|
|  |  |  | private ManageAccountRoleMapper manageAccountRoleMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | LogUtils logUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public List<ManageRole> getAll() { | 
|---|
|  |  |  | return manageRoleMapper.getAll(); | 
|---|
|  |  |  | public Map<String,Object> getAllWithPagingQuery(Map map) { | 
|---|
|  |  |  | Map<String,Object> resultMap = new HashMap<>(); | 
|---|
|  |  |  | int current = Integer.parseInt(map.get("current").toString()); | 
|---|
|  |  |  | int size = Integer.parseInt(map.get("size").toString()); | 
|---|
|  |  |  | com.baomidou.mybatisplus.extension.plugins.pagination.Page<ManageRole> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size); | 
|---|
|  |  |  | QueryWrapper<ManageRole> wrapper = new QueryWrapper(); | 
|---|
|  |  |  | wrapper.eq("is_delete",0); | 
|---|
|  |  |  | wrapper.orderByDesc("create_time"); | 
|---|
|  |  |  | com.baomidou.mybatisplus.extension.plugins.pagination.Page resultpage = manageRoleMapper.selectPage(page,wrapper); | 
|---|
|  |  |  | List<ManageRole> manageRoles = resultpage.getRecords(); | 
|---|
|  |  |  | SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
|---|
|  |  |  | List<Map<String,Object>> manageRolesList = new ArrayList<>(); | 
|---|
|  |  |  | for (ManageRole manageRole:manageRoles) { | 
|---|
|  |  |  | Map manageRoleMap = JSON.parseObject(JSON.toJSONString(manageRole),Map.class); | 
|---|
|  |  |  | String createTime = SDF.format(manageRole.getCreateTime()); | 
|---|
|  |  |  | String updateTime = SDF.format(manageRole.getUpdateTime()); | 
|---|
|  |  |  | manageRoleMap.put("createTime",createTime); | 
|---|
|  |  |  | manageRoleMap.put("updateTime",updateTime); | 
|---|
|  |  |  | manageRoleMap.put("key",manageRole.getId()); | 
|---|
|  |  |  | manageRolesList.add(manageRoleMap); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | resultMap.put("manageRoles",manageRolesList); | 
|---|
|  |  |  | int totalNumber = manageRoleMapper.selectCount(wrapper); | 
|---|
|  |  |  | resultMap.put("totalNumber",totalNumber); | 
|---|
|  |  |  | resultMap.put("current",current); | 
|---|
|  |  |  | int totalPageNumber = totalNumber/size; | 
|---|
|  |  |  | if(totalNumber%size != 0){ | 
|---|
|  |  |  | totalPageNumber += 1; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | resultMap.put("totalPageNumber",totalPageNumber); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public Map<String,Object> insertOne(ManageRole manageRole,List list) { | 
|---|
|  |  |  | public Map<String,Object> insertOne(ManageRole manageRole, List list) { | 
|---|
|  |  |  | Map<String,Object> resultMap = new HashMap<>(); | 
|---|
|  |  |  | if (manageRole.getName()==null){ | 
|---|
|  |  |  | resultMap.put("flag",false); | 
|---|
|  |  |  | resultMap.put("msg","名称为空!"); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (manageRoleMapper.getManageRoleByName(manageRole.getName()) != null){ | 
|---|
|  |  |  | resultMap.put("flag",false); | 
|---|
|  |  |  | resultMap.put("msg","该角色已存在!"); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | manageRoleMapper.insertOne(manageRole); | 
|---|
|  |  |  | resultMap.put("flag",true); | 
|---|
|  |  |  | resultMap.put("msg","添加成功!"); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.ROLE_IS_EXIST.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.ROLE_IS_EXIST.getMsg()); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | boolean flag = Boolean.parseBoolean(resultMap.get("flag").toString()); | 
|---|
|  |  |  | if (flag){ | 
|---|
|  |  |  | ManageRole manageRoleIns = manageRoleMapper.getManageRoleByName(manageRole.getName()); | 
|---|
|  |  |  | int role_id = manageRoleIns.getId(); | 
|---|
|  |  |  | System.out.println(role_id); | 
|---|
|  |  |  | manageRoleMapper.insertOne(manageRole); | 
|---|
|  |  |  | ManageRole manageRoleIns = manageRoleMapper.getManageRoleByName(manageRole.getName()); | 
|---|
|  |  |  | int role_id = manageRoleIns.getId(); | 
|---|
|  |  |  | if (list.size()>0){ | 
|---|
|  |  |  | //补充父菜单 | 
|---|
|  |  |  | List supplementList = new ArrayList(); | 
|---|
|  |  |  | supplementList.addAll(list); | 
|---|
|  |  |  | for (int i=0;i<supplementList.size();i++) { | 
|---|
|  |  |  | supplementList.addAll(supplementParentMenus(Integer.parseInt(supplementList.get(i).toString()))); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LinkedHashSet<Object> hashSet = new LinkedHashSet<>(supplementList); | 
|---|
|  |  |  | ArrayList<Object> menuList = new ArrayList<>(hashSet); | 
|---|
|  |  |  | List insertList = new ArrayList(); | 
|---|
|  |  |  | for (Object temp: list) { | 
|---|
|  |  |  | for (Object temp: menuList) { | 
|---|
|  |  |  | Map<String,Integer> insertMap = new HashMap<>(); | 
|---|
|  |  |  | insertMap.put("role_id",role_id); | 
|---|
|  |  |  | insertMap.put("menu_id",Integer.parseInt(temp.toString())); | 
|---|
|  |  |  | insertList.add(insertMap); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | System.out.println(insertList); | 
|---|
|  |  |  | manageRoleMenuMapper.insertBatch(insertList); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //操作插入日志 | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|---|
|  |  |  | String content = "添加角色:"+manageRole.getName()+";"; | 
|---|
|  |  |  | logUtils.saveOperationForManage(request,content,Constants.INSERT_OPERATE_TYPE); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | public Map<String,Object> updateManageRole(Map map) { | 
|---|
|  |  |  | Map<String,Object> resultMap = new HashMap<>(); | 
|---|
|  |  |  | if (!map.containsKey("id")){ | 
|---|
|  |  |  | resultMap.put("flag",false); | 
|---|
|  |  |  | resultMap.put("msg","未选择更新目标!"); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (manageRoleMapper.getManageRoleById(Integer.parseInt(map.get("id").toString())) == null){ | 
|---|
|  |  |  | resultMap.put("flag",false); | 
|---|
|  |  |  | resultMap.put("msg","该角色不存在!"); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.ROLE_IS_NULL.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.ROLE_IS_NULL.getMsg()); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | //在更新之前获取原来信息 | 
|---|
|  |  |  | ManageRole manageRoleOld = manageRoleMapper.selectById(Integer.parseInt(map.get("id").toString())); | 
|---|
|  |  |  | manageRoleMapper.updateManageRoleById(map); | 
|---|
|  |  |  | resultMap.put("flag",true); | 
|---|
|  |  |  | resultMap.put("msg","更新成功!"); | 
|---|
|  |  |  | //操作插入日志 | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|---|
|  |  |  | String content = "修改了角色:"+manageRoleOld.getName()+";"; | 
|---|
|  |  |  | for (Object key:map.keySet()) { | 
|---|
|  |  |  | if (key.toString().equals("name")&&map.get(key)!=null){ | 
|---|
|  |  |  | content = content+"角色名:"+manageRoleOld.getName()+"->"+map.get(key)+";"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (key.toString().equals("desc")&&map.get(key)!=null){ | 
|---|
|  |  |  | content = content+"备注:"+manageRoleOld.getDesc()+"->"+map.get(key)+";"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | logUtils.saveOperationForManage(request,content,Constants.UPDATE_OPERATE_TYPE); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public List<ManageRole> getManageRoleByNameFuzzy(ManageRole manageRole) { | 
|---|
|  |  |  | return manageRoleMapper.getManageRoleByNameFuzzy(manageRole); | 
|---|
|  |  |  | public Map<String,Object> getManageRoleByNameFuzzy(Map map) { | 
|---|
|  |  |  | Map<String,Object> resultMap = new HashMap<>(); | 
|---|
|  |  |  | int current = Integer.parseInt(map.get("current").toString()); | 
|---|
|  |  |  | int size = Integer.parseInt(map.get("size").toString()); | 
|---|
|  |  |  | com.baomidou.mybatisplus.extension.plugins.pagination.Page<ManageRole> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(current, size); | 
|---|
|  |  |  | QueryWrapper<ManageRole> wrapper = new QueryWrapper(); | 
|---|
|  |  |  | wrapper.eq("is_delete",0); | 
|---|
|  |  |  | wrapper.like("name",map.get("name")); | 
|---|
|  |  |  | wrapper.orderByDesc("create_time"); | 
|---|
|  |  |  | com.baomidou.mybatisplus.extension.plugins.pagination.Page resultpage = manageRoleMapper.selectPage(page,wrapper); | 
|---|
|  |  |  | List<ManageRole> manageRoles = resultpage.getRecords(); | 
|---|
|  |  |  | SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
|---|
|  |  |  | List<Map<String,Object>> manageRolesList = new ArrayList<>(); | 
|---|
|  |  |  | for (ManageRole manageRole:manageRoles) { | 
|---|
|  |  |  | Map manageRoleMap = JSON.parseObject(JSON.toJSONString(manageRole),Map.class); | 
|---|
|  |  |  | String createTime = SDF.format(manageRole.getCreateTime()); | 
|---|
|  |  |  | String updateTime = SDF.format(manageRole.getUpdateTime()); | 
|---|
|  |  |  | manageRoleMap.put("createTime",createTime); | 
|---|
|  |  |  | manageRoleMap.put("updateTime",updateTime); | 
|---|
|  |  |  | manageRolesList.add(manageRoleMap); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | resultMap.put("manageRoles",manageRolesList); | 
|---|
|  |  |  | int totalNumber = manageRoleMapper.selectCount(wrapper); | 
|---|
|  |  |  | resultMap.put("totalNumber",totalNumber); | 
|---|
|  |  |  | resultMap.put("current",current); | 
|---|
|  |  |  | int totalPageNumber = totalNumber/size; | 
|---|
|  |  |  | if(totalNumber%size != 0){ | 
|---|
|  |  |  | totalPageNumber += 1; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | resultMap.put("totalPageNumber",totalPageNumber); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Map<String, Object> deleteManageRole(Map map) { | 
|---|
|  |  |  | Map<String,Object> resultMap = new HashMap<>(); | 
|---|
|  |  |  | if (!map.containsKey("id")){ | 
|---|
|  |  |  | resultMap.put("flag",false); | 
|---|
|  |  |  | resultMap.put("msg","未选择删除目标!"); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (manageRoleMapper.getManageRoleById(Integer.parseInt(map.get("id").toString())) == null){ | 
|---|
|  |  |  | resultMap.put("flag",false); | 
|---|
|  |  |  | resultMap.put("msg","该角色不存在或已被删除!"); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.ROLE_IS_NULL.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.ROLE_IS_NULL.getMsg()); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | ManageRole manageRole = new ManageRole(); | 
|---|
|  |  |  | manageRole.setId(Integer.parseInt(map.get("id").toString())); | 
|---|
|  |  |  | manageRole.setIsDelete("1"); | 
|---|
|  |  |  | manageRole.setIsDelete(Constants.DELETE); | 
|---|
|  |  |  | Map deleteMap = new HashMap(); | 
|---|
|  |  |  | deleteMap.put("id",Integer.parseInt(map.get("id").toString())); | 
|---|
|  |  |  | deleteMap.put("is_delete",1); | 
|---|
|  |  |  | manageRoleMapper.updateManageRoleById(deleteMap); | 
|---|
|  |  |  | resultMap.put("flag",true); | 
|---|
|  |  |  | resultMap.put("msg","删除成功!"); | 
|---|
|  |  |  | ManageAccountRole manageAccountRole = new ManageAccountRole(); | 
|---|
|  |  |  | manageAccountRole.setIsDelete("1"); | 
|---|
|  |  |  | QueryWrapper<ManageAccountRole> wrapper = new QueryWrapper(); | 
|---|
|  |  |  | wrapper.eq("role_id",Integer.parseInt(map.get("id").toString())); | 
|---|
|  |  |  | manageAccountRoleMapper.update(manageAccountRole,wrapper); | 
|---|
|  |  |  | //操作插入日志 | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|---|
|  |  |  | ManageRole manageRole1 = manageRoleMapper.selectById(Integer.parseInt(map.get("id").toString())); | 
|---|
|  |  |  | String content = "删除角色:"+manageRole1.getName()+";"; | 
|---|
|  |  |  | logUtils.saveOperationForManage(request,content,Constants.DELETE_OPERATE_TYPE); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public ManageRole getRoleByAccountId(Integer accountId) { | 
|---|
|  |  |  | QueryWrapper<ManageAccountRole> queryAccountRoleWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | queryAccountRoleWrapper.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | queryAccountRoleWrapper.eq("account_id",accountId); | 
|---|
|  |  |  | ManageAccountRole accountRole = manageAccountRoleMapper.selectOne(queryAccountRoleWrapper); | 
|---|
|  |  |  | if(ObjectUtils.isEmpty(accountRole)) | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | Integer roleId = accountRole.getRoleId(); | 
|---|
|  |  |  | QueryWrapper<ManageRole> queryRoleWrapper = new QueryWrapper(); | 
|---|
|  |  |  | queryRoleWrapper.eq("is_delete", Constants.NOT_DELETE); | 
|---|
|  |  |  | queryRoleWrapper.eq("id", roleId); | 
|---|
|  |  |  | return  manageRoleMapper.selectOne(queryRoleWrapper); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 补充父菜单 | 
|---|
|  |  |  | * @Param: [list, menuId]list:menuId集合 | 
|---|
|  |  |  | * @return: void | 
|---|
|  |  |  | * @Author: 李子杰 | 
|---|
|  |  |  | * @Date: 2021/4/28 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private List supplementParentMenus(int menuId) { | 
|---|
|  |  |  | List list = new ArrayList(); | 
|---|
|  |  |  | ManageMenu manageMenu = manageMenuMapper.getManageMenuById(menuId); | 
|---|
|  |  |  | if (manageMenu.getParentId()!=0){ | 
|---|
|  |  |  | list.add(manageMenu.getParentId()); | 
|---|
|  |  |  | list.addAll(supplementParentMenus(manageMenu.getParentId())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|