|
|
@ -63,6 +63,9 @@ public class ToBuysService { |
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private RecipeStuffsRepository RecipeStuffsRepository; |
|
|
|
private RecipeStuffsRepository RecipeStuffsRepository; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
|
|
private StocksService stocksService; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 購入リストに新しいアイテムを追加する |
|
|
|
* 購入リストに新しいアイテムを追加する |
|
|
|
* |
|
|
|
* |
|
|
@ -171,6 +174,16 @@ public class ToBuysService { |
|
|
|
return toBuysRepository.findByUserIdOrderByTobuyIdAsc(user.getId()); |
|
|
|
return toBuysRepository.findByUserIdOrderByTobuyIdAsc(user.getId()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 指定された購入リストIDに基づいて「数量」を変更する |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param tobuyId 購入リストID |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Transactional |
|
|
|
|
|
|
|
public int updateToBuysAmountByTobuyId(Long tobuyId, int amount) { |
|
|
|
|
|
|
|
return toBuysRepository.updateAmountByTobuyId(tobuyId, amount); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 指定された購入リストIDに基づいて「買うもの」を削除する |
|
|
|
* 指定された購入リストIDに基づいて「買うもの」を削除する |
|
|
|
* |
|
|
|
* |
|
|
@ -207,9 +220,17 @@ public class ToBuysService { |
|
|
|
stock.setBuyDate(dto.getBuyDate()); |
|
|
|
stock.setBuyDate(dto.getBuyDate()); |
|
|
|
stock.setExpDate(dto.getExpDate()); |
|
|
|
stock.setExpDate(dto.getExpDate()); |
|
|
|
|
|
|
|
|
|
|
|
// 買うものリストから削除
|
|
|
|
// まだ買うべき数量を計算
|
|
|
|
|
|
|
|
int remainAmount = Math.max(tobuy.getAmount() - dto.getAmount(), 0); |
|
|
|
|
|
|
|
System.out.println("remainAmount=" + remainAmount); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 買うものリストから削除または数量変更
|
|
|
|
System.out.println("tobuy.getTobuyId()=" + tobuy.getTobuyId()); |
|
|
|
System.out.println("tobuy.getTobuyId()=" + tobuy.getTobuyId()); |
|
|
|
deleteToBuysByTobuyId(tobuy.getTobuyId()); |
|
|
|
if (remainAmount > 0) { |
|
|
|
|
|
|
|
updateToBuysAmountByTobuyId(tobuy.getTobuyId(), remainAmount); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
deleteToBuysByTobuyId(tobuy.getTobuyId()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// データベースに保存
|
|
|
|
// データベースに保存
|
|
|
|
return stocksRepository.save(stock); |
|
|
|
return stocksRepository.save(stock); |
|
|
@ -222,7 +243,7 @@ public class ToBuysService { |
|
|
|
* @param authentication 認証情報 |
|
|
|
* @param authentication 認証情報 |
|
|
|
* @return 追加された「買うもの」のリスト |
|
|
|
* @return 追加された「買うもの」のリスト |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public List<ToBuyResponseDTO> addByRecipeId(Long recipeId, Long servings,Authentication authentication) { |
|
|
|
public List<ToBuyResponseDTO> addByRecipeId(Long recipeId, Long servings, Boolean difference, Authentication authentication) { |
|
|
|
// ユーザー情報を取得
|
|
|
|
// ユーザー情報を取得
|
|
|
|
String username = authentication.getName(); |
|
|
|
String username = authentication.getName(); |
|
|
|
User user = userRepository.findByUsername(username) |
|
|
|
User user = userRepository.findByUsername(username) |
|
|
@ -236,13 +257,23 @@ public class ToBuysService { |
|
|
|
for (RecipeStuffs rs : recipeStuffsList) { |
|
|
|
for (RecipeStuffs rs : recipeStuffsList) { |
|
|
|
Stuffs stuff = rs.getStuff(); |
|
|
|
Stuffs stuff = rs.getStuff(); |
|
|
|
|
|
|
|
|
|
|
|
// 材料の数量をサービング数に基づいて計算
|
|
|
|
// 材料の数量を在庫数とサービング数に基づいて計算
|
|
|
|
|
|
|
|
int stockAmount = stocksService.calcAmountByStuffId(user.getId(), stuff.getStuffId()); |
|
|
|
int requiredAmount = rs.getAmount() * (servings != null ? servings.intValue() : 1); |
|
|
|
int requiredAmount = rs.getAmount() * (servings != null ? servings.intValue() : 1); |
|
|
|
|
|
|
|
if (difference) { |
|
|
|
|
|
|
|
// 差分を計算
|
|
|
|
|
|
|
|
requiredAmount = requiredAmount - stockAmount; |
|
|
|
|
|
|
|
if (requiredAmount <= 0) { |
|
|
|
|
|
|
|
continue; // 在庫が十分なので追加しない
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// 差分を考慮しない場合はrequiredAmountをそのまま使用
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Optional<ToBuys> existingToBuyOpt = toBuysRepository.findByUserAndStuff(user, stuff); |
|
|
|
Optional<ToBuys> existingToBuyOpt = toBuysRepository.findByUserAndStuff(user, stuff); |
|
|
|
|
|
|
|
|
|
|
|
ToBuys toBuy; |
|
|
|
ToBuys toBuy; |
|
|
|
if (existingToBuyOpt.isPresent()) { |
|
|
|
if (existingToBuyOpt.isPresent()) { // 既存の「買うもの」がある場合
|
|
|
|
toBuy = existingToBuyOpt.get(); |
|
|
|
toBuy = existingToBuyOpt.get(); |
|
|
|
toBuy.setAmount(toBuy.getAmount() + requiredAmount); // 既存の数量を更新
|
|
|
|
toBuy.setAmount(toBuy.getAmount() + requiredAmount); // 既存の数量を更新
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|