Browse Source

调整购物车显示逻辑

master
xyiege 3 years ago
parent
commit
6cc519c5ba
  1. 58
      src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java
  2. 30
      src/test/java/cn/chjyj/szwh/OtherTest.java

58
src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java

@ -671,39 +671,56 @@ public class GoodsServiceImpl implements GoodsService {
//shop car infomation //shop car infomation
String shopRedisKey = "car_" + userIsli; String shopRedisKey = "car_" + userIsli;
JSONObject carjson = new JSONObject(); JSONObject carjson = new JSONObject();
// 购物车列表
List carList = new ArrayList();
// car item
Map cimap = new HashMap();
// {"user_isli":"ssss",data:["sss","sss","sss"]}
//object //object
Object shopCar = RedisUtil.get(shopRedisKey); Object shopCar = RedisUtil.get(shopRedisKey);
// 购物车中有记录的
if (shopCar != null) { if (shopCar != null) {
JSONArray jsonArray = carjson.getJSONArray("data"); JSONArray jsonArray = carjson.getJSONArray("data");
// 检查goods_isli if (jsonArray == null) { //为空需要添新的元素
List goodsIsliList =new ArrayList(); cimap.put("goods_isli", goodsIsli);
cimap.put("use_years", userYears);
carList.add(cimap);
} else {
//抽取出goods_isli
List cgilist = new ArrayList();
for (int x = 0; x < jsonArray.size(); x++) { for (int x = 0; x < jsonArray.size(); x++) {
JSONObject tmpjson = (JSONObject) jsonArray.get(x); JSONObject tmpjson = (JSONObject) jsonArray.get(x);
String tgoodsisli = tmpjson.getString("goods_islid"); cgilist.add(tmpjson.getString("goods_isli"));
goodsIsliList.add(tgoodsisli);
} }
//检查是否在列表中 // goods_isli has been exist in car?
if(goodsIsliList.contains(goodsIsli)){ if(cgilist.contains(goodsIsli)){
retmap.put("code", 400); retmap.put("code", 400);
retmap.put("msg", "该标的已添加到购物车"); retmap.put("msg", "该标的已添加到购物车");
}else {
cimap.put("goods_isli", goodsIsli);
cimap.put("use_years", userYears);
carList.add(cimap);
}
} }
List tmpCarItem = new ArrayList(); // 添加到购物车json
Map tmpcar = new HashMap(); carjson.put("user_isli",userIsli);
tmpcar.put("goods_isli",goodsIsli); carjson.put("data",carList);
tmpcar.put("use_years",userYears);
tmpCarItem.add(tmpcar);
carjson.put("data",tmpCarItem);
RedisUtil.set(shopRedisKey, carjson.toJSONString()); RedisUtil.set(shopRedisKey, carjson.toJSONString());
} else { } else {
Map tmpCar = new HashMap(); //first biuld car item
tmpCar.put("goods_islicode",goodsIsli); cimap.put("user_isli",goodsIsli);
tmpCar.put("use_years",userYears); cimap.put("use_years",userYears);
List carItem = new ArrayList(); // second add to carlist
carItem.add(tmpCar); carList.add(cimap);
carjson.put("data",carItem); //third add new wraper
carjson.put("user_isli",userIsli);
carjson.put("data",carList);
RedisUtil.set(shopRedisKey, carjson.toJSONString()); RedisUtil.set(shopRedisKey, carjson.toJSONString());
} }
//购物车添加成功
retmap.put("code", 200);
retmap.put("msg", "成功");
return retmap; return retmap;
} }
@ -727,6 +744,7 @@ public class GoodsServiceImpl implements GoodsService {
AccountRatioSetting accountRatioSetting = acrlist.get(0); AccountRatioSetting accountRatioSetting = acrlist.get(0);
AccountRatioDetail accountRatioDetail = accountRatioDetailMapper.getAccRatioDetailByIdRole(accountRatioSetting.getId(), 2); AccountRatioDetail accountRatioDetail = accountRatioDetailMapper.getAccRatioDetailByIdRole(accountRatioSetting.getId(), 2);
if (jsonArray.size() > 0) {
//goods information //goods information
for (int x = 0; x < jsonArray.size(); x++) { for (int x = 0; x < jsonArray.size(); x++) {
JSONObject jsonObject = jsonArray.getJSONObject(x); JSONObject jsonObject = jsonArray.getJSONObject(x);
@ -775,7 +793,7 @@ public class GoodsServiceImpl implements GoodsService {
// //
retmap.put("goods", goods); retmap.put("goods", goods);
} }
}
return retmap; return retmap;
} }

30
src/test/java/cn/chjyj/szwh/OtherTest.java

@ -3,13 +3,16 @@ package cn.chjyj.szwh;
import cn.chjyj.szwh.utils.RedisKeys; import cn.chjyj.szwh.utils.RedisKeys;
import cn.chjyj.szwh.utils.RedisUtil; import cn.chjyj.szwh.utils.RedisUtil;
import cn.chjyj.szwh.utils.RedisUtils; import cn.chjyj.szwh.utils.RedisUtils;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@SpringBootTest @SpringBootTest
@ -45,4 +48,31 @@ public class OtherTest {
String ss =RedisKeys.getSysConfigKey("ss"); String ss =RedisKeys.getSysConfigKey("ss");
System.out.println(ss); System.out.println(ss);
} }
@Test
public void carTest(){
String user ="899998-000000001640-3";
String goodsIsli="010007-00000000348099999999-3";
List carList =new ArrayList();
carList.add(goodsIsli);
carList.add("010007-00000000348099999999-6");
//
Map itmap =new HashMap();
itmap.put("user_isli",user);
itmap.put("data",carList);
//
JSONObject oujson =new JSONObject(itmap);
System.out.println(oujson.toJSONString());
}
@Test
public void cartest2(){
String orstr ="{\"data\":[\"010007-00000000348099999999-3\",\"010007-00000000348099999999-6\"],\"user_isli\":\"899998-000000001640-3\"}\n";
JSONObject orjson = JSONObject.parseObject(orstr);
JSONArray jsonArray = orjson.getJSONArray("data");
//jsonArray.toJavaList(String.class);
for(int a=0;a<jsonArray.size();a++){
System.out.println(jsonArray.get(a));
}
}
} }

Loading…
Cancel
Save