/**
* 新增 儿童成长记录
*/
@ResponseBody
@RequestMapping(value = { "/addGrowthRecord" }, method = RequestMethod.POST)
public String addGrowthRecord(HttpServletRequest request, HttpServletResponse response,
@RequestParam("pictureFile") MultipartFile[] pictureFile, String rcType, String stTime, String edTime,
String record, String privacyType, String userPhone, String archiveNum, String cc_type, String xx_color,
String bb_type, String bb_color, String br_type, String br_ml) throws Exception {
JSONObject json = new JSONObject();
// 处理图片组
// 接受保存图片的 ID
List pcidList = new ArrayList();
if (pictureFile != null && pictureFile.length > 0) {
for (int i = 0; i < pictureFile.length; i++) {
MultipartFile file = pictureFile[i];
// 保存图片
Picture pc = new ImgToProjectTools().saveImgThree(file);
int pc_id = picturemanagecontroller.addPicture(request, response, pc);
pcidList.add(pc_id);
}
}
// 创建存储对象
GrowthRecord gr = new GrowthRecord();
if ((null != rcType && rcType.equals("臭臭")) || (null !=br_type && br_type.equals("瓶装母乳")) || (null !=br_type &&br_type.equals("配方奶"))) {
// 臭臭类型不计结束时间,不计时长;瓶装喂奶,配方奶 不计结束时间和时长
} else {
// 计算时长
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date d1 = df.parse(edTime);
Date d2 = df.parse(stTime);
long diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别
long days = diff / (1000 * 60 * 60 * 24);
long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
System.out.println("" + days + "天" + hours + "小时" + minutes + "分");
if (days != 0) {
hours += (days * 24);
}
if (hours != 0) {
minutes += (hours * 60);
}
gr.setEd_time(edTime);
gr.setTime_length(minutes + "");
}
gr.setId(UUID.randomUUID().toString());
gr.setAddtime(new java.sql.Date(new Date().getTime()));
gr.setSt_time(stTime);
gr.setPcids(StringUtils.strip(pcidList.toString(), "[]"));
gr.setPrivacy_type(privacyType);
gr.setRc_type(rcType);
gr.setRecord(record);
// 用户信息
gr.setUser_phone(userPhone);
gr.setArchiveNum(archiveNum);
// 便便
gr.setCc_type(cc_type);
gr.setXx_color(xx_color);
gr.setBb_color(bb_color);
gr.setBb_type(bb_type);
// 母乳
gr.setBr_type(br_type);
gr.setBr_ml(br_ml);
int rus = igrowthrecordservice.addGrowthRecord(gr);
if (rus != 0) {
json.put("error", 0);
json.put("data", "保存成功");
} else {
json.put("error", 1);
json.put("data", "保存失败,错误原因:" + rus);
}
return json.toString();
}
注意:
form 表单添加属性 enctype="multipart/form-data"