package com.fs.live.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import com.fs.live.param.LiveTrafficLogParam;
import com.fs.live.vo.LiveTrafficLogListVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.LiveTrafficLog;
import com.fs.live.service.ILiveTrafficLogService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 直播流量记录Controller
 *
 * @author fs
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/live/trafficLog")
public class LiveTrafficLogController extends BaseController
{
    @Autowired
    private ILiveTrafficLogService liveTrafficLogService;

    /**
     * 查询直播流量记录列表
     */
    @PreAuthorize("@ss.hasPermi('live:trafficLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveTrafficLogParam param)
    {
        startPage();
        if (param.getTime() != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String formattedDate = sdf.format(param.getTime());
            String[] parts = formattedDate.split("-");
            param.setYear(Integer.parseInt(parts[0]));
            param.setMonth(Integer.parseInt(parts[1]));
        }
        List<LiveTrafficLogListVO> list = liveTrafficLogService.selectTrafficByCompany(param);
        return getDataTable(list);
    }

    /**
     * 导出直播流量记录列表
     */
    @PreAuthorize("@ss.hasPermi('live:trafficLog:export')")
    @Log(title = "直播流量记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LiveTrafficLogParam param)
    {
        if (param.getTime() != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String formattedDate = sdf.format(param.getTime());
            String[] parts = formattedDate.split("-");
            param.setYear(Integer.parseInt(parts[0]));
            param.setMonth(Integer.parseInt(parts[1]));
        }

        List<LiveTrafficLogListVO> list = liveTrafficLogService.selectTrafficByCompany(param);
        ExcelUtil<LiveTrafficLogListVO> util = new ExcelUtil<LiveTrafficLogListVO>(LiveTrafficLogListVO.class);
        return util.exportExcel(list, "直播流量记录数据");
    }

    /**
     * 获取直播流量记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('live:trafficLog:query')")
    @GetMapping(value = "/{logId}")
    public AjaxResult getInfo(@PathVariable("logId") Long logId)
    {
        return AjaxResult.success(liveTrafficLogService.selectLiveTrafficLogById(logId));
    }

    /**
     * 新增直播流量记录
     */
    @PreAuthorize("@ss.hasPermi('live:trafficLog:add')")
    @Log(title = "直播流量记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveTrafficLog liveTrafficLog)
    {
        return toAjax(liveTrafficLogService.insertLiveTrafficLog(liveTrafficLog));
    }

    /**
     * 修改直播流量记录
     */
    @PreAuthorize("@ss.hasPermi('live:trafficLog:edit')")
    @Log(title = "直播流量记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveTrafficLog liveTrafficLog)
    {
        return toAjax(liveTrafficLogService.updateLiveTrafficLog(liveTrafficLog));
    }

    /**
     * 删除直播流量记录
     */
    @PreAuthorize("@ss.hasPermi('live:trafficLog:remove')")
    @Log(title = "直播流量记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{logIds}")
    public AjaxResult remove(@PathVariable Long[] logIds)
    {
        return toAjax(liveTrafficLogService.deleteLiveTrafficLogByIds(logIds));
    }
}
