一,商品的浏览
(1)商品详情页
通过前台传过来的商品id,去数据库查找对应的商品,如果存在返回这个商品的所有信息,如果不存在返回此商品不存在错误信息;
@RequestMapping({"/api/goods/detail.do"}) public void goods(HttpServletRequest request, HttpServletResponse response, Long id) { try { Goods obj = this.goodsService.getObjById(id); if (obj.getGoods_status() == 0) {// 商品上架状态 //店铺是否正常 if (obj.getGoods_store().getStore_status() == 2) {// 店铺状态正常 //使用的SKU,这里需要去查看商品的SKU List<InventoryDetail> inventoryDetailList = (List<InventoryDetail>) GsonUtil.jsonToList(obj.getGoods_inventory_detail(),InventoryDetail[].class); Set<GoodsSpecProperty> set = new HashSet<>(); Set<String> idSet = new HashSet<>(); for(InventoryDetail inventoryDetail : inventoryDetailList){ String[] ids = inventoryDetail.getId().split("_"); for(String idStr: ids){ idSet.add(idStr); } } List<GoodsSpecProperty> specs = obj.getGoods_specs(); if (specs != null) { for (GoodsSpecProperty spec : specs) { if(idSet.contains(spec.getId())) { spec.setSpec_id(spec.getSpec().getId()); spec.setSpec_name(spec.getSpec().getName()); set.add(spec); } } } obj.setGoods_specs(new ArrayList<GoodsSpecProperty>(set)); returnData(response, getObject(obj)); } else {// 店铺状态异常 returnErrorData(request, response, "message.buyer.store_not_open", StatusCode.BUYER_STORE_NOT_OPEN); } } catch (Exception e) { e.printStackTrace(); try { returnErrorData(request, response, "message.general.error", StatusCode.GENERAL_ERROR); } catch (IOException e1) { e1.printStackTrace(); } } }
//这里提到了商品的SKU那么当我们去选择商品的SKU时,商品会有价格的变化,这点需要写一个接口。
下面的流程就是根据查到的商品信息,获取前台传过来的SKU值,数量,价钱。计算出总价返回给前台;
/** * 根据商品规格查询商品规格价格 * * @param request * @param response * @param gsp 规格 * @param id */ @RequestMapping({"/api/goods/load_goods_gsp.do"}) public void load_goods_gsp(HttpServletRequest request, HttpServletResponse response, String gsp, String id) { try { Goods goods = this.goodsService.getObjById(CommUtil.null2Long(id)); int count = 0; double price = 0.0D; if ((goods.getGroup() != null) && (goods.getGroup_buy() == 2)) { for (GroupGoods gg : goods.getGroup_goods_list()) if (gg.getGroup().getId().equals(goods.getGroup().getId())) { count = gg.getGg_group_count() - gg.getGg_def_count(); price = CommUtil.null2Double(gg.getGg_price()); } } else { count = goods.getGoods_inventory(); price = CommUtil.null2Double(goods.getStore_price()); if (goods.getInventory_type().equals("spec")) { List<Map> list = (List) Json.fromJson(ArrayList.class, goods.getGoods_inventory_detail()); String[] gsp_ids = gsp.split(","); for (Map temp : list) { String[] temp_ids = CommUtil.null2String(temp.get("id")).split("_"); Arrays.sort(gsp_ids); Arrays.sort(temp_ids); if (Arrays.equals(gsp_ids, temp_ids)) { count = CommUtil.null2Int(temp.get("count")); price = CommUtil.null2Double(temp.get("price")); } } } } LoadgoodsGspBean bean = new LoadgoodsGspBean(); bean.setCount(count); bean.setPrice(price); returnData(response, getObject(bean)); } catch (Exception e) { e.printStackTrace(); try { returnErrorData(request, response, "message.general.error", StatusCode.GENERAL_ERROR); } catch (IOException e1) { e1.printStackTrace(); } } }
下图就是Goods 类中的一部分,仅供参考!