소스 검색

根据优惠查询

詹子聪 5 년 전
부모
커밋
a3668d8e5a

+ 15 - 1
src/main/java/com/miekir/shibei/bean/CouponBean.java

@@ -1,6 +1,8 @@
 package com.miekir.shibei.bean;
 
 
+import com.sun.org.apache.xpath.internal.operations.Bool;
+
 import javax.persistence.*;
 
 /**
@@ -35,11 +37,18 @@ public class CouponBean {
     @Column(name = "couponName", nullable = true, insertable = true, updatable = true)
     public String couponName;
 
+    /**
+     * 模板描述
+     */
+    @Basic
+    @Column(name = "couponContent", nullable = true, insertable = true, updatable = true)
+    public String couponContent;
+
     /**
      * 点击之后跳转的链接
      */
     @Basic
-    @Column(columnDefinition = "MEDIUMTEXT", name = "jumpUrl", nullable = true, insertable = true, updatable = true)
+    @Column(columnDefinition = "MEDIUMTEXT", name = "jumpUrl", nullable = false, insertable = true, updatable = true)
     public String jumpUrl;
 
     /**
@@ -61,6 +70,11 @@ public class CouponBean {
     @Column(name = "isLocal", nullable = true, insertable = true, updatable = true)
     public boolean isLocal;
 
+    /**是否被收藏*/
+    @Basic
+    @Column(name = "isFavorite", nullable = true, insertable = true, updatable = true)
+    public Boolean isFavorite = false;
+
     /**
      * 创建时间
      */

+ 46 - 5
src/main/java/com/miekir/shibei/controller/api/CouponController.java

@@ -8,6 +8,7 @@ import com.miekir.shibei.bean.ResultCode;
 import com.miekir.shibei.repository.CouponRepository;
 import com.miekir.shibei.repository.UserRepository;
 import com.miekir.shibei.tool.RequestTool;
+import com.miekir.shibei.tool.StringTool;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Sort;
@@ -116,22 +117,62 @@ public class CouponController {
      */
     @RequestMapping(value = "/api/getCouponList", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
     @ResponseBody
-    public String getCouponList(int pageNum, int pageSize) {
+    public String getCouponList(int couponType, int pageNum, int pageSize) {
         ResponseResult<List<CouponBean>> responseResult = new ResponseResult<List<CouponBean>>();
         responseResult.setMessage("获取失败");
 
         // 自带的分页查询
-        List<CouponBean> couponBeanList;
+        List<CouponBean> couponBeanList = null;
+        if (couponType == 0) {
+            // 查全部
+            try {
+                Sort sort = new Sort(Sort.Direction.DESC,"updateTimeMillis");
+                couponBeanList = couponRepository.findAll(new PageRequest(pageNum, pageSize, sort)).getContent();
+            } catch (Exception e) {
+                e.printStackTrace();
+                return JSON.toJSONString(responseResult);
+            }
+        } else {
+            couponBeanList = couponRepository.getCouponListByType(couponType, pageNum*pageSize, pageSize);
+        }
+
         try {
-            Sort sort = new Sort(Sort.Direction.DESC,"updateTimeMillis");
-            couponBeanList = couponRepository.findAll(new PageRequest(pageNum, pageSize, sort)).getContent();
+            responseResult.setContent(couponBeanList);
         } catch (Exception e) {
             e.printStackTrace();
             return JSON.toJSONString(responseResult);
         }
 
+        responseResult.setCode(ResultCode.SUCCESS);
+        responseResult.setMessage("获取成功");
+        return JSON.toJSONString(responseResult);
+    }
+
+    /**
+     * 根据关键字查询优惠
+     */
+    @RequestMapping(value = "/api/getCouponListByKeyword", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
+    @ResponseBody
+    public String getGoodsListByKeyword(String keywords, int pageNum, int pageSize) {
+        String keywordsUtf8 = StringTool.getUtf8String(keywords);
+
+        ResponseResult<List<CouponBean>> responseResult = new ResponseResult<List<CouponBean>>();
+        responseResult.setMessage("获取失败");
+
+        // 自带的分页查询
+        List<CouponBean> goodsBeanList;
         try {
-            responseResult.setContent(couponBeanList);
+            // 如果pageNum为1,pageSize为20,则查询第1*20条到第1*20+20-1(这里的第几条是从0开始的)
+            // 模糊查询,要拼上%%
+            keywordsUtf8 = "%"+keywordsUtf8+"%";
+            goodsBeanList = couponRepository.getCouponListByKeyword(keywordsUtf8, pageNum*pageSize, pageSize);
+        } catch (Exception e) {
+            e.printStackTrace();
+            return JSON.toJSONString(responseResult);
+        }
+
+        try {
+            responseResult.setContent(goodsBeanList);
         } catch (Exception e) {
             e.printStackTrace();
             return JSON.toJSONString(responseResult);

+ 10 - 0
src/main/java/com/miekir/shibei/repository/CouponRepository.java

@@ -8,6 +8,8 @@ import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.util.List;
+
 /**
  * Created by sjj on 2015/10/24 0024.
  */
@@ -22,6 +24,14 @@ public interface CouponRepository extends JpaRepository<CouponBean, Integer> {
     public CouponBean findCouponById(@Param("id") Long id);
 
 
+    // 使用原生SQL MYSQL 不支持TOP,要用连接符号|连接,否则会被当成字符串常量拼成%?1%
+    @Query(value="SELECT * FROM t_coupon where title like ?1 limit ?2, ?3", nativeQuery = true)
+    public List<CouponBean> getCouponListByKeyword(String keywords, int startNum, int pageSize);
+
+    // 使用原生SQL MYSQL 不支持TOP,要用连接符号|连接,否则会被当成字符串常量拼成%?1%
+    @Query(value="SELECT * FROM t_coupon where couponType = ?1 limit ?2, ?3", nativeQuery = true)
+    public List<CouponBean> getCouponListByType(int couponType, int startNum, int pageSize);
+
 
 //    @Transactional
 //    // 根据邮件查询用户所有密码记事