|
@@ -0,0 +1,216 @@
|
|
|
+package com.fs.app.controller;
|
|
|
+
|
|
|
+import com.fs.app.annotation.Login;
|
|
|
+import com.fs.foods.param.FoodRecordAddParam;
|
|
|
+import com.fs.foods.param.FoodRecordEditParam;
|
|
|
+import com.fs.foods.param.FoodRecordQueryParam;
|
|
|
+import com.fs.common.core.domain.R;
|
|
|
+import com.fs.common.core.page.TableDataInfo;
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
+import com.fs.foods.domain.FsFoodRecord;
|
|
|
+import com.fs.foods.service.IFsFoodRecordService;
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.format.annotation.DateTimeFormat;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.validation.Valid;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 饮食记录控制器
|
|
|
+ */
|
|
|
+@Api("饮食记录管理")
|
|
|
+@RestController
|
|
|
+@Slf4j
|
|
|
+@RequestMapping(value = "/app/food-record")
|
|
|
+public class FoodRecordController extends AppBaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IFsFoodRecordService foodRecordService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户饮食记录详情
|
|
|
+ */
|
|
|
+ @Login
|
|
|
+ @ApiOperation("获取饮食记录详情")
|
|
|
+ @GetMapping("/getRecordInfo/{id}")
|
|
|
+ public R getRecordInfo(@PathVariable Long id, HttpServletRequest request) {
|
|
|
+ try {
|
|
|
+ FsFoodRecord record = foodRecordService.selectFsFoodRecordById(id);
|
|
|
+ if (record == null || !record.getUserId().equals(Long.parseLong(getUserId()))) {
|
|
|
+ return R.error("记录不存在或无权限访问");
|
|
|
+ }
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("record", record);
|
|
|
+ return R.ok(map);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取饮食记录详情异常:", e);
|
|
|
+ return R.error("操作异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户某日的饮食记录列表
|
|
|
+ */
|
|
|
+ @Login
|
|
|
+ @ApiOperation("获取某日饮食记录列表")
|
|
|
+ @GetMapping("/getDayRecords")
|
|
|
+ public R getDayRecords(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate recordDate,
|
|
|
+ HttpServletRequest request) {
|
|
|
+ try {
|
|
|
+ Long userId = Long.parseLong(getUserId());
|
|
|
+ List<FsFoodRecord> list = foodRecordService.selectFoodRecordsByUserAndDate(userId, recordDate);
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("records", list);
|
|
|
+ map.put("recordDate", recordDate);
|
|
|
+ return R.ok(map);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取某日饮食记录异常:", e);
|
|
|
+ return R.error("操作异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户饮食记录分页列表
|
|
|
+ */
|
|
|
+ @Login
|
|
|
+ @ApiOperation("获取用户饮食记录列表")
|
|
|
+ @GetMapping("/getMyRecordList")
|
|
|
+ public R getMyRecordList(FoodRecordQueryParam param, HttpServletRequest request) {
|
|
|
+ try {
|
|
|
+ PageHelper.startPage(param.getPageNum(), param.getPageSize());
|
|
|
+ param.setUserId(Long.parseLong(getUserId()));
|
|
|
+ List<FsFoodRecord> list = foodRecordService.selectFoodRecordList(param);
|
|
|
+ PageInfo<FsFoodRecord> listPageInfo = new PageInfo<>(list);
|
|
|
+ return R.ok().put("data", listPageInfo);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取饮食记录列表异常:", e);
|
|
|
+ return R.error("操作异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增饮食记录
|
|
|
+ */
|
|
|
+ @Login
|
|
|
+ @ApiOperation("新增饮食记录")
|
|
|
+ @PostMapping("/addRecord")
|
|
|
+ public R addRecord(@RequestBody @Valid FoodRecordAddParam param, HttpServletRequest request) {
|
|
|
+ try {
|
|
|
+ log.info("【新增饮食记录】:{}", param);
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(param.getMealDescription())) {
|
|
|
+ return R.error("用餐描述不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ FsFoodRecord record = new FsFoodRecord();
|
|
|
+ BeanUtils.copyProperties(param, record);
|
|
|
+ record.setUserId(Long.parseLong(getUserId()));
|
|
|
+
|
|
|
+ if (foodRecordService.insertFsFoodRecord(record) > 0) {
|
|
|
+ return R.ok("添加成功");
|
|
|
+ } else {
|
|
|
+ return R.error("添加失败");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("新增饮食记录异常:", e);
|
|
|
+ return R.error("操作异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改饮食记录
|
|
|
+ */
|
|
|
+ @Login
|
|
|
+ @ApiOperation("修改饮食记录")
|
|
|
+ @PostMapping("/editRecord")
|
|
|
+ public R editRecord(@RequestBody @Valid FoodRecordEditParam param, HttpServletRequest request) {
|
|
|
+ try {
|
|
|
+ log.info("【修改饮食记录】:{}", param);
|
|
|
+
|
|
|
+ // 验证记录是否存在且属于当前用户
|
|
|
+ FsFoodRecord existRecord = foodRecordService.selectFsFoodRecordById(param.getId());
|
|
|
+ if (existRecord == null || !existRecord.getUserId().equals(Long.parseLong(getUserId()))) {
|
|
|
+ return R.error("记录不存在或无权限修改");
|
|
|
+ }
|
|
|
+
|
|
|
+ FsFoodRecord record = new FsFoodRecord();
|
|
|
+ BeanUtils.copyProperties(param, record);
|
|
|
+ record.setUserId(Long.parseLong(getUserId()));
|
|
|
+
|
|
|
+ if (foodRecordService.updateFsFoodRecord(record) > 0) {
|
|
|
+ return R.ok("修改成功");
|
|
|
+ } else {
|
|
|
+ return R.error("修改失败");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("修改饮食记录异常:", e);
|
|
|
+ return R.error("操作异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除饮食记录
|
|
|
+ */
|
|
|
+ @Login
|
|
|
+ @ApiOperation("删除饮食记录")
|
|
|
+ @PostMapping("/deleteRecord/{id}")
|
|
|
+ public R deleteRecord(@PathVariable("id") Long id, HttpServletRequest request) {
|
|
|
+ try {
|
|
|
+ // 验证记录是否存在且属于当前用户
|
|
|
+ FsFoodRecord existRecord = foodRecordService.selectFsFoodRecordById(id);
|
|
|
+ if (existRecord == null || !existRecord.getUserId().equals(Long.parseLong(getUserId()))) {
|
|
|
+ return R.error("记录不存在或无权限删除");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (foodRecordService.deleteFsFoodRecordById(id) > 0) {
|
|
|
+ return R.ok("删除成功");
|
|
|
+ } else {
|
|
|
+ return R.error("删除失败");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("删除饮食记录异常:", e);
|
|
|
+ return R.error("操作异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户饮食记录统计
|
|
|
+ */
|
|
|
+ @Login
|
|
|
+ @ApiOperation("获取饮食记录统计")
|
|
|
+ @GetMapping("/getRecordStats")
|
|
|
+ public R getRecordStats(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
|
|
|
+ @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
|
|
|
+ HttpServletRequest request) {
|
|
|
+ try {
|
|
|
+ Long userId = Long.parseLong(getUserId());
|
|
|
+ Map<String, Object> stats = foodRecordService.getFoodRecordStats(userId, startDate, endDate);
|
|
|
+ return R.ok().put("data", stats);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取饮食记录统计异常:", e);
|
|
|
+ return R.error("操作异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 管理端查询饮食记录列表
|
|
|
+ */
|
|
|
+ @ApiOperation("管理端查询饮食记录")
|
|
|
+ @GetMapping("/admin/list")
|
|
|
+ public TableDataInfo adminList(FoodRecordQueryParam param) {
|
|
|
+ startPage();
|
|
|
+ List<FsFoodRecord> list = foodRecordService.selectFoodRecordList(param);
|
|
|
+ return getDataTable(list);
|
|
|
+ }
|
|
|
+}
|