YPLee 6 лет назад
Родитель
Сommit
e50b9f6b00

+ 3 - 6
src/main/java/com/example/hotel/bl/hotel/HotelService.java

@@ -1,6 +1,7 @@
 package com.example.hotel.bl.hotel;
 
 import com.example.hotel.po.HotelRoom;
+import com.example.hotel.util.ServiceException;
 import com.example.hotel.vo.HotelVO;
 
 import java.util.List;
@@ -10,8 +11,9 @@ public interface HotelService {
     /**
      * 添加酒店
      * @param hotelVO
+     * @throws
      */
-    void addHotel(HotelVO hotelVO);
+    void addHotel(HotelVO hotelVO) throws ServiceException;
 
     /**
      * 列表获取酒店信息
@@ -26,9 +28,4 @@ public interface HotelService {
      */
     HotelVO retrieveHotelDetails(Integer hotelId);
 
-    /**
-     * 添加酒店客房信息
-     * @param hotelRoom
-     */
-    void addHotelRoomInfo(HotelRoom hotelRoom);
 }

+ 21 - 0
src/main/java/com/example/hotel/bl/hotel/RoomService.java

@@ -0,0 +1,21 @@
+package com.example.hotel.bl.hotel;
+
+import com.example.hotel.po.HotelRoom;
+
+import java.util.List;
+
+public interface RoomService {
+
+    /**
+     * 获取某个酒店的全部房间信息
+     * @param hotelId
+     * @return
+     */
+    List<HotelRoom> retrieveHotelRoomInfo(Integer hotelId);
+
+    /**
+     * 添加酒店客房信息
+     * @param hotelRoom
+     */
+    void insertRoomInfo(HotelRoom hotelRoom);
+}

+ 14 - 11
src/main/java/com/example/hotel/blImpl/hotel/HotelServiceImpl.java

@@ -1,14 +1,18 @@
 package com.example.hotel.blImpl.hotel;
 
 import com.example.hotel.bl.hotel.HotelService;
+import com.example.hotel.bl.hotel.RoomService;
+import com.example.hotel.bl.user.AccountService;
 import com.example.hotel.data.hotel.HotelMapper;
 import com.example.hotel.data.hotel.RoomMapper;
 import com.example.hotel.data.user.AccountMapper;
 import com.example.hotel.enums.BizRegion;
 import com.example.hotel.enums.HotelStar;
+import com.example.hotel.enums.UserType;
 import com.example.hotel.po.Hotel;
 import com.example.hotel.po.HotelRoom;
 import com.example.hotel.po.User;
