Ejemplo n.º 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;
  }
Ejemplo n.º 2
0
  /**
   * 在pc端添加食品
   *
   * @param myfile
   * @param request
   * @return
   */
  @RequestMapping(value = "/updateFoods.do")
  public String updateFoods(@RequestParam MultipartFile[] myfile, HttpServletRequest request) {
    try {
      Long foodId = Long.valueOf(request.getParameter("foodId")); // 获取食品id
      Float price = Float.valueOf(request.getParameter("price")); // 获取价格
      String name = request.getParameter("foodName"); // 获取食品名称
      Float discountPrice = Float.valueOf(request.getParameter("discountPrice")); // 获取折扣价
      Short status = Short.valueOf(request.getParameter("status")); // 获取食品上架下架状态
      Short isDiscount = Short.valueOf(request.getParameter("is_discount")); // 是否打折
      String foodFlag = request.getParameter("foodTag"); // 食品标签
      Integer categoryId = Integer.valueOf(request.getParameter("categoyId")); // 获取分类Id
      Float primeCost = null;

      String temp1 = request.getParameter("primeCost"); // 获取成本价
      String temp2 = request.getParameter("foodCount"); // 获取食品数量
      Integer campusId = Integer.valueOf(request.getParameter("campusId")); // 获取校区
      if (temp1 != null && !temp1.trim().equals("")) {
        primeCost = Float.valueOf(request.getParameter("primeCost"));
      }
      Integer foodCount = null;
      if (temp2 != null && !temp2.trim().equals("")) {
        foodCount = Integer.valueOf(request.getParameter("foodCount"));
      }

      Short isDefault = Short.valueOf(request.getParameter("default_special"));
      String realPath = request.getSession().getServletContext().getRealPath("/");
      realPath = realPath.replace("SJFood", "MickeyImage");
      realPath = realPath.concat("\\food\\");
      System.out.println(realPath); // 打印出服务器路径

      List<String> imageUrl = new ArrayList<String>();
      for (MultipartFile file : myfile) {
        if (file.isEmpty()) {
          System.out.println("文件未上传");
          imageUrl.add(null);
        } else {
          String contentType = file.getContentType();

          if (contentType.startsWith("image")) {
            String newFileName = new Date().getTime() + "" + new Random().nextInt() + ".jpg";
            FileUtils.copyInputStreamToFile(
                file.getInputStream(), new File(realPath, newFileName)); // 写文件
            imageUrl.add(Constants.localIp + "/food/" + newFileName);
          }
        }
      }
      Food food =
          new Food(
              campusId,
              foodId,
              name,
              price,
              discountPrice,
              imageUrl.get(0),
              imageUrl.get(1),
              status,
              foodFlag,
              isDiscount,
              categoryId,
              primeCost);

      Map<String, Object> paramMap = new HashMap<String, Object>();
      paramMap.put("campusId", campusId);
      paramMap.put("foodId", foodId);
      Food orignFood = foodService.selectFoodByPrimaryKey(paramMap); // 查看该食品是否存在
      int flag = 0;
      if (orignFood == null) {
        // 不存在即添加
        flag = foodService.insertFoodSelective(food);
        // 只有在增加食品的时候添加默认口味才有效
        if (flag != -1 && flag != 0) {
          if (isDefault == 1 && foodCount != null) {
            // 添加默认口味
            FoodSpecial foodSpecial = new FoodSpecial(campusId, foodId, "默认口味", foodCount);
            foodSpecial.setSpecialId(1);
            flag = foodService.addFoodSpecial(foodSpecial);
            if (flag != -1 && flag != 0) {
              return "redirect:/pages/food.html";
            }
          }
        }
      } else {
        // 存在即更新
        flag = foodService.updateFoodByPrimaryKeySelective(food);

        // 删除原食品主图片
        if (food.getImgUrl() != null && orignFood.getImgUrl() != null) {
          String[] temp = orignFood.getImgUrl().split("/");
          String imageName = temp[(temp.length - 1)];

          String name2 = realPath + imageName;

          File file = new File(name2);
          if (file.isFile()) {
            file.delete(); // 删除
          }
        }

        // 删除原食品辅助图片
        if (food.getInfo() != null && orignFood.getInfo() != null) {
          String[] temp = orignFood.getInfo().split("/");
          String imageName = temp[(temp.length - 1)];

          String name2 = realPath + imageName;

          File file = new File(name2);
          if (file.isFile()) {
            file.delete(); // 删除
          }
        }

        if (flag != -1 && flag != 0) {
          return "redirect:/pages/food.html";
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
      return "redirect:/pages/uploadError.html";
    }
    return "redirect:/pages/uploadError.html";
  }