Jelajahi Sumber

获取用户区域权限、修改五级下拉列表

zhj 2 tahun lalu
induk
melakukan
0304f5c809

+ 270 - 36
src/main/java/com/welampiot/controller/UserController.java

@@ -16,9 +16,9 @@ import org.springframework.web.bind.annotation.*;
 import javax.servlet.http.HttpServletRequest;
 import javax.validation.Valid;
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 @RestController
 @CrossOrigin
@@ -276,14 +276,13 @@ public class UserController {
         List<GlobalLocationDTO> provinceList;
         List<GlobalLocationDTO> cityList;
         List<GlobalLocationDTO> areaList;
-        List<SectionDTO> sectionList;
+        List<SectionDTO> sectionList = sectionService.getAllSectionList();
 
         if (pid == 0) {
             countryList = globalLocationService.getGlobalList(1, version);
             provinceList = globalLocationService.getGlobalList(2, version);
             cityList = globalLocationService.getGlobalList(3, version);
             areaList = globalLocationService.getGlobalList(4, version);
-            sectionList = sectionService.getAllSectionList();
             for (GlobalLocationDTO g : areaList) {
                 list2 = new ArrayList<>();
                 boolean isFlag = false;
@@ -319,11 +318,24 @@ public class UserController {
                     g.setAreaList(list1);
                     list2 = new ArrayList<>();
                     GlobalLocationDTO dto1 = g.getAreaList().get(0);
-                    SectionDTO sectionDTO = new SectionDTO();
-                    sectionDTO.setId(g.getId());
-                    sectionDTO.setName(g.getName());
-                    sectionDTO.setPid(g.getPid());
-                    list2.add(sectionDTO);
+                    boolean isRight = false;
+                    for (SectionDTO s : sectionList) {
+                        if (Objects.equals(g.getId(),s.getPid())) {
+                            SectionDTO sectionDTO = new SectionDTO();
+                            sectionDTO.setId(s.getId());
+                            sectionDTO.setName(s.getName());
+                            sectionDTO.setPid(s.getPid());
+                            list2.add(sectionDTO);
+                            isRight = true;
+                        }
+                    }
+                    if (!isRight) {
+                        SectionDTO sectionDTO = new SectionDTO();
+                        sectionDTO.setId(g.getId());
+                        sectionDTO.setName(g.getName());
+                        sectionDTO.setPid(g.getPid());
+                        list2.add(sectionDTO);
+                    }
                     dto1.setSectionList(list2);
                 }
             }
@@ -346,12 +358,25 @@ public class UserController {
                     list1.add(new GlobalLocationDTO(g.getId(),g.getPid(),g.getName()));
                     dto1.setAreaList(list1);
                     GlobalLocationDTO dto2 = g.getCityList().get(0).getAreaList().get(0);
-                    SectionDTO sectionDTO = new SectionDTO();
-                    sectionDTO.setId(g.getId());
-                    sectionDTO.setName(g.getName());
-                    sectionDTO.setPid(g.getPid());
                     list2 = new ArrayList<>();
-                    list2.add(sectionDTO);
+                    boolean isRight = false;
+                    for (SectionDTO s : sectionList) {
+                        if (Objects.equals(g.getId(),s.getPid())) {
+                            SectionDTO sectionDTO = new SectionDTO();
+                            sectionDTO.setId(s.getId());
+                            sectionDTO.setName(s.getName());
+                            sectionDTO.setPid(s.getPid());
+                            list2.add(sectionDTO);
+                            isRight = true;
+                        }
+                    }
+                    if (!isRight) {
+                        SectionDTO sectionDTO = new SectionDTO();
+                        sectionDTO.setId(g.getId());
+                        sectionDTO.setName(g.getName());
+                        sectionDTO.setPid(g.getPid());
+                        list2.add(sectionDTO);
+                    }
                     dto2.setSectionList(list2);
                 }
             }
@@ -379,12 +404,25 @@ public class UserController {
                     list1.add(new GlobalLocationDTO(dto.getId(),dto.getPid(),dto.getName()));
                     dto2.setAreaList(list1);
                     GlobalLocationDTO dto3 = g.getProvinceList().get(0).getCityList().get(0).getAreaList().get(0);
-                    SectionDTO sectionDTO = new SectionDTO();
-                    sectionDTO.setId(dto.getId());
-                    sectionDTO.setName(dto.getName());
-                    sectionDTO.setPid(dto.getPid());
                     list2 = new ArrayList<>();
-                    list2.add(sectionDTO);
+                    boolean isRight = false;
+                    for (SectionDTO s : sectionList) {
+                        if (Objects.equals(g.getId(),s.getPid())) {
+                            SectionDTO sectionDTO = new SectionDTO();
+                            sectionDTO.setId(s.getId());
+                            sectionDTO.setName(s.getName());
+                            sectionDTO.setPid(s.getPid());
+                            list2.add(sectionDTO);
+                            isRight = true;
+                        }
+                    }
+                    if (!isRight) {
+                        SectionDTO sectionDTO = new SectionDTO();
+                        sectionDTO.setId(g.getId());
+                        sectionDTO.setName(g.getName());
+                        sectionDTO.setPid(g.getPid());
+                        list2.add(sectionDTO);
+                    }
                     dto3.setSectionList(list2);
                 }
             }
