package com.fs.qw.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.company.mapper.CompanyUserMapper;
import com.fs.qw.domain.QwWatchLog;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qw.mapper.QwUserMapper;
import com.fs.qw.mapper.QwWatchLogMapper;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 企微看课Service业务层处理
 *
 * @author fs
 * @date 2025-03-08
 */
@Service
public class QwWatchLogServiceImpl extends ServiceImpl<QwWatchLogMapper, QwWatchLog> implements IQwWatchLogService {
    @Autowired
    private QwUserMapper qwUserMapper;
    @Autowired
    private QwWatchLogMapper qwWatchLogMapper;
    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;
    @Autowired
    private CompanyUserMapper companyUserMapper;
    /**
     * 查询企微看课
     *
     * @param id 企微看课主键
     * @return 企微看课
     */
    @Override
    public QwWatchLog selectQwWatchLogById(Long id)
    {
        return baseMapper.selectQwWatchLogById(id);
    }

    /**
     * 查询企微看课列表
     *
     * @param qwWatchLog 企微看课
     * @return 企微看课
     */
    @Override
    public List<QwWatchLog> selectQwWatchLogList(QwWatchLog qwWatchLog)
    {
        return baseMapper.selectQwWatchLogList(qwWatchLog);
    }

    /**
     * 新增企微看课
     *
     * @param qwWatchLog 企微看课
     * @return 结果
     */
    @Override
    public int insertQwWatchLog(QwWatchLog qwWatchLog)
    {
        qwWatchLog.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertQwWatchLog(qwWatchLog);
    }

    /**
     * 修改企微看课
     *
     * @param qwWatchLog 企微看课
     * @return 结果
     */
    @Override
    public int updateQwWatchLog(QwWatchLog qwWatchLog)
    {
        return baseMapper.updateQwWatchLog(qwWatchLog);
    }

    /**
     * 批量删除企微看课
     *
     * @param ids 需要删除的企微看课主键
     * @return 结果
     */
    @Override
    public int deleteQwWatchLogByIds(Long[] ids)
    {
        return baseMapper.deleteQwWatchLogByIds(ids);
    }

    /**
     * 删除企微看课信息
     *
     * @param id 企微看课主键
     * @return 结果
     */
    @Override
    public int deleteQwWatchLogById(Long id)
    {
        return baseMapper.deleteQwWatchLogById(id);
    }

    @Override
    public List<QwWatchLogStatisticsListVO> selectQwWatchLogStatisticsListVO(QwWatchLogStatisticsListParam param) {

        Date sTime = param.getSTime();
        Date eTime = param.getETime();
        List<Date> datesBetween = getDatesBetween(sTime, eTime);
        if (datesBetween.size() > 7) {
            return new ArrayList<>();
        }
        if (param.getCompanyUserId()!=null){
            param.setIds(companyUserMapper.selectQwUserIdsByCompany(param.getCompanyUserId()));
        }
        List<QwWatchLogStatisticsListVO> vos = qwWatchLogMapper.selectQwExtCountByDayAnd(param);
        for (QwWatchLogStatisticsListVO vo : vos) {
            Long id = vo.getId();
            Date createTime = vo.getCreateTime();
            QwWatchLogStatisticsListVO stat = qwWatchLogMapper.selectQwWatchLogByQwUserId(id, createTime);
            vo.setD1Online(stat.getD1Online());
            vo.setD1Over(stat.getD1Over());
            vo.setFirstOnline(stat.getFirstOnline());
            vo.setFirstOver(stat.getFirstOver());
        }





        return vos;
    }

    @Override
    public List<QwWatchLogAllStatisticsListVO> selectQwWatchLogAllStatisticsListVO(QwWatchLogStatisticsListParam param) {
        Date sTime = param.getSTime();
        Date eTime = param.getETime();
        List<Date> datesBetween = getDatesBetween(sTime, eTime);
        if (datesBetween.size() > 7) {
            return new ArrayList<>();
        }
        if (param.getCompanyUserId()!=null){
            param.setIds(companyUserMapper.selectQwUserIdsByCompany(param.getCompanyUserId()));
        }
        ArrayList<QwWatchLogAllStatisticsListVO> list = new ArrayList<>();
        List<QwWatchLogAllStatisticsListVO> vos = qwWatchLogMapper.selectQwExtCountByDayAndLine(param);
        for (QwWatchLogAllStatisticsListVO vo : vos) {
            Long id = vo.getId();
            Date createTime = vo.getCreateTime();
            QwWatchLogAllStatisticsListVO stat = qwWatchLogMapper.selectQwWatchLogAllStatisticsListVO(id, createTime);
            stat.setCreateTime(vo.getCreateTime());
            stat.setQwUserName(vo.getQwUserName());
            stat.setLine(vo.getLine());
            list.add(stat);
        }




        return list;
    }

    public List<Date> getDatesBetween(Date sTime, Date eTime) {
        List<Date> dates = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sTime);

        while (!calendar.getTime().after(eTime)) { // 遍历直到结束时间
            dates.add(calendar.getTime()); // 添加当前日期
            calendar.add(Calendar.DAY_OF_MONTH, 1); // 增加一天
        }

        return dates;
    }
}
