package com.fs.live.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.live.domain.LiveVideo;
import com.fs.live.service.ILiveVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 直播视频Controller
 *
 * @author fs
 * @date 2025-01-17
 */
@RestController
@RequestMapping("/live/liveVideo")
public class LiveVideoController extends BaseController
{
    @Autowired
    private ILiveVideoService liveVideoService;

    /**
     * 查询直播视频列表
     */
//    @PreAuthorize("@ss.hasPermi('live:liveVideo:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveVideo liveVideo)
    {
        startPage();
        List<LiveVideo> list = liveVideoService.selectLiveVideoList(liveVideo);
        return getDataTable(list);
    }

    /**
     * 导出直播视频列表
     */
//    @PreAuthorize("@ss.hasPermi('live:liveVideo:export')")
    @Log(title = "直播视频", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LiveVideo liveVideo)
    {
        List<LiveVideo> list = liveVideoService.selectLiveVideoList(liveVideo);
        ExcelUtil<LiveVideo> util = new ExcelUtil<LiveVideo>(LiveVideo.class);
        return util.exportExcel(list, "直播视频数据");
    }

    /**
     * 获取直播视频详细信息
     */
//    @PreAuthorize("@ss.hasPermi('live:liveVideo:query')")
    @GetMapping(value = "/{videoId}")
    public AjaxResult getInfo(@PathVariable("videoId") Long videoId)
    {
        return AjaxResult.success(liveVideoService.selectLiveVideoByVideoId(videoId));
    }

    /**
     * 获取直播视频详细信息
     */
//    @PreAuthorize("@ss.hasPermi('live:liveVideo:query')")
    @GetMapping(value = "/liveVideoByLiveId/{liveId}")
    public AjaxResult getLiveVideoByLiveId(@PathVariable("liveId") Long liveId)
    {
        return AjaxResult.success(liveVideoService.selectLiveVideoByLiveId(liveId));
    }

    /**
     * 获取直播视频详细信息
     */
    @GetMapping(value = "/preview/{liveId}")
    public AjaxResult getLiveVideoByLiveIdAndType(@PathVariable("liveId") Long liveId)
    {
        return AjaxResult.success(liveVideoService.selectLiveVideoByLiveIdAndType(liveId,3));
    }

    /**
     * 新增直播视频
     */
//    @PreAuthorize("@ss.hasPermi('live:liveVideo:add')")
    @Log(title = "直播视频", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveVideo liveVideo)
    {
        return toAjax(liveVideoService.insertLiveVideo(liveVideo));
    }

    /**
     * 修改直播视频
     */
//    @PreAuthorize("@ss.hasPermi('live:liveVideo:edit')")
    @Log(title = "直播视频", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveVideo liveVideo)
    {
        return toAjax(liveVideoService.updateLiveVideo(liveVideo));
    }

    /**
     * 删除直播视频
     */
//    @PreAuthorize("@ss.hasPermi('live:liveVideo:remove')")
    @Log(title = "直播视频", businessType = BusinessType.DELETE)
	@DeleteMapping("/{videoIds}")
    public AjaxResult remove(@PathVariable Long[] videoIds)
    {
        return toAjax(liveVideoService.deleteLiveVideoByVideoIds(videoIds));
    }

    /**
     * 批量修改视频分类
     */
    @Log(title = "批量修改视频分类", businessType = BusinessType.UPDATE)
    @PutMapping("/batchUpdateCategory")
    public AjaxResult batchUpdateCategory(@RequestBody Map<String, Object> params)
    {
        Object videoIdsObj = params.get("videoIds");
        Object categoryObj = params.get("category");

        if (videoIdsObj == null) {
            return AjaxResult.error("视频编号不能为空");
        }

        Long[] videoIds;
        if (videoIdsObj instanceof List) {
            @SuppressWarnings("unchecked")
            List<Object> list = (List<Object>) videoIdsObj;
            videoIds = list.stream().map(id -> {
                if (id instanceof Number) {
                    return ((Number) id).longValue();
                }
                return Long.parseLong(id.toString());
            }).toArray(Long[]::new);
        } else if (videoIdsObj instanceof Long[]) {
            videoIds = (Long[]) videoIdsObj;
        } else {
            return AjaxResult.error("视频编号格式错误");
        }

        String category = categoryObj != null ? categoryObj.toString() : null;

        int result = liveVideoService.batchUpdateCategory(videoIds, category);
        return toAjax(result);
    }
}
