123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- package com.iamberry.app.api.controller;
- import static com.iamberry.app.config.Response.BARCODE_NOT_FOUND;
- import static com.iamberry.app.config.Response.INVALID_TOKEN;
- import static com.iamberry.app.config.Response.SUCCESS;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.WebDataBinder;
- import org.springframework.web.bind.annotation.InitBinder;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.multipart.MultipartFile;
- import com.iamberry.app.config.Response;
- import com.iamberry.app.config.ResponseHeader;
- import com.iamberry.app.core.dto.RecordDTO;
- import com.iamberry.app.core.entity.MilkDatetimeValue;
- import com.iamberry.app.core.entity.MilkImages;
- import com.iamberry.app.core.entity.MilkMakingRecord;
- import com.iamberry.app.core.entity.MilkPowderInfo;
- import com.iamberry.app.core.entity.User;
- import com.iamberry.wechat.file.FileUtils;
- import com.iamberry.wechat.file.UploadResultBean;
- import com.iamberry.wechat.tools.ResponseJson;
- /**
- * UserController
- *
- * @author Moon Cheng
- * @date 2016年1月18日 上午11:53:04
- */
- @Controller
- @RequestMapping("/secure/milk")
- public class MilkController extends BaseController {
- /**
- * 上传奶粉信息的域名,及保存的路径,需要tomcat单独域名映射/或者是nginx
- */
- private static String SAVE_MILK_IMAGE_DOMAIN = "http://milk.iamberry.com/";
- private static String SAVE_MILK_IMAGE_PATH = "/common/";
- @InitBinder("makingRecord")
- public void initBinderRecord(WebDataBinder binder) {
- binder.setFieldDefaultPrefix("makingRecord.");
- }
- @InitBinder("milkPowderInfo")
- public void initBinderMilkPowder(WebDataBinder binder) {
- binder.setFieldDefaultPrefix("milkPowderInfo.");
- }
- /**
- * 添加冲奶信息
- * @author Moon Cheng
- * @param token 唯一标识符
- * @param makingRecord 冲奶信息实体类
- * @return 1000:请求成功;2100:无效token;
- */
- @RequestMapping(value = "/set_milk_making_record", method = RequestMethod.POST)
- @ResponseBody
- private Response setMilkMakingRecord(@RequestParam("token") String token, MilkMakingRecord makingRecord) {
- makingRecord = milkService.setMilkMakingRecord(makingRecord, token);
- if (makingRecord == null) {
- return INVALID_TOKEN;
- }
- return SUCCESS.setData(makingRecord, 1);
- }
- /**
- * 通过日期,日期类型,统计某时间的,冲奶总量
- * @param token 唯一标识符
- * @param dateStatus 标识符 : day week year
- * @param chosenDate 结束时间
- * @param machineMac 机器mac地址
- * @return
- */
- @RequestMapping(value = "/search_user_records_by_time", method = RequestMethod.POST)
- @ResponseBody
- private Response searchUserRecordsByTime(@RequestParam(value = "token") String token,
- @RequestParam("date_status") String dateStatus, @RequestParam("chosen_date") String chosenDate,
- @RequestParam("devid") String machineMac) {
- User userInfo = userService.validateUserToken(token);
- if (userInfo == null) {
- return INVALID_TOKEN;
- }
- Map<String, List<RecordDTO>> makingRecords = milkService.searchUserRecordsByTime(userInfo.getId(), dateStatus,
- chosenDate, machineMac);
- return SUCCESS.setData(makingRecords, makingRecords.size());
- }
- /**
- * 根据用户id获取用户,冲奶总容量
- * @author Moon Cheng
- * @param userId 用户id
- * @return
- */
- @RequestMapping(value = "/get_user_total_milk_volume", method = RequestMethod.POST)
- @ResponseBody
- private Response getTotalMilkVolumeMadeByUser(@RequestParam(value = "user_id") Long userId) {
- return SUCCESS.setData(milkService.getTotalMilkVolumeMadeByUser(userId), 0);
- }
- /**
- * 手动添加,奶粉信息!
- * @author Moon Cheng
- * @param token 唯一标识符
- * @param milkPowderInfo 奶粉信息
- * @return
- */
- @RequestMapping(value = "/set_milk_powder_info", method = RequestMethod.POST)
- @ResponseBody
- private Response setMilkPowderInfo(@RequestParam(value = "token") String token, MilkPowderInfo milkPowderInfo) {
-
- // 勺量,每勺多少克 量勺 克数范围2-18
- String amountStr = request.getParameter("a_mount");
- if (amountStr == null) {
- // 兼容低版本
- milkPowderInfo = milkService.setMilkPowderInfo(milkPowderInfo, token);
- if (milkPowderInfo == null) {
- return INVALID_TOKEN;
- }
- return SUCCESS.setData(milkPowderInfo, 1);
- }
-
- float amount = Float.parseFloat(amountStr);
- // 勺数,多少少 量勺勺数范围 1-10
- int spoonNum = Integer.parseInt(request.getParameter("spoon_num"));
- // ML,1勺多少奶量 水量范围30-300
- float spoonML = Float.parseFloat(request.getParameter("spoon_ml"));
-
- // 毫升/勺
- float s = spoonML / spoonNum;
-
- // 混合比 计算公式:勺量 * 勺数 / 毫升 * 100 = 混合比
- float ratio = amount * spoonNum / spoonML * 100;
- milkPowderInfo.setPowder_ratio(ratio);
- milkPowderInfo.setWeight_per_spoon(amount);
- milkPowderInfo.setMilk_volume_per_spoon(s);
-
- // 密度
- /*
- * A<13.2 6
- 13.21<A<15.3 7
- 15.31<A<17.4 8
- 17.41<A<19.5 9
- 19.51<A<22.0 10
- 22.1 <A 10
- */
- int m = 6;
- if (ratio < 13.2) {
- m = 6;
- } else if (ratio > 13.21 && ratio < 15.3) {
- m = 7;
- } else if (ratio > 15.31 && ratio < 17.4) {
- m = 8;
- } else if (ratio > 17.41 && ratio < 19.5) {
- m = 9;
- } else {
- m = 10;
- }
- milkPowderInfo.setDensity((float)m);
-
- // 保存操作
- milkPowderInfo = milkService.setMilkPowderInfo(milkPowderInfo, token);
- if (milkPowderInfo == null) {
- return INVALID_TOKEN;
- }
-
- // 保存图片
- String imageFront = request.getParameter("imageFront");
- String imageBar = request.getParameter("imageBar");
- String imageFeed = request.getParameter("imageFeed");
- MilkImages images = new MilkImages();
- images.setImage_Front(imageFront);
- images.setImage_Bar(imageBar);
- images.setImage_Feed(imageFeed);
- images.setImage_Milk_ID(milkPowderInfo.getId());
- milkService.setMilkImages(images);
-
- return SUCCESS.setData(milkPowderInfo, 1);
- }
- /**
- * 通过,条形码获取奶粉信息
- * @author Moon Cheng
- * @param barcode 条形码code
- * @return
- */
- @RequestMapping(value = "/search_milk_powder_info_by_barcode", method = RequestMethod.POST)
- @ResponseBody
- private Response searchMilkPowderInfoByBarCode(@RequestParam(value = "barcode") String barcode) {
- MilkPowderInfo milkPowderInfo = milkService.searchMilkPowderInfoByBarCode(barcode);
- if (milkPowderInfo == null) {
- return BARCODE_NOT_FOUND;
- }
- return SUCCESS.setData(milkPowderInfo, 1);
- }
- /**
- * 通过品牌名获取奶粉信息
- * @author Moon Cheng, Yin
- * @param brand 品牌名
- * @return
- */
- @RequestMapping(value = "/search_milk_powder_infos_by_brand", method = RequestMethod.POST)
- @ResponseBody
- private Response searchMilkPowderInfosByBrand(@RequestParam(value = "brand") String brand) {
- Map<String, List<String>> milkPowderInfos = milkService.searchMilkPowderInfosByBrand(brand);
- return SUCCESS.setData(milkPowderInfos, milkPowderInfos.size());
- }
- /**
- * 获取奶粉品牌
- * @author Yin
- * @return 过滤掉品牌名中有.的 例如 a.b 这里或只查询到a
- */
- @RequestMapping(value = "/search_milk_powder_brand", method = RequestMethod.GET)
- @ResponseBody
- private Response searchMilkPowderBrand() {
- List<String> milkPowderInfos = milkService.searchMilkPowderBrand();
- return SUCCESS.setData(milkPowderInfos, milkPowderInfos.size());
- }
- /**
- * @param model
- * @param request
- * @return
- */
- @ResponseBody
- @RequestMapping(value = "/upload_image", method = RequestMethod.POST)
- public Response uploadFileImage(@RequestParam("file")MultipartFile[] files, HttpServletRequest request) {
- Map<String, String> filemap= new HashMap<String, String>();
-
- for (int i = 0; i < files.length; i++) {
- MultipartFile file = files[i];
- // 保存文件
- UploadResultBean bean = FileUtils.saveFile(request.getServletContext(), file, SAVE_MILK_IMAGE_PATH, false);
- if (bean == null) {
- // 文件有误
- return new Response(new ResponseHeader(4204, "文件有误!", -1));
- }
- filemap.put("file"+i,SAVE_MILK_IMAGE_DOMAIN + bean.getSaveFileName());
- }
- return SUCCESS.setData(filemap, filemap.size());
- }
-
- /**
- * 获取冲奶记录详情
- * @param mac 机器id
- * @param userToken 用户token
- * @param date 时间,格式为yyyy-MM-dd,可以为空,如果为空,默认为当天
- * @return
- * @author 献
- * @Time 2016年12月12日
- */
- @ResponseBody
- @RequestMapping(value = "/{mac}/milk_record", method = RequestMethod.POST)
- public ResponseJson machineRecordByTime(@PathVariable("mac") String mac,
- @RequestParam("token") String userToken,
- @RequestParam(value = "date", required = false) String date) {
- User userInfo = userService.validateUserToken(userToken);
-
- if (userInfo == null) {
- //token不存在,表示登录的时候没有..
- return new ResponseJson(200, "SUCCESS", 404);
- }
-
- // 找到机器
- ResponseJson json = new ResponseJson(200, "SUCCESS", 200);
- List<MilkDatetimeValue> values = milkService.getMilkRecordByTime(mac, date);
- if (values == null) {
- json.addResponseKeyValue("data", false);
- } else {
- json.addResponseKeyValue("data", true);
- }
- json.addResponseKeyValue("values", values);
- return json;
- }
-
- /**
- * 根据机器号,按照日期统计
- * @param mac 机器号
- * @param userToken 用户token
- * @param date 日期,可以为空,如果为空,默认查询本月数据
- * @return
- * @author 献
- * @Time 2016年12月12日
- */
- @ResponseBody
- @RequestMapping(value = "/{mac}/date/milk_record")
- public ResponseJson machineRecordByDate(
- @PathVariable("mac") String mac,
- @RequestParam("token") String userToken,
- @RequestParam(value = "date", required = false) String date) {
- User userInfo = userService.validateUserToken(userToken);
-
- if (userInfo == null) {
- //token不存在,表示登录的时候没有..
- return new ResponseJson(200, "SUCCESS", 404);
- }
-
- // 提取数据
- ResponseJson json = new ResponseJson(200, "SUCCESS", 200);
- List<MilkDatetimeValue> values = milkService.getMilkRecordByDate(mac, date);
- if (values == null) {
- json.addResponseKeyValue("data", false);
- } else {
- json.addResponseKeyValue("data", true);
- }
- json.addResponseKeyValue("values", values);
- return json;
- }
- }
|