package com.moka.gdtauto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moka.gdtauto.entity.TencentAccountIdImage;
import com.moka.gdtauto.mapper.TencentAccountIdImageMapper;
import com.moka.gdtauto.service.TencentAccountIdImageService;
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 TencentAccountIdImageServiceImp extends ServiceImpl<TencentAccountIdImageMapper, TencentAccountIdImage> implements TencentAccountIdImageService {

    private static final String CACHE_NAME = "tencentImageId";

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

    @Override
    @CacheEvict(value = CACHE_NAME, key = "#accountId + ':' + #fileMd5")
    public void saveImageId(Long accountId, String fileMd5, Long imageId, String imageUrl) {
        TencentAccountIdImage record = new TencentAccountIdImage();
        record.setAccountId(accountId);
        record.setFileMd5(fileMd5);
        record.setImageId(imageId);
        record.setImageUrl(imageUrl);
        record.setCreateTime(LocalDateTime.now());
        this.baseMapper.insertIgnore(record);
    }

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