package com.fs.course.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsUserCourseVideoRedPackageMapper;
import com.fs.course.domain.FsUserCourseVideoRedPackage;
import com.fs.course.service.IFsUserCourseVideoRedPackageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程公司红包Service业务层处理
 *
 * @author fs
 * @date 2024-10-25
 */
@Service
public class FsUserCourseVideoRedPackageServiceImpl implements IFsUserCourseVideoRedPackageService
{
    @Autowired
    private FsUserCourseVideoRedPackageMapper fsUserCourseVideoRedPackageMapper;

    /**
     * 查询课程公司红包
     *
     * @param id 课程公司红包主键
     * @return 课程公司红包
     */
    @Override
    public FsUserCourseVideoRedPackage selectFsUserCourseVideoRedPackageById(Long id)
    {
        return fsUserCourseVideoRedPackageMapper.selectFsUserCourseVideoRedPackageById(id);
    }

    /**
     * 查询课程公司红包列表
     *
     * @param fsUserCourseVideoRedPackage 课程公司红包
     * @return 课程公司红包
     */
    @Override
    public List<FsUserCourseVideoRedPackage> selectFsUserCourseVideoRedPackageList(FsUserCourseVideoRedPackage fsUserCourseVideoRedPackage)
    {
        return fsUserCourseVideoRedPackageMapper.selectFsUserCourseVideoRedPackageList(fsUserCourseVideoRedPackage);
    }

    /**
     * 新增课程公司红包
     *
     * @param fsUserCourseVideoRedPackage 课程公司红包
     * @return 结果
     */
    @Override
    public int insertFsUserCourseVideoRedPackage(FsUserCourseVideoRedPackage fsUserCourseVideoRedPackage)
    {
        return fsUserCourseVideoRedPackageMapper.insertFsUserCourseVideoRedPackage(fsUserCourseVideoRedPackage);
    }

    /**
     * 修改课程公司红包
     *
     * @param fsUserCourseVideoRedPackage 课程公司红包
     * @return 结果
     */
    @Override
    public int updateFsUserCourseVideoRedPackage(FsUserCourseVideoRedPackage fsUserCourseVideoRedPackage)
    {
        return fsUserCourseVideoRedPackageMapper.updateFsUserCourseVideoRedPackage(fsUserCourseVideoRedPackage);
    }

    /**
     * 批量删除课程公司红包
     *
     * @param ids 需要删除的课程公司红包主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCourseVideoRedPackageByIds(Long[] ids)
    {
        return fsUserCourseVideoRedPackageMapper.deleteFsUserCourseVideoRedPackageByIds(ids);
    }

    /**
     * 删除课程公司红包信息
     *
     * @param id 课程公司红包主键
     * @return 结果
     */
    @Override
    public int deleteFsUserCourseVideoRedPackageById(Long id)
    {
        return fsUserCourseVideoRedPackageMapper.deleteFsUserCourseVideoRedPackageById(id);
    }

    @Override
    public int batchSaveFsUserCourseVideoRedPackage(List<FsUserCourseVideoRedPackage> fsUserCourseVideoRedPackageList) {
        return fsUserCourseVideoRedPackageMapper.batchSaveFsUserCourseVideoRedPackage(fsUserCourseVideoRedPackageList);
    }

    @Override
    public int batchDeleteParams(List<FsUserCourseVideoRedPackage> videoRedPackageList) {
        return fsUserCourseVideoRedPackageMapper.batchDeleteParams(videoRedPackageList);
    }

    @Override
    @Transactional
    public int batchSaveCompanyRedPackage(List<FsUserCourseVideoRedPackage> fsUserCourseVideoRedPackageList) {
        if (fsUserCourseVideoRedPackageList == null || fsUserCourseVideoRedPackageList.isEmpty()) {
            return 0;
        }

        // 一次性查询所有匹配的数据
        List<FsUserCourseVideoRedPackage> existingData = fsUserCourseVideoRedPackageMapper
                .selectByParamsList(fsUserCourseVideoRedPackageList);

        // 创建匹配键集合，用于辅助快速查找
        Set<String> existingKeys = new HashSet<>();
        for (FsUserCourseVideoRedPackage data : existingData) {
            String key = generateKey(data.getPeriodId(), data.getVideoId(), data.getCompanyId());
            existingKeys.add(key);
        }

        // 分离需要更新和需要新增的数据
        List<FsUserCourseVideoRedPackage> updateList = new ArrayList<>();
        List<FsUserCourseVideoRedPackage> insertList = new ArrayList<>();

        for (FsUserCourseVideoRedPackage redPackage : fsUserCourseVideoRedPackageList) {
            String key = generateKey(redPackage.getPeriodId(), redPackage.getVideoId(), redPackage.getCompanyId());

            if (existingKeys.contains(key)) {
                // 存在则加入更新列表
                updateList.add(redPackage);
            } else {
                // 不存在则加入新增列表
                insertList.add(redPackage);
            }
        }

        int result = 0;
        if (!updateList.isEmpty()) {
            result += fsUserCourseVideoRedPackageMapper.batchUpdateFsUserCourseVideoRedPackage(updateList);
        }
        if (!insertList.isEmpty()) {
            result += fsUserCourseVideoRedPackageMapper.batchSaveFsUserCourseVideoRedPackage(insertList);
        }
        return result;
    }

    private String generateKey(Long periodId, Long videoId, Long companyId) {
        return periodId + "_" + videoId + "_" + companyId;
    }

    @Override
    public List<FsUserCourseVideoRedPackage> listByCompanyIdAndVideoIds(Long companyId, List<Long> videoIdList) {
        return fsUserCourseVideoRedPackageMapper.listByCompanyIdAndVideoIds(companyId, videoIdList);
    }

    @Override
    public List<FsUserCourseVideoRedPackage> listByRuleIds(List<Long> ids) {
        return fsUserCourseVideoRedPackageMapper.listByRuleIds(ids);
    }
}