+import com.example.hotel.util.ServiceException;
 import com.example.hotel.vo.HotelVO;
 import com.example.hotel.vo.RoomVO;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -20,16 +24,20 @@ import java.util.stream.Collectors;
 @Service
 public class HotelServiceImpl implements HotelService {
 
-    @Autowired
-    private AccountMapper accountMapper;
     @Autowired
     private HotelMapper hotelMapper;
-    @Autowired
-    private RoomMapper roomMapper;
 
+    @Autowired
+    private AccountService accountService;
+    @Autowired
+    private RoomService roomService;
 
     @Override
-    public void addHotel(HotelVO hotelVO) {
+    public void addHotel(HotelVO hotelVO) throws ServiceException {
+        User manager = accountService.getUserInfo(hotelVO.getManagerId());
+        if(manager == null || !manager.getUserType().equals(UserType.HotelManager)){
+            throw new ServiceException("管理员不存在或者无权限!创建酒店失败!");
+        }
         Hotel hotel = new Hotel();
         hotel.setDescription(hotelVO.getDescription());
         hotel.setAddress(hotelVO.getAddress());
@@ -40,7 +48,6 @@ public class HotelServiceImpl implements HotelService {
         hotel.setBizRegion(BizRegion.valueOf(hotelVO.getBizRegion()));
         hotel.setHotelStar(HotelStar.valueOf(hotelVO.getHotelStar()));
         hotelMapper.insertHotel(hotel);
-
     }
 
     @Override
@@ -52,7 +59,7 @@ public class HotelServiceImpl implements HotelService {
     @Override
     public HotelVO retrieveHotelDetails(Integer hotelId) {
         HotelVO hotelVO = hotelMapper.selectById(hotelId);
-        List<HotelRoom> rooms = roomMapper.selectRoomsByHotelId(hotelId);
+        List<HotelRoom> rooms = roomService.retrieveHotelRoomInfo(hotelId);
         List<RoomVO> roomVOS = rooms.stream().map(r -> {
             RoomVO roomVO = new RoomVO();
             roomVO.setId(r.getId());
@@ -67,8 +74,4 @@ public class HotelServiceImpl implements HotelService {
         return hotelVO;
     }
 
-    @Override
-    public void addHotelRoomInfo(HotelRoom hotelRoom) {
-        roomMapper.insertRoom(hotelRoom);
-    }
 }

+ 28 - 0
src/main/java/com/example/hotel/blImpl/hotel/RoomServiceImpl.java

@@ -0,0 +1,28 @@
+package com.example.hotel.blImpl.hotel;
+
+import com.example.hotel.bl.hotel.RoomService;
+import com.example.hotel.data.hotel.RoomMapper;
+import com.example.hotel.po.HotelRoom;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class RoomServiceImpl implements RoomService {
+
+    @Autowired
+    private RoomMapper roomMapper;
+
+    @Override
+    public List<HotelRoom> retrieveHotelRoomInfo(Integer hotelId) {
+        return roomMapper.selectRoomsByHotelId(hotelId);
+    }
+
+    @Override
+    public void insertRoomInfo(HotelRoom hotelRoom) {
+        roomMapper.insertRoom(hotelRoom);
+    }
+
+
+}

+ 6 - 6
src/main/java/com/example/hotel/controller/hotel/HotelController.java

@@ -1,12 +1,12 @@
 package com.example.hotel.controller.hotel;
 
 import com.example.hotel.bl.hotel.HotelService;
+import com.example.hotel.bl.hotel.RoomService;
 import com.example.hotel.po.HotelRoom;
-import com.example.hotel.util.Result;
+import com.example.hotel.util.ServiceException;
 import com.example.hotel.vo.HotelVO;
 import com.example.hotel.vo.ResponseVO;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
 @RestController
@@ -15,10 +15,11 @@ public class HotelController {
 
     @Autowired
     private HotelService hotelService;
+    @Autowired
+    private RoomService roomService;
 
     @PostMapping()
-    public ResponseVO createHotel(@RequestBody HotelVO hotelVO) {
-
+    public ResponseVO createHotel(@RequestBody HotelVO hotelVO) throws ServiceException {
         hotelService.addHotel(hotelVO);
         return ResponseVO.buildSuccess();
     }
@@ -30,13 +31,12 @@ public class HotelController {
 
     @PostMapping("/roomInfo")
     public ResponseVO addRoomInfo(@RequestBody HotelRoom hotelRoom) {
-        hotelService.addHotelRoomInfo(hotelRoom);
+        roomService.insertRoomInfo(hotelRoom);
         return ResponseVO.buildSuccess();
     }
 
     @GetMapping("/{hotelId}/detail")
     public ResponseVO retrieveHotelDetail(@PathVariable Integer hotelId) {
-
         return ResponseVO.buildSuccess(hotelService.retrieveHotelDetails(hotelId));
     }
 

+ 32 - 0
src/main/java/com/example/hotel/util/ServiceException.java

@@ -0,0 +1,32 @@
+package com.example.hotel.util;
+
+import org.springframework.http.HttpStatus;
+
+public class ServiceException extends Exception {
+    private int error;
+
+    public ServiceException() {
+        this(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
+    }
+
+    public ServiceException(int error) {
+        this(error, message(error));
+    }
+
+    public ServiceException(String message) {
+        this(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
+    }
+
+    public ServiceException(int error, String message) {
+        super(message);
+        this.error = error;
+    }
+
+    private static String message(int error) {
+        try {
+            return HttpStatus.valueOf(error).getReasonPhrase();
+        } catch (IllegalArgumentException e) {
+            return HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase();
+        }
+    }
+}

+ 1 - 1
src/main/resources/application.yml

@@ -2,7 +2,7 @@ spring:
   datasource:
      url: jdbc:mysql://localhost:3306/Hotel?serverTimezone=CTT&characterEncoding=UTF-8
      username: root
-     password: 123456
+     password: root
      driver-class-name: com.mysql.cj.jdbc.Driver
      max-active: 200
      max-idle: 20