Procházet zdrojové kódy

Merge remote-tracking branch 'origin/ScrmStores' into ScrmStores

zhangqin před 6 dny
rodič
revize
15b682ff68

+ 32 - 2
fs-service/src/main/java/com/fs/hisStore/mapper/FsStoreScrmMapper.java

@@ -137,12 +137,42 @@ public interface FsStoreScrmMapper
     List<FsStoreScrm> getStoreInfoByproductId(@Param("productIds") Set<Long> productIds);
 
 
-    @Select("SELECT COUNT(DISTINCT merchant_id) FROM fs_store_scrm WHERE merchant_id LIKE CONCAT(#{datePrefix}, '%')")
-    Long countMerchantsByDatePrefix(@Param("datePrefix") String datePrefix );
 
     /**
      * 查询当天已生成的店铺数量
      */
     @Select("SELECT COUNT(*) FROM fs_store_scrm WHERE store_seq LIKE CONCAT(#{date}, '%')")
     Long countByStoreSeqPrefix(@Param("date") String date);
+
+    /**
+     * 检查营业执照是否已存在
+     */
+    @Select(" SELECT COUNT(*) > 0 FROM fs_store_scrm WHERE business_code = #{businessCode}")
+    boolean existsByBusinessCode(@Param("businessCode") String businessCode);
+
+    /**
+     * 根据营业执照编码查找商家ID
+     */
+    @Select("SELECT merchant_id FROM fs_store_scrm \n" +
+            "    WHERE business_code = #{businessCode} \n" +
+            "    LIMIT 1")
+    String findMerchantIdByBusinessCode(@Param("businessCode") String businessCode);
+
+    /**
+     * 检查同一商家下店铺名称是否重复
+     */
+    @Select(" SELECT COUNT(*) > 0 FROM fs_store_scrm \n" +
+            "    WHERE merchant_id = #{merchantId} AND store_name = #{storeName}")
+    boolean existsStoreNameInMerchant(@Param("merchantId") String merchantId,
+                                      @Param("storeName") String storeName);
+
+    /**
+     * 查询当天新商家的数量(基于营业执照)
+     */
+    Long countNewMerchantsByDate(@Param("datePrefix") String datePrefix);
+
+    /**
+     * 查询商家下的店铺数量
+     */
+    Integer countStoresByMerchant(@Param("merchantId") String merchantId);
 }

+ 55 - 20
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreScrmServiceImpl.java

@@ -3,6 +3,7 @@ package com.fs.hisStore.service.impl;
 import com.alibaba.fastjson.JSONArray;
 import com.fs.common.exception.CustomException;
 import com.fs.common.exception.ServiceException;
+import com.fs.common.exception.base.BaseException;
 import com.fs.common.utils.DateUtils;
 import com.fs.common.utils.StringUtils;
 import com.fs.his.param.FsStoreAuditParam;
@@ -102,10 +103,23 @@ public class FsStoreScrmServiceImpl implements IFsStoreScrmService {
         StoreMD5PasswordEncoder storeMD5PasswordEncoder = new StoreMD5PasswordEncoder();
         fsStore.setPassword(storeMD5PasswordEncoder.encode("XyzAbc~12"));
         fsStore.setCreateTime(DateUtils.getNowDate());
+        if (fsStore.getBusinessCode() != null && !fsStore.getBusinessCode().trim().isEmpty()) {
+            boolean exists = fsStoreMapper.existsByBusinessCode(fsStore.getBusinessCode());
+            if (exists) {
+                // 检查是否是同一个商家
+                String existingMerchantId = fsStoreMapper.findMerchantIdByBusinessCode(fsStore.getBusinessCode());
+                if (existingMerchantId != null) {
+                    // 同一商家下检查店铺名称是否重复
+                    if (fsStoreMapper.existsStoreNameInMerchant(existingMerchantId, fsStore.getStoreName())) {
+                        throw new BaseException("该商家下已存在同名店铺");
+                    }
+                }
+            }
+        }
         //生成商家id
-        String merchantId = generateMerchantId();
+        String merchantId = generateMerchantId(fsStore.getBusinessCode());
         //生成店铺id
-        String sequence = generateStoreSequence();
+        String sequence = generateStoreSequence(merchantId,fsStore.getBusinessCode());
         fsStore.setMerchantId(merchantId);
         fsStore.setStoreSeq(sequence);
         fsStoreMapper.insertFsStore(fsStore);
@@ -116,36 +130,57 @@ public class FsStoreScrmServiceImpl implements IFsStoreScrmService {
      * 生成商家ID:YJB202510180001
      * 格式:前缀 + 年月日 + 4位序列号
      */
-    public String generateMerchantId() {
-        // 获取当天日期
-        String datePart = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
+    @Transactional
+    public String generateMerchantId(String businessCode) {
+        // 如果营业执照已存在,返回已有的商家ID
+        if (businessCode != null && !businessCode.trim().isEmpty()) {
+            String existingMerchantId = fsStoreMapper.findMerchantIdByBusinessCode(businessCode);
+            if (existingMerchantId != null) {
+                return existingMerchantId;
+            }
+        }
 
-        // 查询当天已生成的商家数量
-        Long todayCount = fsStoreMapper.countMerchantsByDatePrefix(MERCHANT_PREFIX + datePart);
+        // 新营业执照,生成新的商家ID
+        String datePart = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
+        String prefix = MERCHANT_PREFIX + datePart;
 
-        // 生成序号 (从0001开始)
-        Long sequence = (todayCount == null) ? 1L : todayCount + 1;
-        String sequencePart = String.format("%04d", sequence);
+        // 查询当天已生成的新商家数量(基于营业执照)
+        Long todayNewMerchantCount = fsStoreMapper.countNewMerchantsByDate(prefix);
+        Long sequence = (todayNewMerchantCount == null) ? 1L : todayNewMerchantCount + 1;
 
-        return MERCHANT_PREFIX + datePart + sequencePart;
+        return prefix + String.format("%04d", sequence);
     }
 
+
+
     /**
      * 生成店铺序号:年月日 + 当天第几家店
      * 格式:202510180001
      */
-    public String generateStoreSequence() {
-        // 获取当天日期
+    @Transactional
+    public String generateStoreSequence(String merchantId, String businessCode) {
         String datePart = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
 
-        // 查询当天已生成的店铺数量
-        Long todayCount = fsStoreMapper.countByStoreSeqPrefix(datePart);
-
-        // 生成序号 (从1开始)
-        Long sequence = (todayCount == null) ? 1L : todayCount + 1;
-        String sequencePart = String.format("%04d", sequence);
+        // 检查是否是新商家(根据营业执照是否已存在)
+        boolean isNewMerchant = false;
+        if (businessCode != null && !businessCode.trim().isEmpty()) {
+            String existingMerchantId = fsStoreMapper.findMerchantIdByBusinessCode(businessCode);
+            isNewMerchant = (existingMerchantId == null);
+        } else {
+            // 如果没有营业执照,检查商家ID是否已存在
+            Integer existingStoreCount = fsStoreMapper.countStoresByMerchant(merchantId);
+            isNewMerchant = (existingStoreCount == null || existingStoreCount == 0);
+        }
 
-        return datePart + sequencePart;
+        if (isNewMerchant) {
+            // 新商家,店铺序列从1开始
+            return datePart + "0001";
+        } else {
+            // 现有商家,获取当前商家的店铺数量+1
+            Integer storeCount = fsStoreMapper.countStoresByMerchant(merchantId);
+            Long sequence = (storeCount == null) ? 1L : storeCount + 1L;
+            return datePart + String.format("%04d", sequence);
+        }
     }
 
     /**