Pārlūkot izejas kodu

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

yjwang 3 dienas atpakaļ
vecāks
revīzija
de4e79aef0

+ 103 - 106
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreProductScrmServiceImpl.java

@@ -5,6 +5,7 @@ import java.math.BigDecimal;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
+import java.util.function.Consumer;
 import java.util.stream.Collectors;
 
 import cn.hutool.core.collection.ListUtil;
@@ -69,27 +70,31 @@ import org.springframework.transaction.annotation.Transactional;
 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
 @Slf4j
 public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService {
+
     @Autowired
     private FsStoreProductScrmMapper fsStoreProductMapper;
+
     @Autowired
     private FsStoreProductAttrValueScrmMapper fsStoreProductAttrValueMapper;
+
     @Autowired
     private FsStoreProductAttrScrmMapper fsStoreProductAttrMapper;
 
-
     @Autowired
     private IFsStoreProductAttrValueScrmService attrValueService;
+
     @Autowired
     private FsStoreProductPackageScrmMapper fsStoreProductPackageMapper;
+
     @Autowired
     private FsStoreProductGroupScrmMapper fsStoreProductGroupMapper;
 
     @Autowired
     private ICompanyCacheService companyCacheService;
 
-
     @Autowired
     private ConfigUtil configUtil;
+
     @Autowired
     private ISysConfigService configService;
 
@@ -103,13 +108,13 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
 
     @Autowired
     private StoreAuditLogUtil storeAuditLogUtil;
+
     @Autowired
     private FsStoreScrmServiceImpl fsStoreScrmService;
 
     @Autowired
     private CloudHostProper cloudHostProper;
 
-
     @Autowired
     private IFsStoreProductCategoryScrmService fsStoreProductCategoryService;
 
@@ -119,6 +124,8 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
         forbiddenListManager.initDefaultList();
     }
 
