Skip to content

Commit

Permalink
οΏ½fix: admin api (#146)
Browse files Browse the repository at this point in the history
* fix: getUserStatistics

* fix: clothingSalesCount admin dashboard

* fix: change state to selling when refund

* refactor: productService
  • Loading branch information
hyunihs authored Aug 22, 2024
1 parent 633e739 commit d60aec5
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.swagger.v3.oas.annotations.media.Schema;

public record GetClothingSalesAndProductOrderCount(
@Schema(name = "수거 μ‹ μ²­ 수") int collectionRequestCount,
@Schema(name = "수거 μ‹ μ²­ 수") int collectionRequestCount, // 수거 μ‹ μ²­ 수 = 리픽백 배솑 μ‹ μ²­ 수
@Schema(name = "결제 μ™„λ£Œ 수") int paymentCompleteCount,
@Schema(name = "λ°˜ν’ˆ μš”μ²­ 수") int returnRequestCount
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import io.swagger.v3.oas.annotations.media.Schema;

public record GetClothingSalesCount(
@Schema(name = "수거 μ‹ μ²­ 수") long collectionRequestCount,
@Schema(name = "μƒν’ˆν™” 진행 쀑 수") long productionProgressCount,
@Schema(name = "수거 μ‹ μ²­ 수") long collectionRequestCount, // 수거 μ‹ μ²­ 수 = 리픽백 배솑 μ‹ μ²­ 수
@Schema(name = "진행 μš”μ²­ 수") long collectingCount, // 진행 μš”μ²­ 수 = 리픽백/λ°•μŠ€ 수거 μ‹ μ²­ 수
@Schema(name = "μƒν’ˆν™” μ™„λ£Œ 수") long productionCompleteCount,
@Schema(name = "νŒλ§€μ€‘ 수") long sellingCount,
@Schema(name = "μ •μ‚° μš”μ²­ 수") long settlementRequestCount

) {
public static GetClothingSalesCount of(long collectionRequestCount, long productionProgressCount, long productionCompleteCount, long sellingCount, long settlementRequestCount) {
return new GetClothingSalesCount(collectionRequestCount, productionProgressCount, productionCompleteCount, sellingCount, settlementRequestCount);
public static GetClothingSalesCount of(long collectionRequestCount, long collectingCount, long productionCompleteCount, long sellingCount, long settlementRequestCount) {
return new GetClothingSalesCount(collectionRequestCount, collectingCount, productionCompleteCount, sellingCount, settlementRequestCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,24 +241,24 @@ public GetClothingSalesUser getClothingSalesUser(Long clothingSalesId){
public GetClothingSalesCount getClothingSalesCount() {
List<ClothingSalesState> clothingSalesStateList = clothingSalesStateRepository.findByCreatedDateAfter(LocalDateTime.now().minusMonths(1));
long collectionRequestCount = 0;
long productionProgressCount = 0;
long collectingCount = 0;
long productionCompleteCount = 0;
long sellingCount = 0;
long settlementRequestCount = 0; // TODO: μ •μ‚° μš”μ²­ 수 (μ •μ‚°κΈˆ 좜금 μ‹ μ²­ ν”Œλ‘œμš° κ΅¬ν˜„ ν›„ μΆ”κ°€)
for (ClothingSalesState clothingSalesState : clothingSalesStateList) {
switch (clothingSalesState.getClothingSalesStateType()) {
case BOX_COLLECT_REQUEST, BAG_COLLECT_REQUEST -> collectionRequestCount++;
case COLLECTED, SHOOTING, SHOOTED, PRODUCTING -> productionProgressCount++;
case BAG_INIT_REQUEST -> collectionRequestCount++;
case BOX_COLLECT_REQUEST, BAG_COLLECT_REQUEST -> collectingCount++;
case PRODUCTED, PRODUCT_REGISTERED -> productionCompleteCount++;
case SELLING -> sellingCount++;
}
}
return GetClothingSalesCount.of(collectionRequestCount, productionProgressCount, productionCompleteCount, sellingCount, settlementRequestCount);
return GetClothingSalesCount.of(collectionRequestCount, collectingCount, productionCompleteCount, sellingCount, settlementRequestCount);
}

public GetClothingSalesAndProductOrderCount getCountToday(){
int collectionRequestCount = clothingSalesStateRepository.countByClothingSalesStateTypeInAndCreatedDateAfter(
List.of(ClothingSalesStateType.BOX_COLLECT_REQUEST, ClothingSalesStateType.BAG_COLLECT_REQUEST),
List.of(ClothingSalesStateType.BAG_INIT_REQUEST),
LocalDate.now().atStartOfDay());
int paymentCompleteCount = productOrderRepository.countByProductOrderStateAndLastModifiedDateAfter(
ProductOrderState.PAYMENT_COMPLETED, LocalDate.now().atStartOfDay());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public void updateProductDiscountRate() {

private void handleExpiredSales(ClothingSales clothingSales) {
clothingSales.getProductList().forEach(p -> {
productService.addProductSellingState(p.getId(), ProductStateType.SELLING_END);
p.updateProductState(ProductStateType.SELLING_END);
productRepository.save(p);
productService.changeSellingState(p, ProductStateType.SELLING_END);
});
ClothingSalesState clothingSalesState = ClothingSalesState.of(clothingSales.getId(), ClothingSalesStateType.SELLING_EXPIRED);
clothingSales.updateClothingSalesState(ClothingSalesStateType.SELLING_EXPIRED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ProductOrderService {
private final IamportClient iamportClient;
private final ProductValidator productValidator;
private final ProductStateRepository productStateRepository;
private final ProductService productService;

@Transactional
public GetProductOrderPreparation prepareProductOrder(PostProductOrder postProductOrder) {
Expand Down Expand Up @@ -135,12 +136,9 @@ public Boolean validatePayment(PostPayment postPayment){
productOrders.forEach(productOrder -> {
productOrder.updateProductOrderState(ProductOrderState.PAYMENT_COMPLETED);
productCartRepository.deleteByUserIdAndProductId(productOrder.getUserId(), productOrder.getProductId());
productStateRepository.save(ProductState.of(productOrder.getProductId(), ProductStateType.SOLD_OUT));
// Product μ—”ν‹°ν‹°μ˜ productState μ—…λ°μ΄νŠΈ
Product product = productRepository.findById(productOrder.getProductId())
.orElseThrow(() -> new CustomException(INVALID_PRODUCT_ID));
product.updateProductState(ProductStateType.SOLD_OUT);
productRepository.save(product);
productService.changeSellingState(product, ProductStateType.SOLD_OUT);
});
productOrderRepository.saveAll(productOrders);

Expand Down Expand Up @@ -263,8 +261,16 @@ public Boolean registerTrackingNumber(Long productOrderId, TrackingNumberRequest
public Boolean updateProductOrderState(Long productOrderId, ProductOrderSateRequest productOrderStateRequest){
ProductOrder productOrder = productOrderRepository.findById(productOrderId)
.orElseThrow(() -> new CustomException(PRODUCT_ORDER_NOT_FOUND));
productOrder.updateProductOrderState(ProductOrderState.fromValue(productOrderStateRequest.state()));
ProductOrderState productOrderState = ProductOrderState.fromValue(productOrderStateRequest.state());
// productOrderState λ³€κ²½
productOrder.updateProductOrderState(productOrderState);
productOrderRepository.save(productOrder);
// ν™˜λΆˆλ˜μ—ˆμ„ 경우 μƒν’ˆ λ‹€μ‹œ νŒλ§€μ€‘μœΌλ‘œ λ³€κ²½
if(productOrderState == ProductOrderState.REFUND_COMPLETED){
Product product = productRepository.findById(productOrder.getProductId())
.orElseThrow(() -> new CustomException(INVALID_PRODUCT_ID));
productService.changeSellingState(product, ProductStateType.SELLING);
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.example.repick.domain.clothingSales.entity.ClothingSales;
import com.example.repick.domain.clothingSales.repository.ClothingSalesRepository;
import com.example.repick.domain.product.dto.product.*;
import com.example.repick.domain.product.dto.productClothingSales.GetKgSellProductClothingSales;
import com.example.repick.domain.product.dto.productOrder.GetProductCart;
import com.example.repick.domain.product.entity.*;
import com.example.repick.domain.product.repository.*;
Expand Down Expand Up @@ -86,11 +85,6 @@ private void addMaterials(List<String> materials, Product product) {
}
}


public void addProductSellingState(Long productId, ProductStateType productStateType) {
productStateRepository.save(ProductState.of(productId, productStateType));
}

@Transactional
public ProductResponse registerProduct(List<MultipartFile> images, PostProduct postProduct) {
User user = userRepository.findById(postProduct.userId())
Expand Down Expand Up @@ -121,7 +115,7 @@ public ProductResponse registerProduct(List<MultipartFile> images, PostProduct p
if (!postProduct.materials().isEmpty()) addMaterials(postProduct.materials(), product);

// productSellingState
addProductSellingState(product.getId(), ProductStateType.PREPARING);
productStateRepository.save(ProductState.of(product.getId(), ProductStateType.PREPARING));

return ProductResponse.fromProduct(product);

Expand All @@ -138,7 +132,7 @@ private Product handleRejectedProduct(PostProduct postProduct, User user, List<M
product.updateThumbnailImageUrl(thumbnailGeneratedUrl);

// productSellingState
addProductSellingState(product.getId(), ProductStateType.REJECTED);
productStateRepository.save(ProductState.of(product.getId(), ProductStateType.REJECTED));

return product;

Expand Down Expand Up @@ -396,8 +390,9 @@ public PageResponse<List<GetProductCart>> getCarted(PageCondition pageCondition)
}

public void changeSellingState(Product product, ProductStateType sellingState) {
addProductSellingState(product.getId(), sellingState);
productStateRepository.save(ProductState.of(product.getId(), sellingState));
product.updateProductState(sellingState);
productRepository.save(product);
}

public void calculateDiscountPriceAndPredictDiscountRateAndSave(Product product) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

public record GetUserStatistics(
@Schema(name = "총 μœ μ € 수") long totalUserCount,
@Schema(name = "μ‹ κ·œ κ°€μž… μœ μ € 수") long newUserCount
@Schema(name = "μ‹ κ·œ κ°€μž… μœ μ € 수") long newUserCount,
@Schema(name = "μ—¬μ„± μœ μ € λΉ„μœ¨") double femaleUserRatio,
@Schema(name = "남성 μœ μ € λΉ„μœ¨") double maleUserRatio
){
public static GetUserStatistics of(long totalUserCount, long newUserCount) {
return new GetUserStatistics(totalUserCount, newUserCount);
public static GetUserStatistics of(long totalUserCount, long newUserCount, double femaleUserRatio, double maleUserRatio) {
return new GetUserStatistics(totalUserCount, newUserCount, femaleUserRatio, maleUserRatio);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
import java.time.LocalDateTime;
import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {

Optional<User> findByProviderId(String providerId);

Long countByIsDeletedFalse();
Long countByIsDeletedFalseAndCreatedDateAfter(LocalDateTime createdDate);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.repick.domain.user.repository;

import com.example.repick.domain.user.dto.GetUserStatistics;


public interface UserRepositoryCustom {
GetUserStatistics getUserStatistics();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.example.repick.domain.user.repository;

import com.example.repick.domain.user.dto.GetUserStatistics;
import com.example.repick.domain.user.entity.Gender;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.time.LocalDateTime;

import static com.example.repick.domain.user.entity.QUser.user;

@RequiredArgsConstructor
public class UserRepositoryImpl implements UserRepositoryCustom{

private final JPAQueryFactory queryFactory;

@Override
public GetUserStatistics getUserStatistics() {
Tuple result = queryFactory
.select(
user.count(), // 총 μœ μ € 수
new CaseBuilder()
.when(user.createdDate.after(LocalDateTime.now().minusMonths(1))).then(1).otherwise(0).sum(), // μ‹ κ·œ μœ μ € 수
new CaseBuilder()
.when(user.gender.eq(Gender.FEMALE)).then(1).otherwise(0).sum(), // μ—¬μ„± μœ μ € 수
new CaseBuilder()
.when(user.gender.eq(Gender.MALE)).then(1).otherwise(0).sum() // 남성 μœ μ € 수
)
.from(user)
.where(user.isDeleted.isFalse())
.fetchOne();

long totalUserCount = result.get(0, Number.class).longValue();
long newUserCount = result.get(1, Number.class).longValue();
long femaleUserCount = result.get(2, Number.class).longValue();
long maleUserCount = result.get(3, Number.class).longValue();

double femaleRatio = (double) femaleUserCount / totalUserCount;
double maleRatio = (double) maleUserCount / totalUserCount;

return GetUserStatistics.of(totalUserCount, newUserCount, femaleRatio, maleRatio);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,7 @@ public GetMyPage getMyPage() {

@Transactional(readOnly = true)
public GetUserStatistics getUserStatistics() {
long totalUserCount = userRepository.countByIsDeletedFalse();
long newUserCount = userRepository.countByIsDeletedFalseAndCreatedDateAfter(LocalDateTime.now().minusMonths(1));
return GetUserStatistics.of(totalUserCount, newUserCount);
return userRepository.getUserStatistics();
}

@Transactional
Expand Down

0 comments on commit d60aec5

Please sign in to comment.