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.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.live.domain.LiveAnchor;
import com.fs.live.service.ILiveAnchorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 主播Controller
 *
 * @author fs
 * @date 2025-01-17
 */
@RestController
@RequestMapping("/live/liveAnchor")
public class LiveAnchorController extends BaseController
{
    @Autowired
    private ILiveAnchorService liveAnchorService;

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

        startPage();
        List<LiveAnchor> list = liveAnchorService.selectLiveAnchorList(liveAnchor);
        return getDataTable(list);
    }

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

        List<LiveAnchor> list = liveAnchorService.selectLiveAnchorList(liveAnchor);
        ExcelUtil<LiveAnchor> util = new ExcelUtil<LiveAnchor>(LiveAnchor.class);
        return util.exportExcel(list, "主播数据");
    }

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

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

    /**
     * 修改主播
     */
    @PreAuthorize("@ss.hasPermi('live:liveAnchor:edit')")
    @Log(title = "主播", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveAnchor liveAnchor)
    {
        return toAjax(liveAnchorService.updateLiveAnchor(liveAnchor));
    }

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

    /**
     * 设置企业ID 企业用户ID
     * @param liveAnchor 主播信息
     */
    private void setCompanyId(LiveAnchor liveAnchor) {
        // 设置企业ID 企业用户ID
        LoginUser loginUser = SecurityUtils.getLoginUser();
        CompanyUser user = loginUser.getUser();
        liveAnchor.setCompanyId(user.getCompanyId());
        liveAnchor.setCompanyUserId(user.getUserId());
    }
}
