区域管理
This commit is contained in:
parent
36fe985f26
commit
3f44dae2e6
|
|
@ -5,7 +5,9 @@ import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.nflg.mobilebroken.admin.pojo.dto.BaseAreaEditDTO;
|
import com.nflg.mobilebroken.admin.pojo.dto.BaseAreaEditDTO;
|
||||||
|
import com.nflg.mobilebroken.admin.pojo.query.BaseAreaQuery;
|
||||||
import com.nflg.mobilebroken.admin.pojo.query.DepartmentQuery;
|
import com.nflg.mobilebroken.admin.pojo.query.DepartmentQuery;
|
||||||
|
import com.nflg.mobilebroken.admin.service.AdminBaseAreaService;
|
||||||
import com.nflg.mobilebroken.common.constant.STATE;
|
import com.nflg.mobilebroken.common.constant.STATE;
|
||||||
import com.nflg.mobilebroken.common.pojo.ApiResult;
|
import com.nflg.mobilebroken.common.pojo.ApiResult;
|
||||||
import com.nflg.mobilebroken.common.pojo.PageData;
|
import com.nflg.mobilebroken.common.pojo.PageData;
|
||||||
|
|
@ -33,6 +35,9 @@ public class BaseAreaController extends ControllerBase {
|
||||||
@Resource
|
@Resource
|
||||||
ITBaseAreaService baseAreaService;
|
ITBaseAreaService baseAreaService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
AdminBaseAreaService adminBaseAreaService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取区域列表
|
* 获取区域列表
|
||||||
* @param query
|
* @param query
|
||||||
|
|
@ -40,10 +45,9 @@ public class BaseAreaController extends ControllerBase {
|
||||||
*/
|
*/
|
||||||
@PostMapping("getList")
|
@PostMapping("getList")
|
||||||
@MethodInfoMark(value = "获取区域列表" ,menuName = "区域管理")
|
@MethodInfoMark(value = "获取区域列表" ,menuName = "区域管理")
|
||||||
public ApiResult<PageData<TBaseAreaVO>> getList(@RequestBody DepartmentQuery query){
|
public ApiResult<PageData<TBaseAreaVO>> getList(@RequestBody BaseAreaQuery query){
|
||||||
Page<TBaseAreaVO> result = baseAreaService.getList(new Page<>(query.getPage(),query.getPageSize()), query);
|
//
|
||||||
|
return adminBaseAreaService.getList(query);
|
||||||
return ApiResult.success(result.getRecords(),query,result.getTotal());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -78,7 +82,7 @@ public class BaseAreaController extends ControllerBase {
|
||||||
ent.setDataModifyUserNo(AdminUserUtil.getUserNo());
|
ent.setDataModifyUserNo(AdminUserUtil.getUserNo());
|
||||||
ent.setDataModifyUserName(AdminUserUtil.getUserName());
|
ent.setDataModifyUserName(AdminUserUtil.getUserName());
|
||||||
ent.setDataModifyTime(LocalDateTime.now());
|
ent.setDataModifyTime(LocalDateTime.now());
|
||||||
ent.setAreaCode(StrUtil.padPre(count.toString(),3,"0"));
|
ent.setAreaCode(StrUtil.padPre(count.toString(),6,"0"));
|
||||||
baseAreaService.save(ent);
|
baseAreaService.save(ent);
|
||||||
return ApiResult.success(true);
|
return ApiResult.success(true);
|
||||||
|
|
||||||
|
|
@ -97,6 +101,9 @@ public class BaseAreaController extends ControllerBase {
|
||||||
VUtils.trueThrowBusinessError(Objects.isNull(oldEnt)).throwMessage("区域不存在");
|
VUtils.trueThrowBusinessError(Objects.isNull(oldEnt)).throwMessage("区域不存在");
|
||||||
oldEnt.setParentAreaRowId(baseAreaEditDTO.getParentAreaRowId());
|
oldEnt.setParentAreaRowId(baseAreaEditDTO.getParentAreaRowId());
|
||||||
oldEnt.setAreaName(baseAreaEditDTO.getAreaName());
|
oldEnt.setAreaName(baseAreaEditDTO.getAreaName());
|
||||||
|
oldEnt.setDataModifyUserNo(AdminUserUtil.getUserNo());
|
||||||
|
oldEnt.setDataModifyUserName(AdminUserUtil.getUserName());
|
||||||
|
oldEnt.setDataModifyTime(LocalDateTime.now());
|
||||||
baseAreaService.updateById(oldEnt);
|
baseAreaService.updateById(oldEnt);
|
||||||
return ApiResult.success(true);
|
return ApiResult.success(true);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.nflg.mobilebroken.admin.pojo.query;
|
||||||
|
|
||||||
|
|
||||||
|
import com.nflg.mobilebroken.common.pojo.query.PageBaseQuery;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class BaseAreaQuery extends PageBaseQuery {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 区域编码or 名称
|
||||||
|
*/
|
||||||
|
private String areaCodeOrName;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,108 @@
|
||||||
|
package com.nflg.mobilebroken.admin.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.nflg.mobilebroken.admin.pojo.query.BaseAreaQuery;
|
||||||
|
import com.nflg.mobilebroken.admin.pojo.query.DepartmentQuery;
|
||||||
|
import com.nflg.mobilebroken.admin.pojo.vo.BaseDepartmentVO;
|
||||||
|
import com.nflg.mobilebroken.common.pojo.ApiResult;
|
||||||
|
import com.nflg.mobilebroken.common.pojo.PageData;
|
||||||
|
import com.nflg.mobilebroken.common.pojo.vo.TBaseAreaVO;
|
||||||
|
import com.nflg.mobilebroken.repository.entity.TBaseArea;
|
||||||
|
import com.nflg.mobilebroken.repository.entity.TBaseDepartment;
|
||||||
|
import com.nflg.mobilebroken.repository.service.ITBaseAreaService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AdminBaseAreaService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
ITBaseAreaService baseAreaService;
|
||||||
|
|
||||||
|
public ApiResult<PageData<TBaseAreaVO>> getList(@RequestBody BaseAreaQuery query){
|
||||||
|
Page<TBaseAreaVO> result = baseAreaService.getList(new Page<>(query.getPage(),query.getPageSize()), query);
|
||||||
|
List<TBaseAreaVO> dataResult = Convert.toList(TBaseAreaVO.class, result);
|
||||||
|
if(StrUtil.isNotBlank(query.getAreaCodeOrName())){
|
||||||
|
List<TBaseAreaVO> allAreaList = Convert.toList(TBaseAreaVO.class,baseAreaService.lambdaQuery().eq(TBaseArea::getAreaState,1).list()) ;
|
||||||
|
Map<Long, TBaseAreaVO> collect = allAreaList.stream().collect(Collectors.toMap(TBaseAreaVO::getRowId, Function.identity()));
|
||||||
|
List<TBaseAreaVO> allParents=new ArrayList<>();
|
||||||
|
for (TBaseAreaVO data:dataResult){
|
||||||
|
allParents.addAll(getAllParents(data, collect)) ;
|
||||||
|
}
|
||||||
|
allParents.addAll(dataResult);
|
||||||
|
return ApiResult.success(buildTree(allParents),query,result.getTotal());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for (TBaseAreaVO data:dataResult){
|
||||||
|
initNodeChildren(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ApiResult.success(dataResult,query,result.getTotal());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initNodeChildren(TBaseAreaVO node) {
|
||||||
|
List<TBaseAreaVO> immediateChildren = findChildDepartmentsByParentId(node.getRowId());
|
||||||
|
node.setChildren(immediateChildren);
|
||||||
|
for (TBaseAreaVO child : immediateChildren) {
|
||||||
|
initNodeChildren(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<TBaseAreaVO> findChildDepartmentsByParentId(Long parentId) {
|
||||||
|
List<TBaseArea> areas = baseAreaService.lambdaQuery().eq(TBaseArea::getParentAreaRowId, parentId).eq(TBaseArea::getAreaState,1).list();
|
||||||
|
return Convert.toList(TBaseAreaVO.class,areas);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private List<TBaseAreaVO> getAllParents(TBaseAreaVO node, Map<Long, TBaseAreaVO> idToNodeMap) {
|
||||||
|
List<TBaseAreaVO> parents = new ArrayList<>();
|
||||||
|
Long parentId = node.getParentAreaRowId();
|
||||||
|
while (parentId != null && parentId>0) {
|
||||||
|
TBaseAreaVO parentNode = idToNodeMap.get(parentId);
|
||||||
|
if (parentNode == null) break; // 防止数据不一致导致无限循环
|
||||||
|
parents.add(parentNode);
|
||||||
|
parentId = parentNode.getParentAreaRowId(); // 移动到上一级
|
||||||
|
}
|
||||||
|
Collections.reverse(parents); // 如果需要从根节点开始排序,则反转列表
|
||||||
|
return parents;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* list 构建树
|
||||||
|
* @param nodes
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private List<TBaseAreaVO> buildTree(List<TBaseAreaVO> nodes) {
|
||||||
|
// 使用Map存储id到Node的映射,便于快速查找父节点
|
||||||
|
Map<Long, TBaseAreaVO> idToNodeMap = nodes.stream()
|
||||||
|
.collect(Collectors.toMap(TBaseAreaVO::getRowId, Function.identity()));
|
||||||
|
|
||||||
|
List<TBaseAreaVO> roots = new ArrayList<>();
|
||||||
|
|
||||||
|
for (TBaseAreaVO node : nodes) {
|
||||||
|
TBaseAreaVO parent = idToNodeMap.get(node.getParentAreaRowId());
|
||||||
|
if (parent != null) {
|
||||||
|
if(null==parent.getChildren()) {
|
||||||
|
parent.setChildren(new ArrayList<>());
|
||||||
|
}
|
||||||
|
// 如果有父节点,则添加到父节点的孩子列表中
|
||||||
|
parent.getChildren().add(node);
|
||||||
|
} else {
|
||||||
|
// 没有父节点则为根节点
|
||||||
|
roots.add(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return roots;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ import lombok.experimental.Accessors;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
|
|
@ -76,4 +77,9 @@ public class TBaseAreaVO implements Serializable {
|
||||||
* 最近修改时间
|
* 最近修改时间
|
||||||
*/
|
*/
|
||||||
private LocalDateTime dataModifyTime;
|
private LocalDateTime dataModifyTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子级列表
|
||||||
|
*/
|
||||||
|
private List<TBaseAreaVO> children;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
<mapper namespace="com.nflg.mobilebroken.repository.mapper.TBaseAreaMapper">
|
<mapper namespace="com.nflg.mobilebroken.repository.mapper.TBaseAreaMapper">
|
||||||
|
|
||||||
<sql id="whr">
|
<sql id="whr">
|
||||||
<if test="query.areaCodeOrName!=null and query.areaCodeOrName!=''">
|
<if test="query.areaCodeOrName!=null and query.areaCodeOrName!=''">
|
||||||
and (area_code =#{query.areaCodeOrName} or area_name = #{query.areaCodeOrName} )
|
and (area_code =#{query.areaCodeOrName} or area_name = #{query.areaCodeOrName} )
|
||||||
</if>
|
</if>
|
||||||
<if test="query.areaState!=null and query.areaState!=''">
|
<if test="query.areaState!=null and query.areaState!=''">
|
||||||
|
|
@ -12,7 +12,11 @@
|
||||||
|
|
||||||
</sql>
|
</sql>
|
||||||
<select id="getList" resultType="com.nflg.mobilebroken.common.pojo.vo.TBaseAreaVO">
|
<select id="getList" resultType="com.nflg.mobilebroken.common.pojo.vo.TBaseAreaVO">
|
||||||
select * from t_base_area where parent_area_row_id=0 <include refid="whr" />
|
select * from t_base_area where 1=1
|
||||||
|
<if test="query.areaCodeOrName==null or query.areaCodeOrName==''">
|
||||||
|
and parent_area_row_id=0
|
||||||
|
</if>
|
||||||
|
<include refid="whr" />
|
||||||
|
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue