package com.org.web.room.controller;

import com.org.system.annotation.Log;
import com.org.system.controller.BaseController;
import com.org.system.enums.BusinessType;
import com.org.utils.AjaxResult;
import com.org.utils.core.IdParams;
import com.org.utils.page.TableDataInfo;
import com.org.web.room.domain.BcRoom;
import com.org.web.room.domain.BcRoomBed;
import com.org.web.room.service.BcRoomBedService;
import com.org.web.room.service.BcRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/room")
public class BcRoomController extends BaseController {

	@Autowired
	BcRoomService bcRoomService;

	@Autowired
	BcRoomBedService bcRoomBedService;

	/**
	 * 新增
	 */
	@PostMapping("/addOrUpdate")
	@PreAuthorize("@ss.hasPermi('room:add')")
	@Log(title = "房间新增/编辑", businessType = BusinessType.INSERT)
	public AjaxResult addOrUpdate(@Validated @RequestBody BcRoom bcRoom) {
		log.info("BcRoomController - addOrUpdate bcRoom:{}", bcRoom);

		return AjaxResult.success(bcRoomService.executeSaveOrUpdate(bcRoom));
	}

	/**
	 * 删除
	 */
	@PostMapping("/del")
	@PreAuthorize("@ss.hasPermi('room:del')")
	@Log(title = "房间删除", businessType = BusinessType.DELETE)
	public AjaxResult del(@Validated @RequestBody IdParams params) {
		log.info("BcRoomController - del params:{}", params);

		return AjaxResult.success(bcRoomService.del(params.getId()));
	}

	/**
	 * 详情
	 */
	@PostMapping("/detail")
	@PreAuthorize("@ss.hasAnyPermi('room:detail')")
	@Log(title = "房间详情", businessType = BusinessType.SELECT)
	public AjaxResult detail(@Validated @RequestBody IdParams params) {
		log.info("BcRoomController - detail params:{}", params);

		return AjaxResult.success(bcRoomService.get(params.getId()));
	}

	/**
	 * 列表查询
	 */
	@GetMapping("/list")
	@PreAuthorize("@ss.hasAnyPermi('room:detail')")
	@Log(title = "房间列表查询", businessType = BusinessType.SELECT)
	public TableDataInfo list(BcRoom bcRoom) {
		log.info("BcRoomController - list ");

		startPage();

		List<BcRoom> list = bcRoomService.queryList(bcRoom);

		TableDataInfo dataTable = getDataTable(list);

		return dataTable;
	}

	/**
	 * 查询所有房间
	 */
	@PostMapping("/listAllRoom")
	@PreAuthorize("@ss.hasAnyPermi('room:listBed')")
	@Log(title = "床位列表查询", businessType = BusinessType.SELECT)
	public AjaxResult listAllRoom( @RequestBody BcRoom room) {

		List<BcRoom> list = bcRoomService.getBaseMapper().queryList(room);

		return AjaxResult.success(list);
	}

	/**
	 * 查询所有床位
	 */
	@PostMapping("/listAllBed")
	@PreAuthorize("@ss.hasAnyPermi('room:listBed')")
	@Log(title = "床位列表查询", businessType = BusinessType.SELECT)
	public AjaxResult listAllBed() {

		List<BcRoomBed> list = bcRoomBedService.getBaseMapper().listAllRoom();

		return AjaxResult.success(list);
	}

	/**
	 * 根据房间id查询床位
	 */
	@PostMapping("/listBed")
	@PreAuthorize("@ss.hasAnyPermi('room:listBed')")
	@Log(title = "床位列表查询", businessType = BusinessType.SELECT)
	public AjaxResult listBed(@RequestBody IdParams params) {
		log.info("BcRoomController - listBed params:{}", params);

		List<BcRoomBed> list = bcRoomBedService.listByRoomId(params.getId());

		return AjaxResult.success(list);
	}
}
