浏览代码

重构场地所属区域功能,支持1对多关系。

bruce 2 年之前
父节点
当前提交
60f34d7ad9

+ 4 - 6
imcs-bt-be/imcs-business-biz/src/main/java/com/github/zuihou/business/place/dao/PlaceMapper.java

@@ -8,15 +8,13 @@ import org.springframework.stereotype.Repository;
 import java.util.List;
 
 /**
- * <p>
- * Mapper 接口
- * 场地管理
- * </p>
+ * 场地Mapper接口
  *
  * @author zuihou
  * @date 2022-01-08
  */
 @Repository
-public interface PlaceMapper extends SuperMapper<Place> {
+public interface PlaceMapper extends SuperMapper<Place>
+{
     List<Place> getAll(Long id);
-}
+}

+ 46 - 0
imcs-bt-be/imcs-business-biz/src/main/java/com/github/zuihou/business/place_region/dao/PlaceRegionMapper.java

@@ -0,0 +1,46 @@
+package com.github.zuihou.business.place_region.dao;
+
+import com.github.zuihou.base.mapper.SuperMapper;
+import com.github.zuihou.business.place.entity.Place;
+import com.github.zuihou.business.place_region.PlaceRegion;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.MapKey;
+import org.apache.ibatis.annotations.Select;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 场地-所属区域Mapper接口
+ *
+ * @author bruce
+ * @date 2023-02-23
+ */
+@Repository
+public interface PlaceRegionMapper extends SuperMapper<PlaceRegion>
+{
+    /**
+     * 按照场地分组汇总统计所属区域
+     *
+     * @param ids 场地ID列表
+     * @return {@link Map}<{@link String}, {@link Map}<{@link String}, {@link Object}>>
+     */
+    @MapKey("PlaceId")
+    @Select
+    (
+      "SELECT c.place_id AS PlaceId, GROUP_CONCAT(c.region_id) AS RegionIds, GROUP_CONCAT(c.name) AS Regions " +
+      "FROM" +
+      "(" +
+      "  SELECT a.place_id, a.region_id, b.name " +
+      "  FROM bt_place_region a " +
+      "  LEFT JOIN c_common_dictionary_item b ON a.region_id = b.id " +
+      "  WHERE a.place_id IN (${ids})" +
+      ") c " +
+      "GROUP BY c.place_id"
+    )
+    Map<Long, Map<String, Object>> findByPlaceIds(String ids);
+
+    @Delete("DELETE FROM bt_place_region WHERE place_id = #{id}")
+    void deleteByPlaceId(Long id);
+}

+ 73 - 28
imcs-bt-be/imcs-business-controller/src/main/java/com/github/zuihou/business/controller/place/PlaceController.java

@@ -3,43 +3,42 @@ package com.github.zuihou.business.controller.place;
 import cn.hutool.core.bean.BeanUtil;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.github.zuihou.authority.dao.common.DictionaryItemMapper;
+import com.github.zuihou.authority.dto.core.OrgSaveDTO;
 import com.github.zuihou.authority.entity.auth.User;
 import com.github.zuihou.authority.entity.common.DictionaryItem;
 import com.github.zuihou.authority.entity.core.Org;
 import com.github.zuihou.authority.service.auth.UserService;
 import com.github.zuihou.authority.service.core.OrgService;
+import com.github.zuihou.base.R;
+import com.github.zuihou.base.controller.SuperController;
 import com.github.zuihou.base.request.PageParams;
-import com.github.zuihou.business.company.entity.Company;
-import com.github.zuihou.business.order.dto.OrderPageDTO;
-import com.github.zuihou.business.order.entity.Order;
-import com.github.zuihou.business.place.entity.Place;
+import com.github.zuihou.business.place.dto.PlacePageDTO;
 import com.github.zuihou.business.place.dto.PlaceSaveDTO;
 import com.github.zuihou.business.place.dto.PlaceUpdateDTO;
-import com.github.zuihou.business.place.dto.PlacePageDTO;
+import com.github.zuihou.business.place.entity.Place;
 import com.github.zuihou.business.place.service.PlaceService;
