package com.fs.his.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.FsUser;
import com.fs.his.domain.FsUserOnlineState;
import com.fs.his.mapper.FsUserOnlineStateMapper;
import com.fs.his.service.IFsUserOnlineStateService;
import com.google.common.collect.Lists;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 用户上线情况Service业务层处理
 *
 * @author fs
 * @date 2025-06-30
 */
@Service
public class FsUserOnlineStateServiceImpl extends ServiceImpl<FsUserOnlineStateMapper, FsUserOnlineState> implements IFsUserOnlineStateService {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询用户上线情况
     *
     * @param userId 用户上线情况主键
     * @return 用户上线情况
     */
    @Override
    public FsUserOnlineState selectFsUserOnlineStateById(Long userId)
    {
        return baseMapper.selectFsUserOnlineStateById(userId);
    }

    /**
     * 查询用户上线情况列表
     *
     * @param fsUserOnlineState 用户上线情况
     * @return 用户上线情况
     */
    @Override
    public List<FsUserOnlineState> selectFsUserOnlineStateList(FsUserOnlineState fsUserOnlineState)
    {
        return baseMapper.selectFsUserOnlineStateList(fsUserOnlineState);
    }

    /**
     * 新增用户上线情况
     *
     * @param fsUserOnlineState 用户上线情况
     * @return 结果
     */
    @Override
    public int insertFsUserOnlineState(FsUserOnlineState fsUserOnlineState)
    {
        fsUserOnlineState.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsUserOnlineState(fsUserOnlineState);
    }

    /**
     * 修改用户上线情况
     *
     * @param fsUserOnlineState 用户上线情况
     * @return 结果
     */
    @Override
    public int updateFsUserOnlineState(FsUserOnlineState fsUserOnlineState)
    {
        return baseMapper.updateFsUserOnlineState(fsUserOnlineState);
    }

    /**
     * 批量删除用户上线情况
     *
     * @param userIds 需要删除的用户上线情况主键
     * @return 结果
     */
    @Override
    public int deleteFsUserOnlineStateByIds(Long[] userIds)
    {
        return baseMapper.deleteFsUserOnlineStateByIds(userIds);
    }

    /**
     * 删除用户上线情况信息
     *
     * @param userId 用户上线情况主键
     * @return 结果
     */
    @Override
    public int deleteFsUserOnlineStateById(Long userId)
    {
        return baseMapper.deleteFsUserOnlineStateById(userId);
    }

    @Override
    public void insertUserNotOnline() {
        // 1、获取需要新增/更新的数据
        // 获取当前所有没有看课记录的用户
        List<FsUserOnlineState> fsUserOnlineStates = baseMapper.selectUserNotOnline();

        // 2、分批次新增
        this.batchInsertOnline(fsUserOnlineStates);

        // 3、移除有看课记录的用户
        List<FsUser> fsUsers = baseMapper.selectExistWatchLogUser();
        List<Long> moveUserIds = fsUsers.stream().map(FsUser::getUserId).collect(Collectors.toList());
        if(!moveUserIds.isEmpty()){
            baseMapper.deleteFsUserOnlineStateByIds(moveUserIds.toArray(new Long[0]));
        }

    }

    private void batchInsertOnline(List<FsUserOnlineState> list) {
        // 分批次处理，一次提交500条
        List<List<FsUserOnlineState>> batches = Lists.partition(list, 500);
        batches.forEach(batch -> {
            SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
            try {
                FsUserOnlineStateMapper mapper = sqlSession.getMapper(FsUserOnlineStateMapper.class);
                batch.forEach(mapper::insertFsUserOnlineState);
                sqlSession.commit();
            } finally {
                sqlSession.close();
            }
        });
    }
}
