Browse Source

Accomplish /user/messages/productExpiredAlert, /package/<pid>

xuchaolang 6 years ago
parent
commit
e0359d50e8

+ 60 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/controller/PackageController.java

@@ -0,0 +1,60 @@
+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.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
+@RestController
+@RequestMapping(value="/package")
+@Slf4j
+public class PackageController {
+
+    @Autowired
+    private ProductService productService;
+
+    @Autowired
+    private ProductClient productClient;
+
+    @RequestMapping(value = "/{pid}")
+    public APIResult getPackage(@NeedUser DeviceUserVo user, @PathVariable("pid") String pid){
+
+        String uid = user.getUid();
+        String merchantId = user.getMerchantId();
+
+        //get merchant product, with goods field
+        Product product = productClient.getProduct(merchantId, pid);
+
+        //get package, with children field
+        Product pkg = productClient.getPackage(pid);
+
+        if (null == product || null == pkg){
+            log.error("No Package Found, pid={}, merchantId={}", pid, merchantId);
+            return APIResult.ok(new HashMap<>());
+        }
+
+        pkg.setGoods(product.getGoods());
+
+        Map<String, Object> data = new HashMap<>();
+
+        data.put("id", pkg.getPid());
+        data.put("name", pkg.getName());
+        data.put("code", pkg.getCode());
+        data.put("type", pkg.getType());
+        data.put("totalNum", Optional.ofNullable(pkg.getChildrens()).map(v->v.size()).orElse(0));
+        data.put("recs", pkg.getChildrens());
+
+        return APIResult.ok(data);
+    }
+}

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

@@ -35,6 +35,22 @@ public class ProductController {
         //get Pkg
         List<Product> packages = productClient.getRelatedPackages(pid, merchantId, 0L, 10, "id", Sort.Direction.ASC);
 
+        if(null == packages){
+            packages = new ArrayList<>();
+        }
+
+        //if no package, self is a package
+        /*
+        if (packages.isEmpty()){
+            log.info("No RelatedPackage Found, Use Product itself, pid={}", pid);
+            Product targetProduct = productClient.getProduct(merchantId, pid);
+
+            if(null != targetProduct){
+                packages.add(targetProduct);
+            }
+        }
+        */
+
         //get each product in Pkg
         List<Map<String, Object>> recs = new ArrayList<>();
 
@@ -73,6 +89,7 @@ public class ProductController {
             recs.add(rec);
         }
 
+
         Map<String, Object> data = new HashMap<>();
 
         data.put("totalNum", recs.size());

+ 46 - 5
rankin-api-web/src/main/java/cn/rankin/apiweb/controller/UserController.java

@@ -14,10 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 @RestController
 @RequestMapping(value = "/user")
@@ -56,7 +53,7 @@ public class UserController {
             products = new ArrayList<>();
         }
 
-        log.error("Valid-Product-Get-From-Service, product={}, len={}", products, products.size());
+        log.error("Valid Product Get From Service, product={}, len={}", products, products.size());
 
         List<Map<String, Object> > recs = new ArrayList<>();
 
@@ -77,4 +74,48 @@ public class UserController {
         return APIResult.ok(data);
     }
 
+    @RequestMapping(value="/messages/productExpiredAlert", method = RequestMethod.GET)
+    public APIResult getMessagesProductExpired(@NeedUser DeviceUserVo user){
+
+        String uid = user.getUid();
+
+        //get Valid Product
+        List<Map<String, Object>> validItems =  userService.getProductValid(uid);
+
+        List<Map<String, Object> > recs = new ArrayList<>();
+
+        Long now = new Date().getTime();
+
+        // Five Days
+        Long remindTime = 5*24*3600*1000L;
+
+        for(Map<String, Object> item : validItems){
+
+            Long endTime = (Long) item.get("endTime");
+
+            if (null == endTime){
+                log.error("Illegal Product endTime, endTime=null");
+                continue;
+            }
+
+            if (endTime  > now + remindTime){
+               continue;
+            }
+
+            Map<String, Object> rec = new HashMap<>();
+            rec.put("id", item.get("pid"));
+            rec.put("title", item.get("name"));
+            rec.put("beginTime", item.get("beginTime"));
+            rec.put("endTime", item.get("endTime"));
+            rec.put("type", item.get("type"));
+            recs.add(rec);
+        }
+
+        Map<String, Object> data = new HashMap<>();
+        data.put("totalNum", recs.size());
+        data.put("recs", recs);
+
+        return APIResult.ok(data);
+    }
+
 }

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

@@ -76,6 +76,10 @@ public interface ProductClient {
     @RequestMapping(value = "/package/{pid}", method = RequestMethod.GET)
     Product getPackage(@PathVariable("pid") String pid);
 
+    @RequestMapping(value = "/merchant/{merchantId}/product/{pid}", method = RequestMethod.GET)
+    Product getProduct(@PathVariable("merchantId") String merchantId, @PathVariable("pid") String pid);
+
+
 
     @Component
     class ProductClientHystrix implements ProductClient {
@@ -163,5 +167,11 @@ public interface ProductClient {
             return null;
         }
 
+
+        @Override
+        public Product getProduct(String merchantId, String pid) {
+            return null;
+        }
+
     }
 }

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

@@ -1,8 +1,15 @@
 package cn.rankin.productservice.controller;
 
 import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.data.api.product.entity.Goods;
+import cn.rankin.data.api.product.entity.MerchantProduct;
 import cn.rankin.data.api.product.entity.Poster;
