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