package com.moka.gdtauto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moka.gdtauto.entity.TencentAccountIdVideoId;
import com.moka.gdtauto.mapper.TencentAccountIdVideoIdMapper;
import com.moka.gdtauto.service.TencentAccountIdVideoIdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 腾讯账户上传视频记录 服务实现类
 * </p>
 *
 * @author mokamvp
 * @since 2026-03-10
 */
@Service
public class TencentAccountIdVideoIdServiceImp extends ServiceImpl<TencentAccountIdVideoIdMapper, TencentAccountIdVideoId> implements TencentAccountIdVideoIdService {

    private static final String CACHE_NAME = "tencentVideoId";

    @Override
    @Cacheable(value = CACHE_NAME, key = "#accountId + ':' + #fileMd5")
    public Long getVideoIdByAccountIdAndMd5(Long accountId, String fileMd5) {
        LambdaQueryWrapper<TencentAccountIdVideoId> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TencentAccountIdVideoId::getAccountId, accountId)
                .eq(TencentAccountIdVideoId::getFileMd5, fileMd5);
        TencentAccountIdVideoId record = this.getOne(queryWrapper);
        return record != null ? record.getVideoId() : null;
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "#accountId + ':' + #fileMd5")
    public void saveVideoId(Long accountId, String fileMd5, Long videoId, String videoUrl, Long coverImageId) {
        TencentAccountIdVideoId record = new TencentAccountIdVideoId();
        record.setAccountId(accountId);
        record.setFileMd5(fileMd5);
        record.setVideoId(videoId);
        record.setVideoUrl(videoUrl);
        record.setCoverImageId(coverImageId);
        record.setCreateTime(LocalDateTime.now());
        this.baseMapper.insertIgnore(record);
    }

    @Override
    public List<TencentAccountIdVideoId> batchGetByAccountIdsAndMd5s(List<Long> accountIds, List<String> fileMd5s) {
        if (accountIds == null || accountIds.isEmpty() || fileMd5s == null || fileMd5s.isEmpty()) {
            return Collections.emptyList();
        }
        return this.lambdaQuery()
                .in(TencentAccountIdVideoId::getAccountId, accountIds)
                .in(TencentAccountIdVideoId::getFileMd5, fileMd5s)
                .list();
    }
}