@@ -394,7 +432,6 @@ public class UserController {
             provinceList = globalLocationService.getLocationListByPid(pid, version);
             cityList = globalLocationService.getCityListByPid(pid,version);
             areaList = globalLocationService.getAreaListByPid(pid,version);
-            sectionList = sectionService.getAllSectionListByPid(pid);
             for (GlobalLocationDTO g : areaList) {
                 list2 = new ArrayList<>();
                 boolean isFlag = false;
@@ -430,11 +467,24 @@ public class UserController {
                     g.setAreaList(list1);
                     list2 = new ArrayList<>();
                     GlobalLocationDTO dto1 = g.getAreaList().get(0);
-                    SectionDTO sectionDTO = new SectionDTO();
-                    sectionDTO.setId(g.getId());
-                    sectionDTO.setName(g.getName());
-                    sectionDTO.setPid(g.getPid());
-                    list2.add(sectionDTO);
+                    boolean isRight = false;
+                    for (SectionDTO s : sectionList) {
+                        if (Objects.equals(g.getId(),s.getPid())) {
+                            SectionDTO sectionDTO = new SectionDTO();
+                            sectionDTO.setId(s.getId());
+                            sectionDTO.setName(s.getName());
+                            sectionDTO.setPid(s.getPid());
+                            list2.add(sectionDTO);
+                            isRight = true;
+                        }
+                    }
+                    if (!isRight) {
+                        SectionDTO sectionDTO = new SectionDTO();
+                        sectionDTO.setId(g.getId());
+                        sectionDTO.setName(g.getName());
+                        sectionDTO.setPid(g.getPid());
+                        list2.add(sectionDTO);
+                    }
                     dto1.setSectionList(list2);
                 }
             }
@@ -457,12 +507,25 @@ public class UserController {
                     list1.add(new GlobalLocationDTO(g.getId(),g.getPid(),g.getName()));
                     dto1.setAreaList(list1);
                     GlobalLocationDTO dto2 = g.getCityList().get(0).getAreaList().get(0);
-                    SectionDTO sectionDTO = new SectionDTO();
-                    sectionDTO.setId(g.getId());
-                    sectionDTO.setName(g.getName());
-                    sectionDTO.setPid(g.getPid());
                     list2 = new ArrayList<>();
-                    list2.add(sectionDTO);
+                    boolean isRight = false;
+                    for (SectionDTO s : sectionList) {
+                        if (Objects.equals(g.getId(),s.getPid())) {
+                            SectionDTO sectionDTO = new SectionDTO();
+                            sectionDTO.setId(s.getId());
+                            sectionDTO.setName(s.getName());
+                            sectionDTO.setPid(s.getPid());
+                            list2.add(sectionDTO);
+                            isRight = true;
+                        }
+                    }
+                    if (!isRight) {
+                        SectionDTO sectionDTO = new SectionDTO();
+                        sectionDTO.setId(g.getId());
+                        sectionDTO.setName(g.getName());
+                        sectionDTO.setPid(g.getPid());
+                        list2.add(sectionDTO);
+                    }
                     dto2.setSectionList(list2);
                 }
             }
@@ -480,12 +543,25 @@ public class UserController {
                 list1.add(new GlobalLocationDTO(dto.getId(),dto.getPid(),dto.getName()));
                 dto2.setAreaList(list1);
                 GlobalLocationDTO dto3 = dto.getProvinceList().get(0).getCityList().get(0).getAreaList().get(0);
-                SectionDTO sectionDTO = new SectionDTO();
-                sectionDTO.setId(dto.getId());
-                sectionDTO.setName(dto.getName());
-                sectionDTO.setPid(dto.getPid());
                 list2 = new ArrayList<>();
-                list2.add(sectionDTO);
+                boolean isRight = false;
+                for (SectionDTO s : sectionList) {
+                    if (Objects.equals(dto.getId(),s.getPid())) {
+                        SectionDTO sectionDTO = new SectionDTO();
+                        sectionDTO.setId(s.getId());
+                        sectionDTO.setName(s.getName());
+                        sectionDTO.setPid(s.getPid());
+                        list2.add(sectionDTO);
+                        isRight = true;
+                    }
+                }
+                if (!isRight) {
+                    SectionDTO sectionDTO = new SectionDTO();
+                    sectionDTO.setId(dto.getId());
+                    sectionDTO.setName(dto.getName());
+                    sectionDTO.setPid(dto.getPid());
+                    list2.add(sectionDTO);
+                }
                 dto3.setSectionList(list2);
             }
             countryList = new ArrayList<>();
