营养信息添加食材编码校验

This commit is contained in:
jjLv 2025-06-10 18:24:10 +08:00
parent 60435f52a2
commit b71047cc11
3 changed files with 30 additions and 6 deletions

View File

@ -91,4 +91,11 @@ public interface CookNutritionMapper {
* @return 食材营养信息
*/
NutritionEntity getNutritionEntity(CookDishesMaterial i);
/**
* 检测营养类别编码是否存在
* @param nutritionCode 营养类别编码
* @param nutritionId 营养类别ID
* @return 营养类别信息
*/
int checkIsExistByCode(@Param("nutritionCode") String nutritionCode,@Param("nutritionId") Long nutritionId);
}

View File

@ -60,11 +60,17 @@ public class CookNutritionServiceImpl implements ICookNutritionService {
if (StringUtils.isBlank(cookNutrition.getNutritionName())){
throw new ServiceException("食材名称不能为空!");
}
if (StringUtils.isBlank(cookNutrition.getNutritionCode())){
throw new ServiceException("营养信息名称不能为空!");
}
if (StringUtils.isNull(cookNutrition.getNutritionTypeId()) || cookNutrition.getNutritionTypeId() == 0){
throw new ServiceException("食材类别不能为空!");
throw new ServiceException("营养信息类别不能为空!");
}
if (cookNutritionMapper.checkIsExistByName(cookNutrition.getNutritionName(),null) > 0) {
throw new ServiceException("该食材营养基础信息已存在!");
throw new ServiceException("该营养信息名称已存在!");
}
if(cookNutritionMapper.checkIsExistByCode(cookNutrition.getNutritionCode(), null) > 0) {
throw new ServiceException("该食材编码已存在!");
}
return cookNutritionMapper.insertCookNutrition(cookNutrition);
} catch (Exception e) {
@ -83,16 +89,22 @@ public class CookNutritionServiceImpl implements ICookNutritionService {
cookNutrition.setUpdateTime(DateUtils.getNowDate());
try {
if (StringUtils.isNull(cookNutrition.getNutritionId())){
throw new ServiceException("未携带食营养基础信息ID");
throw new ServiceException("未携带食营养信息ID");
}
if (StringUtils.isBlank(cookNutrition.getNutritionName())){
throw new ServiceException("食材名称不能为空!");
throw new ServiceException("营养信息名称不能为空!");
}
if (StringUtils.isBlank(cookNutrition.getNutritionCode())){
throw new ServiceException("食材编码不能为空!");
}
if (StringUtils.isNull(cookNutrition.getNutritionTypeId()) || cookNutrition.getNutritionTypeId() == 0){
throw new ServiceException("食材类别不能为空!");
throw new ServiceException("营养信息类别不能为空!");
}
if (cookNutritionMapper.checkIsExistByName(cookNutrition.getNutritionName(),cookNutrition.getNutritionId()) > 0) {
throw new ServiceException("该食材营养基础信息已存在!");
throw new ServiceException("该营养信息名称已存在!");
}
if(cookNutritionMapper.checkIsExistByCode(cookNutrition.getNutritionCode(), cookNutrition.getNutritionId()) > 0) {
throw new ServiceException("该食材编码已存在!");
}
return cookNutritionMapper.updateCookNutrition(cookNutrition);
} catch (Exception e) {

View File

@ -478,4 +478,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{nutritionId}
</foreach>
</delete>
<select id="checkIsExistByCode" resultType="int">
select count(1) from cook_nutrition where nutrition_code = #{nutritionCode}
<if test="nutritionId != null"> and nutrition_id != #{nutritionId}</if>
</select>
</mapper>