From 66962af2d5c9c97bf9f411f35b6a0941b1252910 Mon Sep 17 00:00:00 2001 From: xyiege Date: Thu, 3 Nov 2022 12:16:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=AE=80=E5=88=A0=E9=99=A4=E8=B4=AD?= =?UTF-8?q?=E7=89=A9=E8=BD=A6=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../szwh/controller/api/GoodsController.java | 15 ++++--- .../cn/chjyj/szwh/service/GoodsService.java | 2 +- .../szwh/service/impl/GoodsServiceImpl.java | 42 +++++++---------- .../java/cn/chjyj/szwh/utils/SzCarUtils.java | 45 ++++++++++++++++++- 4 files changed, 68 insertions(+), 36 deletions(-) diff --git a/src/main/java/cn/chjyj/szwh/controller/api/GoodsController.java b/src/main/java/cn/chjyj/szwh/controller/api/GoodsController.java index 28acc0e..f8f676c 100644 --- a/src/main/java/cn/chjyj/szwh/controller/api/GoodsController.java +++ b/src/main/java/cn/chjyj/szwh/controller/api/GoodsController.java @@ -95,20 +95,21 @@ public class GoodsController extends BaseController { /** * delelete user's shop car from redis - * + * @param reqstr Parse the json data from POST method * @return */ @RequestMapping("/delShoppingCar") - public JSONObject delShoppingCar(HttpServletRequest request) { + public JSONObject delShoppingCar(@RequestBody String reqstr) { long start = System.currentTimeMillis(); - String userIsli = request.getParameter("user_isli"); - String goodsIsli = request.getParameter("goods_isli"); - goodsService.delShoppingCar(userIsli, goodsIsli); + JSONObject request = JSONObject.parseObject(reqstr); + String userIsli = request.getString("user_isli"); + String goodsIsli = request.getString("goods_isli"); + Map retmap = goodsService.delShoppingCar(userIsli, goodsIsli); long end = System.currentTimeMillis(); long howuse = (end - start) / 1000l; - jsonObject.put("code", 200); - jsonObject.put("msg", "成功"); + jsonObject.put("code", retmap.get("code")); + jsonObject.put("msg", retmap.get("msg")); jsonObject.put("usetime", howuse); return jsonObject; diff --git a/src/main/java/cn/chjyj/szwh/service/GoodsService.java b/src/main/java/cn/chjyj/szwh/service/GoodsService.java index 76a7e53..d4aaca8 100644 --- a/src/main/java/cn/chjyj/szwh/service/GoodsService.java +++ b/src/main/java/cn/chjyj/szwh/service/GoodsService.java @@ -134,5 +134,5 @@ public interface GoodsService { * @param userIsli * @param goodsIsli */ - void delShoppingCar(String userIsli,String goodsIsli); + Map delShoppingCar(String userIsli,String goodsIsli); } diff --git a/src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java b/src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java index 8fbdb52..1fee681 100644 --- a/src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java +++ b/src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java @@ -766,34 +766,24 @@ public class GoodsServiceImpl implements GoodsService { return retlist; } + /** + * 从购物车中移除一个元素 + * @param userIsli + * @param goodsIsli + * @return + */ @Override - public void delShoppingCar(String userIsli, String goodsIsli) { - String shopRedisKey = "car_" + userIsli; - Object shopCar = RedisUtil.get(shopRedisKey); - if (shopCar != null) { - JSONObject shopCarJson = JSONObject.parseObject(shopCar.toString()); - JSONArray jsonArray = shopCarJson.getJSONArray("data"); - // - JSONObject carjson = new JSONObject(); - List carlist = new ArrayList(); - // - if (jsonArray.size() > 0) { - for (int x = 0; x < jsonArray.size(); x++) { - JSONObject tmpJson = (JSONObject) jsonArray.get(x); - String tmpgoodsisli = tmpJson.getString("goods_isli"); - // not equal save into list - if (!goodsIsli.equals(tmpgoodsisli)) { - carlist.add(jsonArray.get(x)); - } - } - // save into new car - carjson.put("data", carlist); - RedisUtil.set(shopRedisKey, carjson); - } else { - RedisUtil.del(shopRedisKey); - } + public Map delShoppingCar(String userIsli, String goodsIsli) { + Map retmap =new HashMap(); + JSONObject retjson =SzCarUtils.delCart(userIsli,goodsIsli); + if(retjson==retjson){ + retmap.put("code",400); + retmap.put("msg","购物车中没有此标的"); + return retmap; } - + retmap.put("code",200); + retmap.put("msg","成功"); + return retmap; } /** diff --git a/src/main/java/cn/chjyj/szwh/utils/SzCarUtils.java b/src/main/java/cn/chjyj/szwh/utils/SzCarUtils.java index 1e7413a..70cf45a 100644 --- a/src/main/java/cn/chjyj/szwh/utils/SzCarUtils.java +++ b/src/main/java/cn/chjyj/szwh/utils/SzCarUtils.java @@ -23,12 +23,12 @@ public class SzCarUtils { // get shopping car from redis Object shopCar = RedisUtil.get(carkey); // 对象存在 - if(shopCar!=null) { + if (shopCar != null) { JSONObject shopCarJson = JSONObject.parseObject(shopCar.toString()); JSONArray jsonArray = shopCarJson.getJSONArray("data"); if (jsonArray != null) { for (int x = 0; x < jsonArray.size(); x++) { - carItem.add((JSONObject)jsonArray.get(x)); + carItem.add((JSONObject) jsonArray.get(x)); } } } @@ -41,4 +41,45 @@ public class SzCarUtils { RedisUtil.set(carkey, outjson.toJSONString()); return outjson; } + + /** + * 删除购物中的商品 + * + * @param userIsli redis键名,用户编号 + * @param goodsIsli 删除的商品编号 + * @return + */ + public static JSONObject delCart(String userIsli, String goodsIsli) { + // get shopping car from redis + String carkey = "car_" + userIsli; + Object shopCar = RedisUtil.get(carkey); + if (shopCar == null) { + return null; + } + List ncarlist = new ArrayList(); + //find jsonarray + JSONObject shopCarJson = JSONObject.parseObject(shopCar.toString()); + JSONArray jsonArray = shopCarJson.getJSONArray("data"); + if (jsonArray == null) { + return null; + } + // loop and check + for (int x = 0; x < jsonArray.size(); x++) { + JSONObject tmpCarItem = (JSONObject) jsonArray.get(x); + String tgisli = tmpCarItem.getString("goods_isli"); + if (!tgisli.equals(goodsIsli)) { + ncarlist.add(tmpCarItem); + } + } + // 返回结果 + JSONObject outjson = new JSONObject(); + outjson.put("user_isli", userIsli); + outjson.put("data", ncarlist); + //1 del original car + RedisUtil.del(carkey); + // 2 add new + RedisUtil.set(carkey, outjson.toJSONString()); + // 3 retun result + return outjson; + } }