package com.ssyf.module.robot.controller.auth;

import cn.hutool.core.bean.BeanUtil;
import com.ssyf.common.web.aop.RequestSynchronization;
import com.ssyf.common.web.apicode.ApiCodeUser;
import com.ssyf.common.web.result.Result;
import com.ssyf.plugs.logger.Log;
import com.ssyf.third.bmiss.model.LiveRoom;
import com.ssyf.third.bmiss.services.LiveService;
import com.ssyf.module.robot.domain.query.RLiveServiceQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 直播服务开放接口
 * 提供直播间列表获取、直播间信息查询等功能
 * 
 * @author auth
 * @date 2024-12-31
 */
@Api(tags="直播-直播间管理")
@RestController
@RequestMapping("/auth/LiveService")
public class RLiveServiceAuthController {

    @Autowired
    private LiveService liveService;

    @Log(title = "直播服务-获取直播间列表")
    @ApiOperation("获取直播间列表")
    @PostMapping("/getLiveRoomList")
    @RequestSynchronization
    public Result<List<LiveRoom>> getLiveRoomList() {
        return Result.success(liveService.getLiveRoomList());
    }

    @Log(title = "直播服务-根据ID获取直播间信息")
    @ApiOperation(value = "根据ID获取直播间信息", notes = "根据直播间ID获取详细的直播间信息")
    @PostMapping(value = "/getLiveRoomById")
    public Result<LiveRoom> getLiveRoomById(@RequestBody @Validated RLiveServiceQuery query) {
        if (query.getRoomId() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode(), "直播间ID不能为空");
        }
        LiveRoom liveRoom = liveService.getLiveRoomById(query.getRoomId());
        return Result.success(liveRoom);
    }

    @Log(title = "直播服务-根据名称获取直播间信息")
    @ApiOperation(value = "根据名称获取直播间信息", notes = "根据直播间名称获取详细的直播间信息")
    @PostMapping(value = "/getLiveRoomByName")
    public Result<LiveRoom> getLiveRoomByName(@RequestBody @Validated RLiveServiceQuery query) {
        if (query.getRoomName() == null || query.getRoomName().trim().isEmpty()) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode(), "直播间名称不能为空");
        }
        LiveRoom liveRoom = liveService.getLiveRoomByName(query.getRoomName());
        return Result.success(liveRoom);
    }

    @Log(title = "直播服务-重置直播间缓存")
    @RequestSynchronization
    @ApiOperation(value = "重置直播间缓存", notes = "清除所有直播间相关的缓存数据")
    @PostMapping(value = "/resetCache")
    public Result resetCache() {
        liveService.resetCache();
        return Result.success();
    }

    @Log(title = "直播服务-清除指定直播间缓存")
    @RequestSynchronization
    @ApiOperation(value = "清除指定直播间缓存", notes = "清除指定直播间ID的缓存数据")
    @PostMapping(value = "/clearLiveRoomCache")
    public Result clearLiveRoomCache(@RequestBody @Validated RLiveServiceQuery query) {
        if (query.getRoomId() == null) {
            return Result.fail(ApiCodeUser.PARAMETER_ERROR.getCode(), "直播间ID不能为空");
        }
        liveService.clearLiveRoomCache(query.getRoomId());
        return Result.success();
    }
}