コード例 #1
0
  /**
   * 根据食品id和校区获取某个零食
   *
   * @param foodId
   * @param campusId
   * @return
   */
  @RequestMapping("/getFoodById")
  public @ResponseBody Map<String, Object> selectFoods(
      @RequestParam Long foodId, @RequestParam Integer campusId) {
    Map<String, Object> map = new HashMap<String, Object>();
    try {
      DecimalFormat df = new DecimalFormat("#.0");

      Map<String, Object> paramMap = new HashMap<>();
      paramMap.put("foodId", foodId);
      paramMap.put("campusId", campusId);

      Food food = foodService.selectFoodByPrimaryKey(paramMap);
      List<FoodSpecial> foodSpecials = foodService.getFoodSpecial(paramMap);

      if (food != null) {
        Float gradeFloat = foodService.getAvageGrade(paramMap);
        if (gradeFloat == null) {
          food.setGrade(0f);
        } else {
          food.setGrade(Float.parseFloat(df.format(gradeFloat)));
        }

        food.setCommentNumber(foodService.getCommentCountsById(paramMap));
        food.setFoodSpecial(foodSpecials);
        map.put(Constants.STATUS, Constants.SUCCESS);
        map.put(Constants.MESSAGE, "获取食品成功");
        map.put("food", food);
      } else {
        map.put(Constants.STATUS, Constants.FAILURE);
        map.put(Constants.MESSAGE, "没有该零食");
      }
    } catch (Exception e) {
      e.getStackTrace();
      map.put(Constants.STATUS, Constants.FAILURE);
      map.put(Constants.MESSAGE, "获取零食失败");
    }
    return map;
  }