package com.ruoyi.web.app;

import com.aliyun.core.annotation.Path;
import com.ruoyi.bst.booth.domain.BoothQuery;
import com.ruoyi.bst.booth.domain.BoothVO;
import com.ruoyi.bst.booth.service.BoothService;
import com.ruoyi.bst.device.service.DeviceIotService;
import com.ruoyi.bst.device.service.DeviceService;
import com.ruoyi.bst.order.domain.dto.OrderCreateDTO;
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.common.enums.LogBizType;
import com.ruoyi.common.utils.ServiceUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/app/booth")
@Slf4j
public class AppBoothController extends BaseController {

    @Autowired
    private BoothService boothService;

    @ApiOperation("爆灯")
    @PutMapping("/{boothNo}/lighting")
    public AjaxResult lighting(@PathVariable @ApiParam("设备id") String boothNo,boolean requiredIot) {
        if (boothNo == null) {
            return error("卡座编号不能为空");
        }
        BoothVO booth = boothService.selectBoothByBoothNo(boothNo);
        ServiceUtil.assertion(booth==null,"当前卡座信息不存在");
        ServiceUtil.assertion(booth.getUserId()==null,"请先绑定该卡座后再进行操作");
        ServiceUtil.assertion(!booth.getUserId().equals(getUserId()),"您无权操作当前卡座设备");
        return success(boothService.lighting(booth,getUserId(),requiredIot));
    }

    @ApiOperation("用户绑定卡座")
    @PutMapping("/{boothNo}/bindUser")
    public AjaxResult bindBooth(@PathVariable String boothNo) {
        if (boothNo == null) {
            return error("卡座编号不能为空");
        };
        return AjaxResult.success(boothService.bindUser(getUserId(),boothNo));
    }

    @ApiOperation("用户解绑卡座")
    @PutMapping("/{boothNo}/unbind")
    public AjaxResult unbindBooth(@PathVariable String boothNo) {
        if (boothNo == null) {
            return error("卡座编号不能为空");
        };
        BoothVO booth = boothService.selectBoothByBoothNo(boothNo);
        ServiceUtil.assertion(booth==null,"当前卡座不存在");
        ServiceUtil.assertion(!booth.getUserId().equals(getUserId()),"您无权操作当前卡座设备");
        return AjaxResult.success(boothService.unbindUser(booth));
    }

    @ApiOperation("用户换绑卡座")
    @PutMapping("/userChangeBind")
    public AjaxResult userChangeBind( String usingBoothNo ,String changeBoothNo) {
        if (usingBoothNo == null || changeBoothNo == null) {
            return error("卡座编号不能为空");
        };
        return AjaxResult.success(boothService.userChangeBind(usingBoothNo,changeBoothNo,getUserId()));
    }

    @ApiOperation("查询用户绑定的卡座和设备信息")
    @GetMapping("/getBindInfo")
    public AjaxResult getBindInfo() {
        BoothQuery query = new BoothQuery();
        query.setUserId(getUserId());
        query.setTime(LocalDateTime.now());
        BoothVO vo = boothService.selectOne(query);
        if (vo == null) {
            return error("当前用户未绑定卡座");
        }
        boothService.getDevices(vo);
        return success(vo);
    }


    @ApiOperation("根据Id查询卡座及设备信息")
    @GetMapping("/getBoothDetail/{boothId}")
    public AjaxResult getBoothDetail(@PathVariable Long boothId) {
        BoothQuery query = new BoothQuery();
        query.setBoothId(getUserId());
        BoothVO booth = boothService.selectBoothByBoothId(boothId);
        boothService.getDevices(booth);
        return success(booth);
    }

}
