package com.fs.company.controller.course;

import cn.hutool.core.util.ObjectUtil;
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.company.service.impl.CompanyDeptServiceImpl;
import com.fs.course.domain.FsCourseWatchLog;
import com.fs.course.param.*;
import com.fs.course.service.IFsCourseWatchLogService;
import com.fs.course.service.IFsUserCoursePeriodDaysService;
import com.fs.course.service.IFsUserCoursePeriodService;
import com.fs.course.vo.FsCourseOverVO;
import com.fs.course.vo.FsCourseUserStatisticsListVO;
import com.fs.course.vo.FsCourseWatchLogListVO;
import com.fs.course.vo.FsCourseWatchLogStatisticsListVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.qw.param.QwWatchLogStatisticsListParam;
import com.fs.qw.service.IQwWatchLogService;
import com.fs.qw.vo.QwWatchLogAllStatisticsListVO;
import com.fs.qw.vo.QwWatchLogStatisticsListVO;
import com.fs.sop.mapper.SopUserLogsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 短链课程看课记录Controller
 *
 * @author fs
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/course/courseWatchLog")
public class FsCourseWatchLogController extends BaseController
{
    @Autowired
    private IFsCourseWatchLogService fsCourseWatchLogService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private SopUserLogsMapper sopUserLogsMapper;
    @Autowired
    private IQwWatchLogService qwWatchLogService;

    @Autowired
    private CompanyDeptServiceImpl companyDeptService;

    @Autowired
    private IFsUserCoursePeriodService userCoursePeriodService;

    @Autowired
    private IFsUserCoursePeriodDaysService userCoursePeriodDaysService;