-
-import java.lang.reflect.Array;
-import java.util.ArrayList;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-import com.github.zuihou.base.controller.SuperController;
-import com.github.zuihou.base.R;
-import com.github.zuihou.database.mybatis.conditions.Wraps;
+import com.github.zuihou.business.place_region.PlaceRegion;
+import com.github.zuihou.business.place_region.dao.PlaceRegionMapper;
 import com.github.zuihou.database.mybatis.conditions.query.LbqWrapper;
 import com.github.zuihou.database.mybatis.conditions.query.QueryWrap;
+import com.github.zuihou.security.annotation.PreAuth;
+import com.github.zuihou.utils.BeanPlusUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.connection.ReactiveGeoCommands;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.*;
-import com.github.zuihou.security.annotation.PreAuth;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * 场地控制器
@@ -58,7 +57,7 @@ public class PlaceController extends SuperController<PlaceService, Long, Place,
     @Resource
     private OrgService orgService;
     @Resource
-    private DictionaryItemMapper dictionaryItemMapper;
+    private PlaceRegionMapper placeRegionMapper;
 
     /**
      * 分页查询
@@ -81,7 +80,8 @@ public class PlaceController extends SuperController<PlaceService, Long, Place,
         lbqWrapper.eq(Place::getPlcCid, model.getPlcCid());
         baseService.page(page, wrapper);
 
-        List<Long> regionIds = new LinkedList<>();
+        // 场地ID列表
+        List<Long> ids = new LinkedList<>();
         List<Place> placeList = page.getRecords();
         for (Place place : placeList)
         {
@@ -89,20 +89,65 @@ public class PlaceController extends SuperController<PlaceService, Long, Place,
             place.setPlcComDesc(getPlaceComDescById(place.getCreateUser()));
             place.setSelectedOptions(arr);
 
-            if (null != place.getRegionId())
+            ids.add(place.getId());
+        }
+
+        // 补充所属区域属性
+        if (ids.size() > 0)
+        {
+            Map<Long, Map<String, Object>> placeRegions = placeRegionMapper.findByPlaceIds(StringUtils.join(ids, ","));
+
+            Map<String, Object> rows;
+            for (Place place : placeList)
             {
-                regionIds.add(place.getRegionId());
+                rows = placeRegions.get(place.getId());
+                if (null != rows)
+                {
+                    place.setRegionIds((String) rows.getOrDefault("RegionIds", ""));
+                    place.setRegions((String) rows.getOrDefault("Regions", ""));
+                }
             }
         }
+    }
 
-        // 补充所属区域属性
-        if (regionIds.size() > 0)
+    @Override
+    public R<Place> handlerSave(PlaceSaveDTO model)
+    {
+        // 1 保存场地
+        Place p = BeanPlusUtil.toBean(model, Place.class);
+        this.baseService.save(p);
+
+        // 2 保存所属区域
+        String[] regionIds = p.getRegionIds().split(",");
+        for (String rId : regionIds)
         {
-           Map<Long, String> names = dictionaryItemMapper.selectBatchIds(regionIds).stream()
-              .collect(Collectors.toMap(DictionaryItem::getId, DictionaryItem::getName));
+            placeRegionMapper.insert(PlaceRegion.builder().placeId(p.getId()).regionId(Long.parseLong(rId)).build());
+        }
+
+        return success(p);
+    }
 
-            placeList.forEach(p -> p.setRegion(names.getOrDefault(p.getRegionId(), "")));
+    @Override
+    public R<Place> handlerUpdate(PlaceUpdateDTO model)
+    {
+        // 1 保存场地
+        Place p = BeanPlusUtil.toBean(model, Place.class);
+        this.baseService.updateAllById(p);
+
+        // 2 保存所属区域
+        // 2.1 删除老数据
+        placeRegionMapper.deleteByPlaceId(p.getId());
+        // 2.2 插入新数据
+        String[] regionIds = p.getRegionIds().split(",");
+        for (String rId : regionIds)
+        {
+            placeRegionMapper.insert(PlaceRegion.builder()
+              .placeId(p.getId())
+              .regionId(Long.parseLong(rId))
+              .build());
         }
+
+        return success(p);
     }
 
     /**

+ 12 - 4
imcs-bt-be/imcs-business-entity/src/main/java/com/github/zuihou/business/place/dto/PlaceSaveDTO.java

@@ -1,5 +1,6 @@
 package com.github.zuihou.business.place.dto;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.*;
@@ -128,9 +129,16 @@ public class PlaceSaveDTO implements Serializable
     @Length(max = 255, message = "场地维度长度不能超过10")
     private String plcLat;
     /**
-     * 所属区域id
+     * 所属区域id列表
      */
