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

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、所有文章未经授权禁止转载、摘编、复制或建立镜像,如有违反,追究法律责任。
2、本站文章部分来源注册用户发布或互联网收集而来,若有侵权,请邮件联系作者。
邮箱地址:wtao219@qq.com
THE END
分享
二维码
< <上一篇
下一篇>>