package com.jeeplus.hotelorder.room.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.hotelorder.room.domain.HotelRoomType;
import com.jeeplus.hotelorder.room.service.HotelRoomTypeService;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 房间类型管理Controller
 * @author jeeplus
 */
@Api("房间类型管理")
@RestController
@RequestMapping(value = "/roomType")
public class HotelRoomTypeController {

    private static Logger log = LoggerFactory.getLogger(HotelRoomTypeController.class);

    @Resource
    private HotelRoomTypeService hotelRoomTypeService;

    /**
     * 分页查询房间类型
     */
    @ApiLog("查询房间类型列表")
    @PreAuthorize("hasAuthority('hotel:admin:roomType:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<HotelRoomType>> list(HotelRoomType hotelRoomType, Page<HotelRoomType> page) throws Exception {
        IPage<HotelRoomType> result = hotelRoomTypeService.findPage(page, hotelRoomType);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询所有启用的房间类型（下拉框用）
     */
    @ApiLog("查询所有房间类型")
    @GetMapping("all")
    public ResponseEntity<List<HotelRoomType>> all() {
        List<HotelRoomType> list = hotelRoomTypeService.findAllEnabled();
        return ResponseEntity.ok(list);
    }

    /**
     * 查询房间类型详情
     */
    @ApiLog("查询房间类型详情")
    @PreAuthorize("hasAuthority('hotel:admin:roomType:list')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        HotelRoomType hotelRoomType = hotelRoomTypeService.queryById(id);
        return ResponseEntity.ok(hotelRoomType);
    }

    /**
     * 新增/编辑房间类型
     */
    @ApiLog(value = "新增/编辑房间类型", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('hotel:admin:roomType:add','hotel:admin:roomType:edit')")
    @PostMapping("save")
    public ResponseEntity save(@RequestBody HotelRoomType hotelRoomType) {
        hotelRoomTypeService.saveRoomType(hotelRoomType);
        return ResponseEntity.ok("操作成功");
    }

    /**
     * 切换状态（启用/禁用）
     */
    @ApiLog(value = "切换房间类型状态", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAuthority('hotel:admin:roomType:edit')")
    @PostMapping("toggleStatus")
    public ResponseEntity toggleStatus(@RequestParam("id") String id) {
        hotelRoomTypeService.toggleStatus(id);
        return ResponseEntity.ok("操作成功");
    }

    /**
     * 删除房间类型
     */
    @ApiLog("删除房间类型")
    @PreAuthorize("hasAuthority('hotel:admin:roomType:delete')")
    @PostMapping("delete")
    public ResponseEntity delete(@RequestParam("id") String id) {
        hotelRoomTypeService.deleteRoomType(id);
        return ResponseEntity.ok("删除成功");
    }
}
