Преглед изворни кода

Accomplish Api /product/<pid>/relatedPkg

xuchaolang пре 6 година
родитељ
комит
c6b6a14078

+ 82 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/controller/ProductController.java

@@ -0,0 +1,82 @@
+package cn.rankin.apiweb.controller;
+
+import cn.rankin.apiweb.assist.resolver.NeedUser;
+import cn.rankin.apiweb.service.product.ProductClient;
+import cn.rankin.apiweb.service.product.ProductService;
+import cn.rankin.common.utils.api.model.APIResult;
+import cn.rankin.data.api.app.vo.DeviceUserVo;
+import cn.rankin.data.api.product.entity.Product;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Sort;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.*;
+
+
+@RestController
+@RequestMapping(value = "/product")
+@Slf4j
+public class ProductController {
+
+    @Autowired
+    private ProductService productService;
+
+    @Autowired
+    private ProductClient productClient;
+
+    @RequestMapping(value = "/{pid}/relatedPkg", method = RequestMethod.GET)
+    public APIResult getRelatedPkg(@NeedUser DeviceUserVo user, @PathVariable("pid") String pid) {
+        String merchantId = user.getMerchantId();
+
+        //get Pkg
+        List<Product> packages = productClient.getRelatedPackages(pid, merchantId, 0L, 10, "id", Sort.Direction.ASC);
+
+        //get each product in Pkg
+        List<Map<String, Object>> recs = new ArrayList<>();
+
+        for(Product pkg : packages){
+
+            if(null == pkg.getPid()){
+                log.error("Related Package pid is Null, pid={}", pid);
+                continue;
+            }
+
+            Product targetPkg =  productClient.getPackage(pkg.getPid());
+
+            List<Product> pkgContents = Optional.ofNullable(targetPkg).map(v -> v.getChildrens()).orElse(new ArrayList<>());
+
+            if(null == pkgContents){
+                pkgContents = new ArrayList<>();
+            }
+
+            List<Map<String, Object>> lightProducts = new ArrayList<>();
+            for(Product content : pkgContents){
+               Map<String, Object> tmp = new HashMap<>();
+               tmp.put("id", content.getPid());
+               tmp.put("name", content.getName());
+               tmp.put("type", content.getType());
+               lightProducts.add(tmp);
+            }
+
+            Map<String, Object> rec = new HashMap<String, Object>();
+            rec.put("id", pkg.getPid());
+            rec.put("name", pkg.getName());
+            rec.put("goods", pkg.getGoods());
+            rec.put("recs", lightProducts);
+
+            recs.add(rec);
+        }
+
+        Map<String, Object> data = new HashMap<>();
+
+        data.put("totalNum", recs.size());
+        data.put("recs", recs);
+
+        return APIResult.ok(data);
+
+    }
+}

+ 19 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/service/product/ProductClient.java

@@ -68,6 +68,14 @@ public interface ProductClient {
                                               @RequestParam("offset") Integer offset, @RequestParam("sortKey") String sortKey,
                                               @RequestParam("direction") Sort.Direction direction);
 
+    @RequestMapping(value = "/product/{pid}/relatedPkg", method = RequestMethod.GET)
+    List<Product> getRelatedPackages(@PathVariable("pid") String pid, @RequestParam("merchantId") String merchantId, @RequestParam("start") Long start,
+                                              @RequestParam("offset") Integer offset, @RequestParam("sortKey") String sortKey,
+                                              @RequestParam("direction") Sort.Direction direction);
+
+    @RequestMapping(value = "/package/{pid}", method = RequestMethod.GET)
+    Product getPackage(@PathVariable("pid") String pid);
+
 
     @Component
     class ProductClientHystrix implements ProductClient {
@@ -144,5 +152,16 @@ public interface ProductClient {
             return new ArrayList<>();
         }
 
+        @Override
+        public List<Product> getRelatedPackages(String pid, String merchantId, Long start,
+                                                         Integer offset, String sortKey, Sort.Direction direction){
+            return new ArrayList<>();
+        }
+
+        @Override
+        public Product getPackage(String pid){
+            return null;
+        }
+
     }
 }

+ 1 - 1
rankin-data-api/src/main/java/cn/rankin/data/api/product/entity/Poster.java

