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.LiveMsg;
import com.fs.live.service.ILiveMsgService;
import com.fs.live.vo.LiveMsgExportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 直播讨论Controller
 *
 * @author fs
 * @date 2025-01-17
 */
@RestController
@RequestMapping("/live/liveMsg")
public class LiveMsgController extends BaseController
{
    @Autowired
    private ILiveMsgService liveMsgService;

    /**
     * 查询直播讨论列表
     */
//    @PreAuthorize("@ss.hasPermi('live:liveMsg:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveMsg liveMsg)
    {
        startPage();
        List<LiveMsg> list = liveMsgService.selectLiveMsgList(liveMsg);
        return getDataTable(list);
    }

    @GetMapping("/singleList")
    public TableDataInfo singleList(LiveMsg liveMsg)
    {
        startPage();
        List<LiveMsg> list = liveMsgService.selectLiveMsgSingleList(liveMsg);
        return getDataTable(list);
    }

    /**
     * 导出直播讨论列表
     */
//    @PreAuthorize("@ss.hasPermi('live:liveMsg:export')")
    @Log(title = "直播讨论", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LiveMsg liveMsg)
    {
        List<LiveMsg> list = liveMsgService.selectLiveMsgList(liveMsg);
        ExcelUtil<LiveMsg> util = new ExcelUtil<LiveMsg>(LiveMsg.class);
        return util.exportExcel(list, "直播讨论数据");
    }

    /**
     * 获取直播讨论详细信息
     */
//    @PreAuthorize("@ss.hasPermi('live:liveMsg:query')")
    @GetMapping(value = "/{msgId}")
    public AjaxResult getInfo(@PathVariable("msgId") Long msgId)
    {
        CompanyUser user = SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success(liveMsgService.selectLiveMsgByMsgId(msgId));
    }

    /**
     * 新增直播讨论
     */
//    @PreAuthorize("@ss.hasPermi('live:liveMsg:add')")
    @Log(title = "直播讨论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveMsg liveMsg)
    {
        return toAjax(liveMsgService.insertLiveMsg(liveMsg));
    }

    /**
     * 修改直播讨论
     */
//    @PreAuthorize("@ss.hasPermi('live:liveMsg:edit')")
    @Log(title = "直播讨论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveMsg liveMsg)
    {
        return toAjax(liveMsgService.updateLiveMsg(liveMsg));
    }

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

    /**
     * 导出直播评论
     */
    @Log(title = "直播评论导出", businessType = BusinessType.EXPORT)
    @GetMapping("/exportComments/{liveId}")
    public AjaxResult exportComments(@PathVariable("liveId") Long liveId)
    {
        try {
            CompanyUser user = SecurityUtils.getLoginUser().getUser();
            Long userId = user.getUserId();
            
            List<LiveMsgExportVO> list = liveMsgService.exportLiveMsgComments(liveId, userId);
            ExcelUtil<LiveMsgExportVO> util = new ExcelUtil<LiveMsgExportVO>(LiveMsgExportVO.class);
            return util.exportExcel(list, "直播评论数据");
        } catch (Exception e) {
            return AjaxResult.error("导出失败：" + e.getMessage());
        }
    }
}
