package com.moka.gdtauto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moka.gdtauto.entity.TencentAccountIdBrandImage;
import com.moka.gdtauto.mapper.TencentAccountIdBrandImageMapper;
import com.moka.gdtauto.service.TencentAccountIdBrandImageService;
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-11
 */
@Service
public class TencentAccountIdBrandImageServiceImp extends ServiceImpl<TencentAccountIdBrandImageMapper, TencentAccountIdBrandImage> implements TencentAccountIdBrandImageService {

    private static final String CACHE_NAME = "tencentBrandImageId";

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

    @Override
    @CacheEvict(value = CACHE_NAME, key = "#accountId + ':' + #fileMd5")
    public void saveBrandImageId(Long accountId, String fileMd5, Long imageId, String imageUrl, Long width, Long height) {
        TencentAccountIdBrandImage record = new TencentAccountIdBrandImage();
        record.setAccountId(accountId);
        record.setFileMd5(fileMd5);
        record.setImageId(imageId);
        record.setImageUrl(imageUrl);
        record.setWidth(width != null ? width.intValue() : null);
        record.setHeight(height != null ? height.intValue() : null);
        record.setCreateTime(LocalDateTime.now());
        this.baseMapper.insertIgnore(record);
    }

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