package com.datacenter.service.impl;

import com.datacenter.mapper.ExperienceGiftExpireMapper;
import com.datacenter.service.ExperienceGiftExpireService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class ExperienceGiftExpireServiceImpl implements ExperienceGiftExpireService {

    private final ExperienceGiftExpireMapper expireMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int expireUnusedRecords() {
        // 先查询待过期的 ID 列表，用于日志追踪
        List<Long> ids = expireMapper.findExpiredUnusedIds();
        if (ids.isEmpty()) {
            return 0;
        }
        log.info("体验金到期处理：共 {} 张待过期券，id={}", ids.size(), ids);

        int updated = expireMapper.expireUnused();
        log.info("体验金到期处理：成功置为已过期 {} 张", updated);
        return updated;
    }
}
