-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] 구매 관리 api 구현 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/com/sampoom/purchase/api/purchase/controller/PurchaseController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package com.sampoom.purchase.api.purchase.controller; | ||
|
|
||
| import com.sampoom.purchase.api.purchase.dto.PurchaseOrderRequestDto; | ||
| import com.sampoom.purchase.api.purchase.dto.PurchaseOrderResponseDto; | ||
| import com.sampoom.purchase.api.purchase.entity.OrderStatus; | ||
| import com.sampoom.purchase.api.purchase.entity.UrgencyLevel; | ||
| import com.sampoom.purchase.api.purchase.service.PurchaseService; | ||
| import com.sampoom.purchase.common.response.ApiResponse; | ||
| import com.sampoom.purchase.common.response.PageResponseDto; | ||
| import com.sampoom.purchase.common.response.SuccessStatus; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @Tag(name = "Purchase", description = "Purchase 관련 API 입니다.") | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class PurchaseController { | ||
|
|
||
| private final PurchaseService purchaseService; | ||
|
|
||
| @Operation(summary = "자재 주문 생성", description = "공장에 필요한 자재 주문을 생성합니다.") | ||
| @PostMapping() | ||
| public ResponseEntity<ApiResponse<PurchaseOrderResponseDto>> createMaterialOrder( | ||
| @RequestBody PurchaseOrderRequestDto requestDto) { | ||
| return ApiResponse.success(SuccessStatus.CREATED, | ||
| purchaseService.createMaterialOrder(requestDto)); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| @Operation(summary = "자재 주문 취소", description = "주문을 취소 처리합니다.") | ||
| @PatchMapping("/{orderId}/cancel") | ||
| public ResponseEntity<ApiResponse<PurchaseOrderResponseDto>> cancelOrder( | ||
| @PathVariable Long orderId) { | ||
| return ApiResponse.success(SuccessStatus.OK, purchaseService.cancelOrder(orderId)); | ||
| } | ||
|
|
||
| @Operation(summary = "자재 주문 삭제", description = "주문을 삭제합니다(소프트 삭제).") | ||
| @DeleteMapping("/{orderId}") | ||
| public ResponseEntity<ApiResponse<Void>> deleteOrder( | ||
| @PathVariable Long orderId) { | ||
| purchaseService.deleteOrder(orderId); | ||
| return ApiResponse.success_only(SuccessStatus.OK); | ||
| } | ||
|
|
||
| @Operation(summary = "자재 주문 단건 조회", description = "특정 자재 주문의 상세를 조회합니다.") | ||
| @GetMapping("/{orderId}") | ||
| public ResponseEntity<ApiResponse<PurchaseOrderResponseDto>> getOrder( | ||
| @PathVariable Long orderId) { | ||
| return ApiResponse.success(SuccessStatus.OK, purchaseService.getOrder(orderId)); | ||
| } | ||
|
|
||
| @Operation(summary = "자재 주문 목록 조회", description = "주문 상태 필터와 검색(자재명/자재코드/주문코드), 긴급도 필터로 목록을 조회합니다.") | ||
| @GetMapping() | ||
| public ResponseEntity<ApiResponse<PageResponseDto<PurchaseOrderResponseDto>>> getOrders( | ||
| @RequestParam(required = false) OrderStatus status, | ||
| @RequestParam(required = false) UrgencyLevel urgency, | ||
| @RequestParam(required = false) String query, | ||
| @RequestParam(defaultValue = "0") int page, | ||
| @RequestParam(defaultValue = "10") int size) { | ||
| return ApiResponse.success(SuccessStatus.OK, purchaseService.getOrders(status, urgency, query, page, size)); | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
src/main/java/com/sampoom/purchase/api/purchase/dto/PurchaseOrderItemDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.sampoom.purchase.api.purchase.dto; | ||
|
|
||
| import com.sampoom.purchase.api.purchase.entity.PurchaseOrderItem; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Builder | ||
| public class PurchaseOrderItemDto { | ||
| private String materialCode; | ||
| private String materialName; | ||
| private String unit; | ||
| private Long quantity; | ||
| private BigDecimal unitPrice; | ||
|
|
||
| public static PurchaseOrderItemDto from(PurchaseOrderItem item) { | ||
| return PurchaseOrderItemDto.builder() | ||
| .materialCode(item.getMaterialCode()) | ||
| .materialName(item.getMaterialName()) | ||
| .unit(item.getUnit()) | ||
| .quantity(item.getQuantity()) | ||
| .unitPrice(item.getUnitPrice()) | ||
| .build(); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/sampoom/purchase/api/purchase/dto/PurchaseOrderRequestDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.sampoom.purchase.api.purchase.dto; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.springframework.format.annotation.DateTimeFormat; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class PurchaseOrderRequestDto { | ||
| private Long factoryId; | ||
| private String factoryName; | ||
|
|
||
| @JsonFormat(pattern = "yyyy-MM-dd") | ||
| private LocalDate requiredAt; | ||
|
|
||
| private String requesterName; // 요청자 이름 추가 | ||
|
|
||
| private List<PurchaseOrderItemDto> items; | ||
| } |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/sampoom/purchase/api/purchase/dto/PurchaseOrderResponseDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package com.sampoom.purchase.api.purchase.dto; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonFormat; | ||
| import com.sampoom.purchase.api.purchase.entity.OrderStatus; | ||
| import com.sampoom.purchase.api.purchase.entity.PurchaseOrder; | ||
| import com.sampoom.purchase.api.purchase.entity.PurchaseOrderItem; | ||
| import com.sampoom.purchase.api.purchase.entity.UrgencyLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Builder | ||
| public class PurchaseOrderResponseDto { | ||
| private Long id; | ||
| private String orderCode; | ||
| private LocalDateTime orderAt; | ||
|
|
||
| @JsonFormat(pattern = "yyyy-MM-dd") | ||
| private LocalDate requiredAt; // 날짜만 | ||
|
|
||
| private Long factoryId; | ||
| private String factoryName; | ||
| private String requesterName; | ||
| private UrgencyLevel urgency; | ||
| private BigDecimal expectedAmount; | ||
| private OrderStatus status; | ||
| private List<PurchaseOrderItemDto> items; | ||
|
|
||
| public static PurchaseOrderResponseDto from(PurchaseOrder order, List<PurchaseOrderItem> orderItems) { | ||
| return PurchaseOrderResponseDto.builder() | ||
| .id(order.getId()) | ||
| .status(order.getStatus()) | ||
| .orderCode(order.getCode()) | ||
| .orderAt(order.getOrderAt()) | ||
| .requiredAt(order.getRequiredAt()) | ||
| .factoryId(order.getFactoryId()) | ||
| .factoryName(order.getFactoryName()) | ||
| .requesterName(order.getRequesterName()) | ||
| .urgency(order.getUrgency()) | ||
| .expectedAmount(order.getExpectedAmount()) | ||
| .items(orderItems.stream().map(PurchaseOrderItemDto::from).collect(Collectors.toList())) | ||
| .build(); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/sampoom/purchase/api/purchase/entity/OrderStatus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.sampoom.purchase.api.purchase.entity; | ||
|
|
||
|
|
||
| public enum OrderStatus { | ||
| ORDERED, // 주문됨 | ||
| RECEIVED, // 입고됨 | ||
| CANCELED // 발주 취소됨 | ||
| } |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/sampoom/purchase/api/purchase/entity/PurchaseOrder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package com.sampoom.purchase.api.purchase.entity; | ||
|
|
||
| import com.sampoom.purchase.common.entitiy.SoftDeleteEntity; | ||
| import com.sampoom.purchase.common.exception.BadRequestException; | ||
| import com.sampoom.purchase.common.response.ErrorStatus; | ||
| import jakarta.persistence.*; | ||
| import lombok.*; | ||
| import org.hibernate.annotations.SQLDelete; | ||
| import org.hibernate.annotations.SQLRestriction; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Table(name = "purchase_order") | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @AllArgsConstructor | ||
| @Builder | ||
| @SQLDelete(sql = "UPDATE purchase_order SET deleted = true, deleted_at = now() WHERE purchase_order_id = ?") | ||
| @SQLRestriction("deleted = false") | ||
| public class PurchaseOrder extends SoftDeleteEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "purchase_order_id") | ||
| private Long id; | ||
|
|
||
| private String code; | ||
| private LocalDateTime orderAt; | ||
| private LocalDateTime receivedAt; | ||
| private LocalDateTime canceledAt; | ||
| private LocalDate requiredAt; | ||
|
|
||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| private OrderStatus status; | ||
|
|
||
|
|
||
| private Long factoryId; | ||
|
|
||
| private String factoryName; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| private UrgencyLevel urgency; // 긴급도 | ||
|
|
||
| private String requesterName; // 요청자 이름 | ||
|
|
||
| @Column(precision = 19, scale = 2) | ||
| private BigDecimal expectedAmount; // 예상 금액 | ||
|
|
||
| public void receive() { | ||
| if (this.status != OrderStatus.ORDERED) { | ||
| throw new BadRequestException(ErrorStatus.ORDER_ALREADY_PROCESSED); | ||
| } | ||
| this.status = OrderStatus.RECEIVED; | ||
| this.receivedAt = LocalDateTime.now(); | ||
| } | ||
|
|
||
| public void cancel() { | ||
|
|
||
| if (this.status != OrderStatus.ORDERED) { | ||
| throw new BadRequestException(ErrorStatus.ORDER_ALREADY_PROCESSED); | ||
| } | ||
| this.status = OrderStatus.CANCELED; | ||
| this.canceledAt = LocalDateTime.now(); | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/sampoom/purchase/api/purchase/entity/PurchaseOrderItem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.sampoom.purchase.api.purchase.entity; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.*; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Table(name = "purchase_order_item") | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @AllArgsConstructor | ||
| @Builder | ||
| public class PurchaseOrderItem { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "purchase_order_item_id") | ||
| private Long id; | ||
|
|
||
| private Long quantity; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "purchase_order_id") | ||
| private PurchaseOrder purchaseOrder; | ||
|
|
||
| private String materialCode; | ||
|
|
||
| private String materialName; | ||
|
|
||
| private String unit; | ||
|
|
||
| @Column(precision = 19, scale = 2) | ||
| private BigDecimal unitPrice; | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/sampoom/purchase/api/purchase/entity/UrgencyLevel.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.sampoom.purchase.api.purchase.entity; | ||
|
|
||
| public enum UrgencyLevel { | ||
| HIGH, | ||
| MEDIUM, | ||
| LOW | ||
| } | ||
|
|
11 changes: 11 additions & 0 deletions
11
src/main/java/com/sampoom/purchase/api/purchase/repository/PurchaseOrderItemRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.sampoom.purchase.api.purchase.repository; | ||
|
|
||
| import com.sampoom.purchase.api.purchase.entity.PurchaseOrderItem; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface PurchaseOrderItemRepository extends JpaRepository<PurchaseOrderItem, Long> { | ||
| List<PurchaseOrderItem> findByPurchaseOrderId(Long purchaseOrderId); | ||
| List<PurchaseOrderItem> findByPurchaseOrderIdIn(List<Long> orderIds); | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/sampoom/purchase/api/purchase/repository/PurchaseOrderRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.sampoom.purchase.api.purchase.repository; | ||
|
|
||
| import com.sampoom.purchase.api.purchase.entity.OrderStatus; | ||
| import com.sampoom.purchase.api.purchase.entity.PurchaseOrder; | ||
| import com.sampoom.purchase.api.purchase.entity.UrgencyLevel; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface PurchaseOrderRepository extends JpaRepository<PurchaseOrder, Long> { | ||
|
|
||
| @Query("select distinct po from PurchaseOrder po left join PurchaseOrderItem i on i.purchaseOrder = po " + | ||
| "where (:status is null or po.status = :status) " + | ||
| "and (:urgency is null or po.urgency = :urgency) " + | ||
| "and (:query is null or :query = '' or lower(po.code) like lower(concat('%', :query, '%')) " + | ||
| "or lower(i.materialCode) like lower(concat('%', :query, '%')) " + | ||
| "or lower(i.materialName) like lower(concat('%', :query, '%')))" ) | ||
| Page<PurchaseOrder> search(@Param("status") OrderStatus status, | ||
| @Param("urgency") UrgencyLevel urgency, | ||
| @Param("query") String query, | ||
| Pageable pageable); | ||
|
|
||
| Optional<PurchaseOrder> findTopByCodeStartingWithOrderByCodeDesc(String prefix); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.