1. HTTP3W博客首页
  2. 建站
  3. 源代码

购物车中含有不同商家的商品,全部结算生成对应订单问题

public int addOrder(Order order) throws Exception {  //实际上order只含有userId
		DbUtil.getConn();
		User user = new User();
		user.setUserId(order.getUserId());
		ResultSet res = cartDao.findCartByUserId(user);  //查询该用户购物车
		List<Cart> cartList = new ArrayList<>();
		while(res.next()){
			Cart cart = new Cart();
			cart.setGoodsId(res.getInt(1));
			cart.setGoodsCode(res.getString(2));
			cart.setGoodsPic(res.getString(3));
			cart.setGoodsName(res.getString(4));
			cart.setGoodsPrice(res.getFloat(5));
			cart.setGoodsNum(res.getInt(6));
			cart.setCartId(res.getInt(7));
			cart.setUserId(res.getInt(8));  //用来区分订单的关键参数sell_id (商家id)  
			cartList.add(cart);
		}
		List<List<Cart>> list = new ArrayList<>();
		for(int i = 0; i < cartList.size(); i++){   //对购物车list进行遍历
			
			List<Cart> childList = new ArrayList<>();  
			if(cartList.get(i).getCartSize() != -1){
				cartList.get(i).setCartSize(-1);  //访问过该商品  做标记
				int sell_id = cartList.get(i).getUserId();  
				for(int j = i+1; j < cartList.size(); j++){  对当前访问商品之后商品进行对比
					if(sell_id == cartList.get(j).getUserId()){
						childList.add(cartList.get(j));  //相同sell_id的商品归类
						cartList.get(j).setCartSize(-1);  //访问过做标记
					}
				}
				childList.add(cartList.get(i));  //对添加最初的商品
				list.add(childList);  
			}
		}
		int count = 0;
		for(int i = 0; i < list.size(); i++){  //对已经按商家分类的list进行遍历
			String str = "0123456789";
			Random ran = new Random();
			StringBuffer sb = new StringBuffer();
			for(int j = 0; j < 16; j++){
				sb.append(str.charAt(ran.nextInt(str.length())));
			}
			String orderCode = sb.toString();  //随机生成编码
			Order newOrder = new Order();  
			
			newOrder.setOrderCode(orderCode);
			newOrder.setUserId(order.getUserId());
			newOrder.setOrderTime(new Date());
			newOrder.setOrderStatus(1);
			newOrder.setSell_id(list.get(i).get(0).getUserId());
			orderDao.addOrder(newOrder);
			for(int j = 0; j < list.get(i).size(); j++){  //对子列表商品进行遍历
				
				Order childOrder = new Order();
				childOrder.setOrderCode(orderCode);
				childOrder.setGoods_num(list.get(i).get(j).getGoodsNum());
				childOrder.setGoods_price(list.get(i).get(j).getGoodsPrice());
				if(addOrderList(childOrder, list.get(i).get(j).getGoodsId()) == 1){  //将子列表商品添加到同一个订单列表中
					count ++;
				}
			}
		}
		return count;
	}

1、原创文章,作者:诺米,如若转载,请注明出处:https://www.http3w.com/archives/603

2、本站内容若有雷同从属巧合,若侵犯了您的权益,请联系本站删除,E-mail: wtao219@qq.com

发表评论

您的电子邮箱地址不会被公开。

联系我们

254007489

在线咨询:点击这里给我发消息

邮件:wtao219@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息