Browse Source

accomplish /recommend/posters Api

xuchaolang 7 years ago
parent
commit
e1653b3aba

+ 30 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/controller/RecommendController.java

@@ -6,11 +6,14 @@ 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.app.vo.RecommendVo;
+import cn.rankin.data.api.product.entity.Poster;
 import org.springframework.beans.factory.annotation.Autowired;
 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;
 
 @RestController
@@ -29,4 +32,31 @@ public class RecommendController {
         String merchantId = user.getMerchantId();
         return productService.getRecommendCourses(merchantId);
     }
+
+    /**
+     * Get recommend posters
+      * @param user
+     * @return
+     */
+    @RequestMapping(value = "/posters", method = RequestMethod.GET)
+    public APIResult<Object> getPosters(@NeedUser DeviceUserVo user) {
+        if (user == null) {
+            return APIResult.error(ApiWebCode.NOT_EXISTS);
+        }
+
+        String merchantId = user.getMerchantId();
+
+        //productService.getRecommendCourses(merchantId);
+
+        List<Poster> posters = productService.getPosters(merchantId);
+        if (null == posters){
+            posters = new ArrayList<>();
+        }
+
+        HashMap<String, Object> data = new HashMap<String, Object>();
+        data.put("num", posters.size());
+        data.put("recs",posters);
+
+        return APIResult.ok(data);
+    }
 }

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

@@ -11,9 +11,11 @@ import cn.rankin.data.api.product.vo.CourseItemVo;
 import cn.rankin.data.api.product.vo.SupportItemVo;
 import cn.rankin.data.api.product.vo.TrainingItemVo;
 import org.springframework.cloud.netflix.feign.FeignClient;
+import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Component;
 import org.springframework.web.bind.annotation.*;
 
+import java.util.ArrayList;
 import java.util.List;
 
 @FeignClient(name = "${service.product.name}")
@@ -55,6 +57,12 @@ public interface ProductClient {
     @RequestMapping(value = "/goods/{goodsId}", method = RequestMethod.GET)
     APIResult<Goods> findGoods(@PathVariable("goodsId") String goodsId);
 
+    @RequestMapping(value = "/{merchantId}/posters", method = RequestMethod.GET)
+    List<Poster> getPosters(@PathVariable("merchantId") String merchantId, @RequestParam("start") Long start,
+                                   @RequestParam("offset") Integer offset, @RequestParam("sortKey") String sortKey,
+                                   @RequestParam("direction") Sort.Direction direction);
+
+
     @Component
     class ProductClientHystrix implements ProductClient {
 
@@ -117,5 +125,12 @@ public interface ProductClient {
         public APIResult<Goods> findGoods(String goodsId) {
             return APIResult.error(ApiWebCode.SERVER_ERROR);
         }
+
+        public List<Poster> getPosters(String merchantId, Long start,
+                                Integer offset, String sortKey, Sort.Direction direction){
+            return new ArrayList<>();
+        }
+
+
     }
 }

+ 5 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/service/product/ProductService.java

@@ -12,6 +12,7 @@ import cn.rankin.data.api.product.vo.CourseItemVo;
 import cn.rankin.data.api.product.vo.SupportItemVo;
 import cn.rankin.data.api.product.vo.TrainingItemVo;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
@@ -101,4 +102,8 @@ public class ProductService {
         return APIResult.error(ApiWebCode.ACCESS_DENIED);
     }
 
+    public List<Poster> getPosters(String merchantId){
+        return productClient.getPosters(merchantId, 0L, 10, "sort", Sort.Direction.ASC);
+    }
+
 }

+ 5 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/utils/RequestHeaderManager.java