-    @ApiModelProperty(value = "所属区域id")
+    @TableField(exist = false)
     @NotNull(message = "请选择所属区域")
-    private Long regionId;
-}
+    @ApiModelProperty(value = "所属区域id列表")
+    private String regionIds;
+    /**
+     * 所属区域列表
+     */
+    @TableField(exist = false)
+    @ApiModelProperty(value = "所属区域列表")
+    private String regions;
+}

+ 12 - 4
imcs-bt-be/imcs-business-entity/src/main/java/com/github/zuihou/business/place/dto/PlaceUpdateDTO.java

@@ -1,5 +1,6 @@
 package com.github.zuihou.business.place.dto;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.github.zuihou.base.entity.SuperEntity;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
@@ -133,9 +134,16 @@ public class PlaceUpdateDTO implements Serializable
     @Length(max = 255, message = "场地维度长度不能超过10")
     private String plcLat;
     /**
-     * 所属区域id
+     * 所属区域id列表
      */
-    @ApiModelProperty(value = "所属区域id")
+    @TableField(exist = false)
     @NotNull(message = "请选择所属区域")
-    private Long regionId;
-}
+    @ApiModelProperty(value = "所属区域id列表")
+    private String regionIds;
+    /**
+     * 所属区域列表
+     */
+    @TableField(exist = false)
+    @ApiModelProperty(value = "所属区域列表")
+    private String regions;
+}

+ 8 - 6
imcs-bt-be/imcs-business-entity/src/main/java/com/github/zuihou/business/place/entity/Place.java

@@ -25,7 +25,7 @@ import static com.github.zuihou.utils.DateUtils.DEFAULT_DATE_TIME_FORMAT;
 import static com.baomidou.mybatisplus.annotation.SqlCondition.LIKE;
 
 /**
- * 场地管理实体类
+ * 场地实体类
  *
  * @author zuihou
  * @since 2022-01-08
@@ -209,15 +209,17 @@ public class Place extends Entity<Long>
     private String [] selectedOptions;
 
     /**
-     * 所属区域id
+     * 所属区域id列表
      */
-    @ApiModelProperty(value = "所属区域id")
+    @TableField(exist = false)
     @NotNull(message = "请选择所属区域")
-    private Long regionId;
+    @ApiModelProperty(value = "所属区域id列表")
+    private String regionIds;
 
     /**
-     * 所属区域
+     * 所属区域列表
      */
     @TableField(exist = false)
-    private String region;
+    @ApiModelProperty(value = "所属区域列表")
+    private String regions;
 }

+ 33 - 0
imcs-bt-be/imcs-business-entity/src/main/java/com/github/zuihou/business/place_region/PlaceRegion.java

@@ -0,0 +1,33 @@
+package com.github.zuihou.business.place_region;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.github.zuihou.base.entity.SuperEntity;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.*;
+import lombok.experimental.Accessors;
+
+/**
+ * 场地-所属区域实体类
+ *
+ * @author bruce
+ * @since 2023-02-23
+ */
+@TableName("bt_place_region")
+@ApiModel(value = "PlaceRegion", description = "场地-所属区域")
+@ToString(callSuper = true) @EqualsAndHashCode(callSuper = true)
+@Data @Builder @NoArgsConstructor @AllArgsConstructor @Accessors(chain = true)
+public class PlaceRegion extends SuperEntity<Long>
+{
+    /**
+     * 场地ID
+     */
+    @ApiModelProperty(value = "场地ID")
+    private Long placeId;
+
+    /**
+     * 所属区域ID
+     */
+    @ApiModelProperty(value = "所属区域ID")
+    private Long regionId;
+}