@@ -927,4 +1003,162 @@ public class UserController {
         privilegeVO.setList(list);
         return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,privilegeVO);
     }
+
+    /**
+     * 获取区域权限列表
+     * @param request username
+     * @return 区域权限列表
+     */
+    @RequestMapping(value = "/sectionList", method = RequestMethod.POST)
+    public BaseResult<?> sectionList(HttpServletRequest request) {
+        Integer version = (Integer) toolUtils.getRequestContent(request,"version",1);
+        Integer id = (Integer) toolUtils.getRequestContent(request,"id",1);
+        if (id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
+        UserDTO userDTO = userService.getZoneListByUserid(id);
+        if (userDTO == null) return toolUtils.response(InterfaceResultEnum.PARAM_FAIL,version);
+        GlobalLocationVO globalLocationVO = new GlobalLocationVO();
+        globalLocationVO.setList(new ArrayList<>());
+        if (userDTO.getZoneList() == null || userDTO.getZoneList().isEmpty()) {
+            return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,globalLocationVO);
+        }
+
+        List<String> zones = Arrays.asList(userDTO.getZoneList().split(","));
+        List<GlobalLocationDTO> list1;
+        List<SectionDTO> list2;
+        List<GlobalLocationDTO> oneCountryList = globalLocationService.getCountryOfSectionList(version);
+        List<GlobalLocationDTO> provinceList = globalLocationService.getGlobalList(2, version);
+        List<GlobalLocationDTO> cityList = globalLocationService.getGlobalList(3, version);
+        List<GlobalLocationDTO> areaList = globalLocationService.getGlobalList(4, version);
+        List<SectionDTO> sectionList = sectionService.getListByIdList(zones);
+
+        // 将只有一级区域与所有二级区域合并
+        provinceList = Stream.of(provinceList, oneCountryList)
+                .flatMap(Collection::stream)
+                .collect(Collectors.toList());
+
+        List<GlobalLocationDTO> newAreaList = new ArrayList<>();
+        for (GlobalLocationDTO g : areaList) {
+            list2 = new ArrayList<>();
+            boolean isFlag = false;
+            for (SectionDTO s : sectionList) {
+                if (Objects.equals(g.getId(),s.getPid())) {
+                    s.setSelect(1);
+                    list2.add(s);
+                    isFlag = true;
+                }
+            }
+            if (isFlag) {
+                GlobalLocationDTO dto = new GlobalLocationDTO();
+                dto.setId(g.getId());
+                dto.setPid(g.getPid());
+                dto.setName(g.getName());
+                dto.setSectionList(list2);
+                newAreaList.add(dto);
+            }
+        }
+
+        List<GlobalLocationDTO> newCityList = new ArrayList<>();
+        for (GlobalLocationDTO g : cityList) {
+            list1 = new ArrayList<>();
+            boolean isFlag = false;
+            for (GlobalLocationDTO g1 : newAreaList) {
+                if (Objects.equals(g.getId(),g1.getPid())) {
+                    list1.add(g1);
+                    isFlag = true;
+                }
+            }
+            if (isFlag) {
+                GlobalLocationDTO dto = new GlobalLocationDTO();
+                dto.setId(g.getId());
+                dto.setPid(g.getPid());
+                dto.setName(g.getName());
+                dto.setAreaList(list1);
+                newCityList.add(dto);
+            }
+        }
+
+        for (GlobalLocationDTO g : cityList) {
+            list2 = new ArrayList<>();
+            list1 = new ArrayList<>();
+            boolean isFlag = false;
+            for (SectionDTO s : sectionList) {
+                if (Objects.equals(s.getPid(),g.getId())) {
+                    s.setSelect(1);
+                    list2.add(s);
+                    isFlag = true;
+                }
+            }
+            if (isFlag) {
+                GlobalLocationDTO dto = new GlobalLocationDTO();
+                dto.setId(g.getId());
+                dto.setPid(g.getPid());
+                dto.setName(g.getName());
+                dto.setSectionList(list2);
+                list1.add(dto);
+                dto = new GlobalLocationDTO();
+                dto.setId(g.getId());
+                dto.setPid(g.getPid());
+                dto.setName(g.getName());
+                dto.setAreaList(list1);
+                newCityList.add(dto);
+            }
+        }
+
+        List<GlobalLocationDTO> newProvinceList = new ArrayList<>();
+        for (GlobalLocationDTO g : provinceList) {
+            list1 = new ArrayList<>();
+            boolean isFlag = false;
+            for (GlobalLocationDTO g1 : newCityList) {
+                if (Objects.equals(g.getId(),g1.getPid())) {
+                    list1.add(g1);
+                    isFlag = true;
+                }
+            }
+            if (isFlag) {
+                GlobalLocationDTO dto = new GlobalLocationDTO();
+                dto.setId(g.getId());
+                dto.setPid(g.getPid());
+                dto.setName(g.getName());
+                dto.setCityList(list1);
+                newProvinceList.add(dto);
+            }
+        }
+
+        for (GlobalLocationDTO g : provinceList) {
+            list2 = new ArrayList<>();
+            list1 = new ArrayList<>();
+            boolean isFlag = false;
+            for (SectionDTO s : sectionList) {
+                if (Objects.equals(s.getPid(),g.getId())) {
+                    s.setSelect(1);
+                    list2.add(s);
+                    isFlag = true;
+                }
+            }
+            if (isFlag) {
+                GlobalLocationDTO dto = new GlobalLocationDTO();
+                dto.setId(g.getId());
+                dto.setPid(g.getPid());
+                dto.setName(g.getName());
+                dto.setSectionList(list2);
+                list1.add(dto);
+                dto = new GlobalLocationDTO();
+                dto.setId(g.getId());
+                dto.setPid(g.getPid());
+                dto.setName(g.getName());
+                dto.setAreaList(list1);
+                list1 = new ArrayList<>();
+                list1.add(dto);
+                dto = new GlobalLocationDTO();
+                dto.setId(g.getId());
+                dto.setPid(g.getPid());
+                dto.setName(g.getName());
+                dto.setCityList(list1);
+                newProvinceList.add(dto);
+            }
+        }
+
+        globalLocationVO.setList(newProvinceList);
+        return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,globalLocationVO);
+    }
 }

