|
@@ -14,18 +14,20 @@ import cn.rankin.data.api.product.entity.*;
|
|
|
import cn.rankin.data.api.product.entity.Package;
|
|
|
import cn.rankin.productservice.code.ProductServiceAPICode;
|
|
|
import cn.rankin.productservice.repository.MerchantProductRepository;
|
|
|
+import cn.rankin.productservice.repository.PackageProductRelationRepository;
|
|
|
import cn.rankin.productservice.repository.ProductRepository;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.transaction.Transactional;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
|
|
|
import static cn.rankin.productservice.utils.DTOConverter.convert;
|
|
|
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class ProductService {
|
|
|
|
|
|
@Autowired
|
|
@@ -46,6 +48,9 @@ public class ProductService {
|
|
|
@Autowired
|
|
|
private TrainingService trainingService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private PackageProductRelationRepository packageProductRelationRepository;
|
|
|
+
|
|
|
public APIResult<Page<Product>> search(Product product, Integer pageNo, Integer pageSize, LinkedHashMap<String, BaseOrderEnum> sort) {
|
|
|
Long count = productRepository.count(product);
|
|
|
Page<Product> page = new Page<>(count, pageNo, pageSize);
|
|
@@ -89,7 +94,7 @@ public class ProductService {
|
|
|
}
|
|
|
|
|
|
public Map<String, Object> getProduct(String productId) {
|
|
|
- Map<String, Object> result = null;
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
|
|
|
|
Product product = productRepository.findByPid(productId);
|
|
@@ -139,6 +144,46 @@ public class ProductService {
|
|
|
return packageService.getPackage(productId);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Get Related Packge by pid
|
|
|
+ * @param pid
|
|
|
+ * @param start
|
|
|
+ * @param offset
|
|
|
+ * @param sortKey
|
|
|
+ * @param direction
|
|
|
+ * @return Plain Product, without extra fields like childrens etc.
|
|
|
+ */
|
|
|
+ public List<Product> getRelatedPackages(String pid, String merchantId, Long start, Integer offset, String sortKey, Sort.Direction direction){
|
|
|
+ List<PackageProductRelation> currentRelationList = packageProductRelationRepository.findByPid(pid);
|
|
|
+
|
|
|
+ //get pkgs
|
|
|
+ List<String> pids = new ArrayList<>();
|
|
|
+
|
|
|
+ for(PackageProductRelation rel : currentRelationList){
|
|
|
+ //skip pid not belong merchant
|
|
|
+
|
|
|
+ if(null == merchantProductRepository.findByPidAndMerchantId(pid, merchantId)){
|
|
|
+ log.info("Skip, Pid not Belong Merchant, pid={}, merchantId={}", pid, merchantId);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ pids.add(rel.getPkgId());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (pids.isEmpty()){
|
|
|
+ log.info("No Related Package Find By pid, pid={}", pid);
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Product> pkgs = productRepository.findByPids(pids);
|
|
|
+
|
|
|
+ if (null == pkgs){
|
|
|
+ pkgs = new ArrayList<>();
|
|
|
+ }
|
|
|
+ return pkgs;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Transactional
|
|
|
public APIResult delete(String productId) {
|
|
|
Product product = productRepository.findByPid(productId);
|