package com.fs.company.controller.live;

import java.util.*;

import com.fs.framework.security.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.framework.service.TokenService;
import com.fs.live.vo.ColumnsConfigVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.live.domain.LiveData;
import com.fs.live.service.ILiveDataService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 直播数据Controller
 *
 * @author fs
 * @date 2025-03-05
 */
@RestController
@RequestMapping("/liveData/liveData")
public class LiveDataController extends BaseController
{
    @Autowired
    private ILiveDataService liveDataService;
    @Autowired
    private TokenService tokenService;

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

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

    /**
     * 获取直播数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:query')")
    @GetMapping(value = "/{liveId}")
    public AjaxResult getInfo(@PathVariable("liveId") Long liveId)
    {
        return AjaxResult.success(liveDataService.selectLiveDataByLiveId(liveId));
    }

    /**
     * 新增直播数据
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:add')")
    @Log(title = "直播数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveData liveData)
    {
        return toAjax(liveDataService.insertLiveData(liveData));
    }

    /**
     * 修改直播数据
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:edit')")
    @Log(title = "直播数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveData liveData)
    {
        return toAjax(liveDataService.updateLiveData(liveData));
    }

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

    /**
     * 直播数据页面近期直播卡片数据
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:recentLive')")
    @GetMapping("/recentLive")
    public AjaxResult recentLive(){
        return AjaxResult.success(liveDataService.getRecentLive());
    }

    /**
     * 查询直播top榜数据
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:getLiveTop')")
    @GetMapping("/getLiveTop")
    public AjaxResult getLiveTop(String rankType){
        System.out.println(rankType);
        return AjaxResult.success(liveDataService.getLiveTop(rankType));
    }
    /**
     * 查询直播趋势数据
     * @param
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('liveData:liveData:getTrendData')")
    @PostMapping("/getTrendData")
    public AjaxResult getTrendData(@RequestBody Map<String,String> queryParams) {
        String type = queryParams.get("type");
        String date = queryParams.get("date");
        String category = queryParams.get("category");
        return AjaxResult.success(liveDataService.getTrendData(type, date,category));
    }
    /*@PreAuthorize("@ss.hasPermi('liveData:liveData:columns')")
    @GetMapping("/columns")
    public AjaxResult getTableColumns(HttpServletRequest request, HttpServletResponse response) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        //LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUser().getUserId();
        List<ColumnsConfigVo> columnsConfig = liveDataService.getColumnsConfig(userId.toString());
       return AjaxResult.success(columnsConfig);
    }*/

    @PostMapping("/updateColumns")
    public AjaxResult updateColumns(@RequestBody List<ColumnsConfigVo> columns, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUser().getUserId();
        liveDataService.saveColumnsConfig(userId.toString(), columns);
        return AjaxResult.success("列配置已保存");
    }



}
