Skip to content

Commit

Permalink
Merge pull request #119 from Yanol-Market/feature/14-negoFinalRefactor
Browse files Browse the repository at this point in the history
Feature/14 nego final refactor
  • Loading branch information
WestSilver99 authored Jan 18, 2024
2 parents 8e302d2 + abe8c55 commit 228ac74
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public record PriceProposeRequest(
public Integer price() {
return price;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ public interface NegoRepository extends JpaRepository<Nego, Long> {

Optional<Nego> findByUserAndProduct(User user, Product product);

Nego findByProduct(Product product);

List<Nego> findByProductAndStatus(Product product, NegotiationStatus negotiationStatus);

Boolean existsByUser_IdAndProduct_Id(Long userId, Long productId);

List<Nego> findAllByUser_IdAndProduct_Id(Long userId, Long productId);

List<Nego> findAllByProduct(Product product);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,31 @@ public void changeStatus() {
LocalDateTime currentTime = LocalDateTime.now();

List<Nego> pendingNegos = negoRepository.findByStatus(PAYMENT_PENDING);
// List<Nego> completedNegos = negoRepository.findByStatus(NEGOTIATION_COMPLETED);
List<Nego> transferNegos = negoRepository.findByStatus(TRANSFER_PENDING);
//List<Nego> transferNegos = negoRepository.findByStatus(TRANSFER_PENDING);

for (Nego nego : pendingNegos) {
Product product = productService.getProduct(nego.getProductId());
LocalDateTime updatedAt = nego.getUpdatedAt();
if (updatedAt != null && currentTime.isAfter(updatedAt.plusSeconds(10))) {
if (updatedAt != null && currentTime.isAfter(updatedAt.plusSeconds(30))) {
productService.updateProductForNego(product);
product.setProductStatus(ProductStatus.SELLING);
productService.updateProductForNego(product);
nego.setStatus(NEGOTIATION_TIMEOUT);
nego.setUpdatedAt(currentTime);
}
}

for(Nego transferNego : transferNegos){
Product product = productService.getProduct(transferNego.getProductId());
LocalDateTime updatedAt = transferNego.getUpdatedAt();
if (updatedAt != null && currentTime.isAfter(updatedAt.plusSeconds(10))) {
transferNego.setStatus(NEGOTIATION_COMPLETED);
transferNego.setUpdatedAt(currentTime);
product.setProductStatus(ProductStatus.SOLD_OUT);
productService.updateProductForNego(product);
}
}

// for (Nego completedNego : completedNegos){
// Long productId = completedNego.getProductId();
// List<Nego> relatedNegos = negoRepository.findByProductIdAndStatusNot(productId, NEGOTIATION_COMPLETED);
// for (Nego relatedNego : relatedNegos) {
// relatedNego.setStatus(NEGOTIATION_COMPLETED);
} //상품 상태 판매중

// for (Nego transferNego : transferNegos) {
// Product product = productService.getProduct(transferNego.getProductId());
// LocalDateTime updatedAt = transferNego.getUpdatedAt();
// if (updatedAt != null && currentTime.isAfter(updatedAt.plusSeconds(30))) {
// transferNego.setStatus(NEGOTIATION_COMPLETED);
// transferNego.setUpdatedAt(currentTime);
// product.setProductStatus(ProductStatus.SOLD_OUT);
// productService.updateProductForNego(product);
// }
// }
// negoRepository.saveAll(completedNegos);

negoRepository.saveAll(transferNegos);
// } // 3시간 뒤 자동양도
//negoRepository.saveAll(transferNegos);
negoRepository.saveAll(pendingNegos);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package site.goldenticket.domain.nego.service;

import site.goldenticket.domain.nego.dto.request.PriceProposeRequest;
import site.goldenticket.domain.nego.dto.response.HandoverResponse;
import site.goldenticket.domain.nego.dto.response.NegoAvailableResponse;
import site.goldenticket.domain.nego.dto.response.NegoResponse;
import site.goldenticket.domain.nego.dto.response.PayResponse;
import site.goldenticket.domain.nego.dto.response.PriceProposeResponse;
import site.goldenticket.domain.nego.dto.response.*;
import site.goldenticket.domain.nego.entity.Nego;
import site.goldenticket.domain.security.PrincipalDetails;

Expand All @@ -14,22 +10,19 @@
public interface NegoService {

// 판매자 입장
NegoResponse confirmPrice(Long negoId,PrincipalDetails principalDetails); // 가격승낙
NegoResponse denyPrice(Long negoId,PrincipalDetails principalDetails); // 거절하기
NegoResponse confirmPrice(Long negoId, PrincipalDetails principalDetails); // 가격승낙

NegoResponse denyPrice(Long negoId, PrincipalDetails principalDetails); // 거절하기

// 구매자 입장
PriceProposeResponse proposePrice(Long productId, PriceProposeRequest request, PrincipalDetails principalDetails);
PayResponse pay(Long negoId,PrincipalDetails principalDetails); // 결제하기

//PayResponse payOriginPrice(Long negoId,PrincipalDetails principalDetails); //원래 가격으로 결제하기
PayResponse pay(Long negoId, PrincipalDetails principalDetails); // 결제하기

HandoverResponse handOverProduct(Long negoId, PrincipalDetails principalDetails);

NegoResponse denyHandoverProduct(Long negoId, PrincipalDetails principalDetails);

void updateNegotiationStatusForCompletedProduct(Long productId);


Optional<Nego> getNego(Long userId, Long productId);

Nego save(Nego nego);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import java.util.NoSuchElementException;
import java.util.Optional;


// 네고 종료는 실패값만 유지 + 상품 상태 확인해서 예외처리 + 네고에서만 확인할거면 enum 추가해야됨
// 상품만료


@Service
@RequiredArgsConstructor
public class NegoServiceImpl implements NegoService {
Expand All @@ -41,9 +46,12 @@ public NegoResponse confirmPrice(Long negoId, PrincipalDetails principalDetails)
Nego nego = negoRepository.findById(negoId)
.orElseThrow(() -> new NoSuchElementException("Nego not found with id: " + negoId));

Product product = productService.getProduct(nego.getProductId());

if (nego.getStatus() == NegotiationStatus.NEGOTIATING) {
nego.confirmNego(LocalDateTime.now(), NegotiationStatus.PAYMENT_PENDING, LocalDateTime.now().plusMinutes(20), Boolean.TRUE);
product.setProductStatus(ProductStatus.RESERVED);
productService.updateProductForNego(product);
negoRepository.save(nego);
} else {
// 다른 상태의 네고는 가격 승낙을 처리할 수 없음
Expand All @@ -53,7 +61,7 @@ public NegoResponse confirmPrice(Long negoId, PrincipalDetails principalDetails)
throw new CustomException("네고를 승인할수 없습니다.", ErrorCode.CANNOT_CONFIRM_NEGO);
}
return NegoResponse.fromEntity(nego);
}
} // 여기다가 상품 상태 예약중 코드 추가해야됨 ㅜ


@Override
Expand Down Expand Up @@ -88,30 +96,36 @@ public PriceProposeResponse proposePrice(Long productId, PriceProposeRequest req
.orElseThrow(() -> new NoSuchElementException("Nego not found with id: " + userId));

Product product = productService.getProduct(productId);
Nego nego = negoRepository.findByProduct(product);

List<Nego> allNegosForProduct = negoRepository.findAllByProduct(product);
for (Nego nego : allNegosForProduct) {
if (nego != null && nego.getProduct().getProductStatus() == ProductStatus.SOLD_OUT) {
throw new CustomException("다른 유저가 네고를 성공해 제안할수 없습니다.", ErrorCode.NEGO_COMPLETED);
}
if (nego != null && nego.getStatus() == NegotiationStatus.TRANSFER_PENDING) {
throw new CustomException("다른 유저가 네고를 성공해 제안할수 없습니다.", ErrorCode.NEGO_COMPLETED);
} //OK

if (nego != null && nego.getStatus() == NegotiationStatus.PAYMENT_PENDING) {
throw new CustomException("승인된 네고는 가격 제안을 할 수 없습니다.", ErrorCode.NEGO_ALREADY_APPROVED);
} //OK
}

// 사용자별 상품에 대한 네고 조회
Nego userNego = negoRepository.findByUserAndProduct(user, product)
.orElse(new Nego(user, product)); // 네고가 없으면 새로 생성

if (nego != null && nego.getStatus() == NegotiationStatus.NEGOTIATION_COMPLETED) {
throw new CustomException("다른 유저가 네고를 성공해 제안할수 없습니다.", ErrorCode.NEGO_COMPLETED);
}
if (nego != null && nego.getStatus() == NegotiationStatus.TRANSFER_PENDING) {
if (userNego.getProduct().getProductStatus() == ProductStatus.SOLD_OUT) {
throw new CustomException("다른 유저가 네고를 성공해 제안할수 없습니다.", ErrorCode.NEGO_COMPLETED);
}

if (nego != null && nego.getStatus() == NegotiationStatus.PAYMENT_PENDING) {
throw new CustomException("승인된 네고는 가격 제안을 할 수 없습니다.", ErrorCode.NEGO_ALREADY_APPROVED);
}

if (userNego.getStatus() == NegotiationStatus.NEGOTIATION_CANCELLED) {
throw new CustomException("취소된 네고는 가격 제안을 할 수 없습니다.", ErrorCode.NEGO_ALREADY_APPROVED);
}
throw new CustomException("취소된 네고는 가격 제안을 할 수 없습니다.", ErrorCode.CANNOT_NEGOTIATE);
} //OK

if (userNego.getStatus() == NegotiationStatus.NEGOTIATION_TIMEOUT) {
throw new CustomException("20분이 지나 제안할수 없습니다.", ErrorCode.CANNOT_PROPOSE_NEGO);
}
} //OK

// count 증가
int newCount = userNego.getCount() + 1;
Expand Down Expand Up @@ -141,12 +155,16 @@ public PayResponse pay(Long negoId, PrincipalDetails principalDetails) {
Nego nego = negoRepository.findById(negoId)
.orElseThrow(() -> new NoSuchElementException("해당 ID의 네고를 찾을 수 없습니다: " + negoId));

Product product = productService.getProduct(nego.getProductId());

if (nego.getStatus() == NegotiationStatus.NEGOTIATION_COMPLETED || nego.getStatus() == NegotiationStatus.TRANSFER_PENDING) {
throw new CustomException("다른 유저가 네고를 성공해 제안할수 없습니다.", ErrorCode.NEGO_COMPLETED);
}


if (nego.getConsent()) {
product.setProductStatus(ProductStatus.RESERVED);
productService.updateProductForNego(product);
nego.setStatus(NegotiationStatus.TRANSFER_PENDING);
nego.setUpdatedAt(LocalDateTime.now());
negoRepository.save(nego);
Expand Down Expand Up @@ -202,12 +220,23 @@ public HandoverResponse handOverProduct(Long negoId, PrincipalDetails principalD
// Nego의 Product ID로 Product 정보 가져오기
Product product = productService.getProduct(nego.getProductId());

// 상태가 결제 완료인 경우에만 양도 가능
// 상태가 양도 대기인 경우에만 양도 가능
List<Nego> allNegosForProduct = negoRepository.findAllByProduct(product);

// 상태가 양도 대기인 경우에만 양도 가능
if (nego.getStatus() == NegotiationStatus.TRANSFER_PENDING) {
nego.setUpdatedAt(LocalDateTime.now());
nego.setStatus(NegotiationStatus.NEGOTIATION_COMPLETED);
product.setProductStatus(ProductStatus.SOLD_OUT);
negoRepository.save(nego);
productService.updateProductForNego(product);

// 해당 Product에 대한 모든 네고 상태를 변경
for (Nego otherNego : allNegosForProduct) {
// 다른 네고는 양도 완료 상태로 변경
otherNego.setStatus(NegotiationStatus.NEGOTIATION_COMPLETED);
negoRepository.save(otherNego);
}

// 양도 작업이 완료된 경우에는 양도 정보와 함께 반환
return HandoverResponse.fromEntity(product, nego);
} else {
Expand All @@ -226,30 +255,17 @@ public NegoResponse denyHandoverProduct(Long negoId, PrincipalDetails principalD
Nego nego = negoRepository.findById(negoId)
.orElseThrow(() -> new NoSuchElementException("Nego not found with id: " + negoId));

// 상태가 결제 완료인 경우에만 양도 가능
if (nego.getStatus() == NegotiationStatus.TRANSFER_PENDING) {
nego.setConsent(Boolean.FALSE);
nego.setExpirationTime(LocalDateTime.now());
nego.setStatus(NegotiationStatus.NEGOTIATION_CANCELLED);
nego.setStatus(NegotiationStatus.NEGOTIATION_COMPLETED);
negoRepository.save(nego);
return NegoResponse.fromEntity(nego);
} else {
throw new CustomException("Nego not in completed status.", ErrorCode.NEGO_NOT_COMPLETED);
}
}

// 수빈아 이거 쓰면 돼!!
@Override
public void updateNegotiationStatusForCompletedProduct(Long productId) {

Product product = productService.getProduct(productId);

List<Nego> completedNegos = negoRepository.findByProductAndStatus(product, NegotiationStatus.NEGOTIATION_COMPLETED);
for (Nego nego : completedNegos) {
nego.setStatus(NegotiationStatus.NEGOTIATION_COMPLETED);
}
negoRepository.saveAll(completedNegos);
}


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ public interface ProductRepository extends JpaRepository<Product, Long>, Product
List<Product> findTop5DayUseProductsCheckInDateAsc(Pageable pageable);

List<Product> findAllByUserId(Long userId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import site.goldenticket.common.redis.service.RedisService;
import site.goldenticket.domain.product.constants.AreaCode;
import site.goldenticket.domain.product.constants.PriceRange;
import site.goldenticket.domain.product.constants.ProductStatus;
import site.goldenticket.domain.product.dto.HomeProductResponse;
import site.goldenticket.domain.product.dto.ProductDetailResponse;
import site.goldenticket.domain.product.dto.ProductRequest;
Expand Down

0 comments on commit 228ac74

Please sign in to comment.