package com.jeeplus.hotelorder.room.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.hotelorder.room.domain.HotelRoom;
import com.jeeplus.hotelorder.room.service.HotelRoomService;
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 javax.validation.Valid;

/**
 * 酒店房间管理Controller
 * @author jeeplus
 */
@Api("酒店房间管理")
@RestController
@RequestMapping(value = "/room")
public class HotelRoomController {

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

    @Resource
    private HotelRoomService hotelRoomService;

    /**
     * 分页查询房间列表
     * @param hotelRoom 查询条件
     * @param page 分页参数
     * @return 分页结果
     * @throws Exception 查询异常
     */
    @ApiLog("查询房间列表")
    @PreAuthorize("hasAuthority('hotel:admin:room:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<HotelRoom>> list(HotelRoom hotelRoom, Page<HotelRoom> page) throws Exception {
        IPage<HotelRoom> result = hotelRoomService.findPage(page, hotelRoom);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询房间详情
     * @param id 房间ID
     * @return 房间信息
     */
    @ApiLog("查询房间详情")
    @PreAuthorize("hasAuthority('hotel:admin:room:list')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        HotelRoom hotelRoom = hotelRoomService.queryById(id);
        return ResponseEntity.ok(hotelRoom);
    }

    /**
     * 新增/编辑房间
     * 新增时自动生成二维码
     * @param hotelRoom 房间信息
     * @return 操作结果
     * @throws Exception 业务异常
     */
    @ApiLog(value = "新增/编辑房间", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('hotel:admin:room:add','hotel:admin:room:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody HotelRoom hotelRoom) throws Exception {
        hotelRoomService.saveRoom(hotelRoom);
        return ResponseEntity.ok("操作成功");
    }

    /**
     * 切换房间状态
     * @param id 房间ID
     * @param status 目标状态（0-正常, 1-暂停, 2-停用）
     * @return 操作结果
     */
    @ApiLog(value = "切换房间状态", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAuthority('hotel:admin:room:edit')")
    @PostMapping("toggleStatus")
    public ResponseEntity toggleStatus(@RequestParam("id") String id, @RequestParam("status") String status) {
        hotelRoomService.toggleStatus(id, status);
        return ResponseEntity.ok("操作成功");
    }

    /**
     * 补打二维码（内容不变，重新生成图片）
     * @param id 房间ID
     * @return 二维码Base64图片
     * @throws Exception 生成异常
     */
    @ApiLog("补打二维码")
    @PreAuthorize("hasAuthority('hotel:admin:room:list')")
    @GetMapping("reprintQr")
    public ResponseEntity reprintQr(@RequestParam("id") String id) throws Exception {
        String base64Image = hotelRoomService.reprintQrCode(id);
        return ResponseUtil.newInstance()
                .add("qrCodeImage", base64Image)
                .ok("补打成功");
    }

    /**
     * 重新生成二维码（内容改变，旧码废弃）
     * @param id 房间ID
     * @return 新的二维码Base64图片
     * @throws Exception 生成异常
     */
    @ApiLog(value = "重新生成二维码", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAuthority('hotel:admin:room:edit')")
    @PostMapping("regenerateQr")
    public ResponseEntity regenerateQr(@RequestParam("id") String id) throws Exception {
        String base64Image = hotelRoomService.regenerateQrCode(id);
        return ResponseUtil.newInstance()
                .add("qrCodeImage", base64Image)
                .ok("重新生成成功");
    }

    /**
     * 删除房间
     * @param id 房间ID
     * @return 操作结果
     */
    @ApiLog("删除房间")
    @PreAuthorize("hasAuthority('hotel:admin:room:delete')")
    @PostMapping("delete")
    public ResponseEntity delete(@RequestParam("id") String id) {
        hotelRoomService.deleteRoom(id);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * 批量删除房间
     * @param ids 房间ID，逗号分隔
     * @return 操作结果
     */
    @ApiLog("批量删除房间")
    @PreAuthorize("hasAuthority('hotel:admin:room:delete')")
    @PostMapping("batchDelete")
    public ResponseEntity batchDelete(@RequestParam("ids") String ids) {
        hotelRoomService.batchDelete(ids);
        return ResponseEntity.ok("批量删除成功");
    }
}
