Skip to content

Commit

Permalink
Merge pull request #62 from ssginc-be/feature/47
Browse files Browse the repository at this point in the history
발주 내역 조회/등록/취소 기능 구현
  • Loading branch information
Queue-ri authored Jan 23, 2025
2 parents bc4a863 + 40a2cc5 commit 532957b
Show file tree
Hide file tree
Showing 17 changed files with 2,040 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/main/java/com/ssginc/nojam/order/controller/OrderController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.ssginc.nojam.order.controller;

import com.ssginc.nojam.order.dto.OrderDTO;
import com.ssginc.nojam.order.vo.ItemVO;
import com.ssginc.nojam.order.dto.OrderListResponseDto;
import com.ssginc.nojam.order.service.OrderService;
import com.ssginc.nojam.order.vo.OrderVO;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequiredArgsConstructor
@RequestMapping("/order")
@Slf4j
public class OrderController {

private final OrderService orderService;

/**
* 발주 등록 페이지 이동
*/
@GetMapping("/items/view")
public String showOrderPage() {

return "order/register";
}

/**
* 아이템 목록 조회(API 예시)
*/
@GetMapping("/items")
public ResponseEntity<List<ItemVO>> getItems() {
return ResponseEntity.ok(orderService.getAllItems());
}

/**
* 발주 등록 페이지 이동
*/
/*@GetMapping("/register")
public String showOrderPage(Model model, HttpSession session) {
String branchId = "B00001";
model.addAttribute("item", orderService.getAllItems());
model.addAttribute("order", orderService.getCurrentOrders());
return "order/register";
}*/

/**
* 발주 등록 처리
*/





@PostMapping("/register")
public String registerOrders(@RequestBody List<OrderDTO> orders,
RedirectAttributes redirectAttributes, HttpSession session) {

List<OrderVO> orderVOs = new ArrayList<>();

for (OrderDTO order : orders) {
OrderVO orderVO = new OrderVO();
orderVO.setBranchId(session.getAttribute("branchId").toString());
orderVO.setItemId(order.getItemId());
orderVO.setQuantity(order.getQuantity());
orderVOs.add(orderVO);
System.out.println("Item ID: " + order.getItemId());
System.out.println("Quantity: " + order.getQuantity());
}


orderService.registerOrders(orderVOs);
redirectAttributes.addFlashAttribute("message", "발주가 성공적으로 등록되었습니다.");
return "redirect:/order/history/view";
}

@GetMapping("/history/view")
public String showOrderHistoryPage() {
return "order/history"; // HTML 템플릿 경로
}

/**
* 발주 내역 조회(히스토리)
* - 검색/페이징을 위해 파라미터 사용
*/
@GetMapping("/history/orderhistory")
public ResponseEntity<OrderListResponseDto> getOrders() {

List<OrderVO> result = orderService.getOrders();
int totalCount = orderService.countAllOrders();
int totalPages = (int) Math.ceil((double) totalCount / 10);

/*Map<String, Object> response = new HashMap<>();
response.put("items", result);
response.put("totalCount", result);
response.put("totalPages", result);*/

OrderListResponseDto response = new OrderListResponseDto(result, totalCount, totalPages);

return ResponseEntity.ok(response);
}

@GetMapping("/cancel/view")
public String showOrderCancelPage(){
return "/order/cancelorder";
}

/**
* 발주 취소 (체크박스에서 선택된 orderId 리스트를 받아옴)
* → status='출고대기' 주문을 실제 DB에서 DELETE
*/
@PostMapping("/history/cancel")
@ResponseBody
public ResponseEntity<String> cancelOrders(@RequestBody List<String> orderIds) {
int updatedCount = orderService.cancelOrders(orderIds);
if (updatedCount > 0) {
return ResponseEntity.ok(updatedCount + "건의 발주가 취소(삭제)되었습니다.");
} else {
return ResponseEntity.ok("취소할 수 있는 발주가 없거나 이미 다른 상태입니다.");
}
}


}
11 changes: 11 additions & 0 deletions src/main/java/com/ssginc/nojam/order/dto/OrderDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ssginc.nojam.order.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class OrderDTO {
private long itemId;
private int quantity;
}
17 changes: 17 additions & 0 deletions src/main/java/com/ssginc/nojam/order/dto/OrderListResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.ssginc.nojam.order.dto;

import com.ssginc.nojam.order.vo.OrderVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderListResponseDto {
private List<OrderVO> items;
private int totalCount;
private int totalPages;
}
33 changes: 33 additions & 0 deletions src/main/java/com/ssginc/nojam/order/mapper/OrderMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.ssginc.nojam.order.mapper;

import com.ssginc.nojam.order.vo.ItemVO;
import com.ssginc.nojam.order.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface OrderMapper {

List<ItemVO> getAllItems();

List<OrderVO> getCurrentOrders();

int insertOrder(OrderVO order);

List<OrderVO> getOrders();

int countAllOrders();

List<OrderVO> getFilteredOrders(@Param("category") String category,
@Param("value") String value,
@Param("startIdx") String startIdx);

int countFilteredOrders(@Param("category") String category,
@Param("value") String value);

// 출고대기 상태 발주 삭제 (취소)
int cancelOrders(@Param("orderIds") List<String> orderIds);

}
61 changes: 61 additions & 0 deletions src/main/java/com/ssginc/nojam/order/service/OrderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.ssginc.nojam.order.service;


import com.ssginc.nojam.order.vo.ItemVO;
import com.ssginc.nojam.order.mapper.OrderMapper;
import com.ssginc.nojam.order.vo.OrderVO;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class OrderService {

private final OrderMapper orderMapper;

public List<ItemVO> getAllItems() {
return orderMapper.getAllItems();
}

public List<OrderVO> getCurrentOrders() {
return orderMapper.getCurrentOrders();
}

public void registerOrders(List<OrderVO> orders) {
for (OrderVO order : orders) {
orderMapper.insertOrder(order);
}
}

public List<OrderVO> getOrders() { //startIdx
return orderMapper.getOrders();
}

public int countAllOrders() {
return orderMapper.countAllOrders();
}

public List<OrderVO> getFilteredOrders(String category, String value, String startIdx) {
return orderMapper.getFilteredOrders(category, value, startIdx);
}

public int countFilteredOrders(String category, String value) {
return orderMapper.countFilteredOrders(category, value);
}

/**
* 출고대기 상태 발주 취소(실제로 DB에서 삭제)
*/
public int cancelOrders(List<String> orderIds) {
if (orderIds == null || orderIds.isEmpty()) {
return 0;
}
return orderMapper.cancelOrders(orderIds);
}
//


}
20 changes: 20 additions & 0 deletions src/main/java/com/ssginc/nojam/order/vo/ItemVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.ssginc.nojam.order.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ItemVO {
private int itemId;
private String name;
private String category1;
private String category2;
private int price;
private LocalDateTime lastModifiedAt;

}
26 changes: 26 additions & 0 deletions src/main/java/com/ssginc/nojam/order/vo/OrderVO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ssginc.nojam.order.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderVO {
private String orderId; // 발주ID (PK)
private int quantity; // 수량
private String status; // 발주 상태 (출고대기, 출고중인, 출고완료, 입고완료, 취소 등)
private LocalDateTime orderTime;
private LocalDateTime outgoingStartTime;
private LocalDateTime inEndTime;
private Long itemId; // 상품ID
private String branchId; // 지점ID

// JOIN 결과용 (item 테이블)
private String name; // 상품명
private String category1; // 대분류
private String category2; // 소분류
}
Loading

0 comments on commit 532957b

Please sign in to comment.