Bläddra i källkod

refactor(national-motion-system):重构包场场地处理逻辑

- 修改 AppSitePlace 中的 CategoryId 类型从 Integer 改为 String
- 优化 AppSitePlaceServiceImpl 中的包场场地处理逻辑- 实现场地数量的扩展和裁剪
- 更新场地名称生成规则
- 优化价格规则的保存逻辑
- 处理不再需要的场地和对应的价格规则
lix 2 veckor sedan
förälder
incheckning
725794d3f0

+ 1 - 1
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/system/app/entity/AppSitePlace.java

@@ -56,7 +56,7 @@ public class AppSitePlace implements Serializable {
     /**运动类型*/
     @Excel(name = "运动类型", width = 15)
     @Schema(description = "运动类型")
-    private Integer CategoryId;
+    private String CategoryId;
 	/**乐观锁*/
 	@Excel(name = "乐观锁", width = 15)
     @Schema(description = "乐观锁")

+ 108 - 9
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/system/app/service/impl/AppSitePlaceServiceImpl.java

@@ -27,9 +27,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.stream.Collectors;
 
 import static org.jeecg.common.constant.CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
@@ -361,18 +359,119 @@ public class AppSitePlaceServiceImpl extends ServiceImpl<AppSitePlaceMapper, App
         AppSitePlaceCuDTO placeCuDTO = placeDTO.getAppSitePlaceCuDTO();
         List<AppSiteCategoryDOT> siteCategoryDOTS = placeDTO.getSiteCategoryDOTS();
         List<AppSiteRuleDTO> siteRuleDTOS = placeDTO.getSiteRuleDTOS();
-        Map<String, List<AppSiteRuleDTO>> collect = siteRuleDTOS.stream().collect(Collectors.groupingBy(AppSiteRuleDTO::getCategoryId));
+
+        // 按 categoryId 分组价格规则
+        Map<String, List<AppSiteRuleDTO>> ruleMap = siteRuleDTOS.stream()
+                .collect(Collectors.groupingBy(AppSiteRuleDTO::getCategoryId));
+
         AppSite site = appSiteMapper.selectById(placeCuDTO.getSiteId());
-        List<AppSitePlace> appSitePlaces = baseMapper.selectList(Wrappers.<AppSitePlace>lambdaQuery().eq(AppSitePlace::getSiteId, site.getId()));
-        if (appSitePlaces.isEmpty())throw new JeecgBootException("包场数据不存在", SC_INTERNAL_SERVER_ERROR_500);
-        siteCategoryDOTS.forEach(item -> {
+        if (site == null) {
+            throw new JeecgBootException("商户门店不存在", SC_INTERNAL_SERVER_ERROR_500);
+        }
 
-        });
+        // 查询当前场地的所有包场场地
+        List<AppSitePlace> existingPlaces = baseMapper.selectList(Wrappers.<AppSitePlace>lambdaQuery()
+                .eq(AppSitePlace::getSiteId, site.getId())
+                .eq(AppSitePlace::getType, SitePlaceTypeEnum.PACKAGE.getCode()));
 
+        if (existingPlaces.isEmpty()) {
+            throw new JeecgBootException("包场数据不存在", SC_INTERNAL_SERVER_ERROR_500);
+        }
 
+        // 用于存储需要保留的场地ID
+        List<String> updatedPlaceIds = new ArrayList<>();
 
+        // 更新或新增场地
+        for (AppSiteCategoryDOT item : siteCategoryDOTS) {
+            String categoryId = item.getCategoryId();
+            int count = item.getCount();
+            List<AppSiteRuleDTO> rules = ruleMap.getOrDefault(categoryId, Collections.emptyList());
 
-        return null;
+            // 查询该分类已有的场地
+            List<AppSitePlace> categoryPlaces = existingPlaces.stream()
+                    .filter(p -> p.getCategoryId() != null && p.getCategoryId().equals(categoryId))
+                    .sorted(Comparator.comparing(AppSitePlace::getName)) // 按名称排序以确保顺序一致
+                    .collect(Collectors.toList());
+
+            AppCategory category = appCategoryMapper.selectById(categoryId);
+            if (category == null) {
+                throw new JeecgBootException("分类不存在", SC_INTERNAL_SERVER_ERROR_500);
+            }
+
+            // 扩展或裁剪场地数量
+            for (int i = 0; i < count; i++) {
+                AppSitePlace appSitePlace;
+                if (i < categoryPlaces.size()) {
+                    // 更新现有场地
+                    appSitePlace = categoryPlaces.get(i);
+                } else {
+                    // 创建新场地
+                    appSitePlace = new AppSitePlace();
+                    BeanUtils.copyProperties(placeCuDTO, appSitePlace);
+                    appSitePlace.setType(SitePlaceTypeEnum.PACKAGE.getCode());
+                    appSitePlace.setOrgCode(site.getOrgCode());
+                    appSitePlace.setTenantId(site.getTenantId());
+                    appSitePlace.setCategoryId(categoryId);
+                }
+
+                // 设置场地名称
+                appSitePlace.setName(category.getName() + (i + 1));
+                // 保存或更新场地
+                if (appSitePlace.getId() == null) {
+                    int insertResult = baseMapper.insert(appSitePlace);
+                    if (insertResult < 1) {
+                        throw new JeecgBootException("包场场地创建失败", SC_INTERNAL_SERVER_ERROR_500);
+                    }
+                } else {
+                    int updateResult = baseMapper.updateById(appSitePlace);
+                    if (updateResult < 1) {
+                        throw new JeecgBootException("包场场地更新失败", SC_INTERNAL_SERVER_ERROR_500);
+                    }
+                }
+
+                // 更新价格规则
+                List<AppSitePriceRules> priceRules = rules.stream()
+                        .map(rule -> {
+                            AppSitePriceRules priceRule = new AppSitePriceRules();
+                            BeanUtils.copyProperties(rule, priceRule);
+                            priceRule.setSitePlaceId(appSitePlace.getId());
+                            priceRule.setOrgCode(site.getOrgCode());
+                            priceRule.setTenantId(site.getTenantId());
+                            return priceRule;
+                        })
+                        .collect(Collectors.toList());
+
+                // 删除旧的价格规则
+                appSitePriceRulesMapper.delete(Wrappers.<AppSitePriceRules>lambdaQuery()
+                        .eq(AppSitePriceRules::getSitePlaceId, appSitePlace.getId()));
+
+                // 插入新的价格规则
+                for (AppSitePriceRules priceRule : priceRules) {
+                    int insertResult = appSitePriceRulesMapper.insert(priceRule);
+                    if (insertResult < 1) {
+                        throw new JeecgBootException("包场价格规则保存失败", SC_INTERNAL_SERVER_ERROR_500);
+                    }
+                }
+
+                updatedPlaceIds.add(appSitePlace.getId());
+            }
+        }
+
+        // 删除不再需要的场地
+        for (AppSitePlace existingPlace : existingPlaces) {
+            if (!updatedPlaceIds.contains(existingPlace.getId())) {
+                int deleteResult = baseMapper.deleteById(existingPlace.getId());
+                if (deleteResult < 1) {
+                    throw new JeecgBootException("包场场地删除失败", SC_INTERNAL_SERVER_ERROR_500);
+                }
+
+                // 删除对应的价格规则
+                appSitePriceRulesMapper.delete(Wrappers.<AppSitePriceRules>lambdaQuery()
+                        .eq(AppSitePriceRules::getSitePlaceId, existingPlace.getId()));
+            }
+        }
+
+        return Boolean.TRUE;
     }
 
     @Override