package com.centreproject.system.service.guestExperience.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.centreproject.common.constant.GuestConstants;
import com.centreproject.common.constant.KeyConstants;
import com.centreproject.common.utils.DateUtils;
import com.centreproject.system.domain.kpi.guestExperience.KpiYasIslandCampaigns;
import com.centreproject.system.domain.kpi.socialMedia.GoogleReviewsPost;
import com.centreproject.system.mapper.kpi.KpiGoogleReviewsPostMapper;
import com.centreproject.system.mapper.kpi.KpiSocialMediaSentimentalMapper;
import com.centreproject.system.service.guestExperience.IKpiGoogleReviewsPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * get all google reviews post
 * @author zhonglei.xu
 */
@Service
public class KpiGoogleReviewsPostServiceImpl extends ServiceImpl<KpiGoogleReviewsPostMapper, GoogleReviewsPost>
        implements IKpiGoogleReviewsPostService {

    @Autowired
    private KpiGoogleReviewsPostMapper mapper;

    @Autowired
    private RedisTemplate<String, List<GoogleReviewsPost>>  redisTemplate;
    /**
     * get all google reviews post
     *
     * @return google reviews post
     */
    @Override
    public List<GoogleReviewsPost> getGoogleReviewsPost() {
        String redisKey = KeyConstants.GOOGLE_REVIEWS + DateUtils.getRiDate() + GuestConstants.LATEST_60_DAYS;

        // 1. 查缓存
        List<GoogleReviewsPost> cached = redisTemplate.opsForValue().get(redisKey);
        if (cached != null) {
            return cached;
        }

        // 2. 根据类型处理数据
        List<GoogleReviewsPost> result= mapper.googleReviewsPostLatest60Days();


        // 3. 写缓存（仅60天数据需要缓存）
        if (result != null ) {
            redisTemplate.opsForValue().set(redisKey, result, 1, TimeUnit.HOURS);
        }
        return result;
    }
}
