詹子聪 5 лет назад
Родитель
Сommit
7a11da9185

+ 21 - 0
src/main/java/com/miekir/shibei/bean/FavBean.java

@@ -0,0 +1,21 @@
+package com.miekir.shibei.bean;
+
+import javax.persistence.*;
+
+/**
+ * 收藏
+ */
+@Entity
+@Table(name = "t_fav", schema = "shibei", catalog = "")
+public class FavBean {
+    @Id
+    @GeneratedValue(strategy= GenerationType.AUTO)
+    @Column(name = "id", nullable = false, insertable = true, updatable = false)
+    public Long id;
+
+    @Column(name = "email", nullable = false, insertable = true, updatable = false)
+    public String email;
+
+    @Column(name = "couponId", nullable = false, insertable = true, updatable = false, columnDefinition = "bigint default 0")
+    public Long couponId = 0L;
+}

+ 30 - 2
src/main/java/com/miekir/shibei/controller/api/CouponController.java

@@ -6,6 +6,7 @@ import com.miekir.shibei.bean.GoodsBean;
 import com.miekir.shibei.bean.ResponseResult;
 import com.miekir.shibei.bean.ResultCode;
 import com.miekir.shibei.repository.CouponRepository;
+import com.miekir.shibei.repository.FavRepository;
 import com.miekir.shibei.repository.UserRepository;
 import com.miekir.shibei.tool.RequestTool;
 import com.miekir.shibei.tool.StringTool;
