Skip to content

Commit

Permalink
Merge branch 'develop' into PBS-106-CRUD-Assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanhTriIsCoding authored Nov 9, 2024
2 parents 86aae1d + 2e43143 commit cca0665
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
Expand Down Expand Up @@ -201,7 +202,12 @@ else if (account.getRole().equals(AccountRole.Manager)) {
.build();
}
else {
throw new AppException(ErrorCode.UNAUTHORIZED);
List<AccountOrderResponse> list = new ArrayList<>();
list.add(new AccountOrderResponse(account.getId(), account.getName(), account.getEmail(), account.getAvatar(), account.getRole(), account.getBuildingNumber(), account.getRankingName()));
return ApiResponse.<List<AccountOrderResponse>>builder()
.message("Không có quyền truy cập")
.data(list)
.build();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public interface AssignmentRepository extends JpaRepository<Assignment, String>
@Query("SELECT a.staffId FROM Assignment a WHERE a.weekDate = :weekDate AND a.slot = :slot")
List<String> findStaffIdsByWeekDateAndSlot(@Param("weekDate") String weekDate, @Param("slot") String slot);

@Query("SELECT a.staffId " +
"FROM Assignment a " +
"JOIN Account ac ON ac.id = a.staffId " +
"WHERE a.slot = :slot " +
"AND a.weekDate = :weekDate " +
"AND ac.buildingNumber = :buildingId")
String findStaffForMatchingOrder(@Param("slot") String slot,
@Param("weekDate") String weekDate,
@Param("buildingId") Integer buildingId);


@Query("SELECT a FROM Assignment a " +
"JOIN Account acc ON a.staffId = acc.id " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@
public interface RoomTypeRepository extends JpaRepository<RoomType, Integer> {
@Query("SELECT rt FROM RoomType rt " +
"JOIN rt.building b " +
"LEFT JOIN Room r ON r.roomType.id = rt.id " +
"LEFT JOIN OrderDetail od ON r.id = od.room.id " +
"WHERE (:address IS NULL OR b.address LIKE %:address%) " +
"AND b.status = com.swp.PodBookingSystem.enums.BuildingStatus.Active " +
"AND (:capacity IS NULL OR rt.capacity = :capacity) " +
"AND ((:startTime IS NULL AND :endTime IS NULL) " +
" OR NOT EXISTS (SELECT 1 FROM OrderDetail od2 " +
" WHERE od2.room.id = r.id " +
" AND ((od2.startTime BETWEEN :startTime AND :endTime) " +
" OR (od2.endTime BETWEEN :startTime AND :endTime)))) " +
"GROUP BY rt.id " +
"HAVING (:startTime IS NOT NULL AND :endTime IS NOT NULL AND COUNT(r.id) >= 1) " +
" OR (:startTime IS NULL AND :endTime IS NULL)")
"AND (:startTime IS NULL AND :endTime IS NULL OR " +
" rt.quantity > (" +
" SELECT COUNT(DISTINCT r.id) FROM Room r " +
" JOIN OrderDetail od ON r.id = od.room.id " +
" WHERE r.roomType = rt " +
" AND ((od.startTime < :endTime AND od.endTime > :startTime) OR " +
" (od.startTime >= :startTime AND od.startTime < :endTime))" +
" ))" +
"GROUP BY rt.id")
Page<RoomType> findFilteredRoomTypes(@Param("address") String address,
@Param("capacity") Integer capacity,
@Param("startTime") LocalDateTime startTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public List<AssignmentResponse> getAssignmentsByStaffId(String staffId) {
String nameStaff = accountRepository.findById(assignment.getStaffId())
.map(Account::getName)
.orElse("Unknown");
response.setNameStaff(nameStaff); // Set the staff name
response.setNameStaff(nameStaff);
return response;
})
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -53,6 +50,7 @@ public class OrderDetailService {
private final RoomService roomService;
private final RoomRepository roomRepository;
private final OrderDetailAmenityRepository orderDetailAmenityRepository;
private final AssignmentRepository assignmentRepository;

//GET:
public List<OrderDetailResponse> getAllOrders() {
Expand Down Expand Up @@ -351,13 +349,28 @@ private void createOrderDetailAmenities(OrderDetail orderDetail, List<Amenity> a
}
}

public OrderDetail createOrderDetail(OrderDetailCreationRequest request, Order order, Room room, OrderStatus status, Account account, LocalDateTime startTime, LocalDateTime endTime) {
public OrderDetail createOrderDetail(OrderDetailCreationRequest request, Order order, Room room, OrderStatus status, Account account, LocalDateTime startTime, LocalDateTime endTime) {
try {
ServicePackage servicePackage = servicePackageRepository.findById(request.getServicePackage().getId())
.orElseThrow(() -> new IllegalArgumentException("Service package not found"));
Building building = buildingRepository.findById(request.getBuilding().getId())
.orElseThrow(() -> new IllegalArgumentException("Building not found"));


DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
String formattedStartTime = startTime.format(timeFormatter);
String formattedEndTime = endTime.format(timeFormatter);
String slot = formattedStartTime + " - " + formattedEndTime;

DayOfWeek dayOfWeek = startTime.getDayOfWeek();
String weekDate = getDayLabel(dayOfWeek);
Account orderHandler;
String orderHandlerId = assignmentRepository.findStaffForMatchingOrder(slot, weekDate, building.getId());
if (orderHandlerId == null){
orderHandler = null;
} else {
orderHandler = accountRepository.getById(orderHandlerId);
}
OrderDetail response = new OrderDetail();

response.setCustomer(account);
Expand All @@ -366,7 +379,7 @@ public OrderDetail createOrderDetail(OrderDetailCreationRequest request, Order o
response.setBuilding(building);
response.setRoom(room);
response.setServicePackage(servicePackage);
response.setOrderHandler(null);
response.setOrderHandler(orderHandler);
response.setPriceRoom(request.getPriceRoom());
response.setDiscountPercentage(servicePackage.getDiscountPercentage());
response.setStartTime(startTime);
Expand All @@ -385,6 +398,27 @@ public OrderDetail createOrderDetail(OrderDetailCreationRequest request, Order o
}
}

private String getDayLabel(DayOfWeek dayOfWeek) {
switch (dayOfWeek) {
case MONDAY:
return "T2";
case TUESDAY:
return "T3";
case WEDNESDAY:
return "T4";
case THURSDAY:
return "T5";
case FRIDAY:
return "T6";
case SATURDAY:
return "T7";
case SUNDAY:
return "CN";
default:
return "";
}
}

public List<OrderDetail> getNextDayBookings(LocalDate dayNow) {
LocalDateTime startOfDay = dayNow.plusDays(1).atStartOfDay();
LocalDateTime endOfDay = dayNow.plusDays(1).atTime(LocalTime.MAX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public PaymentResDTO generatePaymentUrl(long amount, String orderId, String clie
vnp_Params.put("vnp_OrderType", "100000");
vnp_Params.put("vnp_OrderInfo", "Thanh toan don hang:" + orderId);

Calendar cld = Calendar.getInstance(TimeZone.getTimeZone("Etc/GMT+7"));
Calendar cld = Calendar.getInstance(TimeZone.getTimeZone("Asia/Ho_Chi_Minh"));
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String vnp_CreateDate = formatter.format(cld.getTime());

Expand Down

0 comments on commit cca0665

Please sign in to comment.