package org.jeecg.modules.spyderUtils.spider;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.jeecg.common.config.TenantContext;
import org.jeecg.modules.demo.externalinternalsite.service.IExternalInternalSiteService;
import org.jeecg.modules.demo.linkcaptureerrorlog.entity.LinkCaptureErrorLog;
import org.jeecg.modules.demo.linkcaptureerrorlog.service.ILinkCaptureErrorLogService;
import org.jeecg.modules.demo.linkcaptureerrormessage.entity.LinkCaptureErrorMessage;
import org.jeecg.modules.demo.linkcaptureerrormessage.service.ILinkCaptureErrorMessageService;
import org.jeecg.modules.demo.linkerrorwordstatistics.entity.LinkErrorWordStatistics;
import org.jeecg.modules.demo.linkerrorwordstatistics.service.ILinkErrorWordStatisticsService;
import org.jeecg.modules.demo.linkouterchaindetection.entity.LinkOuterChainDetection;
import org.jeecg.modules.demo.linkouterchainlog.entity.LinkOuterChainLog;
import org.jeecg.modules.demo.linkouterchainlog.service.ILinkOuterChainLogService;
import org.jeecg.modules.demo.linkpublishingtimegrasping.service.ILinkPublishingTimeGraspingService;
import org.jeecg.modules.demo.linkpublishingtitle.service.ILinkPublishingTitleService;
import org.jeecg.modules.demo.linkwhiteword.service.ILinkWhiteWordService;
import org.jeecg.modules.demo.mainsite.entity.MainSite;
import org.jeecg.modules.demo.mainsite.service.IMainSiteService;
import org.jeecg.modules.demo.pagecontentcapture.entity.PageContentCapture;
import org.jeecg.modules.demo.pagecontentcapture.service.IPageContentCaptureService;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@Component
public class LinkOrWordSaveUtils {
    @Autowired
    private IPageContentCaptureService pageContentCaptureService;

    @Autowired
    private IExternalInternalSiteService externalInternalSiteService;

    @Autowired
    private IPageContentCaptureService iPageContentCaptureService;

    @Autowired
    private ILinkPublishingTitleService linkPublishingTitleService;

    @Autowired
    private ILinkPublishingTimeGraspingService iLinkPublishingTimeGraspingService;

    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private ISysDictItemService iSysDictItemService;

    @Autowired
    private ILinkWhiteWordService iLinkWhiteWordService;
    @Autowired
    private ILinkCaptureErrorLogService iLinkCaptureErrorLogService;

    @Autowired
    private ILinkCaptureErrorMessageService iLinkCaptureErrorMessageService;

    @Autowired
    private ILinkErrorWordStatisticsService iLinkErrorWordStatisticsService;
    @Autowired
    private ILinkOuterChainLogService linkOuterChainLogService;

    @Autowired
    private IMainSiteService iMainSiteService;

