外食计算

This commit is contained in:
xiezhijun 2021-02-23 18:53:29 +08:00
parent 9de0e2bffc
commit 20b7697ddc
8 changed files with 155 additions and 13 deletions
stdiet-custom/src/main
stdiet-ui/src/views/custom/foodHeatStatistics/investigate

@ -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);
}

@ -0,0 +1,9 @@
package com.stdiet.custom.service;
public interface AsyncCommonService {
/**
* 异步更新每个食材对应的蛋白质脂肪碳水的质量
*/
public void updateFoodNutritionalQualityAndHeat(Long customerHeatId);
}

@ -58,4 +58,10 @@ public interface ISysIngredientService
* @return 结果
*/
public int deleteSysIngredientById(Long id);
}
/**
* 根据食材名称查询食材信息
* @param name
* @return
*/
public SysIngredient selectSysIngredientByName(String name);}

@ -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<SysFoodHeatStatistics> 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);
}
}
}
}
}

@ -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;

@ -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);
}
}

@ -207,4 +207,10 @@
</foreach>
</delete>
<!-- 根据食材名称查询食材 -->
<select id="selectSysIngredientByName" parameterType="String" resultMap="SysIngredientResult">
select id,name,protein_ratio,fat_ratio,carbon_ratio from sys_ingredient
where name = #{name} limit 1
</select>
</mapper>

@ -197,7 +197,7 @@
},
reset(){
this.form = {
edibleDate: nowDate,
edibleDate: this.form.edibleDate,
ingredient: null,
number: null,
unit: null,