| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.mapper.CommonMapper; |
| | | import com.whyc.mapper.PermitGroupMapper; |
| | | import com.whyc.mapper.PermitGroupUserMapper; |
| | | import com.whyc.mapper.UserInfMapper; |
| | | import com.whyc.pojo.PermitGroup; |
| | | import com.whyc.pojo.PermitGroupUser; |
| | | import com.whyc.pojo.UserInf; |
| | | import com.whyc.util.ActionUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.sql.Wrapper; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @Transactional |
| | |
| | | @Resource |
| | | private CommonMapper commonMapper; |
| | | |
| | | public void add(List<PermitGroup> permitGroupList) { |
| | | @Resource |
| | | private UserInfMapper userInfMapper; |
| | | |
| | | @Autowired |
| | | private UserService userService; |
| | | |
| | | @Transactional |
| | | public Response<Boolean> add(List<PermitGroup> permitGroupList) { |
| | | //首先校验当前用户所在的权限组,是否存在权限编辑权限 permit_edit_permit |
| | | if (!checkUserPermit()) return new Response<Boolean>().set(1, false, "当前用户没有权限编辑权限组"); |
| | | Response<Boolean> response = new Response(); |
| | | //权限互斥,查询已存在的权限组,不允许存在权限完全一样的权限组 |
| | | QueryWrapper<PermitGroup> wrapper = Wrappers.query(); |
| | | //wrapper.select("permit_group_id","permit_item_name","permit_item_value"); |
| | | wrapper.select("permit_group_id","permit_item_value").ge("permit_group_id",100); |
| | | List<PermitGroup> existGroupList = mapper.selectList(wrapper); |
| | | Map<Integer, List<PermitGroup>> existGroupListMap = existGroupList.stream().collect(Collectors.groupingBy(PermitGroup::getPermitGroupId)); |
| | | Set<Integer> keySet = existGroupListMap.keySet(); |
| | | Iterator<Integer> iterator = keySet.iterator(); |
| | | while (iterator.hasNext()){ |
| | | boolean diff = false; |
| | | List<PermitGroup> existGroupListII = existGroupListMap.get(iterator.next()); |
| | | for (int i = 0; i < existGroupListII.size(); i++) { |
| | | if(!permitGroupList.get(i).getPermitItemValue().equals(existGroupListII.get(i).getPermitItemValue())){ |
| | | //存在差异,进入下个组校验 |
| | | diff = true; |
| | | break; |
| | | } |
| | | } |
| | | //如果每项都不存在差异 |
| | | if(!diff){ |
| | | return response.set(1,false,"有存在权限完全相同的权限组,无法添加"); |
| | | } |
| | | } |
| | | //权限组id生成 |
| | | int maxValue = (int) commonMapper.getMaxValue("db_user", "tb_user_permitgroup", "permit_group_id"); |
| | | int newPermitGroupId = maxValue+1; |
| | | permitGroupList.stream().forEach(permitGroup -> permitGroup.setPermitGroupId(newPermitGroupId)); |
| | | mapper.insertBatchSomeColumn(permitGroupList); |
| | | return response.set(1,true,"添加成功"); |
| | | } |
| | | //验证权限 |
| | | private boolean checkUserPermit() { |
| | | Long uId = ActionUtil.getUser().getUId(); |
| | | List<String> itemList = permitGroupUserMapper.getItemList(uId); |
| | | if (!itemList.contains("permit_edit_permit")){ |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | //验证指定权限 |
| | | public boolean checkUserPermitWithName(String permitName) { |
| | | Long uId = ActionUtil.getUser().getUId(); |
| | | List<String> itemList = permitGroupUserMapper.getItemList(uId); |
| | | if (!itemList.contains(permitName)){ |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | |
| | | public void update(List<PermitGroup> permitGroupList) { |
| | | |
| | | @Transactional |
| | | public Response<Boolean> update(List<PermitGroup> permitGroupList) { |
| | | //首先校验当前用户所在的权限组,是否存在权限编辑权限 permit_edit_permit |
| | | if (!checkUserPermit()) return new Response<Boolean>().set(1, false, "当前用户没有权限编辑权限组"); |
| | | Response<Boolean> response = new Response<>(); |
| | | //权限互斥,查询已存在的权限组,不允许存在权限完全一样的权限组(应该剔除掉数据库中的自己权限组的校验,可以不更新) |
| | | QueryWrapper<PermitGroup> wrapper = Wrappers.query(); |
| | | //wrapper.select("permit_group_id","permit_item_name","permit_item_value"); |
| | | wrapper.select("permit_group_id","permit_item_value").ge("permit_group_id",100).ne("permit_group_id",permitGroupList.get(0).getPermitGroupId()); |
| | | List<PermitGroup> existGroupList = mapper.selectList(wrapper); |
| | | Map<Integer, List<PermitGroup>> existGroupListMap = existGroupList.stream().collect(Collectors.groupingBy(PermitGroup::getPermitGroupId)); |
| | | Set<Integer> keySet = existGroupListMap.keySet(); |
| | | Iterator<Integer> iterator = keySet.iterator(); |
| | | while (iterator.hasNext()){ |
| | | boolean diff = false; |
| | | List<PermitGroup> existGroupListII = existGroupListMap.get(iterator.next()); |
| | | for (int i = 0; i < existGroupListII.size(); i++) { |
| | | if(!permitGroupList.get(i).getPermitItemValue().equals(existGroupListII.get(i).getPermitItemValue())){ |
| | | //存在差异,进入下个组校验 |
| | | diff = true; |
| | | break; |
| | | } |
| | | } |
| | | //如果每项都不存在差异 |
| | | if(!diff){ |
| | | return response.set(1,false,"有存在权限完全相同的权限组,无法更新"); |
| | | } |
| | | } |
| | | //先删除,后增加 |
| | | UpdateWrapper<PermitGroup> wrapper = Wrappers.update(); |
| | | wrapper.eq("permit_group_id",permitGroupList.get(0).getPermitGroupId()); |
| | | mapper.delete(wrapper); |
| | | UpdateWrapper<PermitGroup> updateWrapper = Wrappers.update(); |
| | | updateWrapper.eq("permit_group_id",permitGroupList.get(0).getPermitGroupId()); |
| | | mapper.delete(updateWrapper); |
| | | |
| | | mapper.insertBatchSomeColumn(permitGroupList); |
| | | return response.set(1,true,"更新成功"); |
| | | } |
| | | |
| | | |
| | | public void delete(Integer permitGroupId) { |
| | | @Transactional |
| | | public boolean delete(Integer permitGroupId) { |
| | | //首先校验当前用户所在的权限组,是否存在权限编辑权限 permit_edit_permit |
| | | if (!checkUserPermit()) return false; |
| | | //删除权限组表记录 |
| | | UpdateWrapper<PermitGroup> groupWrapper = Wrappers.update(); |
| | | groupWrapper.eq("permit_group_id",permitGroupId); |
| | |
| | | UpdateWrapper<PermitGroupUser> groupUserWrapper = Wrappers.update(); |
| | | groupUserWrapper.eq("permit_group_id",permitGroupId); |
| | | permitGroupUserMapper.delete(groupUserWrapper); |
| | | return true; |
| | | |
| | | } |
| | | |
| | |
| | | wrapper.select("distinct permit_group_id,permit_group_name"); |
| | | return mapper.selectList(wrapper); |
| | | } |
| | | |
| | | public Map getPermitGroupInfo(Integer permitGroupId) { |
| | | Map<String,List> resultMap = new HashMap<>(); |
| | | //查询权限组已添加的用户 |
| | | List<UserInf> relatedUserInfList = permitGroupUserMapper.getUserInf(permitGroupId); |
| | | //查询权限组未添加的用户 |
| | | //List<UserInf> allUserInfList = userInfMapper.selectList(null); |
| | | //List<UserInf> notRelatedUserInfList = allUserInfList.stream().filter(userInf -> !relatedUserInfList.contains(userInf)).collect(Collectors.toList()); |
| | | //查询未被添加到所有权限组的用户 |
| | | List<UserInf> notRelatedUserInfList = userService.searchCS_All2(); |
| | | //查询权限组的明细权限 |
| | | QueryWrapper<PermitGroup> wrapper = Wrappers.query(); |
| | | wrapper.eq("permit_group_id",permitGroupId); |
| | | List<PermitGroup> permitGroupList = mapper.selectList(wrapper); |
| | | |
| | | resultMap.put("usersAdded",relatedUserInfList); |
| | | resultMap.put("usersNotAdded",notRelatedUserInfList); |
| | | resultMap.put("permits",permitGroupList); |
| | | return resultMap; |
| | | } |
| | | |
| | | public List<PermitGroup> getPermitList() { |
| | | QueryWrapper<PermitGroup> wrapper = Wrappers.query(); |
| | | wrapper.select("distinct permit_item_name"); |
| | | |
| | | List<PermitGroup> permitGroups = mapper.selectList(wrapper); |
| | | return permitGroups; |
| | | } |
| | | } |