    public boolean iLinkCaptureErrorLogServicesave(PageContentCapture pageContentCapture, String LinkCaptureErrorLogId ){
        LinkCaptureErrorLog linkCaptureErrorLog = new LinkCaptureErrorLog();
        linkCaptureErrorLog.setId(LinkCaptureErrorLogId);
        linkCaptureErrorLog.setCheckTime(new Date());
        linkCaptureErrorLog.setUpdateTime(new Date());
        try {
            linkCaptureErrorLog.setLinkPageId(pageContentCapture.getId());
        }catch (Exception e){
        }
        linkCaptureErrorLog.setSource(pageContentCapture.getSource());
        linkCaptureErrorLog.setTitle(pageContentCapture.getTitle());
        linkCaptureErrorLog.setTypeSource(pageContentCapture.getTypeSource());
        linkCaptureErrorLog.setStateSource(pageContentCapture.getStateSource());
        linkCaptureErrorLog.setMainUrl(pageContentCapture.getMainUrl());
        linkCaptureErrorLog.setSensitiveList(pageContentCapture.getSensitiveList());
        linkCaptureErrorLog.setSensitiveCount(pageContentCapture.getSensitiveCount());
        linkCaptureErrorLog.setRetCode(pageContentCapture.getRetCode());
        linkCaptureErrorLog.setErrorCount(pageContentCapture.getErrorCount());
        linkCaptureErrorLog.setHeavyCount(pageContentCapture.getHeavyCount());
        linkCaptureErrorLog.setModifyState(pageContentCapture.getModifyState());
        linkCaptureErrorLog.setArticleReleaseTime(pageContentCapture.getArticleReleaseTime());
        linkCaptureErrorLog.setNumberScans(pageContentCapture.getNumberScans());
        linkCaptureErrorLog.setIsIgnore(pageContentCapture.getIsIgnore());
        linkCaptureErrorLog.setCheckTime(pageContentCapture.getCheckTime());
        linkCaptureErrorLog.setCheckState(pageContentCapture.getCheckState());
        linkCaptureErrorLog.setCheckErrorCount(pageContentCapture.getCheckErrorCount());
        linkCaptureErrorLog.setTenantId(pageContentCapture.getTenantId());
        //扫描日志记录
        boolean save = iLinkCaptureErrorLogService.saveOrUpdate(linkCaptureErrorLog);
        return save;
    }
    public void LinkErrorWordStatisticsSave(MainSite mainSite, String type){
        QueryWrapper<PageContentCapture> captureQueryWrapper = new QueryWrapper<PageContentCapture>();
        captureQueryWrapper.isNotNull("sensitive_list");
        captureQueryWrapper.ne("sensitive_list","");
        captureQueryWrapper.eq("is_ignore","2");
        captureQueryWrapper.gt("error_count",0);
        captureQueryWrapper.eq("is_ignore","2");
        captureQueryWrapper.eq("modify_state","1");
        captureQueryWrapper.eq("main_url",mainSite.getMainSiteUrl());
        captureQueryWrapper.select("sum(sensitive_count) as sumAll");
        PageContentCapture onePageContentCapture = iPageContentCaptureService.getOne(captureQueryWrapper);
        LinkErrorWordStatistics linkErrorWordStatistics = new LinkErrorWordStatistics();
        linkErrorWordStatistics.setId(IdWorker.getIdStr());
        linkErrorWordStatistics.setCreateBy(mainSite.getCreateBy());
        linkErrorWordStatistics.setCreateTime(new Date());
        linkErrorWordStatistics.setUpdateTime(mainSite.getUpdateTime());
        linkErrorWordStatistics.setUpdateBy(mainSite.getUpdateBy());
        linkErrorWordStatistics.setMainSiteUrl(mainSite.getMainSiteUrl());
        linkErrorWordStatistics.setMainSiteId(mainSite.getId());
        linkErrorWordStatistics.setMainSiteName(mainSite.getMainSiteName());
        linkErrorWordStatistics.setTestType(type);
        linkErrorWordStatistics.setScanTime(new Date());
        linkErrorWordStatistics.setTenantId(onePageContentCapture.getTenantId());
        Integer sumAll = onePageContentCapture.getSumAll();
        if(!ObjectUtils.isEmpty(sumAll)){
            linkErrorWordStatistics.setErrorWordCount(sumAll);
        }else{
            linkErrorWordStatistics.setErrorWordCount(0);
        }
        linkErrorWordStatistics.setTenantId(mainSite.getTenantId());
        iLinkErrorWordStatisticsService.save(linkErrorWordStatistics);

    }
    public void LinkErrorWordStatisticsSave(MainSite mainSite, String type,String tenantId){
        QueryWrapper<PageContentCapture> captureQueryWrapper = new QueryWrapper<PageContentCapture>();
        captureQueryWrapper.isNotNull("sensitive_list");
        captureQueryWrapper.ne("sensitive_list","");
        captureQueryWrapper.eq("is_ignore","2");
        captureQueryWrapper.gt("error_count",0);
        captureQueryWrapper.eq("is_ignore","2");
        captureQueryWrapper.eq("modify_state","1");
        captureQueryWrapper.eq("main_url",mainSite.getMainSiteUrl());
        captureQueryWrapper.select("sum(sensitive_count) as sumAll");
        TenantContext.setTenant(tenantId);
        PageContentCapture onePageContentCapture = iPageContentCaptureService.getOne(captureQueryWrapper);
        LinkErrorWordStatistics linkErrorWordStatistics = new LinkErrorWordStatistics();
        linkErrorWordStatistics.setId(IdWorker.getIdStr());
        linkErrorWordStatistics.setCreateBy(mainSite.getCreateBy());
        linkErrorWordStatistics.setCreateTime(new Date());
        linkErrorWordStatistics.setUpdateTime(mainSite.getUpdateTime());
        linkErrorWordStatistics.setUpdateBy(mainSite.getUpdateBy());
        linkErrorWordStatistics.setMainSiteUrl(mainSite.getMainSiteUrl());
        linkErrorWordStatistics.setMainSiteId(mainSite.getId());
        linkErrorWordStatistics.setMainSiteName(mainSite.getMainSiteName());
        linkErrorWordStatistics.setTestType(type);
        linkErrorWordStatistics.setScanTime(new Date());
        linkErrorWordStatistics.setTenantId(onePageContentCapture.getTenantId());
        Integer sumAll = onePageContentCapture.getSumAll();
        if(!ObjectUtils.isEmpty(sumAll)){
            linkErrorWordStatistics.setErrorWordCount(sumAll);
        }else{
            linkErrorWordStatistics.setErrorWordCount(0);
        }
        linkErrorWordStatistics.setTenantId(mainSite.getTenantId());
        iLinkErrorWordStatisticsService.save(linkErrorWordStatistics);

    }
    public void saveLinkCaptureErrorMessage(String LinkCaptureErrorLogId,PageContentCapture pageContentCapture ){
        Document parse1 = Jsoup.parse(pageContentCapture.getRetMsg());
        Elements elements1 = parse1.select("span.proofreadSignClass.errorWordMark");
        for (Element element : elements1) {
            LinkCaptureErrorMessage linkCaptureErrorMessage = new LinkCaptureErrorMessage();
            linkCaptureErrorMessage.setErrorMessage(element.toString());
            linkCaptureErrorMessage.setErrerWord(element.text());
            linkCaptureErrorMessage.setLinkCaptureErrorLogId(LinkCaptureErrorLogId);
            linkCaptureErrorMessage.setId(DigestUtils.md5DigestAsHex((linkCaptureErrorMessage.getErrorMessage()+pageContentCapture.getId()).getBytes()));
            linkCaptureErrorMessage.setLinkPageId(pageContentCapture.getId());
            linkCaptureErrorMessage.setTenantId(pageContentCapture.getTenantId());
            iLinkCaptureErrorMessageService.saveOrUpdate(linkCaptureErrorMessage);
        }
    }
    public void savelinkOuterChainLog(LinkOuterChainDetection outerChainDetection,Integer tenantId){
        try {
            LinkOuterChainLog linkOuterChainLog = new LinkOuterChainLog();
            linkOuterChainLog.setId(IdWorker.getIdStr());
            linkOuterChainLog.setOuterChainDetectionId(outerChainDetection.getId());
            linkOuterChainLog.setCreateBy(outerChainDetection.getCreateBy());
            linkOuterChainLog.setCreateTime(outerChainDetection.getCreateTime());
            linkOuterChainLog.setUpdateBy(outerChainDetection.getUpdateBy());
            linkOuterChainLog.setUpdateTime(outerChainDetection.getUpdateTime());
            linkOuterChainLog.setMainSiteId(outerChainDetection.getMainSiteId());
            linkOuterChainLog.setMainSiteName(outerChainDetection.getMainSiteName());
            linkOuterChainLog.setMainSiteUrl(outerChainDetection.getMainSiteUrl());
            linkOuterChainLog.setOuterUrl(outerChainDetection.getOuterUrl());
            linkOuterChainLog.setOuterUrlType(outerChainDetection.getOuterUrlType());
            linkOuterChainLog.setOuterUrlState(outerChainDetection.getOuterUrlState());
            linkOuterChainLog.setNumberApplicationPages(outerChainDetection.getNumberApplicationPages());
            linkOuterChainLog.setNumberScans(outerChainDetection.getNumberScans());
            linkOuterChainLog.setIsRevise(outerChainDetection.getIsRevise());
            linkOuterChainLog.setIsIgnore(outerChainDetection.getIsIgnore());
            linkOuterChainLog.setCheckTime(outerChainDetection.getCheckTime());
            linkOuterChainLog.setCheckState(outerChainDetection.getCheckState());
            linkOuterChainLog.setCheckNumberApplicationPagesv(outerChainDetection.getCheckNumberApplicationPagesv());
            linkOuterChainLog.setCheckMessage(outerChainDetection.getCheckMessage());
            linkOuterChainLog.setTenantId(tenantId);
            linkOuterChainLogService.save(linkOuterChainLog);
        }catch (Exception e){

        }

    }
}
