コード例 #1
0
  /**
   * @Description: (获取确认订单页面的货品和分店数据)
   *
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  @RequestMapping("/findOrderInfo")
  @ResponseBody
  public Object findOrderInfo(String data, HttpServletRequest request) throws Exception {
    log.info("start[CartController.findOrderInfo]");

    // 获取请求参数
    JSONObject jsonData = JSONObject.fromObject(data);
    MemberListDo memberListDo =
        APICommonUtil.getMemberListDo((String) request.getAttribute("sessionid"));
    Integer accountId = memberListDo.getAccountId();

    ///////// 测试代码 start //////
    //		Integer  accountId=152;
    //		jsonData = new JSONObject();
    //		jsonData.put("proIdListString", "[399,445,438]");
    //		jsonData.put("quantityListString", "[3,2,4]");
    ///////// 测试代码 end //////
    String proIdListString = (String) CommonUtil.getJsonValue(jsonData, "proIdListString");
    String quantityListString = (String) CommonUtil.getJsonValue(jsonData, "quantityListString");

    // 检查参数
    if (StringUtils.isEmpty(proIdListString)) {
      return processExpction("货品id不能为空!");
    }
    if (StringUtils.isEmpty(quantityListString)) {
      return processExpction("数量不能为空!");
    }

    PamAccount pamAccount = findAccountId(accountId);
    if (pamAccount != null && !"100".equals(pamAccount.getAccountType())) {
      return new ResultObject(
          new HeadObject(ErrorCode.FAILURE), JSONObject.fromObject("{result:false,isBuyer:false}"));
    }
    if (pamAccount != null) {
      accountId = pamAccount.getAccountId();
    }
    ResultObject resultObject = null;
    HeadObject headObject;
    JSONObject jsonObject = new JSONObject();

    if (accountId != null && accountId != 0) {
      List<Integer> proIdList =
          (List<Integer>)
              JSONArray.toCollection(JSONArray.fromObject(proIdListString), Integer.class);
      if (proIdList != null && proIdList.size() >= 1) {
        // 查询member对象
        Member member = this.selectMember(accountId);
        // 根据proIdList查询购物车对象
        List<CartListDTO> cartList = null;
        List<Integer> quantityList =
            (List<Integer>)
                JSONArray.toCollection(JSONArray.fromObject(quantityListString), Integer.class);
        if (quantityList.size() == proIdList.size()) {
          headObject = CommonHeadUtil.geneHeadObject("cartService.findCartListByProIdList");
          resultObject =
              (ResultObject)
                  orderService.doServiceByServer(
                      new RequestObject(headObject, JSONArray.fromObject(proIdList)));
          cartList =
              (List<CartListDTO>)
                  JSONArray.toCollection(
                      JSONArray.fromObject(resultObject.getContent()), CartListDTO.class);
          if (cartList != null && cartList.size() > 0) {
            for (int j = cartList.size() - 1; j >= 0; j--) {
              if ("1".equals(cartList.get(j).getLimitGoodsdown())
                  || "1".equals(cartList.get(j).getLimitStore())
                  || "1".equals(cartList.get(j).getLimitStoredown())
                  || "1".equals(cartList.get(j).getShopStatus())
                  || "1".equals(cartList.get(j).getStatus())
                  || "1".equals(cartList.get(j).getDisabled())) {
                return new ResultObject(
                    new HeadObject(ErrorCode.FAILURE),
                    JSONObject.fromObject("{ msg:\"很抱歉,该订单中存在失效货品\"}"));
              }
              for (int i = 0; i < proIdList.size(); i++) {
                if (proIdList.get(i).equals(cartList.get(j).getProductId())) {
                  if (quantityList.get(i) > cartList.get(j).getStore()) {
                    return new ResultObject(
                        new HeadObject(ErrorCode.FAILURE),
                        JSONObject.fromObject("{ msg:\"很抱歉,该订单中存在货品库存不足\"}"));
                  } else {
                    cartList.get(j).setQuantity(quantityList.get(i));
                  }
                }
              }
            }
          }
        }
        if (!(cartList != null && cartList.size() >= 1)) {
          headObject = CommonHeadUtil.geneHeadObject("cartService.findCartListByProIdAndMem");
          jsonObject.put("memberId", member.getMemberId());
          jsonObject.put("proIdList", proIdList);
          resultObject =
              (ResultObject)
                  orderService.doServiceByServer(new RequestObject(headObject, jsonObject));
          cartList =
              (List<CartListDTO>)
                  JSONArray.toCollection(
                      JSONArray.fromObject(resultObject.getContent()), CartListDTO.class);
        }
        List<Store> storeList = null;
        if (cartList != null && cartList.size() >= 1) {
          // 查询店铺
          List<Integer> storeIdList = new ArrayList<Integer>();
          for (int i = 0; i < cartList.size(); i++) {
            storeIdList.add(cartList.get(i).getStoreId());
          }
          headObject = CommonHeadUtil.geneHeadObject("storeService.selectByStoreIdList3");
          resultObject =
              (ResultObject)
                  memberService.doServiceByServer(new RequestObject(headObject, storeIdList));

          if (resultObject.getContent() != null) {
            storeList =
                com.alibaba.fastjson.JSONArray.parseArray(
                    resultObject.getContent().toString(), Store.class);
          }
          if (storeList != null) {
            for (int i = 0; i < storeList.size(); i++) {
              if ("1".equals(storeList.get(i).getLimitGoodsdown())
                  || "1".equals(storeList.get(i).getLimitStore())
                  || "1".equals(storeList.get(i).getLimitStoredown())
                  || "1".equals(storeList.get(i).getShopstatus())
                  || "1".equals(storeList.get(i).getStatus())
                  || "1".equals(storeList.get(i).getDisabled())) {
                return new ResultObject(
                    new HeadObject(ErrorCode.FAILURE),
                    JSONObject.fromObject("{isLogin:true,msg:\"很抱歉,该订单中存在失效货品\"}"));
              }
            }
          }

          // 查询货品图片
          this.findProImage(cartList);
          for (int j = cartList.size() - 1; j >= 0; j--) {
            if (cartList.get(j).getQuantity() <= 0
                || "0".equals(cartList.get(j).getgMarketable())
                || "1".equals(cartList.get(j).getgDisabled())
                || "0".equals(cartList.get(j).getpMarketable())
                || "1".equals(cartList.get(j).getpDisabled())) {
              return new ResultObject(
                  new HeadObject(ErrorCode.FAILURE),
                  JSONObject.fromObject("{isLogin:true,msg:\"很抱歉,该订单中存在失效货品\"}"));
            }
          }
        }

        Double sumPrice = this.totalPrice(cartList);

        com.alibaba.fastjson.JSONObject returnJson = new com.alibaba.fastjson.JSONObject();
        DecimalFormat df2 = new DecimalFormat("#.00"); // 四舍五入保留两位小数
        returnJson.put("sumPrice", df2.format(Double.parseDouble(sumPrice.toString())));
        // 按商店号分组
        List<List<CartListDTO>> list = new ArrayList();
        Map<Integer, List<CartListDTO>> map = new HashMap<Integer, List<CartListDTO>>();

        // 图片地址转成httpURL
        String imgUrl = Configuration.getInstance().getConfigValue("images.url");
        if (cartList != null && !cartList.isEmpty()) {
          for (CartListDTO cto : cartList) {
            cto.setGoodsImage(imgUrl + cto.getGoodsImage());

            Integer storeId = cto.getStoreId();
            List<CartListDTO> cList = map.get(storeId);
            if (cList == null) {
              cList = new ArrayList<CartListDTO>();
              map.put(storeId, cList);
            }
            cList.add(cto);
          }
          for (List<CartListDTO> cList : map.values()) {
            list.add(cList);
          }
        }
        returnJson.put("content", list);
        returnJson.put("storeList", storeList);
        returnJson.put("isLogin", true);
        resultObject = new ResultObject(new HeadObject(ErrorCode.SUCCESS), returnJson);
      } else {
        resultObject =
            new ResultObject(
                new HeadObject(ErrorCode.FAILURE), JSONObject.fromObject("{empty:true}"));
      }
    } else {
      resultObject =
          new ResultObject(new HeadObject(ErrorCode.FAILURE), JSONObject.fromObject("{}"));
    }

    log.info("end[CartController.findOrderInfo]");
    return resultObject;
  }
コード例 #2
0
  /**
   * 获取购物车列表
   *
   * @param data
   * @param request
   * @return
   */
  @RequestMapping("/cartList")
  @ResponseBody
  public Object cartList(String data, HttpServletRequest request) {
    log.info("###########cartList-->start");
    log.info("----------------------data:" + data + "-------------------------");
    try {
      // 获取请求参数
      JSONObject jsonData = JSONObject.fromObject(data);
      MemberListDo memberListDo =
          APICommonUtil.getMemberListDo((String) request.getAttribute("sessionid"));
      Integer accountId = memberListDo.getAccountId();
      //				Integer accountId = 152;
      HeadObject headObject = CommonHeadUtil.geneHeadObject("accountService.selectByAccountId");
      ResultObject resultObject =
          (ResultObject) memberService.doServiceByServer(new RequestObject(headObject, accountId));
      PamAccount pamAccount = null;
      if (resultObject != null && resultObject.getContent() != null) {
        pamAccount = (PamAccount) resultObject.getContent();
      }

      if (pamAccount != null && !"100".equals(pamAccount.getAccountType())) {
        return new ResultObject(
            new HeadObject(ErrorCode.FAILURE),
            JSONObject.fromObject("{result:false,isBuyer:false}"));
      }
      if (pamAccount != null) {
        accountId = pamAccount.getAccountId();
      }
      Member member = null;
      if (accountId != null && accountId != 0) {
        member = this.selectMember(accountId);
      }
      List<CartListDTO> cartList = null;
      if (member != null && member.getMemberId() != 0) {
        // 用户已登录
        // 查询数据库中的购物车商品
        headObject = CommonHeadUtil.geneHeadObject("cartService.findCartListByAccountId");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("accountId", accountId);
        resultObject =
            (ResultObject)
                orderService.doServiceByServer(new RequestObject(headObject, jsonObject));
        cartList = (List<CartListDTO>) resultObject.getContent();
      }

      // 查询货品图片
      cartList = this.findProImage(cartList);

      // 查询分店
      cartList = this.findStoresByCartList(cartList);

      // 按商店号分组
      List<List<CartListDTO>> list = new ArrayList();
      Map<Integer, List<CartListDTO>> map = new HashMap<Integer, List<CartListDTO>>();

      // 图片地址转成httpURL
      String imgUrl = Configuration.getInstance().getConfigValue("images.url");
      if (cartList != null && !cartList.isEmpty()) {
        for (CartListDTO cto : cartList) {
          cto.setGoodsImage(imgUrl + cto.getGoodsImage());

          Integer storeId = cto.getStoreId();
          List<CartListDTO> cList = map.get(storeId);
          if (cList == null) {
            cList = new ArrayList<CartListDTO>();
            map.put(storeId, cList);
          }
          cList.add(cto);
        }
        for (List<CartListDTO> cList : map.values()) {
          list.add(cList);
        }
      }

      JSONObject jsonObject = new JSONObject();
      jsonObject.put("cartList", list);
      resultObject = new ResultObject(new HeadObject(ErrorCode.SUCCESS), jsonObject);
      log.info("###########cartList-->end");
      return resultObject;
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return processExpction(e.getMessage());
    }
  }