package com.ruoyi.web.controller.app;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.equipment.domain.EquipmentVO;
import com.ruoyi.ss.equipment.service.IEquipmentService;
import com.ruoyi.ss.feeRule.service.IFeeRuleValidator;
import com.ruoyi.ss.room.domain.*;
import com.ruoyi.ss.room.service.IRoomAssembler;
import com.ruoyi.ss.room.service.IRoomService;
import com.ruoyi.ss.room.service.IRoomValidator;
import com.ruoyi.ss.room.service.RoomConverter;
import com.ruoyi.ss.toilet.domain.ToiletVO;
import com.ruoyi.ss.toilet.service.IToiletService;
import com.ruoyi.ss.toilet.service.IToiletValidator;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 房间Controller
 * @author qzz
 * 2024/3/5
 */
@Api(tags = "房间")
@RestController
@RequestMapping("/app/room")
public class AppRoomController extends BaseController {

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IRoomValidator roomValidator;

    @Autowired
    private RoomConverter roomConverter;

    @Autowired
    private IRoomAssembler roomAssembler;

    @Autowired
    private IFeeRuleValidator feeRuleValidator;

    @Autowired
    private IEquipmentService equipmentService;

    @Resource
    private IToiletService toiletService;

    @Resource
    private IToiletValidator toiletValidator;

    /**
     * 获取房间详细信息
     */
    @GetMapping(value = "/{roomId}")
    public AjaxResult getRoomInfo(@PathVariable("roomId") Long roomId)
    {
        logger.info("获取房间详细信息：【roomId={}】", roomId);
        RoomVO roomVO = roomService.selectERoomByRoomId(roomId);
        roomAssembler.assembleReservedTimePeriods(roomVO);
        roomAssembler.assembleRule(roomVO);
        return success(roomVO);
    }

    /**
     * 获取房间详细信息(商家)
     */
    @GetMapping(value = "/mch/{roomId}")
    public AjaxResult getMchRoomInfo(@PathVariable("roomId") Long roomId)
    {
        logger.info("获取房间详细信息(商家)：【roomId={}】", roomId);
        RoomVO roomVO = roomService.selectERoomByRoomId(roomId);
        roomAssembler.assembleEquipmentList(roomVO);
        roomAssembler.assembleHallDevice(roomVO);
        roomAssembler.assembleRules(roomVO);
        return success(roomVO);
    }

    /**
     * 开房间门
     */
    @GetMapping(value = "/openRoomGate/{roomId}")
    public AjaxResult openRoomGate(@PathVariable("roomId") Long roomId)
    {
        logger.info("开房间门：【roomId={}】", roomId);
        RoomVO eRoomVO = roomService.selectERoomByRoomId(roomId);
        EquipmentVO door = roomValidator.preOpenDoor(eRoomVO);
        return success(roomService.openRoomDoor(door, true, "小程序开房间门", true));
    }

    /**
     * 开卫生间
     */
    @GetMapping(value = "/openToilet/{toiletId}")
    public AjaxResult openToilet(@PathVariable("toiletId") Long toiletId)
    {
        logger.info("开卫生间：【toiletId={}】", toiletId);
        ToiletVO toiletVO = toiletService.selectToiletByToiletId(toiletId);
        toiletValidator.preOpenToilet(toiletVO);
        return success(toiletService.openToilet(toiletVO, "小程序开卫生间", true));
    }

    /**
     * 获取下单前信息
     */
    @GetMapping(value = "/getOrderInfo/{roomId}")
    public AjaxResult getOrderInfo(@PathVariable("roomId") Long roomId)
    {
        logger.info("获取下单前信息：【roomId={}】", roomId);
        RoomVO roomVO = roomService.selectERoomByRoomId(roomId);
        return success(roomVO);
    }

    /**
     * 根据店铺查房间
     */
    @GetMapping(value = "/getRoomListByStoreId")
    public AjaxResult getRoomListByStoreId(Long storeId)
    {
        logger.info("根据店铺查房间：【storeId={}】", storeId);
        return success(roomService.getRoomListByStoreId(storeId));
    }