+import cn.rankin.data.api.product.entity.Product;
+import cn.rankin.productservice.repository.GoodsRepository;
+import cn.rankin.productservice.repository.MerchantProductRepository;
 import cn.rankin.productservice.repository.PosterRepository;
+import cn.rankin.productservice.repository.ProductRepository;
+import cn.rankin.productservice.service.ProductService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Sort;
@@ -19,6 +26,18 @@ public class MerchantController {
     @Autowired
     private PosterRepository posterRepo;
 
+    @Autowired
+    private ProductService productService;
+
+    @Autowired
+    private ProductRepository productRepository;
+
+    @Autowired
+    private MerchantProductRepository merchantProductRepository;
+
+    @Autowired
+    private GoodsRepository goodsRepository;
+
     @RequestMapping(value = "/{merchantId}/posters", method = RequestMethod.GET)
     public List<Poster> getPosters(@PathVariable("merchantId") String merchantId, @RequestParam("start") Long start,
                                          @RequestParam("offset") Integer offset, @RequestParam("sortKey") String sortKey,
@@ -38,5 +57,31 @@ public class MerchantController {
         log.info("Find Posters, len={}, merchantId={}", posters.size(), merchantId);
         return posters;
     }
+
+    @RequestMapping(value = "/{merchantId}/product/{pid}", method = RequestMethod.GET)
+    public Product getProduct(@PathVariable("merchantId") String merchantId, @PathVariable("pid") String pid){
+
+        //check merchant
+        MerchantProduct merchantProduct =  merchantProductRepository.findByPidAndMerchantId(pid, merchantId);
+
+        if (null == merchantProduct){
+            log.error("Cannot Find Merchant Product By pid, pid={}, merchantId={}", pid, merchantId);
+            return null;
+        }
+
+        Product product = productRepository.findByPid(pid);
+
+        if (null == product){
+            log.error("Cannot Find Product By pid, pid={}", pid);
+            return null;
+        }
+
+        //get goods
+        List<Goods> goods = goodsRepository.findByPidAndMerchantId(pid, merchantId);
+
+        product.setGoods(goods);
+
+        return product;
+    }
 }
 

+ 5 - 4
rankin-product-service/src/main/java/cn/rankin/productservice/controller/PackageController.java

@@ -19,6 +19,11 @@ public class PackageController {
     @Autowired
     private GoodsRepository goodsRepository;
 
+    /**
+     * Get plain package info, with childrens field
+     * @param pid
+     * @return Package
+     */
     @RequestMapping(value = "/{pid}", method = RequestMethod.GET)
     public Product getPackage(@PathVariable("pid") String pid) {
 
@@ -33,10 +38,6 @@ public class PackageController {
             return null;
         }
 
-        //List<Goods> goods = goodsRepository.findByPidAndMerchantId(pid, merchantId);
-
-        //pkg.setGoods(goods);
-
         return pkg;
     }
 }

+ 11 - 2
rankin-product-service/src/main/java/cn/rankin/productservice/controller/ProductController.java

@@ -63,7 +63,7 @@ public class ProductController {
 
     @RequestMapping(value = "/{productId}", method = RequestMethod.GET)
     public APIResult getProduct2(@PathVariable("productId") String productId) {
-        Map<String, Object> product = productService.getProduct(productId);
+        Map<String, Object> product = productService.getProductDict(productId);
         if(null == product){
             return APIResult.error(ProductServiceAPICode.NOT_EXISTS);
         } else{
@@ -128,7 +128,16 @@ public class ProductController {
         return productService.findByPids(pidList);
     }
 
-
+    /**
+     *
+     * @param pid
+     * @param merchantId
+     * @param start
+     * @param offset
+     * @param sortKey
+     * @param direction
+     * @return
+     */
     @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,

+ 2 - 2
rankin-product-service/src/main/java/cn/rankin/productservice/service/MerchantProductService.java

@@ -133,7 +133,7 @@ public class MerchantProductService {
         for (MerchantProduct product : merchantProductList) {
             String productId = product.getPid();
 
-            Map<String, Object> productMap = productService.getProduct(productId);
+            Map<String, Object> productMap = productService.getProductDict(productId);
             product.setName(String.valueOf(productMap.get("name")));
 
             String merchantId = product.getMerchantId();
@@ -310,7 +310,7 @@ public class MerchantProductService {
             String productId = merchantProduct.getPid();
             List<Goods> tmp = goodsMap.get(productId);
 
-            Map<String, Object> product = productService.getProduct(productId);
+            Map<String, Object> product = productService.getProductDict(productId);
             merchantProduct.setName(String.valueOf(product.get("name")));
 
             merchantProduct.setGoods(tmp);

+ 11 - 1
rankin-product-service/src/main/java/cn/rankin/productservice/service/ProductService.java

@@ -94,7 +94,8 @@ public class ProductService {
         return APIResult.error(ProductServiceAPICode.NOT_EXISTS);
     }
 
-    public Map<String, Object> getProduct(String productId) {
+
+    public Map<String, Object> getProductDict(String productId) {
         Map<String, Object> result = new HashMap<>();
 
 
@@ -184,6 +185,15 @@ public class ProductService {
         return pkgs;
     }
 
+    /**
+     *
+     * @param pid
+     * @return
+     */
+    public Product getProduct(String pid){
+        return productRepository.findByPid(pid);
+    }
+
 
     @Transactional
     public APIResult delete(String productId) {