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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.SecurityUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyUser;
import com.fs.framework.web.service.TokenService;
import com.fs.live.domain.LiveData;
import com.fs.live.param.LiveDataParam;
import com.fs.live.service.ILiveDataService;
import com.fs.live.vo.LiveUserFirstVo;
import com.fs.live.vo.LiveUserDetailExportVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/liveData/liveData")
public class LiveDataController extends BaseController {

    @Autowired
    private ILiveDataService liveDataService;
    @Autowired
    private TokenService tokenService;

    /**
     * 直播数据页面卡片数据
     */
    @GetMapping("/dashboardData")
    public R dashboardData(@RequestParam Long liveId, HttpServletRequest request) {
        return liveDataService.dashboardData(liveId);
    }

    /**
     * 直播数据页面 分页查询邀请榜单
     */
    @GetMapping("/dashboardInviteList")
    public TableDataInfo inviteList(@RequestParam Long liveId, HttpServletRequest request) {
        startPage();
        List<LiveUserFirstVo> list = liveDataService.inviteList(liveId);
        return getDataTable(list);
    }

    /**
     * 查询新直播数据列表
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:list')")
    @PostMapping("/listLiveData")
    public R listLiveData(@RequestBody LiveDataParam param, HttpServletRequest request)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        return liveDataService.listLiveData(param);
    }

    /**
     * 查询直播数据列表
     * */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:query')")
    @GetMapping("/list")
    public List<LiveData> list(LiveData liveData) {
        return liveDataService.selectLiveDataList(liveData);
    }

    /**
     * 查询直播数据详情
     * */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:query')")
    @GetMapping("/get/{liveId}")
    public R getLiveData(@PathVariable Long liveId) {
        return R.ok().put("liveData", liveDataService.selectLiveDataByLiveId(liveId));
    }

    /**
     * 点赞
     * */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:edit')")
    @GetMapping("/like/{liveId}")
    public R like(@PathVariable Long liveId) {
        return R.ok(liveDataService.updateLikeByLiveId(liveId, SecurityUtils.getLoginUser().getUser().getUserId()));
    }

    /**
     * 收藏
     * */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:edit')")
    @GetMapping("/collect/{liveId}")
    public R collect(@PathVariable Long liveId) {
        return R.ok(liveDataService.collect(liveId, SecurityUtils.getLoginUser().getUser().getUserId()));
    }

    /**
     * 关注
     * */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:edit')")
    @GetMapping("/follow/{liveId}")
    public R follow(@PathVariable Long liveId) {
        return R.ok(liveDataService.follow(liveId, SecurityUtils.getLoginUser().getUser().getUserId()));
    }

    /**
     * 获取直播内数据
     * */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:query')")
    @GetMapping("/getLiveViewData")
    public R getLiveViewData(Long liveId) {
        Map<String,Object> liveViewData =liveDataService.getLiveViewData(liveId);
        return R.ok(liveViewData);
    }

    /**
     * 查询直播间详情数据（SQL方式）
     * @param liveId 直播间ID
     * @return 详情数据
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:query')")
    @GetMapping("/getLiveDataDetailBySql")
    public R getLiveDataDetailBySql(@RequestParam Long liveId) {
        return liveDataService.getLiveDataDetailBySql(liveId);
    }

    /**
     * 查询直播间用户详情列表（SQL方式）
     * @param liveId 直播间ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 用户详情列表
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:query')")
    @GetMapping("/getLiveUserDetailListBySql")
    public R getLiveUserDetailListBySql(@RequestParam Long liveId,
                                        @RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "100") Integer pageSize) {
        // 限制最大每页查询条数为1000
        if (pageSize > 1000) {
            pageSize = 1000;
        }

        PageHelper.startPage(pageNum, pageSize);
        return liveDataService.getLiveUserDetailListBySql(liveId,null,null);
    }

    /**
     * 查询直播间详情数据（查询数据服务器处理方式）
     * @param liveId 直播间ID
     * @return 详情数据
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:query')")
    @GetMapping("/getLiveDataDetailByServer")
    public R getLiveDataDetailByServer(@RequestParam Long liveId) {
        return liveDataService.getLiveDataDetailByServer(liveId);
    }

    /**
     * 查询直播间用户详情列表（查询数据服务器处理方式）
     * @param liveId 直播间ID
     * @return 用户详情列表
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:query')")
    @GetMapping("/getLiveUserDetailListByServer")
    public R getLiveUserDetailListByServer(@RequestParam Long liveId) {
        return liveDataService.getLiveUserDetailListByServer(liveId);
    }

    /**
     * 导出直播间用户详情数据
     * @param liveId 直播间ID
     * @return Excel文件
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:export')")
    @Log(title = "直播间用户详情", businessType = BusinessType.EXPORT)
    @GetMapping("/exportLiveUserDetail")
    public AjaxResult exportLiveUserDetail(@RequestParam Long liveId) {
        List<LiveUserDetailExportVO> list = liveDataService.exportLiveUserDetail(liveId,null,null);
        if (list == null || list.isEmpty()) {
            return AjaxResult.error("未找到用户详情数据");
        }

        ExcelUtil<LiveUserDetailExportVO> util = new ExcelUtil<>(LiveUserDetailExportVO.class);
        return util.exportExcel(list, "直播间用户详情数据");
    }

}