@@ -27,6 +28,9 @@ public class CouponController {
     @Autowired
     private UserRepository userRepository;
 
+    @Autowired
+    private FavRepository favRepository;
+
     /**
      * 新增和更新优惠券
      */
@@ -117,7 +121,7 @@ public class CouponController {
      */
     @RequestMapping(value = "/api/getCouponList", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
     @ResponseBody
-    public String getCouponList(int couponType, int pageNum, int pageSize) {
+    public String getCouponList(@RequestHeader HttpHeaders header, int couponType, int pageNum, int pageSize) {
         ResponseResult<List<CouponBean>> responseResult = new ResponseResult<List<CouponBean>>();
         responseResult.setMessage("获取失败");
 
@@ -137,6 +141,18 @@ public class CouponController {
         }
 
         try {
+            if (couponBeanList != null) {
+                String email = header.getFirst("email");
+                List<Long> favBeanList = favRepository.findFavListByEmail(email);
+                if (favBeanList != null && favBeanList.size() > 0) {
+                    // 当前商品是否已收藏
+                    for (CouponBean goodsBean : couponBeanList) {
+                        if (favBeanList.contains(goodsBean.id)) {
+                            goodsBean.isFavorite = true;
+                        }
+                    }
+                }
+            }
             responseResult.setContent(couponBeanList);
         } catch (Exception e) {
             e.printStackTrace();
@@ -153,7 +169,7 @@ public class CouponController {
      */
     @RequestMapping(value = "/api/getCouponListByKeyword", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
     @ResponseBody
-    public String getGoodsListByKeyword(String keywords, int pageNum, int pageSize) {
+    public String getGoodsListByKeyword(@RequestHeader HttpHeaders header, String keywords, int pageNum, int pageSize) {
         String keywordsUtf8 = StringTool.getUtf8String(keywords);
 
         ResponseResult<List<CouponBean>> responseResult = new ResponseResult<List<CouponBean>>();
@@ -172,6 +188,18 @@ public class CouponController {
         }
 
         try {
+            if (goodsBeanList != null) {
+                String email = header.getFirst("email");
+                List<Long> favBeanList = favRepository.findFavListByEmail(email);
+                if (favBeanList != null && favBeanList.size() > 0) {
+                    // 当前商品是否已收藏
+                    for (CouponBean goodsBean : goodsBeanList) {
+                        if (favBeanList.contains(goodsBean.id)) {
+                            goodsBean.isFavorite = true;
+                        }
+                    }
+                }
+            }
             responseResult.setContent(goodsBeanList);
         } catch (Exception e) {
             e.printStackTrace();

+ 101 - 0
src/main/java/com/miekir/shibei/controller/api/FavController.java

@@ -0,0 +1,101 @@
+package com.miekir.shibei.controller.api;
+
+import com.alibaba.fastjson.JSON;
+import com.miekir.shibei.bean.CouponBean;
+import com.miekir.shibei.bean.FavBean;
+import com.miekir.shibei.bean.ResponseResult;
+import com.miekir.shibei.bean.ResultCode;
+import com.miekir.shibei.repository.*;
+import com.miekir.shibei.tool.RequestTool;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import java.util.List;
+
+@Controller
+public class FavController {
+    @Autowired
+    private UserRepository userRepository;
+
+    @Autowired
+    private FavRepository favRepository;
+
+    @Autowired
+    private CouponRepository goodsRepository;
+
+    @Autowired
+    private SystemRepository systemRepository;
+
+
+    @RequestMapping(value = "/api/favCoupon", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
+    @ResponseBody
+    public String favGoods(@RequestHeader HttpHeaders header, Long couponId) {
+        ResponseResult<String> responseResult = new ResponseResult<String>();
+        responseResult.setMessage("操作失败");
+
+        if (!RequestTool.isRequestValid(header, userRepository)) {
+            responseResult.setMessage("登录过期或没有权限");
+            return JSON.toJSONString(responseResult);
+        }
+
+        String email = header.getFirst("email");
+        FavBean favBean = favRepository.findFavByEmailAndGoodsId(email, couponId);
+        CouponBean goodsBean = goodsRepository.findCouponById(couponId);
+        if (goodsBean == null) {
+            return JSON.toJSONString(responseResult);
+        }
+
+        if (favBean == null) {
+            favBean = new FavBean();
+            favBean.email = email;
+            favBean.couponId = couponId;
+            favRepository.save(favBean);
+            responseResult.setContent("收藏成功");
+        } else {
+            favRepository.delete(favBean);
+            responseResult.setContent("取消收藏成功");
+        }
+
+        goodsRepository.save(goodsBean);
+
+        responseResult.setCode(ResultCode.SUCCESS);
+        return JSON.toJSONString(responseResult);
+    }
+
+
+
+    /**
+     * 查询京东商品
+     */
+    @RequestMapping(value = "/api/getMyFavCoupon", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
+    @ResponseBody
+    public String getMyFavGoods(@RequestHeader HttpHeaders header, int pageNum, int pageSize) {
+        ResponseResult<List<CouponBean>> responseResult = new ResponseResult<List<CouponBean>>();
+        responseResult.setMessage("获取失败");
+
+        if (!RequestTool.isRequestValid(header, userRepository)) {
+            responseResult.setMessage("请重新登录");
+            return JSON.toJSONString(responseResult);
+        }
+        String email = header.getFirst("email");
+        // 保持原生顺序。使用jpa的findAll总是会自动排序
+        List<CouponBean> goodsBeanList = goodsRepository.findCouponByEmail(email, pageNum*pageSize, pageSize);
+        List<Long> favBeanList = favRepository.findFavListByEmail(email);
+        // 当前商品是否已收藏
+        for (CouponBean goodsBean : goodsBeanList) {
+            if (favBeanList.contains(goodsBean.id)) {
+                goodsBean.isFavorite = true;
+            }
+        }
+        responseResult.setContent(goodsBeanList);
+
+        responseResult.setCode(ResultCode.SUCCESS);
+        responseResult.setMessage("获取成功");
+        return JSON.toJSONString(responseResult);
+    }
+}

+ 4 - 17
src/main/java/com/miekir/shibei/repository/CouponRepository.java

@@ -32,21 +32,8 @@ public interface CouponRepository extends JpaRepository<CouponBean, Integer> {
     @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
-//    // 根据邮件查询用户所有密码记事
-//    // @Param注解用于提取参数
-//    @Query(value="select note from NoteBean note where note.userId=:email order by note.updateTime desc")
-//    public List<NoteBean> findNotesByEmail(@Param("email") String email);
-//
-
-
-
-//    // 修改密码记事
-//    @Modifying
-//    // 说明该方法是事务性操作
-//    @Transactional
-//    // @Param注解用于提取参数
-//    @Query("update NoteBean note set note.content=:content, note.updateTime=:updateTime where note.id=:noteId")
-//    public int updateNoteBean(@Param("content") String content, @Param("noteId") Integer noteId, @Param("updateTime") Long updateTime);
+    // 这个版本的MySQL不行
+    //@Query(value="SELECT * FROM t_goods where id IN(SELECT goodsId FROM t_fav where email = ?1 ORDER BY id DESC limit ?2, ?3)", nativeQuery = true)
+    @Query(value="SELECT * FROM t_coupon where id IN (SELECT t.couponId from (SELECT * FROM t_fav where email = ?1 ORDER BY id DESC limit ?2, ?3) as t)", nativeQuery = true)
+    List<CouponBean> findCouponByEmail(String email, int startNum, int pageSize);
 }

+ 48 - 0
src/main/java/com/miekir/shibei/repository/FavRepository.java

@@ -0,0 +1,48 @@
+package com.miekir.shibei.repository;
+
+import com.miekir.shibei.bean.FavBean;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.math.BigInteger;
+import java.util.List;
+
+/**
+ * Created by sjj on 2015/10/24 0024.
+ */
+// 添加注解
+@Repository
+public interface FavRepository extends JpaRepository<FavBean, Long> {
+    // 查询某个人的收藏
+    @Transactional
+    @Query(value="select favBean.couponId from FavBean favBean where favBean.email=:email")
+    public List<Long> findFavListByEmail(@Param("email") String email);
+
+    @Transactional
+    @Query(value="select favBean from FavBean favBean where favBean.email=:email and favBean.couponId=:couponId")
+    public FavBean findFavByEmailAndGoodsId(@Param("email") String email, @Param("couponId") Long couponId);
+
+    // 查询某件商品被收藏的记录
+    @Transactional
+    @Query(value="select favBean from FavBean favBean where favBean.couponId=:couponId")
+    public List<FavBean> findFavListByGoodsId(@Param("couponId") Long couponId);
+
+    // 使用原生SQL MYSQL 不支持TOP,要用连接符号|连接,否则会被当成字符串常量拼成%?1%
+    @Query(value="SELECT couponId FROM t_fav where email = ?1 ORDER BY id DESC limit ?2, ?3", nativeQuery = true)
+    public List<BigInteger> getFavList(String email, int startNum, int pageSize);
+
+    // 这种方式删除,id会从0开始。JPA要求,没有事务支持,不能执行更新和删除操作,但是如果有外键关联,则无法执行
+    @Transactional
+    @Modifying
+    @Query(value = "truncate table t_fav", nativeQuery = true)
+    void truncateTable();
+
+    @Modifying
+    @Transactional
+    @Query(value = "ALTER TABLE t_fav AUTO_INCREMENT=1", nativeQuery = true)
+    void resetId();
+}