FatDietService.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package com.etotem.cfc.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.etotem.cfc.entity.FatDietRecord;
  4. import com.etotem.cfc.mapper.FatDietRecordMapper;
  5. import org.springframework.stereotype.Service;
  6. import javax.annotation.Resource;
  7. import java.util.Date;
  8. import java.util.List;
  9. @Service
  10. public class FatDietService {
  11. @Resource
  12. private FatDietRecordMapper fatDietRecordMapper;
  13. public List<FatDietRecord> list(Long memberId, Date startDate, Date endDate) {
  14. LambdaQueryWrapper<FatDietRecord> wrapper = new LambdaQueryWrapper<FatDietRecord>()
  15. .eq(FatDietRecord::getMemberId, memberId)
  16. .orderByDesc(FatDietRecord::getEatDate);
  17. if (startDate != null) {
  18. wrapper.ge(FatDietRecord::getEatDate, startDate);
  19. }
  20. if (endDate != null) {
  21. wrapper.le(FatDietRecord::getEatDate, endDate);
  22. }
  23. return fatDietRecordMapper.selectList(wrapper);
  24. }
  25. public FatDietRecord create(FatDietRecord record) {
  26. record.setCreatedAt(new Date());
  27. fatDietRecordMapper.insert(record);
  28. return record;
  29. }
  30. public boolean delete(Long id) {
  31. return fatDietRecordMapper.deleteById(id) > 0;
  32. }
  33. /** 统计指定日期总热量 */
  34. public int sumCalories(Long memberId, Date date) {
  35. List<FatDietRecord> records = fatDietRecordMapper.selectList(
  36. new LambdaQueryWrapper<FatDietRecord>()
  37. .eq(FatDietRecord::getMemberId, memberId)
  38. .eq(FatDietRecord::getEatDate, date));
  39. int total = 0;
  40. for (FatDietRecord r : records) {
  41. if (r.getCalories() != null) total += r.getCalories();
  42. }
  43. return total;
  44. }
  45. }