|
@@ -0,0 +1,77 @@
|
|
|
|
|
+package work.baiyun.chronicdiseaseapp.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.mapper.PhysicalDataMapper;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.model.po.PhysicalData;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.model.vo.BaseQueryRequest;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.model.vo.PhysicalDataResponse;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.model.vo.AddPhysicalDataRequest;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.service.PhysicalDataService;
|
|
|
|
|
+
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.math.RoundingMode;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+public class PhysicalDataServiceImpl implements PhysicalDataService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private PhysicalDataMapper physicalDataMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void addPhysicalData(AddPhysicalDataRequest request) {
|
|
|
|
|
+ Long userId = getCurrentUserId();
|
|
|
|
|
+ PhysicalData physicalData = new PhysicalData();
|
|
|
|
|
+ BeanUtils.copyProperties(request, physicalData);
|
|
|
|
|
+ physicalData.setUserId(userId);
|
|
|
|
|
+ physicalDataMapper.insert(physicalData);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Page<PhysicalDataResponse> listPhysicalData(BaseQueryRequest request) {
|
|
|
|
|
+ Long userId = getCurrentUserId();
|
|
|
|
|
+ Page<PhysicalData> page = new Page<>(request.getPageNum(), request.getPageSize());
|
|
|
|
|
+ LambdaQueryWrapper<PhysicalData> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ wrapper.eq(PhysicalData::getUserId, userId)
|
|
|
|
|
+ .ge(request.getStartTime() != null, PhysicalData::getMeasureTime, request.getStartTime())
|
|
|
|
|
+ .le(request.getEndTime() != null, PhysicalData::getMeasureTime, request.getEndTime())
|
|
|
|
|
+ .orderByDesc(PhysicalData::getMeasureTime);
|
|
|
|
|
+
|
|
|
|
|
+ Page<PhysicalData> result = physicalDataMapper.selectPage(page, wrapper);
|
|
|
|
|
+
|
|
|
|
|
+ List<PhysicalDataResponse> responses = result.getRecords().stream()
|
|
|
|
|
+ .map(record -> {
|
|
|
|
|
+ PhysicalDataResponse r = new PhysicalDataResponse();
|
|
|
|
|
+ BeanUtils.copyProperties(record, r);
|
|
|
|
|
+ r.setBmi(calculateBMI(record.getHeight(), record.getWeight()));
|
|
|
|
|
+ return r;
|
|
|
|
|
+ })
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ Page<PhysicalDataResponse> responsePage = new Page<>();
|
|
|
|
|
+ responsePage.setRecords(responses);
|
|
|
|
|
+ responsePage.setCurrent(result.getCurrent());
|
|
|
|
|
+ responsePage.setSize(result.getSize());
|
|
|
|
|
+ responsePage.setTotal(result.getTotal());
|
|
|
|
|
+ return responsePage;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public BigDecimal calculateBMI(BigDecimal height, BigDecimal weight) {
|
|
|
|
|
+ if (height == null || weight == null || height.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
|
|
+ return BigDecimal.ZERO;
|
|
|
|
|
+ }
|
|
|
|
|
+ BigDecimal heightInMeters = height.divide(new BigDecimal("100"), 4, RoundingMode.HALF_UP);
|
|
|
|
|
+ return weight.divide(heightInMeters.pow(2), 2, RoundingMode.HALF_UP);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Long getCurrentUserId() {
|
|
|
|
|
+ // TODO: replace with actual security context lookup
|
|
|
|
|
+ return 1L;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|