package com.fs.company.controller.course;

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.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.course.domain.FsUserWatchCourseStatistics;
import com.fs.course.service.IFsUserWatchCourseStatisticsService;
import com.fs.course.vo.FsUserWatchCourseStatisticsExportVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员看课统计-按课程统计Controller
 *
 * @author fs
 * @date 2025-06-16
 */
@RestController
@RequestMapping("/course/userWatchCourseStatistics")
public class FsUserWatchCourseStatisticsController extends BaseController
{
    @Autowired
    private IFsUserWatchCourseStatisticsService fsUserWatchCourseStatisticsService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询会员看课统计-按课程统计列表
     */
    @PreAuthorize("@ss.hasPermi('course:userWatchCourseStatistics:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserWatchCourseStatistics fsUserWatchCourseStatistics)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsUserWatchCourseStatistics.setCompanyId( loginUser.getCompany().getCompanyId());
        List<FsUserWatchCourseStatistics> list = fsUserWatchCourseStatisticsService.selectFsUserWatchCourseStatisticsList(fsUserWatchCourseStatistics);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('course:userWatchCourseStatistics:export')")
    @Log(title = "会员看课统计-按课程统计", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserWatchCourseStatistics fsUserWatchCourseStatistics)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsUserWatchCourseStatistics.setCompanyId( loginUser.getCompany().getCompanyId());
        List<FsUserWatchCourseStatistics> list = fsUserWatchCourseStatisticsService.selectFsUserWatchCourseStatisticsList(fsUserWatchCourseStatistics);
        ExcelUtil<FsUserWatchCourseStatistics> util = new ExcelUtil<FsUserWatchCourseStatistics>(FsUserWatchCourseStatistics.class);
        return util.exportExcel(list, "会员观看数据明细");
    }

    /**
     * 查询会员观看数据明细汇总
     */
    @PreAuthorize("@ss.hasPermi('course:userWatchCourseStatistics:listTotal')")
    @GetMapping("/listTotal")
    public TableDataInfo listTotal(FsUserWatchCourseStatistics fsUserWatchCourseStatistics)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsUserWatchCourseStatistics.setCompanyId( loginUser.getCompany().getCompanyId());
        List<FsUserWatchCourseStatistics> list = fsUserWatchCourseStatisticsService.selectFsUserWatchCourseStatisticsListTotal(fsUserWatchCourseStatistics);
        if(!list.isEmpty()){
            for (FsUserWatchCourseStatistics userWatchCourseStatistics : list) {
                userWatchCourseStatistics.setCompleteWatchRatePercent(userWatchCourseStatistics.getCompleteWatchRate() + "%");
                userWatchCourseStatistics.setOnlineRatePercent(userWatchCourseStatistics.getOnlineRate() + "%");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出会员看课统计-按课程汇总统计列表
     */
    @Log(title = "会员看课统计-按课程汇总统计", businessType = BusinessType.EXPORT)
    @GetMapping("/exportTotal")
    public AjaxResult exportTotal(FsUserWatchCourseStatistics fsUserWatchCourseStatistics)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsUserWatchCourseStatistics.setCompanyId( loginUser.getCompany().getCompanyId());
        List<FsUserWatchCourseStatistics> list = fsUserWatchCourseStatisticsService.selectFsUserWatchCourseStatisticsListTotal(fsUserWatchCourseStatistics);
        List<FsUserWatchCourseStatisticsExportVO> listVO = list.stream().map(v -> {
            FsUserWatchCourseStatisticsExportVO vo = new FsUserWatchCourseStatisticsExportVO();
            BeanUtils.copyProperties(v, vo);
            vo.setCompleteWatchRatePercent(v.getCompleteWatchRate() + "%");
            vo.setOnlineRatePercent(v.getOnlineRate() + "%");
            return vo;
        }).collect(Collectors.toList());
        ExcelUtil<FsUserWatchCourseStatisticsExportVO> util = new ExcelUtil<FsUserWatchCourseStatisticsExportVO>(FsUserWatchCourseStatisticsExportVO.class);
        return util.exportExcel(listVO, "会员观看数据明细汇总");
    }
    /**
     * 获取会员看课统计-按课程统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:userWatchCourseStatistics:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsUserWatchCourseStatisticsService.selectFsUserWatchCourseStatisticsById(id));
    }

    /**
     * 新增会员看课统计-按课程统计
     */
    @PreAuthorize("@ss.hasPermi('course:userWatchCourseStatistics:add')")
    @Log(title = "会员看课统计-按课程统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserWatchCourseStatistics fsUserWatchCourseStatistics)
    {
        return toAjax(fsUserWatchCourseStatisticsService.insertFsUserWatchCourseStatistics(fsUserWatchCourseStatistics));
    }

    /**
     * 修改会员看课统计-按课程统计
     */
    @PreAuthorize("@ss.hasPermi('course:userWatchCourseStatistics:edit')")
    @Log(title = "会员看课统计-按课程统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUserWatchCourseStatistics fsUserWatchCourseStatistics)
    {
        return toAjax(fsUserWatchCourseStatisticsService.updateFsUserWatchCourseStatistics(fsUserWatchCourseStatistics));
    }

    /**
     * 删除会员看课统计-按课程统计
     */
    @PreAuthorize("@ss.hasPermi('course:userWatchCourseStatistics:remove')")
    @Log(title = "会员看课统计-按课程统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsUserWatchCourseStatisticsService.deleteFsUserWatchCourseStatisticsByIds(ids));
    }


    @PostMapping("/test")
    @ApiOperation("测试看课统计明细定时任务")
    public void userCourseCountTask() {
        fsUserWatchCourseStatisticsService.insertWatchCourseStatistics();
    }
}
