# OooRoomTypeConfigController.java

    package cloud.wconnect.frontoffice.controller.oooroom;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.frontoffice.entity.oooroom.OooRoomTypeConfig;
    import cloud.wconnect.frontoffice.pojo.dto.oooroom.RoomTypeDTO;
    import cloud.wconnect.frontoffice.pojo.query.oooroom.RoomTypeQuery;
    import cloud.wconnect.frontoffice.service.oooroom.IOooRoomTypeConfigService;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.Collections;
    import java.util.Map;

    /**
     * <p>
     * 房间类型-标识 配置 前端控制器
     * </p>
     *
     * @author yyt
     * @since 2023-08-05
     */
    @RestController
    @RequestMapping("/ooo-room/room-type")
    public class OooRoomTypeConfigController {

        @Autowired
        private IOooRoomTypeConfigService oooRoomTypeConfigService;

        @Autowired
        private SessionContext sessionContext;

        /**
         * 获取类型列表
         *
         * @param property 资产标识
         * @param query    查询条件
         * @return 类型列表
         * @author yyt
         * @date 2023/8/5
         */
        @ApiId(533)
        @GetMapping
        public ListPagedDTO<RoomTypeDTO> listRoomTypeDTO(String property, RoomTypeQuery query) {
            Integer propertyId = sessionContext.getPropertyId(property);
            return oooRoomTypeConfigService.listRoomTypeDTO(propertyId, query);
        }

        /**
         * 新增、编辑类型
         *
         * @param id          数据 id
         * @param property    资产标识
         * @param roomTypeDTO 房型数据
         * @return {}
         * @author yyt
         * @date 2023/8/5
         */
        @ApiId(534)
        @PostMapping("/{id:\\d+}")
        public Map save(@PathVariable int id, String property, @Valid @RequestBody RoomTypeDTO roomTypeDTO) {
            Integer propertyId = sessionContext.getPropertyId(property);
            int userId = sessionContext.getLoginUserId();
            if (id == BaseConstant.ZERO) {
                oooRoomTypeConfigService.checkExist(propertyId, roomTypeDTO, null);

                oooRoomTypeConfigService.save(propertyId, roomTypeDTO, userId);
            } else {
                oooRoomTypeConfigService.checkExist(propertyId, roomTypeDTO, id);

                OooRoomTypeConfig config = oooRoomTypeConfigService.checkExist(id);

                checkBelong(config, propertyId);

                oooRoomTypeConfigService.update(id, roomTypeDTO, userId);
            }

            return Collections.emptyMap();
        }

        /**
         * 删除类型
         *
         * @param id       数据 id
         * @param property 资产标识
         * @return {}
         * @author yyt
         * @date 2023/8/5
         */
        @ApiId(535)
        @DeleteMapping("/{id:\\d+}")
        public Map delete(@PathVariable int id, String property) {
            int propertyId = sessionContext.getPropertyId(property);
            OooRoomTypeConfig config = oooRoomTypeConfigService.checkExist(id);

            checkBelong(config, propertyId);

            oooRoomTypeConfigService.checkUsed(propertyId, id);

            oooRoomTypeConfigService.delete(id);

            return Collections.emptyMap();
        }

        /**
         * 校验数据归属
         *
         * @param config     房型
         * @param propertyId 资产id
         * @author yyt
         * @date 2023/8/5
         */

        private void checkBelong(OooRoomTypeConfig config, Integer propertyId) {
            if (!config.getPropertyId().equals(propertyId)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), "the property id parameter is different from " +
                        "the property id of the oooRoomTypeConfig;propertyId:" + propertyId + ",oooRoomTypeConfigPropertyId:" +
                        config.getPropertyId() + ",oooRoomTypeConfigId:" + config.getId());
            }
        }

    }