package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.ServiceConstants;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.equipment.domain.EquipmentVO;
import com.ruoyi.ss.equipment.service.IEquipmentService;
import com.ruoyi.ss.room.service.IRoomAssembler;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.room.domain.Room;
import com.ruoyi.ss.room.domain.RoomVO;
import com.ruoyi.ss.room.domain.RoomQuery;
import com.ruoyi.ss.room.service.IRoomService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 房间Controller
 *
 * @author qzz
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/system/room")
public class RoomController extends BaseController
{
    @Autowired
    private IRoomService eRoomService;

    @Autowired
    private IRoomAssembler roomAssembler;

    @Autowired
    private IEquipmentService equipmentService;


    /**
     * 查询房间列表
     */
    @PreAuthorize("@ss.hasPermi('system:room:list')")
    @GetMapping("/list")
    public TableDataInfo list(RoomQuery query)
    {
        startPage();
        startOrderBy();
        List<RoomVO> list = eRoomService.selectERoomList(query);
        roomAssembler.assembleEquipmentList(list);
        roomAssembler.assembleRuleInfo2(list);
        //将设备信息填充到房间中
        roomAssembler.assembleHallDevice(list);
        return getDataTable(list);
    }

    /**
     * 查询房间列表
     */
    @PreAuthorize("@ss.hasPermi('system:room:list')")
    @GetMapping("/list2")
    public TableDataInfo list2(RoomQuery query)
    {
        startPage();
        startOrderBy();
        List<RoomVO> list = eRoomService.selectERoomList(query);
        roomAssembler.assembleRuleInfo2(list);
        //将设备信息填充到房间中
        roomAssembler.assembleHallDevice(list);
        return getDataTable(list);
    }


    /**
     * 导出房间列表
     */
    @PreAuthorize("@ss.hasPermi('system:room:export')")
    @Log(title = "房间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RoomQuery query)
    {
        List<RoomVO> list = eRoomService.selectERoomList(query);
        ExcelUtil<RoomVO> util = new ExcelUtil<RoomVO>(RoomVO.class);
        util.exportExcel(response, list, "房间数据");
    }

    /**
     * 获取房间详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:room:query')")
    @GetMapping(value = "/{roomId}")
    public AjaxResult getInfo(@PathVariable("roomId") Long roomId)
    {
        RoomVO roomVO = eRoomService.selectERoomByRoomId(roomId);
        roomAssembler.assembleEquipmentList(roomVO);
        roomAssembler.assembleRuleInfo2(roomVO);
        return success(roomVO);
    }

    /**
     * 新增房间
     */
    @PreAuthorize("@ss.hasPermi('system:room:add')")
    @Log(title = "房间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Room eRoom)
    {
        return toAjax(eRoomService.insertERoom(eRoom));
    }

    /**
     * 修改房间
     */
    @PreAuthorize("@ss.hasPermi('system:room:edit')")
    @Log(title = "房间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Room eRoom)
    {
        return toAjax(eRoomService.updateERoom(eRoom));
    }

    /**
     * 删除房间
     */
    @PreAuthorize("@ss.hasPermi('system:room:remove')")
    @Log(title = "房间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{roomIds}")
    public AjaxResult remove(@PathVariable Long[] roomIds)
    {
        return toAjax(eRoomService.deleteERoomByRoomIds(roomIds));
    }

    /**
     * 开房间门(管理员)
     */
    @GetMapping(value = "/openRoomGate/{roomId}")
    public AjaxResult openRoomGate(@PathVariable("roomId") Long roomId)
    {
        logger.info("开房间门：【roomId={}】", roomId);
        RoomVO eRoomVO = eRoomService.selectERoomByRoomId(roomId);
        ServiceUtil.assertion(ObjectUtil.isNull(eRoomVO), "房间不存在");
        EquipmentVO door = equipmentService.selectDoorByRoomId(eRoomVO.getRoomId(), ServiceConstants.FACILITY_TYPE_ROOM_DOOR);
        ServiceUtil.assertion(ObjectUtil.isNull(door), "房间门设施不存在");
        ServiceUtil.assertion(ObjectUtil.isNull(door.getDeviceId()), "房间门未绑定设备");
        return success(eRoomService.openRoomDoor(door,true,"管理员开房间门", false));
    }

}