    /**
     * 根据店铺查标签
     */
    @GetMapping(value = "/getTagListByStoreId")
    public AjaxResult getTagListByStoreId(Long storeId)
    {
        logger.info("根据店铺查标签：【storeId={}】", storeId);
        return success(roomService.getTagListByStoreId(storeId));
    }

    /**
     * 新增房间
     */
    @Log(title = "房间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Room eRoom)
    {
        logger.info("新增房间：【{}】", JSON.toJSONString(eRoom));
        feeRuleValidator.isExistRule(eRoom.getRuleIds());
        eRoom.setCreateBy(getUsername());
        eRoom.setMerchantId(getUserId());
        return toAjax(roomService.insertERoom(eRoom));
    }

    /**
     * 修改房间
     */
    @Log(title = "房间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Room eRoom)
    {
        logger.info("修改房间：【{}】", JSON.toJSONString(eRoom));
        return toAjax(roomService.updateERoom(eRoom));
    }

    /**
     * 删除房间
     */
    @Log(title = "房间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roomIds}")
    public AjaxResult remove(@PathVariable Long[] roomIds)
    {
        logger.info("删除房间：【{}】", JSON.toJSONString(roomIds));
        return toAjax(roomService.deleteERoomByRoomIds(roomIds));
    }

    /**
     * 绑定房间门
     */
    @Log(title = "绑定房间门", businessType = BusinessType.UPDATE)
    @PutMapping("/bandDoor")
    public AjaxResult bandDoor(@RequestBody RoomDTO eRoom)
    {
        logger.info("绑定房间门：【{}】", JSON.toJSONString(eRoom));
        return toAjax(roomService.bandDoor(eRoom));
    }


    /**
     * 应用套餐
     */
    @Log(title = "应用套餐", businessType = BusinessType.BANDRULE)
    @PutMapping("/bandRule")
    public AjaxResult bandRule(@RequestBody BandRuleDTO dto)
    {
        logger.info("应用套餐：【{}】", JSON.toJSONString(dto));
        roomValidator.preBandRule(dto);
        return toAjax(roomService.bandRule(dto));
    }

    /**
     * 批量应用套餐
     */
    @Log(title = "批量应用套餐", businessType = BusinessType.BANDRULE)
    @PutMapping("/bandRules")
    public AjaxResult bandRules(@RequestBody BandRulesDTO dto)
    {
        logger.info("批量应用套餐：【{}】", JSON.toJSONString(dto));
        return toAjax(roomService.bandRules(dto));
    }

    /**
     * 解除套餐
     */
    @PostMapping("/unBandRule/{roomId}/{ruleId}")
    public AjaxResult unBandRule(@PathVariable("roomId") Long roomId,@PathVariable("ruleId") Long ruleId) {
        logger.info("解除套餐：【ruleId={},roomId={}】", ruleId,roomId);
        return toAjax(roomService.unBandRule(ruleId,roomId));
    }

    /**
     * 配置房间wifi
     */
    @PutMapping("/setWifi")
    public AjaxResult setWifi(@RequestBody SetWifiDTO dto) {
        logger.info("配置房间wifi：【{}】", JSON.toJSONString(dto));
        return toAjax(roomService.setWifi(dto));
    }

    /**
     * 查商户下所有wifi
     */
    @GetMapping("/getWifiList")
    public AjaxResult getWifiList()
    {
        logger.info("查商户下所有wifi");
        List<WifiObjVO> list = roomService.selectWifiObjList();
        return success(list);
    }

    /**
     * 查商户下所有房间和设施列表
     */
    @GetMapping("/getRoomList")
    public AjaxResult getRoomList(RoomQuery query)
    {
        logger.info("查询商户下所有房间和设施列表");
        query.setMerchantId(getUserId());
        List<RoomVO> roomVOS = roomService.selectERoomList(query);
        return success(roomVOS);
    }

}
