完善网格的处理逻辑

This commit is contained in:
wanggeng888 2021-05-22 09:19:30 +08:00
parent 45aee17e43
commit d1820f7a3f
4 changed files with 175 additions and 9 deletions

View File

@ -107,6 +107,15 @@ public interface IGridDao {
*/
List<GridDTO> listGridByRelationIds(List<String> relationIds) throws SearchException;
/**
* 网格列表group
*
* @param relationIds
* @return
* @throws SearchException
*/
List<GridDTO> listGridGroupByRelationIds(List<String> relationIds) throws SearchException;
/**
* 获取网格点列表
*
@ -115,4 +124,5 @@ public interface IGridDao {
* @throws SearchException
*/
List<GridPointDTO> listPointByGridIds(List<String> gridIds) throws SearchException;
}

View File

@ -2,11 +2,12 @@ package com.cm.plugin.map.service;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResultList;
import com.cm.plugin.map.pojo.dto.GridDTO;
import com.cm.plugin.map.pojo.dto.GridRelationDTO;
import com.cm.plugin.map.pojo.vo.GridPointVO;
import com.cm.plugin.map.pojo.vo.GridVO;
import org.springframework.stereotype.Service;
import java.util.List;
@ -56,6 +57,22 @@ public interface IGridService {
*/
void deleteGridByGridIds(List<String> gridIds) throws RemoveException;
/**
* 删除空网格没有节点的网格
*
* @param deleteGridIds
*/
void deleteEmptyGrid(List<String> gridIds);
/**
* 删除网格
*
* @param gridId
* @param relationId
* @throws RemoveException
*/
void deleteGridRelation(String gridId, String relationId) throws RemoveException;
/**
* 获取网格关系列表
*
@ -110,4 +127,13 @@ public interface IGridService {
*/
List<GridDTO> listGridByRelationIds(List<String> relationIds) throws SearchException;
/**
* 获取网格分页列表
*
* @param relationIds
* @return
*/
SuccessResultList<List<GridDTO>> listPageGridByRelationIds(ListPage page, List<String> relationIds);
}

View File