+ 4 - 0
src/main/java/com/welampiot/dao/GlobalLocationDao.java

@@ -36,4 +36,8 @@ public interface GlobalLocationDao {
     List<GlobalLocationDTO> getAreaListByPid(@Param("pid") Integer pid, @Param("version") Integer version);
 
     List<GlobalLocationDTO> getCityListByPid(@Param("pid") Integer pid, @Param("version") Integer version);
+
+    List<GlobalLocationDTO> getOnlyOneLevelList(@Param("level") Integer level,@Param("version") Integer version);
+
+    List<GlobalLocationDTO> getCountryOfSectionList(@Param("version") Integer version);
 }

+ 1 - 0
src/main/java/com/welampiot/dao/UserDao.java

@@ -36,4 +36,5 @@ public interface UserDao {
     void updateUserAuth(UserDTO dto);
     void updateUserArea(UserDTO dto);
     UserDTO getPrivilegeList(@Param("username") String username);
+    UserDTO getZoneListByUserid(@Param("id") Integer id);
 }

+ 4 - 0
src/main/java/com/welampiot/dto/GlobalLocationDTO.java

@@ -1,6 +1,8 @@
 package com.welampiot.dto;
 
+import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.NoArgsConstructor;
 
 import java.io.Serializable;
 import java.util.List;
@@ -15,6 +17,8 @@ import java.util.List;
  * @Version: v1.0
  */
 @Data