@@ -33,7 +33,7 @@ public class Poster implements Serializable {
     private String pid;
 
     @Column
-    private Integer img;
+    private String img;
 
     @Column
     private Integer sort;

+ 8 - 0
rankin-data-api/src/main/java/cn/rankin/data/api/product/entity/Product.java

@@ -10,6 +10,7 @@ import org.hibernate.annotations.DynamicUpdate;
 import javax.persistence.*;
 import java.io.Serializable;
 import java.util.Date;
+import java.util.List;
 
 @Data
 @ToString
@@ -45,5 +46,12 @@ public class Product implements Serializable {
     @Column(name = "gmt_modified", updatable = false, insertable = false, columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
     @Temporal(TemporalType.TIMESTAMP)
     private Date gmtModified;
+
+    @Transient
+    private List<Goods> goods;
+
+    //Just For Packages
+    @Transient
+    private List<Product> childrens;
 }
 

+ 2 - 1
rankin-product-service/src/main/java/cn/rankin/productservice/controller/MerchantController.java

@@ -34,7 +34,8 @@ public class MerchantController {
         if (null == posters){
             posters = new ArrayList<Poster>();
         }
-        log.info("Find Posters, len={}", posters.size());
+
+        log.info("Find Posters, len={}, merchantId={}", posters.size(), merchantId);
         return posters;
     }
 }

+ 28 - 0
rankin-product-service/src/main/java/cn/rankin/productservice/controller/PackageController.java

@@ -0,0 +1,28 @@
+package cn.rankin.productservice.controller;
+
+import cn.rankin.data.api.product.entity.Product;
+import cn.rankin.productservice.service.PackageService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+
+@RestController
+@RequestMapping(value = "/package")
+@Slf4j
+public class PackageController {
+
+    @Autowired
+    private PackageService packageService;
+
+    @RequestMapping(value = "/{pid}", method = RequestMethod.GET)
+    public Product getPackage(@PathVariable("pid") String pid) {
+
+        return packageService.get(pid);
+
+    }
+}
+

+ 45 - 0
rankin-product-service/src/main/java/cn/rankin/productservice/controller/ProductController.java

@@ -0,0 +1,45 @@
+package cn.rankin.productservice.controller;
+
+import cn.rankin.data.api.product.entity.Product;
+import cn.rankin.productservice.service.PackageService;
+import cn.rankin.productservice.service.ProductService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Sort;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RestController
+@RequestMapping(value = "/product")
+@Slf4j
+public class ProductController {
+
+    @Autowired
+    private ProductService productService;
+
+    @Autowired
+    private PackageService packageService;
+
+    @RequestMapping(value = "/{pid}/relatedPkg", method = RequestMethod.GET)
+    public List<Product> getRelatedPkg(@PathVariable("pid") String pid, @RequestParam("merchantId") String merchantId, @RequestParam("start") Long start,
+                                   @RequestParam("offset") Integer offset, @RequestParam("sortKey") String sortKey,
+                                   @RequestParam("direction") Sort.Direction direction) {
+
+        //get pkgs
+        List<Product> pkgs = productService.getRelatedPackages(pid, merchantId, start, offset, sortKey, Sort.Direction.ASC);
+        for (Product pkg :pkgs){
+
+            List<Product> childrens = packageService.getChildrens(pkg.getPid());
+
+            pkg.setChildrens(childrens);
+        }
+
+        if(null == pkgs){
+            pkgs = new ArrayList<>();
+        }
+
+        return pkgs;
+    }
+}

+ 3 - 3
rankin-product-service/src/main/java/cn/rankin/productservice/controller/auth/AuthController.java

@@ -60,7 +60,7 @@ public class AuthController {
         HashMap<String, AuthVo> authMap = new HashMap<>();
         for (AuthVo item: authItems ) {
             if(null == item.getPid()) {
-                log.error("Pid-Is-Null, uid={}", uid);
+                log.error("Pid Is Null, uid={}", uid);
                 continue;
             }
             pids.add(item.getPid());
@@ -70,7 +70,7 @@ public class AuthController {
         List< Map<String, Object> > result = new ArrayList<>();
 
         if (pids.isEmpty()){
-            log.error("No-Valid-Pids-For-User, uid={}", uid);
+            log.error("No Valid Pids For User, uid={}", uid);
             return result;
         }
 
@@ -81,7 +81,7 @@ public class AuthController {
 
             String pid = product.getPid();
             if (!authMap.containsKey(pid)){
-                log.error("pid Not Exist in authMap, pid={}", pid);
+                log.error("Pid Not Exist in AuthMap, pid={}", pid);
                 continue;
             }
 

+ 56 - 1
rankin-product-service/src/main/java/cn/rankin/productservice/service/PackageService.java

@@ -12,19 +12,20 @@ import cn.rankin.productservice.code.ProductServiceAPICode;
 import cn.rankin.productservice.repository.PackageRepository;
 import cn.rankin.productservice.repository.ProductRepository;
 import cn.rankin.productservice.utils.DTOConverter;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
 import javax.transaction.Transactional;
-import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 @Service
+@Slf4j
 public class PackageService {
 
     @Autowired
@@ -44,6 +45,60 @@ public class PackageService {
         return count > 0;
     }
 
+    /**
+     *
+     * @param pid
+     * @return package's children list
+     */
+    public List<Product> getChildrens(String pid){
+
+        List<PackageProductVo> packageProductVoList = getPackageProducts(pid);
+
+        //get Product by pids
+        List<String> pids = new ArrayList<>();
+
+        for(PackageProductVo tmp : packageProductVoList){
+            if (null == tmp.getPid() || pid.equals(tmp.getPid())){
+                continue;
+            }
+            pids.add(tmp.getPid());
+        }
+
+        if(pids.isEmpty()){
+            log.info("Cannot Find Package Childrens, pid={}", pid);
+            return new ArrayList<>();
+        }
+
+        List<Product> products = productRepository.findByPids(pids);
+
+        if (null == products){
+            products = new ArrayList<>();
+        }
+
+        return products;
+    }
+
+    /**
+     *
+     * @param pid
+     * @return product, with valid package fields
+     */
+    public Product get(String pid){
+
+        Product pkg = productRepository.findByPid(pid);
+
+        if (null == pkg){
+            log.error("Cannot Find Product By pid, pid={}", pid);
+            return null;
+        }
+
+        List<Product> childrens = getChildrens(pid);
+
+        pkg.setChildrens(childrens);
+
+        return pkg;
+    }
+
     public APIResult<Package> getPackage(String pkgId) {
         Package pkg = packageRepository.find(pkgId);
         if (pkg == null) {

+ 49 - 4
rankin-product-service/src/main/java/cn/rankin/productservice/service/ProductService.java

@@ -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);