@ -4,6 +4,8 @@ import com.alibaba.druid.util.StringUtils;
import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.cm.plugin.map.dao.IGridDao;
@ -13,12 +15,12 @@ import com.cm.plugin.map.pojo.dto.GridRelationDTO;
import com.cm.plugin.map.pojo.vo.GridPointVO;
import com.cm.plugin.map.pojo.vo.GridVO;
import com.cm.plugin.map.service.IGridService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* When you feel like quitting. Think about why you started
@ -104,6 +106,39 @@ public class GridServiceImpl extends AbstractService implements IGridService {
gridDao.deleteGridRelation(params);
}
@Override
public void deleteEmptyGrid(List<String> gridIds) {
if (gridIds.isEmpty()) {
return;
}
// 所有的网格和人员关联关系列表
List<GridRelationDTO> gridRelationDTOs = listGridRelationByGridIds(gridIds);
// 得到为空的网格
List<String> emptyGridIds = new ArrayList<>();
for (String gridId : gridIds) {
boolean isEmpty = true;
for (GridRelationDTO gridRelationDTO : gridRelationDTOs) {
if (StringUtils.equals(gridId, gridRelationDTO.getGridId())) {
isEmpty = false;
break;
}
}
if (isEmpty) {
emptyGridIds.add(gridId);
}
}
// 删除空网格
deleteGridByGridIds(emptyGridIds);
}
@Override
public void deleteGridRelation(String gridId, String relationId) throws RemoveException {
Map<String, Object> params = getHashMap(4);
params.put("gridId", gridId);
params.put("relationId", relationId);
gridDao.deleteGridRelation(params);
}
@Override
public List<GridRelationDTO> listGridRelationByRelationId(String relationId) throws SearchException {
Map<String, Object> params = getHashMap(2);
@ -150,21 +185,48 @@ public class GridServiceImpl extends AbstractService implements IGridService {
if (relationIds == null || relationIds.size() == 0) {
return new ArrayList<>();
}
// List<GridDTO> gridDTOs = gridDao.listGridGroupByRelationIds(relationIds);
List<GridDTO> gridDTOs = gridDao.listGridByRelationIds(relationIds);
if (gridDTOs.isEmpty()) {
return gridDTOs;
}
// 聚合后的网格
List<GridDTO> gridGroupDTOs = listGridGroup(gridDTOs);
// 只能展示包含关联人员列表的网格比如查询AB那么可以展示的应该是AB...而不能是A
for (int i = 0; i < gridGroupDTOs.size(); i++) {
GridDTO gridDTO = gridGroupDTOs.get(i);
// 关联人数多于片区人数排除
if (relationIds.size() > gridDTO.getRelationIdArray().size()) {
gridGroupDTOs.remove(i);
i--;
continue;
}
// 判断关联人是否全部被片区包含
boolean isAllExist = true;
for (String relationId : relationIds) {
if (!gridDTO.getRelationId().contains(relationId)) {
isAllExist = false;
break;
}
}
// 不是全部包含就删除
if (!isAllExist) {
gridGroupDTOs.remove(i);
i--;
continue;
}
}
List<String> gridIds = new ArrayList<>();
for (GridDTO gridDTO : gridDTOs) {
for (GridDTO gridDTO : gridGroupDTOs) {
gridIds.add(gridDTO.getGridId());
}
// 获取所有点
List<GridPointDTO> gridPointDTOs = gridDao.listPointByGridIds(gridIds);
if (gridPointDTOs.isEmpty()) {
return gridDTOs;
return gridGroupDTOs;
}
// 构建所有点
for (GridDTO gridDTO : gridDTOs) {
for (GridDTO gridDTO : gridGroupDTOs) {
List<GridPointDTO> pointArray = new ArrayList<>();
for (GridPointDTO gridPointDTO : gridPointDTOs) {
if (StringUtils.equals(gridDTO.getGridId(), gridPointDTO.getGridId())) {
@ -173,7 +235,48 @@ public class GridServiceImpl extends AbstractService implements IGridService {
}
gridDTO.setPointArray(pointArray);
}
return gridDTOs;
return gridGroupDTOs;
}
/**
* 得到聚合后的列表
*
* @param gridDTOs
* @return
*/
private List<GridDTO> listGridGroup(List<GridDTO> gridDTOs) {
Map<String, GridDTO> gridMap = new HashMap<>(16);
for (GridDTO gridDTO : gridDTOs) {
GridDTO grid = gridMap.get(gridDTO.getGridId());
if (grid == null) {
grid = gridDTO;
List<String> getRelationIdArray = new ArrayList<>();
getRelationIdArray.add(gridDTO.getRelationId());
gridDTO.setRelationIdArray(getRelationIdArray);
gridMap.put(gridDTO.getGridId(), gridDTO);
}
// 合并relationId
grid.getRelationIdArray().add(gridDTO.getRelationId());
}
List<GridDTO> gridGroups = new ArrayList<>();
for (Map.Entry<String, GridDTO> kv : gridMap.entrySet()) {
GridDTO gridDTO = kv.getValue();
Collections.sort(gridDTO.getRelationIdArray());
gridDTO.setRelationId(gridDTO.getRelationIdArray().toString());
gridGroups.add(gridDTO);
}
return gridGroups;
}
@Override
public SuccessResultList<List<GridDTO>> listPageGridByRelationIds(ListPage page, List<String> relationIds) {
if (relationIds == null || relationIds.size() == 0) {
return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
}
PageHelper.startPage(page.getPage(), page.getRows());
List<GridDTO> gridDTOs = gridDao.listGridByRelationIds(relationIds);
PageInfo<GridDTO> pageInfo = new PageInfo<>(gridDTOs);
return new SuccessResultList<>(gridDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
}
/**

View File

@ -92,19 +92,22 @@
<delete id="deleteGridRelation" parameterType="map" flushCache="true">
DELETE FROM
map_grid_relation
WHERE
<where>
<if test="gridId != null and gridId != ''">
grid_id = #{gridId}
</if>
<if test="gridIds != null and gridIds.size > 0">
AND
grid_id IN
<foreach collection="gridIds" index="index" open="(" separator="," close=")">
#{gridIds[${index}]}
</foreach>
</if>
<if test="relationId != null and relationId != ''">
AND
relation_id = #{relationId}
</if>
</where>
</delete>
<!-- 保存网格点 -->
@ -193,6 +196,30 @@
</foreach>
</select>
<select id="listGridGroupByRelationIds" parameterType="java.util.List" resultMap="gridDTO" useCache="true">
SELECT
t1.grid_id,
t1.fill_color,
t1.grid_name
FROM
map_grid t1
LEFT JOIN
map_grid_relation t2
ON
t1.grid_id = t2.grid_id
WHERE
t1.is_delete = 0
AND
t2.relation_id IN
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{list[${index}]}
</foreach>
GROUP BY
t1.grid_id,
t1.fill_color,
t1.grid_name
</select>
<!-- 获取网格点列表通过网格ID -->
<select id="listPointByGridId" parameterType="java.lang.String" resultMap="gridPointDTO" useCache="true">
SELECT