    /**
     * 查询短链课程看课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCourseWatchLogListParam param)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());

        if (param.getSendType()==1&& param.getPeriodETime()!=null && param.getPeriodSTime()!=null) {
            List<Long> periodIds = userCoursePeriodDaysService.selectFsUserCoursePeriodDaysByTime(param.getPeriodSTime(), param.getPeriodETime());

            if (!periodIds.isEmpty()){
                List<Long> longs = userCoursePeriodService.selectFsUserCoursePeriodListByPeriodId(periodIds, loginUser.getCompany().getCompanyId());
                if (!longs.isEmpty()){
                    param.setPeriodIds(longs);
                }else {
                    return getDataTable(new ArrayList<>());
                }
            }else {
                return getDataTable(new ArrayList<>());
            }

        }

        startPage();
        List<FsCourseWatchLogListVO> list = fsCourseWatchLogService.selectFsCourseWatchLogListVO(param);
        return getDataTable(list);
    }


    /**
     * 查询短链课程 我的部门 看课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:deptList')")
    @GetMapping("/deptList")
    public TableDataInfo deptList(FsCourseWatchLogListParam param) {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());


        List<Long> combinedList = new ArrayList<>();
        //本部门
        Long deptId = loginUser.getUser().getDeptId();
        if (deptId!=null){
            combinedList.add(deptId);
        }
        //本部门的下级部门
        List<Long> deptList = companyDeptService.selectCompanyDeptByParentId(deptId);
        if (!deptList.isEmpty()){
            combinedList.addAll(deptList);
        }

        param.setCuDeptIdList(combinedList);
        param.setUserType(loginUser.getUser().getUserType());
        param.setCompanyId(loginUser.getCompany().getCompanyId());


        if (ObjectUtil.isNotEmpty(param.getSendType()) &&param.getSendType()==1&& param.getPeriodETime()!=null && param.getPeriodSTime()!=null) {
            List<Long> periodIds = userCoursePeriodDaysService.selectFsUserCoursePeriodDaysByTime(param.getPeriodSTime(), param.getPeriodETime());

            if (!periodIds.isEmpty()){
                List<Long> longs = userCoursePeriodService.selectFsUserCoursePeriodListByPeriodId(periodIds, loginUser.getCompany().getCompanyId());
                if (!longs.isEmpty()){
                    param.setPeriodIds(longs);
                }else {
                    return getDataTable(new ArrayList<>());
                }
            }else {
                return getDataTable(new ArrayList<>());
            }

        }


        startPage();
        List<FsCourseWatchLogListVO> list = fsCourseWatchLogService.selectFsCourseWatchLogListVO(param);

        return getDataTable(list);
    }
    /**
     * 查询短链课程看课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(FsCourseWatchLogListParam param)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyUserId( loginUser.getUser().getUserId());

        if (param.getSendType()==1&& param.getPeriodETime()!=null && param.getPeriodSTime()!=null) {
            List<Long> periodIds = userCoursePeriodDaysService.selectFsUserCoursePeriodDaysByTime(param.getPeriodSTime(), param.getPeriodETime());
            if (!periodIds.isEmpty()){
                List<Long> longs = userCoursePeriodService.selectFsUserCoursePeriodListByPeriodId(periodIds, loginUser.getCompany().getCompanyId());
                if (!longs.isEmpty()){
                    param.setPeriodIds(longs);
                }else {
                    return getDataTable(new ArrayList<>());
                }
            }else {
                return getDataTable(new ArrayList<>());
            }

        }

        startPage();

        List<FsCourseWatchLogListVO> list = fsCourseWatchLogService.selectFsCourseWatchLogListVO(param);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:statisticsList')")
    @GetMapping("/statisticsList")
    public TableDataInfo statisticsList(FsCourseWatchLogStatisticsListParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        param.setSendType(1); //个微
        List<FsCourseWatchLogStatisticsListVO> list = fsCourseWatchLogService.selectFsCourseWatchLogStatisticsListVO(param);
        return getDataTable(list);
    }

    @GetMapping("/qwWatchLogStatisticsList")
    public TableDataInfo qwWatchLogStatisticsList(QwWatchLogStatisticsListParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        return qwWatchLogService.selectQwWatchLogStatisticsListVONew(param);
    }
    @GetMapping("/myQwWatchLogStatisticsList")
    public TableDataInfo myQwWatchLogStatisticsList(QwWatchLogStatisticsListParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        return qwWatchLogService.selectQwWatchLogStatisticsListVONew(param);
    }


    @GetMapping("/qwWatchLogAllStatisticsList")
    public TableDataInfo qwWatchLogAllStatisticsList(QwWatchLogStatisticsListParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        return qwWatchLogService.selectQwWatchLogAllStatisticsListVONew(param);
    }
    @GetMapping("/myQwWatchLogAllStatisticsList")
    public TableDataInfo myQwWatchLogAllStatisticsList(QwWatchLogStatisticsListParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        return qwWatchLogService.selectQwWatchLogAllStatisticsListVONew(param);
    }
    @GetMapping("/watchLogStatistics")
    public TableDataInfo watchLogStatistics(FsCourseOverParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        List<FsCourseOverVO> list = fsCourseWatchLogService.selectFsCourseWatchLogOverStatisticsListVO(param);
        return getDataTable(list);
    }
    @GetMapping("/watchLogStatisticsExport")
    public AjaxResult watchLogStatisticsExport(FsCourseOverParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        if (param.getSTime()==null||param.getETime()==null){
            return AjaxResult.error("请选择时间");
        }
        List<FsCourseOverVO> list = fsCourseWatchLogService.selectFsCourseWatchLogOverStatisticsListVO(param);
        ExcelUtil<FsCourseOverVO> util = new ExcelUtil<FsCourseOverVO>(FsCourseOverVO.class);
        return util.exportExcel(list, "完课数据");
    }


    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:userStatisticsList')")
    @GetMapping("/userStatisticsList")
    public TableDataInfo userStatisticsList(FsCourseUserStatisticsListParam param)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        if (param.getSTime()==null||param.getETime()==null){
            return getDataTable(new ArrayList<>());
        }
        List<FsCourseUserStatisticsListVO> list = fsCourseWatchLogService.selectFsCourseUserStatisticsListVO(param);
        return getDataTable(list);
    }



    /**
     * 导出短链课程看课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:export')")
    @Log(title = "短链课程看课记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCourseWatchLogListParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        List<FsCourseWatchLogListVO> list = fsCourseWatchLogService.selectFsCourseWatchLogListVOexport(param);
        ExcelUtil<FsCourseWatchLogListVO> util = new ExcelUtil<FsCourseWatchLogListVO>(FsCourseWatchLogListVO.class);
        return util.exportExcel(list, "短链课程看课记录数据");
    }

    /**
     * 导出短链课程看课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseWatchLog:myExport')")
    @Log(title = "短链课程看课记录", businessType = BusinessType.EXPORT)
    @GetMapping("/myExport")
    public AjaxResult myExport(FsCourseWatchLogListParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        param.setCompanyUserId( loginUser.getUser().getUserId());
        List<FsCourseWatchLogListVO> list = fsCourseWatchLogService.selectFsCourseWatchLogListVOexport(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));
    }
}
