package com.fs.course.controller;

import  com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.course.domain.FsCourseTrafficLog;
import com.fs.course.param.FsCourseTrafficLogParam;
import com.fs.course.service.IFsCourseTrafficLogService;
import com.fs.course.service.IFsUserCourseService;
import com.fs.course.vo.FsCourseTrafficLogListVO;
import com.fs.his.vo.OptionsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.List;


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

    @Autowired
    private IFsUserCourseService fsUserCourseMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询短链课程流量记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseTrafficLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCourseTrafficLogParam param)
    {
        startPage();
        List<FsCourseTrafficLogListVO> list = fsCourseTrafficLogService.selectTrafficNew(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) {
            YearMonth yearMonth = param.getTime();
            LocalDateTime startOfMonth = yearMonth.atDay(1).atStartOfDay();

            LocalDateTime startOfNextMonth = yearMonth.plusMonths(1).atDay(1).atStartOfDay()
                    .minusDays(1).withHour(23).withMinute(59).withSecond(59);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            param.setStartDate(startOfMonth.format(formatter));
            param.setEndDate(startOfNextMonth.format(formatter));
        }
        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));
    }

    @GetMapping("/courseList")
    public R courseList()
    {
        List<OptionsVO> optionsVOS = fsUserCourseMapper.selectFsUserCourseAllList();
        return R.ok().put("list", optionsVOS);
    }
}
