package com.fs.course.controller.qw;

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.exception.CustomException;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.course.domain.FsCourseWatchLog;
import com.fs.course.param.FsCourseOverParam;
import com.fs.course.param.FsCourseWatchLogListParam;
import com.fs.course.param.FsCourseWatchLogStatisticsListParam;
import com.fs.course.param.PeriodStatisticCountParam;
import com.fs.course.service.IFsCourseWatchLogService;
import com.fs.course.vo.FsCourseOverVO;
import com.fs.course.vo.FsCourseWatchLogListVO;
import com.fs.course.vo.FsCourseWatchLogStatisticsListByCompanyVO;
import com.fs.course.vo.FsCourseWatchLogStatisticsListVO;
import com.fs.qw.param.QwWatchLogStatisticsListParam;
import com.fs.qw.service.IQwWatchLogService;
import com.fs.qw.vo.QwWatchLogAllStatisticsListVO;
import com.fs.sop.domain.QwSop;
import com.fs.sop.service.IQwSopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 短链课程看课记录Controller
 *
 * @author fs
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/qw/course/courseWatchLog")
public class QwFsCourseWatchLogController extends BaseController
{
    @Value("${cloud_host.company_name}")
    private String signProjectName;

    @Autowired
    private IFsCourseWatchLogService fsCourseWatchLogService;

    @Autowired
    private IQwWatchLogService qwWatchLogService;

    @Autowired
    private IQwSopService qwSopService;
    /**
     * 查询短链课程看课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCourseWatchLogListParam param)
    {
        startPage();
        List<FsCourseWatchLogListVO> list = fsCourseWatchLogService.selectFsCourseWatchLogListVO(param);
        return getDataTable(list);
    }

    @GetMapping("/qwWatchLogAllStatisticsList")
    public TableDataInfo qwWatchLogAllStatisticsList(QwWatchLogStatisticsListParam param)
    {
        logger.info("企微课程数据汇总 参数:{}",param);
        startPage();
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        List<QwWatchLogAllStatisticsListVO> list = qwWatchLogService.selectQwWatchLogAllStatisticsListVO(param);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:statisticsList')")
    @GetMapping("/statisticsList")
    public TableDataInfo statisticsList(FsCourseWatchLogStatisticsListParam param)
    {
        startPage();
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        param.setSendType(2); //企微
        List<FsCourseWatchLogStatisticsListVO> list = fsCourseWatchLogService.selectFsCourseWatchLogStatisticsListVO(param);
        if("济南联志健康".equals(signProjectName)){
            FsCourseWatchLogStatisticsListVO totalData = fsCourseWatchLogService.getTotalDataAddItem(param);
            list.add(totalData);
        }
        return getDataTable(list);
    }

    /**
     * 会员看课统计导出
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:statisticsExport')")
    @Log(title = "企微看课统计导出", businessType = BusinessType.EXPORT)
    @PostMapping("/statisticsExport")
    public AjaxResult statisticsExport(@RequestBody FsCourseWatchLogStatisticsListParam param)
    {

        if (param.getSTime()==null||param.getETime()==null){
            throw new CustomException("必须选择开始时间和结束时间！");
        }

        param.setSendType(2); //企微
        List<FsCourseWatchLogStatisticsListVO> list = fsCourseWatchLogService.selectFsCourseWatchLogStatisticsListVO(param);
        if("济南联志健康".equals(signProjectName)){
            FsCourseWatchLogStatisticsListVO totalData = fsCourseWatchLogService.getTotalDataAddItem(param);
            list.add(totalData);
        }

        ExcelUtil<FsCourseWatchLogStatisticsListVO> util = new ExcelUtil<FsCourseWatchLogStatisticsListVO>(FsCourseWatchLogStatisticsListVO.class);

        return util.exportExcel(list, "企微看课统计");
    }



    @GetMapping("/getSignProjectName")
    public R getSignProjectName(){
        return R.ok().put("signProjectName", signProjectName);
    }

    @GetMapping("/statisticsListByCompany")
    public R statisticsListByCompany(FsCourseWatchLogStatisticsListParam param)
    {
        if (param.getSTime()==null||param.getETime()==null){
            return R.ok().put("rows", new ArrayList<>());
        }
        param.setSendType(2); //企微
        List<FsCourseWatchLogStatisticsListByCompanyVO> list = fsCourseWatchLogService.selectFsCourseWatchLogStatisticsListByCompanyVO(param);
        return R.ok().put("rows", list);
    }
    @GetMapping("/qwWatchLogStatisticsList")
    public TableDataInfo qwWatchLogStatisticsList(QwWatchLogStatisticsListParam param)
    {
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        if(param.getCompanyId() == null){
            throw new CustomException("必须选择公司!");
        }
        return qwWatchLogService.selectQwWatchLogStatisticsListVO(param);
    }
    /**
     * 导出短链课程看课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:export')")
    @Log(title = "短链课程看课记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCourseWatchLogListParam param)
    {
        List<FsCourseWatchLogListVO> list = fsCourseWatchLogService.selectFsCourseWatchLogListVO(param);
        ExcelUtil<FsCourseWatchLogListVO> util = new ExcelUtil<FsCourseWatchLogListVO>(FsCourseWatchLogListVO.class);
        return util.exportExcel(list, "短链课程看课记录数据");
    }

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

    /**
     * 新增短链课程看课记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:add')")
    @Log(title = "短链课程看课记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCourseWatchLog fsCourseWatchLog)
    {
        return toAjax(fsCourseWatchLogService.insertFsCourseWatchLog(fsCourseWatchLog));
    }

    /**
     * 修改短链课程看课记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:edit')")
    @Log(title = "短链课程看课记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCourseWatchLog fsCourseWatchLog)
    {
        return toAjax(fsCourseWatchLogService.updateFsCourseWatchLog(fsCourseWatchLog));
    }

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

//    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:list')")
    @GetMapping("/listBytrainingCampId")
    public TableDataInfo listBytrainingCampId(PeriodStatisticCountParam param)
    {
        startPage();
        List<FsCourseWatchLogListVO> list = fsCourseWatchLogService.selectListBytrainingCampId(param);
        return getDataTable(list);
    }

    @GetMapping("/watchLogStatistics")
    public TableDataInfo watchLogStatistics(FsCourseOverParam param)
    {
        startPage();
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        List<FsCourseOverVO> list = fsCourseWatchLogService.selectFsCourseWatchLogOverStatisticsListVO(param);
        return getDataTable(list);
    }

    /**
     * 查询档期列表
     */
    @GetMapping("/fetchScheduleList")
    public R fetchScheduleList(String sopName) {
        List<QwSop> list = qwSopService.fetchScheduleList(sopName);
        List<Map<String, Object>> result = list.stream().map(sop -> {
            Map<String, Object> map = new HashMap<>();
            map.put("qwSopId", sop.getId()); // 重命名id为qwSopId
            map.put("qwSopName", sop.getName());
            return map;
        }).collect(Collectors.toList());
        return R.ok().put("rows", result);
    }
}
