package org.jeecg.modules.adweb.seo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.adweb.common.util.DateUtil;
import org.jeecg.modules.adweb.seo.entity.SeoKeywordsSerp;
import org.jeecg.modules.adweb.seo.mapper.SeoKeywordsSerpMapper;
import org.jeecg.modules.adweb.seo.service.ISeoKeywordsSerpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: SEO关键词搜索排名
 * @Author: jeecg-boot
 * @Date: 2024-10-15
 * @Version: V1.0
 */
@Slf4j
@Service
public class SeoKeywordsSerpServiceImpl extends ServiceImpl<SeoKeywordsSerpMapper, SeoKeywordsSerp>
        implements ISeoKeywordsSerpService {

    @Autowired private SeoKeywordsSerpMapper seoKeywordsSerpMapper;

    /**
     * 向adweb_seo_keywords_serp表中补充数据，截止到seDatetime的前一天
     *
     * @param keywordId 关键词ID
     * @param seDatetime serp返回时间，此日期不需要填充
     */
    @Override
    public boolean fillKeywordsSerpHistory(int keywordId, Date seDatetime) {
        SeoKeywordsSerp latestSerp = seoKeywordsSerpMapper.getLatestSeoKeywordSerp(keywordId);
        if (Objects.isNull(latestSerp)) {
            return false;
        }

        // Serp最后更新时间 + 一天
        Date startDate =
                DateUtil.getTmrZeroTime(
                        DateUtil.parseDate(latestSerp.getSeDate(), DateUtil.DATE_FORMAT));
        // DateForSEO返回的SearchEngine时间
        Date endDate = DateUtil.getTodayZeroTime(seDatetime);

        List<SeoKeywordsSerp> serpsToFill = new ArrayList<>();
        for (Date currentDate = startDate;
                currentDate.compareTo(endDate) < 0;
                currentDate = DateUtil.addDays(currentDate, 1)) {
            // 设置成跟latestSerp相同的值
            SeoKeywordsSerp serp = new SeoKeywordsSerp();
            serp.setKeywordsId(keywordId);
            serp.setSiteCode(latestSerp.getSiteCode());
            serp.setSearchUrl(latestSerp.getSearchUrl());
            serp.setSeDomain(latestSerp.getSeDomain());
            serp.setLanguageCode(latestSerp.getLanguageCode());
            serp.setType(
                    StringUtils.appendIfMissing(latestSerp.getType(), "_copy")); // 标识出是复制的Serp值
            serp.setPageNumber(latestSerp.getPageNumber());
            serp.setRankGroup(latestSerp.getRankGroup());
            serp.setRankAbsolute(latestSerp.getRankAbsolute());
            serp.setSeDate(DateUtil.formatDate(currentDate, DateUtil.DATE_FORMAT)); // 复制Serp值的目标日期
            serp.setSeDatetime(latestSerp.getSeDatetime()); // 真实Serp返回的SearchingEngine时间
            serpsToFill.add(serp);
        }

        if (CollectionUtils.isNotEmpty(serpsToFill)) {
            log.info("复制Serp记录，keyword ID = {}, 时间范围 = {} to {}", keywordId, startDate, endDate);
            return this.saveBatch(serpsToFill);
        }

        return true;
    }
}
