package com.qunzhixinxi.hnqz.daemon.quartz.zhuqu;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.oss.OssProperties;
import com.qunzhixinxi.hnqz.common.oss.service.OssTemplate;
import com.qunzhixinxi.hnqz.daemon.quartz.config.QuartzAppConfig;
import com.qunzhixinxi.hnqz.daemon.quartz.entity.WmWkArticle;
import com.qunzhixinxi.hnqz.daemon.quartz.entity.WmWkArticleConfig;
import com.qunzhixinxi.hnqz.daemon.quartz.service.WmScorePackageStatusService;
import com.qunzhixinxi.hnqz.daemon.quartz.service.WmWkArticleConfigService;
import com.qunzhixinxi.hnqz.daemon.quartz.service.WmWkArticleService;
import com.qunzhixinxi.hnqz.daemon.quartz.util.FetchImgsUtil;
import com.qunzhixinxi.hnqz.daemon.quartz.util.HttpPostUtil;
import com.qunzhixinxi.hnqz.daemon.quartz.util.HttpUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.entity.ContentType;
import org.jsoup.Connection;
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.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Component("zhuaqu")
@AllArgsConstructor
public class ZhuaquTest {

	private final WmWkArticleService wmWkArticleService;
	private final WmWkArticleConfigService wmWkArticleConfigService;

	private final QuartzAppConfig quartzAppConfig;


	public void init_main(String sessionStr) throws IOException {

		List<Map<String, String>> list = new ArrayList<>();
		Map<String, String> map = new HashMap<>();
		map.put("listUrl", "http://yp.eliancloud.cn/Member/Exclusive/MyEye/50b7ef80-daab-45a8-a3d4-a3fafdb77e36");
		map.put("contentUrl", "http://yp.eliancloud.cn/Member/Project/ZBContent");
		list.add(map);
		for (int jj = 0; jj < list.size(); jj++) {
			int num = 100000;
			int total = 10;
			WmWkArticleConfig config = wmWkArticleConfigService.getById(1);
			if(config !=null && config.getTotal() !=null){
				total = config.getTotal();
			}


			for (int i = 1; i < num; i++) {
				String artileJsonStr = getArtileList(i, sessionStr, list.get(jj).get("listUrl"));
				if (null == artileJsonStr || "".equals(artileJsonStr)) {
					break;
				}
				JSONObject jsonObject = JSONObject.parseObject(artileJsonStr);

				int records = jsonObject.getInteger("records");
				/**
				 *
				 */
				if(i * 20 + total - 20 > records){
					log.info("文章抓取结束 {}",records);
					config.setTotal(records);
					wmWkArticleConfigService.updateById(config);

					log.info("更新最新一天的文章为new {}",records);
					wmWkArticleService.updateNewFlag();
					break;
				}

				JSONArray jsonArray = jsonObject.getJSONArray("rows");


				if (jsonArray.size() > 0) {
					for (int j = 0; j < jsonArray.size(); j++) {
						JSONObject jsonObjectOne = jsonArray.getJSONObject(j);

						if (!"51733db6-fd53-4386-b044-dc90d7f13f98".equals(jsonObjectOne.getString("ChannelID"))) {
							continue;
						}
						if (null == jsonObjectOne.getString("ChannelID") || "".equals(jsonObjectOne.getString("ChannelID"))
								|| null == jsonObjectOne.getString("ArticleID") || "".equals(jsonObjectOne.getString("contentUrl"))) {
							continue;
						}
						WmWkArticle seachWmWkArticle = new WmWkArticle();
						seachWmWkArticle.setChannelId(jsonObjectOne.getString("ChannelID"));
						seachWmWkArticle.setArticleId(jsonObjectOne.getString("ArticleID"));
//						List<WmWkArticle> wmWkArticle = wmWkArticleService.list(Wrappers.query(seachWmWkArticle));
						int count = wmWkArticleService.count(Wrappers.query(seachWmWkArticle));

						if (count > 0) {
							continue;
						}

						Document document = getArtileContent(jsonObjectOne.getString("ChannelID"),
								jsonObjectOne.getString("ArticleID"), sessionStr, list.get(jj).get("contentUrl"));

						Element bodyElement = document.body();

						//获取视图  招标公告
						Elements select = bodyElement.select(".article-container");//css选择器

						if (select.size() == 0) {
							continue;
						}
						Elements title = select.select(".article-title");
						//text()方法:用于获取元素节点内的html标签
						System.out.println("文章名称:" + title.text());

						if (title.size() == 0) {
							continue;
						}

						Elements title1 = select.select(".article-subtitle a");
						//text()方法:用于获取元素节点内的html标签
						System.out.println("文章作者:" + title1.text());
						String author = title1.text();
						Elements title2 = select.select(".article-subtitle-content");
						//text()方法:用于获取元素节点内的html标签
						System.out.println("发布时间:" + title2.get(1).text());

						Elements imgs = document.getElementsByTag("img");
						String imgPath = null;
						for (Element element : imgs) {
							//获取每个img标签URL "abs:"表示绝对路径
							String imgSrc = element.attr("abs:src");
							// 打印URL
							System.out.println(imgSrc);

							if (null != imgSrc && !"".equals(imgSrc) && !imgSrc.contains("/imgs/") && imgSrc.contains("")) {
								try {
									String outpath = FetchImgsUtil.downImages("/Users/gaoyang/Desktop/xlst", imgSrc);
									//下载图片到本地
									File file = new File(outpath);
									FileInputStream fileInputStream = new FileInputStream(file);
									MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
									String path = wmWkArticleService.uploadImg(multipartFile);
									element.attr("style", "width:100% !important");
									element.attr("src", quartzAppConfig.getClientUrl() + path);
									imgPath = path;
								} catch (Exception ex) {

								}
							}

						}

						Elements selec = select.select(".article-content");
						String selecHtml = selec.html();
						//System.out.println("文章分类:" + selecHtml);


						WmWkArticle wkArticle = new WmWkArticle();
						wkArticle.setArticleId(jsonObjectOne.getString("ArticleID"));
						wkArticle.setChannelId(jsonObjectOne.getString("ChannelID"));
						wkArticle.setArea(jsonObjectOne.getString("FullName"));
						wkArticle.setAuthor(author);
						wkArticle.setContent(selecHtml);
						wkArticle.setForwardNum(0);
						wkArticle.setEnableFlag(0);
						wkArticle.setDelFlag(0);
						wkArticle.setDescription("");
						wkArticle.setTitle(title.text());
						wkArticle.setCreateTime(LocalDateTime.now());
						wkArticle.setSource("1");
						if (null != imgPath) {
							wkArticle.setFm(imgPath);
						} else {
							wkArticle.setFm("/admin/sys-file/wmkj/1c69c23531584dbfbce6e2d39afa0a9d.png");
						}
						LocalDateTime localDateTime = LocalDateTime.now();
						try {
//							DateTimeFormatter timeDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

							Date date = sdf.parse(title2.get(1).text().replace("年", "-")
									.replace("月", "-").replace("日", ""));
							Instant instant = date.toInstant();
							ZoneId zoneId = ZoneId.systemDefault();
							localDateTime = instant.atZone(zoneId).toLocalDateTime();


						} catch (Exception ex) {
							ex.printStackTrace();
						}
						wkArticle.setCreateTime(localDateTime);
						wmWkArticleService.save(wkArticle);

					}
				}

			}

		}

	}