@@ -34,10 +34,15 @@ public class RequestHeaderManager {
      */
     public static String getRequestParameter(HttpServletRequest request, String key){
         //first check header
+        /*
         if (null != request.getHeader(key)){
             return request.getHeader(key);
         }
         return request.getParameter(key);
+        */
+
+        //use Header directly
+        return request.getHeader(key);
     }
 
 

+ 43 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/Proxy/ProxyWrapper.java

@@ -0,0 +1,43 @@
+package cn.rankin.common.utils.Proxy;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
+
+class ProxyWrapper implements InvocationHandler {
+    //被代理类的对象
+    private Object target;
+
+    //绑定被代理对象
+    public Object bind(Object target){
+        this.target = target;
+        //返回实现了被代理类所实现的所有接口的Object对象,即动态代理,需要强制转型
+        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
+                target.getClass().getInterfaces(), this);
+    }
+
+    //日志记录方法
+    private void log(String method){
+        return;
+    }
+
+    /**
+     * <p>Discription:覆盖InvocationHandler接口中的invoke()方法</p>
+     * @param proxy 需要代理的对象
+     * @param method 真实主体要调用的执行方法
+     * @param args 调用方法时传递的参数
+     * @return
+     * @throws Throwable
+     * @author       : lcma
+     * @update       : 2016年10月9日下午2:46:29
+     */
+    @Override
+    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+        //使用反射中的invoke()对方法进行动态调用
+        Object obj = method.invoke(this.target, args);
+        return obj;
+    }
+
+}
+

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

@@ -0,0 +1,56 @@
+package cn.rankin.data.api.product.entity;
+
+import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.enums.ProductTypeEnum;
+import lombok.Data;
+import lombok.ToString;
+import org.hibernate.annotations.DynamicInsert;
+import org.hibernate.annotations.DynamicUpdate;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.util.Date;
+
+@Data
+@ToString
+@Entity
+@Table(name = "p_posters")
+@DynamicInsert
+@DynamicUpdate
+public class Poster implements Serializable {
+    /**
+     * 推荐
+     */
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    private String id;
+
+    @Column(name = "merchant_id")
+    private String merchantId;
+
+    @Column
+    private String pid;
+
+    @Column
+    private Integer img;
+
+    @Column
+    private Integer sort;
+
+    @Enumerated(EnumType.ORDINAL)
+    private ProductTypeEnum type;
+
+    @Enumerated(EnumType.ORDINAL)
+    private BaseStatusEnum status;
+
+    @Column(name = "gmt_created", updatable = false, insertable = false, columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP")
+    @Temporal(TemporalType.TIMESTAMP)
+    private Date gmtCreated;
+
+    @Column(name = "gmt_modified", updatable = false, insertable = false, columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
+    @Temporal(TemporalType.TIMESTAMP)
+    private Date gmtModified;
+
+}
+

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

@@ -0,0 +1,41 @@
+package cn.rankin.productservice.controller;
+
+import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.data.api.product.entity.Poster;
+import cn.rankin.productservice.repository.PosterRepository;
+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 = "/merchant")
+@Slf4j
+public class MerchantController {
+
+    @Autowired
+    private PosterRepository posterRepo;
+
+    @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,
+                                         @RequestParam("direction") Sort.Direction direction) {
+
+        //get posters
+        Poster findSample = new Poster();
+        findSample.setMerchantId(merchantId);
+        findSample.setStatus(BaseStatusEnum.NORMAL);
+
+        List<Poster> posters = posterRepo.find(findSample, start, offset, new Sort(direction, sortKey));
+
+        if (null == posters){
+            posters = new ArrayList<Poster>();
+        }
+        log.info("Find Posters, len={}", posters.size());
+        return posters;
+    }
+}
+

+ 9 - 0
rankin-product-service/src/main/java/cn/rankin/productservice/repository/PosterRepository.java

@@ -0,0 +1,9 @@
+package cn.rankin.productservice.repository;
+
+import cn.rankin.common.utils.jpa.BasicJpaRepository;
+import cn.rankin.data.api.product.entity.Poster;
+
+public interface PosterRepository extends BasicJpaRepository<Poster, String> {
+
+}
+