package com.yuchen.saas.park.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yuchen.saas.park.api.entity.park.PmgBatParkNotice;
import com.yuchen.saas.park.service.mapper.PmgBatParkNoticeMapper;
import com.yuchen.saas.park.service.service.PmgBatParkNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;


/**
 * @author 刘通
 * @date 2020/12/7 17:57
 */
@Slf4j
@Service
public class PmgBatParkNoticeServiceImpl extends BaseServiceImpl<PmgBatParkNoticeMapper, PmgBatParkNotice> implements PmgBatParkNoticeService {






    @Override
    public IPage<PmgBatParkNotice> list(PmgBatParkNotice pmgBatParkNotice, Query query){
        QueryWrapper<PmgBatParkNotice> queryWrapper = Condition.getQueryWrapper(pmgBatParkNotice);
        queryWrapper.orderByDesc("release_time");
        IPage<PmgBatParkNotice> page=this.page(Condition.getPage(query),queryWrapper);
             page.getRecords().forEach(t->{
                 t.setContent("");
             });
            return page;
    }

    public  void copy(String url, CloseableHttpClient httpClient, int k, String className,int type){
          String url2=url;
        try {
            for (int i = 0; i < k ; i++) {
                //  String url = "http://192.168.0.230:530/xmtorch/"; // 你要爬取的网页URL
             //   http://xmtorch.xm.gov.cn/gxqdt_82627/82734/index "list_article"
                if(i==0) {
                    url = url2+"index"+".html";
                }else{
                    url = url2+"index"+"_"+i+".html";
                }
                HttpGet httpGet = new HttpGet(url);
                CloseableHttpResponse response = httpClient.execute(httpGet);

                try {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        String html = EntityUtils.toString(entity);
                        Document doc = Jsoup.parse(html,"UTF-8");
                        Elements els= doc.getElementsByClass(className);
                        Element element =els.get(0);
                        extracted( url2, element,"detail_title","Custom_UnionStyle",type);
                    }
                } finally {
                    response.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public  void  copy2(){
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //copy("http://xmtorch.xm.gov.cn/ztzl/aqyq/aqdt/",httpClient,1,"list_article",8);

       // copy("http://xmtorch.xm.gov.cn/gxqdt_82627/mtjj/82690/",httpClient,125,"list_article",1);

      //  crawlWebPage();
    }



   /* public static void main(String[] args) {
        CloseableHttpClient httpClient = HttpClients.createDefault();

      //  copy("http://xmtorch.xm.gov.cn/gxqdt_82627/82734/",httpClient,125,"list_article");

      //  copy("http://xmtorch.xm.gov.cn/gxqdt_82627/mtjj/82690/",httpClient,125,"list_article");



        *//*CloseableHttpClient httpClient = HttpClients.createDefault();
      //*CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            //  String url = "http://192.168.0.230:530/xmtorch/"; // 你要爬取的网页URL
            String url = "http://xmtorch.xm.gov.cn";
            HttpGet httpGet = new HttpGet(url);
            CloseableHttpResponse response = httpClient.execute(httpGet);

            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String html = EntityUtils.toString(entity);
                    Document doc = Jsoup.parse(html,"UTF-8");
                    Elements els= doc.getElementsByClass("tab-list-1");
                    Element element =els.get(0);
                    extracted( "http://xmtorch.xm.gov.cn/", element,"detail_title","Custom_UnionStyle",0);

                    Element element1 =els.get(1);
                    extracted("http://xmtorch.xm.gov.cn/", element1,"detail_title","Custom_UnionStyle",1);

                    Element element2 =els.get(2);
                    extracted("http://xmtorch.xm.gov.cn/", element2,"con_title","Custom_UnionStyle",2);


                    for (int i = 0; i < 5; i++) {
                        Elements els3= doc.getElementsByClass("tab-list-2");
                        Element element3 =els3.get(i);
                        if(i==1){
                            extracted( "http://xmtorch.xm.gov.cn/", element3,"rules_tit","Custom_UnionStyle",i+3);

                        }else{
                            extracted( "http://xmtorch.xm.gov.cn/", element3,"con_title","Custom_UnionStyle",i+3);
                        }
                    }


                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*//*
    }*/
   // @Scheduled(cron = "0 */1 * * * ?")
  /* @Scheduled(cron = "50 0 0 * * ?")
    public void crawlWebPage2(){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            String url = "http://192.168.0.230:530/xmhjzszx/yhzhc/index.htm"; // 你要爬取的网页URL
            //  String url = "http://xmtorch.xm.gov.cn";

            HttpGet httpGet = new HttpGet(url);

            // 发送HTTP GET请求
            CloseableHttpResponse response = httpClient.execute(httpGet);

            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String html = EntityUtils.toString(entity);
                    Document doc = Jsoup.parse(html,"UTF-8");
                    Elements els= doc.getElementsByClass("article-list");
                    Element element =els.get(0);
                    extracted( "", element,"rich_media_title","rich_media_content",4);
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
*/
    //@Scheduled(cron = "0 */1 * * * ?")
    //@Scheduled(cron = "50 0 0 * * ?")
   @XxlJob("crawlWebPageJobHandler")
    public void crawlWebPage(){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            String url = "http://192.168.0.230:530/xmtorch/"; // 你要爬取的网页URL
           // String url = "http://xmtorch.xm.gov.cn";

            HttpGet httpGet = new HttpGet(url);

            // 发送HTTP GET请求
            CloseableHttpResponse response = httpClient.execute(httpGet);

            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String html = EntityUtils.toString(entity);
                    Document doc = Jsoup.parse(html,"UTF-8");
                    Elements els= doc.getElementsByClass("tab-list-1");
                    Element element =els.get(0);
                    extracted( "http://192.168.0.230:530/xmtorch/", element,"detail_title","Custom_UnionStyle",0);

                    Element element1 =els.get(1);
                    extracted("http://192.168.0.230:530/xmtorch/", element1,"detail_title","Custom_UnionStyle",1);

                    Element element2 =els.get(2);
                    extracted("http://192.168.0.230:530/xmtorch/", element2,"con_title","Custom_UnionStyle",2);


                 /*   Elements els3= doc.getElementsByClass("tab-list-2");
                    Element element3 =els3.get(2);
                    extracted( "http://xmtorch.xm.gov.cn/", element3,"con_title","Custom_UnionStyle",3);
*/
                    for (int i = 0; i < 5; i++) {
                        Elements els3= doc.getElementsByClass("tab-list-2");
                        Element element3 =els3.get(i);
                        if(i==1){
                            extracted( "http://192.168.0.230:530/xmtorch/", element3,"rules_tit","Custom_UnionStyle",i+3);

                        }else{
                            extracted( "http://192.168.0.230:530/xmtorch/", element3,"con_title","Custom_UnionStyle",i+3);
                        }
                    }

                    copy("http://192.168.0.230:530/xmtorch/ztzl/aqyq/aqdt/",httpClient,1,"list_article",8);
                }
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private  void extracted(String url, Element element, String titleClass, String contentClass, Integer type) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        Elements lis=   element.getElementsByTag("li");

        for (Element li:lis
        ) {
            Elements links=   li.getElementsByTag("a");
            log.info("类型: " + type);
            log.info("超链接: " + links.get(0).attr("href"));
            Elements date=  li.getElementsByTag("span");
            System.out.println("时间："+date.get(0).html().replaceAll("\\.","-"));
            log.info("超链接: " + url+links.get(0).attr("href").replace("./","").replace("https","http").replace("mp.weixin.qq.com","192.168.0.230:530/weixinmp"));
            CloseableHttpResponse response2 = httpClient.execute( new HttpGet(url+links.get(0).attr("href").replace("https","http").replace("./","").replace("mp.weixin.qq.com","192.168.0.230:530/weixinmp")));
            log.info("超链接: " + links.get(0).attr("href").replace("./","").replace("mp.weixin.qq.com","192.168.0.230:530/weixinmp"));
            HttpEntity entity2 = response2.getEntity();
            if (entity2 != null) {
                String html2 = EntityUtils.toString(entity2);
                Document doc2 = Jsoup.parse(html2,"UTF-8");
                Elements title= doc2.getElementsByClass(titleClass);
                if(Func.isEmpty(title)) {
                     title= doc2.getElementsByClass("detail_title");
                }
                Elements content= doc2.getElementsByClass(contentClass);
                if(Func.isEmpty(title)) {
                    title= doc2.getElementsByClass("con_title");
                }

                if(Func.isNotEmpty(content)&&Func.isNotEmpty(title)) {
                    log.info("标题: " +title.get(0).html());
                    log.info("内容: " +content.get(0).html());

                    LambdaQueryWrapper<PmgBatParkNotice> queryWrapper = Wrappers.lambdaQuery();
                    queryWrapper.eq(PmgBatParkNotice::getTitle, title.get(0).html());
                    queryWrapper.eq(PmgBatParkNotice::getType,type);
                    PmgBatParkNotice pmgBatParkNotice=  this.getOne(queryWrapper);
                    if(Func.isEmpty(pmgBatParkNotice)){
                        PmgBatParkNotice notice=new PmgBatParkNotice();
                        notice.setAppId("wx306ce81471effaf0");
                        notice.setTenantId("108994");
                        notice.setParkId(1457553705928477026L);
                        notice.setTitle(title.get(0).html());
                        notice.setContent(content.get(0).html());
                        log.info("时间" + date.get(0).html());
                        notice.setReleaseTime(date.get(0).html().replaceAll("\\.","-"));
                        notice.setType(type);
                        ThreadLocalRandom random = ThreadLocalRandom.current();
                        notice.setViewCount(random.nextInt(1000));
                        notice.setCreateDept(11111111111111L);
                        notice.setCreateUser(11111111111111L);
                        this.save(notice);
                    }

                }

            }

        }
    }


}
