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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.adweb.seo.entity.SeoKeywords;
import org.jeecg.modules.adweb.seo.mapper.SeoKeywordsMapper;
import org.jeecg.modules.adweb.seo.service.ISeoKeywordsService;
import org.jeecg.modules.adweb.seo.vo.RankInfoVO;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

/**
 * @Description: 关键词
 * @Author: jeecg-boot
 * @Date:   2024-10-15
 * @Version: V1.0
 */
@Service
public class SeoKeywordsServiceImpl extends ServiceImpl<SeoKeywordsMapper, SeoKeywords> implements ISeoKeywordsService {

    @Resource private SeoKeywordsMapper seoKeywordsMapper;

    /**
     * 获取对应站点关键词最新七天时间
     * @param userFlag
     * @param keywordType
     * @param historyId
     * @return
     */
    @Override
    public List<String> getKeywordsLastSevenDays(String userFlag, Integer keywordType, String historyId){
        //获取对应站点最近七天
        List<String> dateList = seoKeywordsMapper.getKeywordsLastSevenDays(userFlag, historyId,keywordType);
        return dateList;
    }

    /**
     * 获取一个站点的达标关键词排名数量
     *
     * @param siteCode 站点code
     * @return 关键词排名数量
     */
    @Override
    public RankInfoVO getSeoRankInfo(String siteCode, String historyId, Integer rankStart, Integer rankEnd, String dateStr, String keywordType) {
        //指定词
        return seoKeywordsMapper.queryKeywordNumByRankRange(siteCode, historyId,rankStart,rankEnd,dateStr,keywordType);
    }

    /**
     * 获取关键词列表
     * @return 关键词列表
     */
    @Override
    public IPage<SeoKeywords> getKeywordList(IPage<SeoKeywords> page,
                                             Integer rankStart,
                                             Integer rankEnd,
                                             String keyword,
                                             String siteCode,
                                             Integer keywordType,
                                             boolean reachStandard,
                                             String historyId,
                                             String column,
                                             String order,
                                             String buttonColumn,
                                             String buttonSort,
                                             List<String> dateList) throws ParseException {
        if (reachStandard) {
            rankStart = 1;
            int standard = 10;
            if (keywordType != null && keywordType == 1) {
                standard = seoKeywordsMapper.getKeywordStandard(siteCode);
            }
            rankEnd = standard;
        }

        boolean isCustomer = true;

        String datesStr = "";
        if(CollectionUtils.isEmpty(dateList)){
            return null;
        }else{
            for(int i = 0; i < dateList.size(); i++){
                if(i == dateList.size() - 1){
                    datesStr +=  "'" + dateList.get(i) + "'";
                }else{
                    datesStr +=  "'" + dateList.get(i) + "'" + ",";
                }
            }
            datesStr = "(" + datesStr + ")";
        }

        return seoKeywordsMapper.getKeywordList(page, rankStart, rankEnd, isCustomer, keyword, siteCode, keywordType, datesStr, historyId,column,order,buttonColumn,buttonSort);
    }

    /**
     * 关键词管理列表数据
     * @param page
     * @param siteCode
     * @param keywords
     * @param historyId
     * @param lastSearchTime
     * @param createTime
     * @param keywordType
     * @param column
     * @param order
     * @param keywordRank
     * @return
     */
    @Override
    public IPage<SeoKeywords> pageList(Page<SeoKeywords> page, String siteCode, String keywords, String historyId, String lastSearchTime, String createTime, Integer keywordType,
                                       String column, String order, Integer keywordRank){
        if(column.equals("createTime")){
            column = "create_time";
            return seoKeywordsMapper.getPageListByCreateTime(page,siteCode,keywords,historyId,lastSearchTime,createTime,keywordType,column,order,keywordRank);
        }else if(column.equals("lastSearchTime")){
            column = "last_search_time";
            return seoKeywordsMapper.getPageListByCreateTime(page,siteCode,keywords,historyId,lastSearchTime,createTime,keywordType,column,order,keywordRank);
        }else if (column.equals("lastRank")){
            column = "last_rank";
        } else if (column.equals("keywords")){
            column = "keywords";
        }
        return seoKeywordsMapper.getPageList(page,siteCode,keywords,historyId,lastSearchTime,createTime,keywordType,column,order,keywordRank);
    }
}
