/**
  * 宠物信息新增和修改
  *
  * @param response
  * @param userName
  * @param petId
  * @param nickname
  * @param sex
  * @param breed
  * @param birthday
  * @param height
  * @param weight
  * @param district
  * @param placeOftenGo
  * @throws IOException
  */
 @RequestMapping(value = "/modify")
 public void modifyPetInfo(
     HttpServletResponse response,
     @RequestParam(value = "username") String userName,
     @RequestParam(value = "petid", required = false) String petId,
     @RequestParam(value = "nickname", defaultValue = "") String nickname,
     @RequestParam(value = "sex", defaultValue = "") String sex,
     @RequestParam(value = "breed", defaultValue = "") String breed,
     @RequestParam(value = "birthday", defaultValue = "0") String birthday,
     @RequestParam(value = "height", defaultValue = "") String height,
     @RequestParam(value = "weight", defaultValue = "") String weight,
     @RequestParam(value = "district", defaultValue = "") String district,
     @RequestParam(value = "placeoftengo", defaultValue = "") String placeOftenGo)
     throws IOException {
   IdBean petUpdateReturnBean = new IdBean();
   if (StringUtil.isNullOrEmpty(petId)) {
     if (!petInfoDao.hasPetInfo(userName)) {
       // create pet info
       long id =
           petInfoDao.createPetInfo(
               userName, nickname, sex, breed, birthday, height, weight, district, placeOftenGo);
       log.info("create pet id: " + id);
       if (id > 0) {
         petUpdateReturnBean.setResult("0");
         petUpdateReturnBean.setId(String.valueOf(id));
       } else {
         petUpdateReturnBean.setResult("4");
       }
     } else {
       petUpdateReturnBean.setResult("3"); // already has pet
     }
   } else {
     // save pet info
     int update =
         petInfoDao.updatePetInfo(
             petId,
             nickname,
             sex,
             breed,
             birthday,
             height,
             weight,
             district,
             placeOftenGo,
             null,
             null);
     if (update > 0) {
       petUpdateReturnBean.setResult("0");
       petUpdateReturnBean.setId(petId);
     } else {
       petUpdateReturnBean.setResult("1");
     }
   }
   response.getWriter().print(JSONUtil.toString(petUpdateReturnBean));
 }
  /**
   * 上传宠物头像
   *
   * @param request
   * @param response
   * @throws IOException
   */
  @RequestMapping(value = "/uploadavatar")
  public void uploadPetAvatar(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());

    upload.setHeaderEncoding("UTF-8");

    List<FileItem> items;
    String userName = "";
    String petId = "";
    IdBean petUpdateReturnBean = new IdBean();
    try {
      items = upload.parseRequest(request);
      log.info("items : " + items);
      if (items == null) {
        throw new FileUploadException();
      }

      FileItem file2Upload = null;
      for (FileItem item : items) {
        log.info("field name: " + item.getFieldName());
        if ("avatar_file".equals(item.getFieldName())) {
          file2Upload = item;
        } else if ("petid".equals(item.getFieldName())) {
          petId = item.getString();
        } else if ("username".equals(item.getFieldName())) {
          userName = item.getString();
        }
      }
      if (file2Upload == null) {
        throw new FileUploadException("No avatar file");
      }

      String avatarFileName = UUID.randomUUID().toString();
      // save avatar file
      FileUtil.saveFile(avatarFileName, file2Upload);

      if (StringUtil.isNullOrEmpty(petId)) {
        if (!petInfoDao.hasPetInfo(userName)) {
          // create new avatar
          long id = petInfoDao.createPetAvatarInfo(avatarFileName, userName);
          log.info("create pet id: " + id);
          if (id > 0) {
            petUpdateReturnBean.setResult("0");
            petUpdateReturnBean.setId(String.valueOf(id));
          } else {
            petUpdateReturnBean.setResult("4");
          }
        } else {
          petUpdateReturnBean.setResult("3"); // already has pet
        }
      } else {
        // update avatar
        // String avatar = petInfoDao.getPetAvatar(petId);
        // if (!StringUtil.isNullOrEmpty(avatar)) {
        // avatarFileName = avatar;
        // }
        int update = petInfoDao.updatePetAvatar(petId, avatarFileName);
        if (update > 0) {
          petUpdateReturnBean.setResult("0");
          petUpdateReturnBean.setId(petId);
        } else {
          petUpdateReturnBean.setResult("1"); // update failed
        }
      }
    } catch (FileUploadException e) {
      e.printStackTrace();
      petUpdateReturnBean.setResult("2"); // no file
    } catch (Exception e) {
      e.printStackTrace();
      petUpdateReturnBean.setResult("5"); // save file failed
    }
    response.getWriter().print(JSONUtil.toString(petUpdateReturnBean));
  }