	public String getArtileList(int page, String sessionStr, String url) throws IOException {
		// 抓取招标信息
		// String url="http://yp.eliancloud.cn/Member/BiddingArea/Information";

		//其他政策信息
//		String url="http://yp.eliancloud.cn/Member/BiddingArea/Information";


		String param = "rows=20&page=" + page + "&sord=asc&ChennelName=51733db6-fd53-4386-b044-dc90d7f13f98";

		String document = HttpPostUtil.sendPost(url, param, sessionStr);
		System.out.println(document);
		return document;
	}

	public Document getArtileContent(String ChannelID, String ArticleID, String sessionStr, String url) throws IOException {
		url = url + "?ChannelID=" + ChannelID + "&ArticleID=" + ArticleID;
		Document document = getDocData(url, sessionStr);
		return document;
	}


	public Document getDocData(String tarurl, String sessionStr) throws IOException {
		String targetURL = tarurl;
		//获取connect
		Connection connection = Jsoup.connect(targetURL);
		//伪造请求头
//		connection.header("Accept", "");
//		connection.header("Accept-Encoding", "");
//		connection.header("Accept-Language", "");
//		connection.header("Cache-Control", "");
//		connection.header("Connection", "");
		connection.header("Cookie", "ASP.NET_SessionId=" + sessionStr);
		connection.header("Host", "yp.eliancloud.cn");
		connection.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36");
		connection.ignoreHttpErrors(true);
		//执行
		Connection.Response execute = connection.method(Connection.Method.GET).execute();
		//获取Document,这里的document就代表者爬取下来的整个页面
		Document document = execute.parse();
		return document;
	}

}
