package com.fs.store.service.impl;

import java.util.List;
import com.fs.common.utils.DateUtils;
import com.fs.course.mapper.FsCourseWatchLogMapper;
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 com.fs.store.mapper.FsUserCourseCountMapper;
import com.fs.store.domain.FsUserCourseCount;
import com.fs.store.service.IFsUserCourseCountService;

/**
 * 用户看课统计Service业务层处理
 *
 * @author fs
 * @date 2025-04-02
 */
@Service
public class FsUserCourseCountServiceImpl implements IFsUserCourseCountService
{
    @Autowired
    private FsUserCourseCountMapper fsUserCourseCountMapper;

    @Autowired
    private FsCourseWatchLogMapper fsCourseWatchLogMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询用户看课统计
     *
     * @param id 用户看课统计ID
     * @return 用户看课统计
     */
    @Override
    public FsUserCourseCount selectFsUserCourseCountById(Long id)
    {
        return fsUserCourseCountMapper.selectFsUserCourseCountById(id);
    }

    /**
     * 查询用户看课统计列表
     *
     * @param fsUserCourseCount 用户看课统计
     * @return 用户看课统计
     */
    @Override
    public List<FsUserCourseCount> selectFsUserCourseCountList(FsUserCourseCount fsUserCourseCount)
    {
        return fsUserCourseCountMapper.selectFsUserCourseCountList(fsUserCourseCount);
    }

    /**
     * 新增用户看课统计
     *
     * @param fsUserCourseCount 用户看课统计
     * @return 结果
     */
    @Override
    public int insertFsUserCourseCount(FsUserCourseCount fsUserCourseCount)
    {
        fsUserCourseCount.setCreateTime(DateUtils.getNowDate());
        return fsUserCourseCountMapper.insertFsUserCourseCount(fsUserCourseCount);
    }

    /**
     * 修改用户看课统计
     *
     * @param fsUserCourseCount 用户看课统计
     * @return 结果
     */
    @Override
    public int updateFsUserCourseCount(FsUserCourseCount fsUserCourseCount)
    {
        fsUserCourseCount.setUpdateTime(DateUtils.getNowDate());
        return fsUserCourseCountMapper.updateFsUserCourseCount(fsUserCourseCount);
    }

    /**
     * 批量删除用户看课统计
     *
     * @param ids 需要删除的用户看课统计ID
     * @return 结果
     */
    @Override
    public int deleteFsUserCourseCountByIds(Long[] ids)
    {
        return fsUserCourseCountMapper.deleteFsUserCourseCountByIds(ids);
    }

    /**
     * 删除用户看课统计信息
     *
     * @param id 用户看课统计ID
     * @return 结果
     */
    @Override
    public int deleteFsUserCourseCountById(Long id)
    {
        return fsUserCourseCountMapper.deleteFsUserCourseCountById(id);
    }

    @Override
    public FsUserCourseCount findByUserId(Long userId) {
        return fsUserCourseCountMapper.findByUserId(userId);
    }

    @Override
    public void insertFsUserCourseCountTask() {
        // 1、获取统计结果
        List<FsUserCourseCount> countResult = fsUserCourseCountMapper.getCountResult();

        // 2、分批插入数据
        this.batchInsert(countResult);

    }

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