package com.fs.course.controller;

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

import com.fs.course.param.FsCourseTrafficLogParam;
import com.fs.course.vo.FsCourseTrafficLogListVO;
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.course.domain.FsCourseTrafficLog;
import com.fs.course.service.IFsCourseTrafficLogService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 短链课程流量记录Controller
 *
 * @author fs
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/course/courseTrafficLog")
public class FsCourseTrafficLogController extends BaseController
{
    @Autowired
    private IFsCourseTrafficLogService fsCourseTrafficLogService;

    /**
     * 查询短链课程流量记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseTrafficLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCourseTrafficLogParam 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<FsCourseTrafficLogListVO> list = fsCourseTrafficLogService.selectTrafficByCompany(param);
        return getDataTable(list);
    }

    /**
     * 导出短链课程流量记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseTrafficLog:export')")
    @Log(title = "短链课程流量记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCourseTrafficLogParam 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<FsCourseTrafficLogListVO> list = fsCourseTrafficLogService.selectTrafficByCompany(param);
        ExcelUtil<FsCourseTrafficLogListVO> util = new ExcelUtil<FsCourseTrafficLogListVO>(FsCourseTrafficLogListVO.class);
        return util.exportExcel(list, "短链课程流量记录数据");
    }

    /**
     * 获取短链课程流量记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:courseTrafficLog:query')")
    @GetMapping(value = "/{logId}")
    public AjaxResult getInfo(@PathVariable("logId") Long logId)
    {
        return AjaxResult.success(fsCourseTrafficLogService.selectFsCourseTrafficLogByLogId(logId));
    }

    /**
     * 新增短链课程流量记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseTrafficLog:add')")
    @Log(title = "短链课程流量记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCourseTrafficLog fsCourseTrafficLog)
    {
        return toAjax(fsCourseTrafficLogService.insertFsCourseTrafficLog(fsCourseTrafficLog));
    }

    /**
     * 修改短链课程流量记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseTrafficLog:edit')")
    @Log(title = "短链课程流量记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCourseTrafficLog fsCourseTrafficLog)
    {
        return toAjax(fsCourseTrafficLogService.updateFsCourseTrafficLog(fsCourseTrafficLog));
    }

    /**
     * 删除短链课程流量记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseTrafficLog:remove')")
    @Log(title = "短链课程流量记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{logIds}")
    public AjaxResult remove(@PathVariable Long[] logIds)
    {
        return toAjax(fsCourseTrafficLogService.deleteFsCourseTrafficLogByLogIds(logIds));
    }
}
