diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysIngredientMapper.java b/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysIngredientMapper.java index bb6c4aa5b..d1e8b9d9f 100644 --- a/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysIngredientMapper.java +++ b/stdiet-custom/src/main/java/com/stdiet/custom/mapper/SysIngredientMapper.java @@ -4,6 +4,7 @@ import java.util.List; import com.stdiet.custom.domain.SysIngredient; import com.stdiet.custom.domain.SysIngredientNotRec; import com.stdiet.custom.domain.SysIngredientRec; +import org.apache.ibatis.annotations.Param; /** * 食材Mapper接口 @@ -72,4 +73,11 @@ public interface SysIngredientMapper public int deleteIngredentRecByIngredientIds(Long[] id); public int deleteIngredentNotRecByIngredientIds(Long[] id); + + /** + * 根据食材名称查询食材信息 + * @param name + * @return + */ + public SysIngredient selectSysIngredientByName(@Param("name") String name); } \ No newline at end of file diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/AsyncCommonService.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/AsyncCommonService.java new file mode 100644 index 000000000..73e5d95bc --- /dev/null +++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/AsyncCommonService.java @@ -0,0 +1,9 @@ +package com.stdiet.custom.service; + +public interface AsyncCommonService { + + /** + * 异步更新每个食材对应的蛋白质、脂肪、碳水的质量 + */ + public void updateFoodNutritionalQualityAndHeat(Long customerHeatId); +} diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysIngredientService.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysIngredientService.java index ab63c4ace..0d8f10d61 100644 --- a/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysIngredientService.java +++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/ISysIngredientService.java @@ -58,4 +58,10 @@ public interface ISysIngredientService * @return 结果 */ public int deleteSysIngredientById(Long id); -} \ No newline at end of file + + /** + * 根据食材名称查询食材信息 + * @param name + * @return + */ + public SysIngredient selectSysIngredientByName(String name);} \ No newline at end of file diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/AsyncCommonServiceImpl.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/AsyncCommonServiceImpl.java new file mode 100644 index 000000000..b4a99b1d1 --- /dev/null +++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/AsyncCommonServiceImpl.java @@ -0,0 +1,102 @@ +package com.stdiet.custom.service.impl; + +import com.stdiet.common.utils.HealthyUtils; +import com.stdiet.common.utils.StringUtils; +import com.stdiet.custom.domain.SysCustomerHeatStatistics; +import com.stdiet.custom.domain.SysFoodHeatStatistics; +import com.stdiet.custom.domain.SysIngredient; +import com.stdiet.custom.mapper.SysCustomerHeatStatisticsMapper; +import com.stdiet.custom.mapper.SysFoodHeatStatisticsMapper; +import com.stdiet.custom.service.AsyncCommonService; +import com.stdiet.custom.service.ISysIngredientService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class AsyncCommonServiceImpl implements AsyncCommonService { + + @Autowired + private ISysIngredientService sysIngredientService; + + @Autowired + private SysCustomerHeatStatisticsMapper sysCustomerHeatStatisticsMapper; + + @Autowired + private SysFoodHeatStatisticsMapper sysFoodHeatStatisticsMapper; + + + /** + * 异步更新每个食材对应的蛋白质、脂肪、碳水的质量 + */ + @Override + @Async + public void updateFoodNutritionalQualityAndHeat(Long customerHeatId){ + SysCustomerHeatStatistics sysCustomerHeatStatistics = sysCustomerHeatStatisticsMapper.selectSysCustomerHeatStatisticsById(customerHeatId); + if(sysCustomerHeatStatistics != null && sysCustomerHeatStatistics.getFoodHeatStatisticsList() != null + && sysCustomerHeatStatistics.getFoodHeatStatisticsList().size() > 0){ + List foodList = sysCustomerHeatStatistics.getFoodHeatStatisticsList(); + //食材是否全部存在食材库,并且存在对应比例 + int totalProteinHeat = 0; + int totalFatHeat = 0; + int totalCarbonWaterHeat = 0; + boolean allExistFlag = true; + for (SysFoodHeatStatistics sysFoodHeatStatistics : foodList) { + //判断食材库中是否存在该食材以及该食材是否存在质量 + if(sysFoodHeatStatistics.getQuantity() == null || StringUtils.isEmpty(sysFoodHeatStatistics.getIngredient())){ + allExistFlag = false; + continue; + } + //如果该食材之前未计算出热量则从食材库中查询并查询计算 + if(sysFoodHeatStatistics.getHeatValue() == null || sysFoodHeatStatistics.getHeatValue().intValue() == 0){ + SysIngredient sysIngredient = sysIngredientService.selectSysIngredientByName(sysFoodHeatStatistics.getIngredient().trim()); + if(sysIngredient == null || sysIngredient.getProteinRatio() == null || sysIngredient.getFatRatio() == null || sysIngredient.getCarbonRatio() == null){ + allExistFlag = false; + continue; + } + Long proteinValue = Math.round(sysFoodHeatStatistics.getQuantity()*sysIngredient.getProteinRatio().doubleValue()/100); + Long fatValue = Math.round(sysFoodHeatStatistics.getQuantity()*sysIngredient.getFatRatio().doubleValue()/100); + Long carbonValue = Math.round(sysFoodHeatStatistics.getQuantity()*sysIngredient.getCarbonRatio().doubleValue()/100); + sysFoodHeatStatistics.setProteinQuality(proteinValue.intValue()); + sysFoodHeatStatistics.setFatQuality(fatValue.intValue()); + sysFoodHeatStatistics.setCarbonWaterQuality(carbonValue.intValue()); + //根据蛋白质、脂肪、碳水计算热量 + sysFoodHeatStatistics.setProteinHeat(HealthyUtils.calculateHeatByProteinQuality(proteinValue.intValue())); + sysFoodHeatStatistics.setFatHeat(HealthyUtils.calculateHeatByFatQuality(fatValue.intValue())); + sysFoodHeatStatistics.setCarbonWaterHeat(HealthyUtils.calculateHeatByCarbonWaterQuality(carbonValue.intValue())); + sysFoodHeatStatistics.setHeatValue(sysFoodHeatStatistics.getProteinHeat()+sysFoodHeatStatistics.getFatHeat()+sysFoodHeatStatistics.getCarbonWaterHeat()); + sysFoodHeatStatisticsMapper.updateSysFoodHeatStatistics(sysFoodHeatStatistics); + }else{ + //根据蛋白质、脂肪、碳水计算热量 + sysFoodHeatStatistics.setProteinHeat(HealthyUtils.calculateHeatByProteinQuality(sysFoodHeatStatistics.getProteinQuality())); + sysFoodHeatStatistics.setFatHeat(HealthyUtils.calculateHeatByFatQuality(sysFoodHeatStatistics.getFatQuality())); + sysFoodHeatStatistics.setCarbonWaterHeat(HealthyUtils.calculateHeatByCarbonWaterQuality(sysFoodHeatStatistics.getCarbonWaterQuality())); + } + totalProteinHeat += sysFoodHeatStatistics.getProteinHeat(); + totalFatHeat += sysFoodHeatStatistics.getFatHeat(); + totalCarbonWaterHeat += sysFoodHeatStatistics.getCarbonWaterHeat(); + } + //是否全部食材都存在于食材库 + if(allExistFlag){ + sysCustomerHeatStatistics.setHeatValue(totalProteinHeat + totalFatHeat + totalCarbonWaterHeat); + sysCustomerHeatStatistics.setProteinHeat(totalProteinHeat); + sysCustomerHeatStatistics.setFatHeat(totalFatHeat); + sysCustomerHeatStatistics.setCarbonWaterHeat(totalCarbonWaterHeat); + sysCustomerHeatStatistics.setHeatGap(sysCustomerHeatStatistics.getMaxHeatValue() - sysCustomerHeatStatistics.getHeatValue()); + sysCustomerHeatStatisticsMapper.updateSysCustomerHeatStatistics(sysCustomerHeatStatistics); + }else{ + //新添加的食材如果不存在食材库,需要将热量数据更新为0,需要重新计算 + if(sysCustomerHeatStatistics.getHeatValue() != null && sysCustomerHeatStatistics.getHeatValue().intValue() > 0){ + sysCustomerHeatStatistics.setHeatValue(0); + sysCustomerHeatStatistics.setProteinHeat(0); + sysCustomerHeatStatistics.setFatHeat(0); + sysCustomerHeatStatistics.setCarbonWaterHeat(0); + sysCustomerHeatStatistics.setHeatGap(sysCustomerHeatStatistics.getMaxHeatValue() - sysCustomerHeatStatistics.getHeatValue()); + sysCustomerHeatStatisticsMapper.updateSysCustomerHeatStatistics(sysCustomerHeatStatistics); + } + } + } + } +} diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysFoodHeatStatisticsServiceImpl.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysFoodHeatStatisticsServiceImpl.java index e727ae8f2..c8c56c467 100644 --- a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysFoodHeatStatisticsServiceImpl.java +++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysFoodHeatStatisticsServiceImpl.java @@ -7,18 +7,14 @@ import com.stdiet.common.utils.DateUtils; import com.stdiet.common.utils.HealthyUtils; import com.stdiet.common.utils.StringUtils; import com.stdiet.common.utils.sign.AesUtils; -import com.stdiet.custom.domain.SysCustomerHealthy; -import com.stdiet.custom.domain.SysCustomerHeatStatistics; -import com.stdiet.custom.domain.SysCustomerPhysicalSigns; +import com.stdiet.custom.domain.*; import com.stdiet.custom.dto.request.FoodHeatCalculatorRequest; -import com.stdiet.custom.service.ISysCustomerHealthyService; -import com.stdiet.custom.service.ISysCustomerHeatStatisticsService; -import com.stdiet.custom.service.ISysCustomerPhysicalSignsService; +import com.stdiet.custom.mapper.SysCustomerHeatStatisticsMapper; +import com.stdiet.custom.service.*; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import com.stdiet.custom.mapper.SysFoodHeatStatisticsMapper; -import com.stdiet.custom.domain.SysFoodHeatStatistics; -import com.stdiet.custom.service.ISysFoodHeatStatisticsService; import org.springframework.transaction.annotation.Transactional; import javax.xml.crypto.Data; @@ -37,7 +33,7 @@ public class SysFoodHeatStatisticsServiceImpl implements ISysFoodHeatStatisticsS private SysFoodHeatStatisticsMapper sysFoodHeatStatisticsMapper; @Autowired - private ISysCustomerHeatStatisticsService sysCustomerHeatStatisticsService; + private SysCustomerHeatStatisticsMapper sysCustomerHeatStatisticsMapper; @Autowired private ISysCustomerPhysicalSignsService sysCustomerPhysicalSignsService; @@ -45,6 +41,9 @@ public class SysFoodHeatStatisticsServiceImpl implements ISysFoodHeatStatisticsS @Autowired private ISysCustomerHealthyService sysCustomerHealthyService; + @Autowired + private AsyncCommonService asyncCommonService; + /** * 查询外食热量统计 * @@ -154,10 +153,10 @@ public class SysFoodHeatStatisticsServiceImpl implements ISysFoodHeatStatisticsS SysCustomerHeatStatistics sysCustomerHeatStatistics = new SysCustomerHeatStatistics(); sysCustomerHeatStatistics.setCustomerId(Long.parseLong(customerId)); sysCustomerHeatStatistics.setEdibleDate(DateUtils.parseDate(dateKey)); - SysCustomerHeatStatistics customerHeatResult = sysCustomerHeatStatisticsService.getCustomerHeatStatisticsByDate(sysCustomerHeatStatistics); + SysCustomerHeatStatistics customerHeatResult = sysCustomerHeatStatisticsMapper.getCustomerHeatStatisticsByDate(sysCustomerHeatStatistics); if(customerHeatResult == null){ sysCustomerHeatStatistics.setMaxHeatValue(maxHeatValue); - sysCustomerHeatStatisticsService.insertSysCustomerHeatStatistics(sysCustomerHeatStatistics); + sysCustomerHeatStatisticsMapper.insertSysCustomerHeatStatistics(sysCustomerHeatStatistics); }else{ sysCustomerHeatStatistics.setId(customerHeatResult.getId()); } @@ -169,6 +168,8 @@ public class SysFoodHeatStatisticsServiceImpl implements ISysFoodHeatStatisticsS map.put("quantity", map.get("quantity") != null && "".equals(map.get("quantity").toString().trim()) ? null : map.get("quantity")); } row = sysFoodHeatStatisticsMapper.insertFoodHeatBatch(dateFoodMap.get(dateKey)); + //异步查询食材对应蛋白质、脂肪、碳水热量、质量,并更新热量缺口 + asyncCommonService.updateFoodNutritionalQualityAndHeat(sysCustomerHeatStatistics.getId()); } } return row; diff --git a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysIngredientServiceImpl.java b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysIngredientServiceImpl.java index eff3414a4..2814d59bd 100644 --- a/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysIngredientServiceImpl.java +++ b/stdiet-custom/src/main/java/com/stdiet/custom/service/impl/SysIngredientServiceImpl.java @@ -145,4 +145,14 @@ public class SysIngredientServiceImpl implements ISysIngredientService { sysIngredientMapper.deleteIngredentNotRecByIngredientId(id); return sysIngredientMapper.deleteSysIngredientById(id); } + + /** + * 根据食材名称查询食材信息 + * @param name + * @return + */ + @Override + public SysIngredient selectSysIngredientByName(String name){ + return sysIngredientMapper.selectSysIngredientByName(name); + } } \ No newline at end of file diff --git a/stdiet-custom/src/main/resources/mapper/custom/SysIngredientMapper.xml b/stdiet-custom/src/main/resources/mapper/custom/SysIngredientMapper.xml index f51d79dd4..68e4ab886 100644 --- a/stdiet-custom/src/main/resources/mapper/custom/SysIngredientMapper.xml +++ b/stdiet-custom/src/main/resources/mapper/custom/SysIngredientMapper.xml @@ -207,4 +207,10 @@ + + + \ No newline at end of file diff --git a/stdiet-ui/src/views/custom/foodHeatStatistics/investigate/index.vue b/stdiet-ui/src/views/custom/foodHeatStatistics/investigate/index.vue index 4694bf971..d5f765c42 100644 --- a/stdiet-ui/src/views/custom/foodHeatStatistics/investigate/index.vue +++ b/stdiet-ui/src/views/custom/foodHeatStatistics/investigate/index.vue @@ -197,7 +197,7 @@ }, reset(){ this.form = { - edibleDate: nowDate, + edibleDate: this.form.edibleDate, ingredient: null, number: null, unit: null,