@ -2,7 +2,10 @@ package com.mathvision.diet.controller;
import com.alibaba.fastjson2.JSON ;
import com.alibaba.fastjson2.JSON ;
import com.alibaba.fastjson2.JSONArray ;
import com.alibaba.fastjson2.JSONArray ;
import com.alibaba.fastjson2.JSONObject ;
import com.alibaba.fastjson2.TypeReference ;
import com.alibaba.fastjson2.TypeReference ;
import com.google.common.collect.Lists ;
import com.google.common.collect.Maps ;
import com.mathvision.diet.domian.RuleItemDTO ;
import com.mathvision.diet.domian.RuleItemDTO ;
import com.mathvision.diet.entity.Nutrition ;
import com.mathvision.diet.entity.Nutrition ;
import com.mathvision.diet.service.EnumService ;
import com.mathvision.diet.service.EnumService ;
@ -21,8 +24,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource ;
import javax.annotation.Resource ;
import java.math.BigDecimal ;
import java.math.BigDecimal ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Map ;
import java.util.stream.Collectors ;
@RequestMapping ( "/api/nutrition" )
@RequestMapping ( "/api/nutrition" )
@Controller
@Controller
@ -35,13 +40,19 @@ public class NutritionController extends BaseController {
@ResponseBody
@ResponseBody
@RequestMapping ( method = RequestMethod . PUT )
@RequestMapping ( method = RequestMethod . PUT )
public Nutrition add ( @RequestParam String name , @RequestParam List < Long > vendors , @RequestParam BigDecimal overflow ) {
public Nutrition add ( @RequestParam String name , @RequestParam ( required = false ) List < String > crows , @RequestParam List < Long > vendors , @RequestParam BigDecimal overflow , @RequestParam ( required = false ) String overflows ) {
Assert . isTrue ( isAdmin ( ) , "[参数错误]无操作权限!" ) ;
Assert . isTrue ( isAdmin ( ) , "[参数错误]无操作权限!" ) ;
Assert . isTrue ( StringUtils . isNotBlank ( name ) , "[参数错误]营养计划名称必填!" ) ;
Assert . isTrue ( StringUtils . isNotBlank ( name ) , "[参数错误]营养计划名称必填!" ) ;
Assert . notNull ( overflow , "[参数错误]溢出范围必填!" ) ;
Assert . notNull ( overflow , "[参数错误]溢出范围必填!" ) ;
Assert . isTrue ( CollectionUtils . isNotEmpty ( vendors ) , "[参数错误]单位列表必填!" ) ;
Assert . isTrue ( CollectionUtils . isNotEmpty ( vendors ) , "[参数错误]单位列表必填!" ) ;
Assert . isTrue ( nutritionService . notExists ( name ) , "[参数错误]营养计划名称已存在!" ) ;
Assert . isTrue ( nutritionService . notExists ( name ) , "[参数错误]营养计划名称已存在!" ) ;
return nutritionService . add ( Nutrition . builder ( ) . name ( name ) . overflow ( overflow ) . vendors ( vendors ) . build ( ) , getUid ( ) ) ;
if ( StringUtils . isBlank ( overflows ) | | ! JSON . isValidObject ( overflows ) ) {
overflows = JSONObject . toJSONString ( Maps . newHashMap ( ) ) ;
}
if ( crows = = null ) {
crows = new ArrayList < > ( ) ;
}
return nutritionService . add ( Nutrition . builder ( ) . name ( name ) . crows ( crows ) . overflow ( overflow ) . overflows ( JSON . parseObject ( overflows , new TypeReference < Map < String , BigDecimal > > ( ) { } ) ) . vendors ( vendors ) . build ( ) , getUid ( ) ) ;
}
}
@ResponseBody
@ResponseBody
@ -56,7 +67,7 @@ public class NutritionController extends BaseController {
@ResponseBody
@ResponseBody
@RequestMapping ( method = RequestMethod . POST )
@RequestMapping ( method = RequestMethod . POST )
public Nutrition update ( @RequestParam Long id , @RequestParam ( required = false ) String name , @RequestParam ( required = false ) BigDecimal overflow , @RequestParam ( required = false ) List < Long > vendors , @RequestParam ( required = false ) String foodCategoryDay , @RequestParam ( required = false ) String foodCategoryWeek , @RequestParam ( required = false ) String ingredient ) {
public Nutrition update ( @RequestParam Long id , @RequestParam ( required = false ) String name , @RequestParam ( required = false ) List < String > crows , @RequestParam ( required = false ) BigDecimal overflow , @RequestParam ( required = false ) String overflows , @RequestParam ( required = false ) List < Long > vendors , @RequestParam ( required = false ) String foodCategoryDay , @RequestParam ( required = false ) String foodCategoryWeek , @RequestParam ( required = false ) String ingredient ) {
Assert . isTrue ( isAdmin ( ) , "[参数错误]无操作权限!" ) ;
Assert . isTrue ( isAdmin ( ) , "[参数错误]无操作权限!" ) ;
Assert . isTrue ( ingredient = = null | | JSON . isValid ( ingredient ) , "[参数错误]食材JSON解析错误!" ) ;
Assert . isTrue ( ingredient = = null | | JSON . isValid ( ingredient ) , "[参数错误]食材JSON解析错误!" ) ;
@ -72,6 +83,9 @@ public class NutritionController extends BaseController {
nutrition . setOverflow ( overflow ) ;
nutrition . setOverflow ( overflow ) ;
flag = true ;
flag = true ;
}
}
if ( StringUtils . isNotBlank ( overflows ) & & JSON . isValidObject ( overflows ) ) {
nutrition . setOverflows ( JSON . parseObject ( overflows , new TypeReference < Map < String , BigDecimal > > ( ) { } ) ) ;
}
if ( CollectionUtils . isNotEmpty ( vendors ) ) {
if ( CollectionUtils . isNotEmpty ( vendors ) ) {
nutrition . setVendors ( vendors ) ;
nutrition . setVendors ( vendors ) ;
flag = true ;
flag = true ;
@ -88,6 +102,16 @@ public class NutritionController extends BaseController {
nutrition . setIngredient ( JSON . parseObject ( ingredient , new TypeReference < Map < String , Map < String , Map < String , BigDecimal > > > > ( ) { } ) ) ;
nutrition . setIngredient ( JSON . parseObject ( ingredient , new TypeReference < Map < String , Map < String , Map < String , BigDecimal > > > > ( ) { } ) ) ;
flag = true ;
flag = true ;
}
}
if ( CollectionUtils . isNotEmpty ( crows ) ) {
Nutrition finalNutrition = nutrition ;
crows = crows . stream ( ) . filter ( x - > finalNutrition . getIngredient ( ) . containsKey ( x ) ) . collect ( Collectors . toList ( ) ) ;
nutrition . setCrows ( crows ) ;
flag = true ;
} else {
if ( nutrition . getCrows ( ) = = null | | nutrition . getCrows ( ) . isEmpty ( ) ) {
nutrition . setCrows ( Lists . newArrayList ( nutrition . getIngredient ( ) . keySet ( ) ) ) ;
}
}
if ( flag ) {
if ( flag ) {
nutrition = nutritionService . update ( nutrition , getUid ( ) ) ;
nutrition = nutritionService . update ( nutrition , getUid ( ) ) ;
}
}