+@NoArgsConstructor
+@AllArgsConstructor
 public class GlobalLocationDTO implements Serializable {
     private Integer id;//主键
     private Integer id1;

+ 2 - 0
src/main/java/com/welampiot/dto/SectionDTO.java

@@ -66,5 +66,7 @@ public class SectionDTO implements Serializable {
 
     private List<Integer> sectionList;
 
+    private Integer select;
+
     private static final long serialVersionUID = 1L;
 }

+ 5 - 3
src/main/java/com/welampiot/service/GlobalLocationService.java

@@ -27,8 +27,10 @@ public interface GlobalLocationService {
     GlobalLocationDTO getOneById(@Param("id") Integer id);
     GlobalLocationDTO getGlobalLocationDTOById(Integer id, Integer version);
     List<GlobalLocationDTO> getLocationListByPid(Integer pid, Integer version);
-    List<GlobalLocationDTO> getGlobalList(@Param("level") Integer level,@Param("version") Integer version);
+    List<GlobalLocationDTO> getGlobalList(Integer level, Integer version);
     Integer getAreaTimezoneById(Integer id);
-    List<GlobalLocationDTO> getAreaListByPid(@Param("pid") Integer pid, @Param("version") Integer version);
-    List<GlobalLocationDTO> getCityListByPid(@Param("pid") Integer pid, @Param("version") Integer version);
+    List<GlobalLocationDTO> getAreaListByPid(Integer pid, Integer version);
+    List<GlobalLocationDTO> getCityListByPid(Integer pid, Integer version);
+    List<GlobalLocationDTO> getOnlyOneLevelList(Integer level, Integer version);
+    List<GlobalLocationDTO> getCountryOfSectionList(Integer version);
 }

+ 1 - 0
src/main/java/com/welampiot/service/UserService.java

@@ -35,4 +35,5 @@ public interface UserService {
     void updateUserAuth(UserDTO dto);
     void updateUserArea(UserDTO dto);
     UserDTO getPrivilegeList(String username);
+    UserDTO getZoneListByUserid(Integer id);
 }

+ 10 - 0
src/main/java/com/welampiot/service/impl/GlobalLocationServiceImpl.java

@@ -190,4 +190,14 @@ public class GlobalLocationServiceImpl implements GlobalLocationService {
     public List<GlobalLocationDTO> getCityListByPid(Integer pid, Integer version) {
         return globalLocationDao.getCityListByPid(pid,version);
     }
+
+    @Override
+    public List<GlobalLocationDTO> getOnlyOneLevelList(Integer level, Integer version) {
+        return globalLocationDao.getOnlyOneLevelList(level,version);
+    }
+
+    @Override
+    public List<GlobalLocationDTO> getCountryOfSectionList(Integer version) {
+        return globalLocationDao.getCountryOfSectionList(version);
+    }
 }

+ 5 - 0
src/main/java/com/welampiot/service/impl/UserServiceImpl.java

@@ -159,4 +159,9 @@ public class UserServiceImpl implements UserService {
     public UserDTO getPrivilegeList(String username) {
         return userDao.getPrivilegeList(username);
     }
+
+    @Override
+    public UserDTO getZoneListByUserid(Integer id) {
+        return userDao.getZoneListByUserid(id);
+    }
 }

+ 44 - 0
src/main/resources/mapper/GlobalLocationMapper.xml

@@ -196,5 +196,49 @@
         WHERE
             c.id = #{pid}
     </select>
+
+    <select id="getOnlyOneLevelList" resultType="GlobalLocationDTO">
+        SELECT
+            g.id,
+            g.pid
+            <choose>
+                <when test="version == 0">
+                    ,g.chinese_name AS `name`
+                </when>
+                <when test="version == 1">
+                    ,g.english_name AS `name`
+                </when>
+                <otherwise>
+                    ,g.ru_name AS `name`
+                </otherwise>
+            </choose>
+        FROM
+            global_location g
+            LEFT JOIN global_location a ON a.pid = g.id
+        WHERE
+            g.`level` = #{level}
+        AND a.id IS NULL
+    </select>
+
+    <select id="getCountryOfSectionList" resultType="com.welampiot.dto.GlobalLocationDTO">
+        SELECT DISTINCT
+            g.id,
+            g.pid
+            <choose>
+                <when test="version == 0">
+                    ,g.chinese_name AS `name`
+                </when>
+                <when test="version == 1">
+                    ,g.english_name AS `name`
+                </when>
+                <otherwise>
+                    ,g.ru_name AS `name`
+                </otherwise>
+            </choose>
+        FROM section s
+        LEFT JOIN global_location g
+        ON g.id = s.pid
+        WHERE g.level = 1
+    </select>
     
 </mapper>

+ 1 - 1
src/main/resources/mapper/SectionMapper.xml

@@ -16,7 +16,7 @@
         </foreach>
     </select>
     <select id="getListByIdList" parameterType="java.util.List" resultType="com.welampiot.dto.SectionDTO">
-        select id,name from section
+        select id,name,pid from section
         <if test="idList != null and !idList.isEmpty()">
             where id in
             <foreach item="item" collection="idList" open="(" separator="," close=")">

+ 6 - 0
src/main/resources/mapper/UserMapper.xml

@@ -344,5 +344,11 @@
         from user u
         where u.username = #{username}
     </select>
+
+    <select id="getZoneListByUserid" resultType="UserDTO">
+        select u.zone_list as zoneList
+        from user u
+        where u.id = #{id}
+    </select>
     
 </mapper>