+    private final static String CZT = "纯正堂";
+
     /**
      * 查询商品
      *
@@ -408,103 +415,63 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
     @Override
     @Transactional
     public R addOrEdit(FsStoreProductAddEditParam param) {
-        ProductAttrCountDto countDto = computedProductCount(param.getValues());
-        FsStoreProductScrm product = new FsStoreProductScrm();
-        BeanUtils.copyProperties(param, product);
-        product.setPrice(countDto.getMinPrice());
-        product.setOtPrice(countDto.getMinOtPrice());
-        product.setCost(countDto.getMinCost());
-        product.setIntegral(Long.parseLong(countDto.getMinIntegral().toString()));
-        product.setStock(Long.parseLong(countDto.getStock().toString()));
-        product.setCompanyIds(param.getCompanyIds());
-        product.setVideo(param.getVideo());
-        Long storeId = param.getStoreId();
-        product.setStoreId(storeId);
-        product.setIsDrug(param.getIsDrug().toString());
-        product.setInstructionManual(param.getInstructionManual());
 
-        List<String> businessExpire = param.getBusinessExpire();
-        if (businessExpire != null && businessExpire.size() >= 2) {
-            product.setBusinessStart(parseLocalDate(businessExpire.get(0)));
-            product.setBusinessEnd(parseLocalDate(businessExpire.get(1)));
-        }
-
-        List<String> licenseExpire = param.getLicenseExpire();
-        if (licenseExpire != null && licenseExpire.size() >= 2) {
-            product.setLicenseStart(parseLocalDate(licenseExpire.get(0)));
-            product.setLicenseEnd(parseLocalDate(licenseExpire.get(1)));
-        }
-
-        List<String> certificateExpire = param.getCertificateExpire();
-        if (certificateExpire != null && certificateExpire.size() >= 2) {
-            product.setCertificateStart(parseLocalDate(certificateExpire.get(0)));
-            product.setCertificateEnd(parseLocalDate(certificateExpire.get(1)));
-        }
-
-        List<String> voucherExpire = param.getVoucherExpire();
-        if (voucherExpire != null && voucherExpire.size() >= 2) {
-            product.setVoucherStart(parseLocalDate(voucherExpire.get(0)));
-            product.setVoucherEnd(parseLocalDate(voucherExpire.get(1)));
+        //店铺判断
+        Long storeId = param.getStoreId();
+        FsStoreScrm store = fsStoreScrmService.selectFsStoreByStoreId(storeId);
+        if (store == null || 1 != store.getStatus()) {
+            return R.error("店铺不存在或未启用");
         }
 
-        List<String> gmpAuthExpire = param.getGmpAuthExpire();
-        if (gmpAuthExpire != null && gmpAuthExpire.size() >= 2) {
-            product.setGmpAuthStart(parseLocalDate(gmpAuthExpire.get(0)));
-            product.setGmpAuthEnd(parseLocalDate(gmpAuthExpire.get(1)));
+        //验证资质
+        switch (param.getProductType()) {
+            case 1://非处方
+                break;
+            case 2://处方
+                if (store.getIsDrugLicensePermanent() == 0 && StringUtils.isEmpty(store.getDrugLicense()) || "".equals(store.getDrugLicense()) && (store.getDrugLicenseExpiryEnd() == null ||  LocalDate.now().isAfter(store.getDrugLicenseExpiryEnd()))) {
+                    return R.error("店铺药品资质为空或已过期,请完善后再添加");
+                }
+                break;
+            case 3://食品
+                if (store.getIsFoodLicenseExpiryPermanent() == 0 && StringUtils.isEmpty(store.getFoodLicense()) || "".equals(store.getFoodLicense()) && (store.getFoodLicenseExpiryEnd() == null ||  LocalDate.now().isAfter(store.getFoodLicenseExpiryEnd()))) {
+                    return R.error("店铺食品资质为空或已过期,请完善后再添加");
+                }
+                break;
+            case 4://器械
+                if (store.getIsMedicalDevice3ExpiryPermanent() == 0 && StringUtils.isEmpty(store.getMedicalDevice3()) || "".equals(store.getMedicalDevice3()) && (store.getMedicalDevice3ExpiryEnd() == null ||  LocalDate.now().isAfter(store.getMedicalDevice3ExpiryEnd()))) {
+                    return R.error("店铺器械资质为空或已过期,请完善后再添加");
+                }
+                break;
+            default:
+                return R.error("商品类型错误");
         }
 
-        //获取商品编码
-        if (!param.getValues().isEmpty()) {
-            if (param.getValues().get(0).getBarCode() != null) {
-                product.setBarCode(param.getValues().get(0).getBarCode());
-            }
-        }
+        //基础信息
+        ProductAttrCountDto countDto = computedProductCount(param.getValues());
+        FsStoreProductScrm product = new FsStoreProductScrm();
+        BeanUtils.copyProperties(param, product);
 
+        //数据转换和提取
+        populateBasicInfo(product, countDto, param);
+        populateDateRanges(product, param);
+        setDefaultBarcodeIfAvailable(product, param);
         product.setBusinessLink(param.getBusinessLink());
 
-        //获取店铺
-        FsStoreScrm store = fsStoreScrmService.selectFsStoreByStoreId(product.getStoreId());
-        if (store == null || 1 != store.getStatus()) {
-            return R.error("店铺不存在或未启用");
-        } else {
-            //验证资质
-            switch (product.getProductType()) {
-                case 1://非处方
-                    break;
-                case 2://处方
-                    if (store.getIsDrugLicensePermanent() == 0 && StringUtils.isEmpty(store.getDrugLicense()) || "".equals(store.getDrugLicense()) && (store.getDrugLicenseExpiryEnd() == null ||  LocalDate.now().isAfter(store.getDrugLicenseExpiryEnd()))) {
-                        return R.error("店铺药品资质为空或已过期,请完善后再添加");
-                    }
-                    break;
-                case 3://食品
-                    if (store.getIsFoodLicenseExpiryPermanent() == 0 && StringUtils.isEmpty(store.getFoodLicense()) || "".equals(store.getFoodLicense()) && (store.getFoodLicenseExpiryEnd() == null ||  LocalDate.now().isAfter(store.getFoodLicenseExpiryEnd()))) {
-                        return R.error("店铺食品资质为空或已过期,请完善后再添加");
-                    }
-                    break;
-                case 4://器械
-                    if (store.getIsMedicalDevice3ExpiryPermanent() == 0 && StringUtils.isEmpty(store.getMedicalDevice3()) || "".equals(store.getMedicalDevice3()) && (store.getMedicalDevice3ExpiryEnd() == null ||  LocalDate.now().isAfter(store.getMedicalDevice3ExpiryEnd()))) {
-                        return R.error("店铺器械资质为空或已过期,请完善后再添加");
-                    }
-                    break;
-                default:
-                    return R.error("商品类型错误");
-            }
-
-            // 根据经营范围校验商品类型
-            if (com.fs.common.utils.StringUtils.isNotEmpty(store.getSelectableProductTypes())) {
-                String productTypeName = getProductTypeName(product.getCateId());
-                if (com.fs.common.utils.StringUtils.isNotEmpty(productTypeName)) {
-                    // 直接使用工具类方法校验
-//                    boolean isAllowed = BusinessScopeProductTypeUtil.isProductTypeAllowed(store.getSelectableProductTypes(), productTypeName);
-                    boolean isAllowed = validateShopBusinessScope(store.getBusinessScope(), productTypeName);
-                    if (!isAllowed) {
-                        return R.error("该商品类型[" + productTypeName + "]不在店铺经营范围允许的商品类型内,请联系管理员核实经营范围");
-                    }
+        // 根据经营范围校验商品类型
+        if (com.fs.common.utils.StringUtils.isNotEmpty(store.getSelectableProductTypes())) {
+            String productTypeName = getProductTypeName(product.getCateId());
+            if (com.fs.common.utils.StringUtils.isNotEmpty(productTypeName)) {
+                // 直接使用工具类方法校验
+                //boolean isAllowed = BusinessScopeProductTypeUtil.isProductTypeAllowed(store.getSelectableProductTypes(), productTypeName);
+                boolean isAllowed = validateShopBusinessScope(store.getBusinessScope(), productTypeName);
+                if (!isAllowed) {
+                    return R.error("该商品类型[" + productTypeName + "]不在店铺经营范围允许的商品类型内,请联系管理员核实经营范围");
                 }
             }
         }
 
-        if("纯正堂".equals(cloudHostProper.getCompanyName())){
+        //判断是否是纯正堂
+        if(CZT.equals(cloudHostProper.getCompanyName())){
             product.setIsAudit("1");
         }
 
@@ -541,26 +508,24 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
             } catch (IllegalAccessException e) {
                 log.error("获取diff出错", e);
             }
-            fsStoreProductMapper.updateFsStoreProduct(product);
-            if (param.getSpecType().equals(0)) {
-                ProductArrtDTO fromatDetailDto = ProductArrtDTO.builder()
-                        .value("规格")
-                        .detail(ListUtil.toList("默认"))
-                        .build();
-                List<ProductArrtDTO> items=new ArrayList<>();
-                items.add(fromatDetailDto);
-                param.getValues().get(0).setSku("默认");
-                addProductAttr(product.getProductId(),items,param.getValues(),storeId);
-            } else {
-                addProductAttr(product.getProductId(),param.getItems(),param.getValues(),storeId);
+            //避免重复审核时第一次审核结果为不通过,第二次申请重新上架导致无法在审核列表中查询到
+            if(oldFsStoreProduct.getIsAudit() != null && "2".equals(oldFsStoreProduct.getIsAudit())){
+                product.setIsAudit("0");
             }
-        }
-        else{
+            fsStoreProductMapper.updateFsStoreProduct(product);
+//            else外面也会调用这个方法,为啥会在if中调用执行handleProductAttributes
+//            handleProductAttributes(param, product, storeId);
+        } else{
             //新添加的商品默认都应该不是上架状态,流程都是需要先审核。即使你新增时候选择了上架状态,也应该是上架并待审核才对。
             product.setIsShow(0);
             fsStoreProductMapper.insertFsStoreProduct(product);
         }
         storeAuditLogUtil.addOperLog(product.getProductId());
+        handleProductAttributes(param, product, storeId); //统一调用handleProductAttributes
+        return R.ok();
+    }
+
+    private void handleProductAttributes(FsStoreProductAddEditParam param, FsStoreProductScrm product, Long storeId) {
         if (param.getSpecType().equals(0)) {
             ProductArrtDTO fromatDetailDto = ProductArrtDTO.builder()
                     .value("规格")
@@ -569,11 +534,10 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
             List<ProductArrtDTO> items=new ArrayList<>();
             items.add(fromatDetailDto);
             param.getValues().get(0).setSku("默认");
-            addProductAttr(product.getProductId(),items,param.getValues(),storeId);
+            addProductAttr(product.getProductId(),items, param.getValues(), storeId);
         } else {
-            addProductAttr(product.getProductId(),param.getItems(),param.getValues(),storeId);
+            addProductAttr(product.getProductId(), param.getItems(), param.getValues(), storeId);
         }
-        return R.ok();
     }
 
     /**
@@ -627,7 +591,6 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
         return res;
     }
 
-
     private void addProductAttr(Long productId, List<ProductArrtDTO> items, List<FsStoreProductAttrValueScrm> values,Long storeId){
         //清空attr
         fsStoreProductAttrMapper.clear(productId);
@@ -670,6 +633,40 @@ public class FsStoreProductScrmServiceImpl implements IFsStoreProductScrmService
         }
     }
 
+    private void populateBasicInfo(FsStoreProductScrm product, ProductAttrCountDto countDto, FsStoreProductAddEditParam param) {
+        product.setPrice(countDto.getMinPrice());
+        product.setOtPrice(countDto.getMinOtPrice());
+        product.setCost(countDto.getMinCost());
+        product.setIntegral(Long.valueOf(countDto.getMinIntegral()));
+        product.setStock(Long.valueOf(countDto.getStock()));
+        product.setCompanyIds(param.getCompanyIds());
+        product.setVideo(param.getVideo());
+        product.setStoreId(param.getStoreId());
+        product.setIsDrug(param.getIsDrug().toString());
+        product.setInstructionManual(param.getInstructionManual());
+    }
+
+    private void populateDateRanges(FsStoreProductScrm product, FsStoreProductAddEditParam param) {
+        setDateRange(product::setBusinessStart, product::setBusinessEnd, param.getBusinessExpire());
+        setDateRange(product::setLicenseStart, product::setLicenseEnd, param.getLicenseExpire());
+        setDateRange(product::setCertificateStart, product::setCertificateEnd, param.getCertificateExpire());
+        setDateRange(product::setVoucherStart, product::setVoucherEnd, param.getVoucherExpire());
+        setDateRange(product::setGmpAuthStart, product::setGmpAuthEnd, param.getGmpAuthExpire());
+    }
+
+    private void setDateRange(Consumer<LocalDate> startSetter, Consumer<LocalDate> endSetter, List<String> dates) {
+        if (dates != null && dates.size() >= 2) {
+            startSetter.accept(parseLocalDate(dates.get(0)));
+            endSetter.accept(parseLocalDate(dates.get(1)));
+        }
+    }
+
+    private void setDefaultBarcodeIfAvailable(FsStoreProductScrm product, FsStoreProductAddEditParam param) {
+        if (!param.getValues().isEmpty() && param.getValues().get(0).getBarCode() != null) {
+            product.setBarCode(param.getValues().get(0).getBarCode());
+        }
+    }
+
     private DetailVO attrFormat(List<ProductArrtDTO> fromatDetailDTOList) {
         List<String> data = new ArrayList<>();
         List<Map<String, Map<String, String>>> res = new ArrayList<>();