22
33import lombok .RequiredArgsConstructor ;
44import medilux .aquabe .common .error .exceptions .BadRequestException ;
5+ import medilux .aquabe .domain .compatibility .entity .CompatibilityRatio ;
6+ import medilux .aquabe .domain .compatibility .entity .ProductScorePerTypeEntity ;
7+ import medilux .aquabe .domain .compatibility .repository .ProductScorePerTypeRepository ;
58import medilux .aquabe .domain .product .entity .ProductEntity ;
69import medilux .aquabe .domain .product .entity .ProductUsedFrequencyEntity ;
710import medilux .aquabe .domain .product .repository .ProductRepository ;
@@ -36,6 +39,7 @@ public class VanityService {
3639 private final ProductRepository productRepository ;
3740 private final UserRepository userRepository ;
3841 private final ProductUsedFrequencyRepository productUsedFrequencyRepository ;
42+ private final ProductScorePerTypeRepository productScorePerTypeRepository ;
3943
4044 // 모든 화장대 제품 조회
4145 @ Transactional (readOnly = true )
@@ -59,31 +63,43 @@ public List<VanityProductsEntity> getProductsByCategory(String loginEmail, Integ
5963 // 화장대에 제품 추가
6064 @ Transactional
6165 public List <VanityProductsEntity > addProducts (String loginEmail , List <AddProductRequest > requests ) {
62- // SkinType 조회
63- UUID userId = userRepository .findUserIdByEmail (loginEmail ).orElseThrow (() -> new IllegalArgumentException ());
66+ // User ID 조회
67+ UUID userId = userRepository .findUserIdByEmail (loginEmail )
68+ .orElseThrow (() -> new IllegalArgumentException ("유효하지 않은 사용자입니다." ));
69+
6470 String skinType = skinTypeService .getSkinType (loginEmail ).getSkinType ();
6571
6672 List <VanityProductsEntity > addedProducts = new ArrayList <>();
6773
68- // 리스트에 모든 제품 저장
6974 for (AddProductRequest request : requests ) {
7075 // 제품 존재 여부 확인
7176 ProductEntity product = productRepository .findById (request .getProductId ())
7277 .orElseThrow (() -> new BadRequestException (ROW_DOES_NOT_EXIST , "존재하지 않는 제품입니다." ));
7378
74- // 화장대에 제품 추가
79+ // ✅ `ProductScorePerTypeEntity`에서 해당 product_id에 대한 정보 가져오기
80+ ProductScorePerTypeEntity productScore = productScorePerTypeRepository .findByProductAndTypeName (product , skinType )
81+ .orElseThrow (() -> new BadRequestException (ROW_DOES_NOT_EXIST , "해당 제품의 점수 데이터가 없습니다." ));
82+
83+ Integer categoryId = productScore .getCategoryId (); // ✅ 카테고리 ID 가져오기
84+ Integer compatibilityScore = productScore .getCompatibilityScore (); // ✅ 제품 궁합 점수 가져오기
85+ CompatibilityRatio compatibilityRatio = productScore .getCompatibilityRatio (); // ✅ 적합도 가져오기
86+
87+ // ✅ 화장대에 제품 추가
7588 VanityProductsEntity vanityProduct = VanityProductsEntity .builder ()
7689 .userId (userId )
7790 .product (product )
78- .compatibilityScore (request .getCompatibilityScore ())
91+ .categoryId (categoryId )
92+ .compatibilityScore (compatibilityScore ) // 자동 설정된 점수 사용
93+ .compatibilityRatio (compatibilityRatio ) // 자동 설정된 적합도 사용
7994 .build ();
95+
8096 vanityProductsRepository .save (vanityProduct );
8197
8298 // 제품 화장대 저장 횟수 업데이트
8399 updateProductFrequency (request .getProductId (), skinType );
84100
85101 // 사용자 화장대 점수 업데이트
86- updateVanityScore (userId , request . getCompatibilityScore () );
102+ updateVanityScore (userId , compatibilityScore );
87103
88104 addedProducts .add (vanityProduct );
89105 }
@@ -92,6 +108,7 @@ public List<VanityProductsEntity> addProducts(String loginEmail, List<AddProduct
92108 }
93109
94110
111+
95112 // ProductUsedFrequency 테이블에 빈도 업데이트
96113 private void updateProductFrequency (UUID productId , String skinType ) {
97114 Optional <ProductUsedFrequencyEntity > existingEntry = productUsedFrequencyRepository .findByProductIdAndTypeName (productId , skinType );
0 commit comments