package com.fs.company.controller.live;

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.company.domain.CompanyUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.live.domain.Live;
import com.fs.live.service.ILiveService;
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/live")
public class LiveController extends BaseController
{
    @Autowired
    private ILiveService liveService;

    /**
     * 查询直播列表
     */
    @PreAuthorize("@ss.hasPermi('live:live:list')")
    @GetMapping("/list")
    public TableDataInfo list(Live live)
    {
        // 设置企业ID和企业用户ID
        setCompanyId(live);

        startPage();
        List<Live> list = liveService.selectLiveList(live);
        return getDataTable(list);
    }

    /**
     * 查询直播列表
     */
    @PreAuthorize("@ss.hasPermi('live:live:selectLiveToStudent')")
    @PostMapping("/selectLiveToStudent")
    public TableDataInfo selectLiveToStudent(@RequestBody Map<String,String> param)
    {
        startPage();
        String liveName = param.get("liveName");
        String status = param.get("status");
        String startTime = param.get("startTime");
        String finishTime = param.get("finishTime");
        List<Live> list = liveService.selectLiveToStudent(liveName,status,startTime,finishTime);
        return getDataTable(list);
    }

    /**
     * 导出直播列表
     */
    @PreAuthorize("@ss.hasPermi('live:live:export')")
    @Log(title = "直播", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Live live)
    {
        // 设置企业ID和企业用户ID
        setCompanyId(live);

        List<Live> list = liveService.selectLiveList(live);
        ExcelUtil<Live> util = new ExcelUtil<Live>(Live.class);
        return util.exportExcel(list, "直播数据");
    }

    /**
     * 获取直播详细信息
     */
    @PreAuthorize("@ss.hasPermi('live:live:query')")
    @GetMapping(value = "/{liveId}")
    public AjaxResult getInfo(@PathVariable("liveId") Long liveId)
    {
        CompanyUser user = SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success(liveService.selectLiveByLiveIdAndCompanyIdAndCompanyUserId(liveId, user.getCompanyId(), user.getUserId()));
    }

    /**
     * 新增直播
     */
    @PreAuthorize("@ss.hasPermi('live:live:add')")
    @Log(title = "直播", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Live live)
    {
        // 设置企业ID和企业用户ID
        setCompanyId(live);
        return toAjax(liveService.insertLive(live));
    }

    /**
     * 修改直播
     */
    @PreAuthorize("@ss.hasPermi('live:live:edit')")
    @Log(title = "直播", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Live live)
    {
        return toAjax(liveService.updateLive(live));
    }

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

    /**
     * 设置企业ID和企业用户ID
     * @param live 直播间
     */
    private void setCompanyId(Live live) {
        CompanyUser user = SecurityUtils.getLoginUser().getUser();
        live.setCompanyId(user.getCompanyId());
        live.setCompanyUserId(user.getUserId());
    }
}
