|
@@ -0,0 +1,98 @@
|
|
|
|
|
+package com.etotem.cfc.controller.fatloss;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
|
|
+import com.etotem.cfc.entity.FatDietRecord;
|
|
|
|
|
+import com.etotem.cfc.service.FatDietService;
|
|
|
|
|
+import com.etotem.cfc.util.ParamUtils;
|
|
|
|
|
+import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+@Tag(name = "减脂-饮食记录")
|
|
|
|
|
+@RestController
|
|
|
|
|
+@RequestMapping("/api/fatloss/diet")
|
|
|
|
|
+public class FatDietController {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FatDietService fatDietService;
|
|
|
|
|
+
|
|
|
|
|
+ @Operation(summary = "获取饮食记录列表")
|
|
|
|
|
+ @PostMapping("/list")
|
|
|
|
|
+ public Result<List<FatDietRecord>> list(@RequestBody(required = false) Map<String, Object> params,
|
|
|
|
|
+ @RequestAttribute("userId") Long userId,
|
|
|
|
|
+ @RequestAttribute(value = "currentMemberId", required = false) Long currentMemberId) {
|
|
|
|
|
+ Long memberId = ParamUtils.getLong(params == null ? null : params.get("memberId"), currentMemberId);
|
|
|
|
|
+ if (memberId == null) return Result.error("memberId不能为空");
|
|
|
|
|
+ Date startDate = parseDateParam(params == null ? null : params.get("startDate"));
|
|
|
|
|
+ Date endDate = parseDateParam(params == null ? null : params.get("endDate"));
|
|
|
|
|
+ return Result.success(fatDietService.list(memberId, startDate, endDate));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Operation(summary = "创建饮食记录")
|
|
|
|
|
+ @PostMapping("/create")
|
|
|
|
|
+ public Result<FatDietRecord> create(@RequestBody Map<String, Object> params,
|
|
|
|
|
+ @RequestAttribute("userId") Long userId,
|
|
|
|
|
+ @RequestAttribute(value = "currentMemberId", required = false) Long currentMemberId) {
|
|
|
|
|
+ FatDietRecord record = new FatDietRecord();
|
|
|
|
|
+ record.setMemberId(ParamUtils.getLong(params.get("memberId"), currentMemberId));
|
|
|
|
|
+ record.setMealType((String) params.get("mealType"));
|
|
|
|
|
+ record.setFoodName((String) params.get("foodName"));
|
|
|
|
|
+ record.setFoodWeight(toBigDecimal(params.get("foodWeight")));
|
|
|
|
|
+ record.setCalories(toInteger(params.get("calories")));
|
|
|
|
|
+ record.setProtein(toBigDecimal(params.get("protein")));
|
|
|
|
|
+ record.setFat(toBigDecimal(params.get("fat")));
|
|
|
|
|
+ record.setCarbs(toBigDecimal(params.get("carbs")));
|
|
|
|
|
+ record.setEatDate(parseDateParam(params.get("eatDate")));
|
|
|
|
|
+ record.setRemark((String) params.get("remark"));
|
|
|
|
|
+ if (record.getMemberId() == null) return Result.error("memberId不能为空");
|
|
|
|
|
+ if (record.getEatDate() == null) return Result.error("进餐日期不能为空");
|
|
|
|
|
+ if (record.getFoodName() == null || record.getFoodName().isEmpty()) return Result.error("食物名称不能为空");
|
|
|
|
|
+ return Result.success(fatDietService.create(record));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Operation(summary = "删除饮食记录")
|
|
|
|
|
+ @PostMapping("/delete")
|
|
|
|
|
+ public Result<Boolean> delete(@RequestBody Map<String, Object> params) {
|
|
|
|
|
+ Long id = ParamUtils.getLong(params.get("id"));
|
|
|
|
|
+ if (id == null) return Result.error("id不能为空");
|
|
|
|
|
+ return Result.success(fatDietService.delete(id));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Operation(summary = "获取当日总热量")
|
|
|
|
|
+ @PostMapping("/calories-summary")
|
|
|
|
|
+ public Result<Integer> caloriesSummary(@RequestBody(required = false) Map<String, Object> params,
|
|
|
|
|
+ @RequestAttribute("userId") Long userId,
|
|
|
|
|
+ @RequestAttribute(value = "currentMemberId", required = false) Long currentMemberId) {
|
|
|
|
|
+ Long memberId = ParamUtils.getLong(params == null ? null : params.get("memberId"), currentMemberId);
|
|
|
|
|
+ if (memberId == null) return Result.error("memberId不能为空");
|
|
|
|
|
+ Date date = parseDateParam(params == null ? null : params.get("date"));
|
|
|
|
|
+ if (date == null) date = new Date();
|
|
|
|
|
+ return Result.success(fatDietService.sumCalories(memberId, date));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private BigDecimal toBigDecimal(Object obj) {
|
|
|
|
|
+ if (obj == null) return null;
|
|
|
|
|
+ String s = obj.toString().trim();
|
|
|
|
|
+ if (s.isEmpty()) return null;
|
|
|
|
|
+ try { return new BigDecimal(s); } catch (NumberFormatException e) { return null; }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Integer toInteger(Object obj) {
|
|
|
|
|
+ if (obj == null) return 0;
|
|
|
|
|
+ String s = obj.toString().trim();
|
|
|
|
|
+ if (s.isEmpty()) return 0;
|
|
|
|
|
+ try { return Integer.parseInt(s); } catch (NumberFormatException e) { return 0; }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Date parseDateParam(Object obj) {
|
|
|
|
|
+ if (obj == null) return null;
|
|
|
|
|
+ try { return new SimpleDateFormat("yyyy-MM-dd").parse(obj.toString()); } catch (Exception e) { return null; }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|