| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package com.etotem.cfc.service;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.etotem.cfc.entity.FatDietRecord;
- import com.etotem.cfc.mapper.FatDietRecordMapper;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.Date;
- import java.util.List;
- @Service
- public class FatDietService {
- @Resource
- private FatDietRecordMapper fatDietRecordMapper;
- public List<FatDietRecord> list(Long memberId, Date startDate, Date endDate) {
- LambdaQueryWrapper<FatDietRecord> wrapper = new LambdaQueryWrapper<FatDietRecord>()
- .eq(FatDietRecord::getMemberId, memberId)
- .orderByDesc(FatDietRecord::getEatDate);
- if (startDate != null) {
- wrapper.ge(FatDietRecord::getEatDate, startDate);
- }
- if (endDate != null) {
- wrapper.le(FatDietRecord::getEatDate, endDate);
- }
- return fatDietRecordMapper.selectList(wrapper);
- }
- public FatDietRecord create(FatDietRecord record) {
- record.setCreatedAt(new Date());
- fatDietRecordMapper.insert(record);
- return record;
- }
- public boolean delete(Long id) {
- return fatDietRecordMapper.deleteById(id) > 0;
- }
- /** 统计指定日期总热量 */
- public int sumCalories(Long memberId, Date date) {
- List<FatDietRecord> records = fatDietRecordMapper.selectList(
- new LambdaQueryWrapper<FatDietRecord>()
- .eq(FatDietRecord::getMemberId, memberId)
- .eq(FatDietRecord::getEatDate, date));
- int total = 0;
- for (FatDietRecord r : records) {
- if (r.getCalories() != null) total += r.getCalories();
- }
- return total;
- }
- }
|