package com.zhiwei.chendong;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.zw.entity.Weixin;

public class ReaderExcel {
	private final static Log log = LogFactory.getLog(ReaderExcel.class);

	public static void main(String[] args) throws IOException {
		System.out.println("https://mp.weixin.qq.com/s?src=11&timestamp=1525385592&ver=855&signature=t61muuTJKorCLU9B6F9khlfr3ezct6GGh-Moxq*9b4RZxgQnHSpElHV9CYCi2YnSA3nOz2*a04k6R9iHOGzctg1KS9Bgx3jelhxEi5NzmXHmhglHbp8Tjf5*rKEFilpi&new=1&key=95dc4253938e3c7ba33e1ff6f7363608ab00e37f99c7f7ff7e7cb4e5b1a6ce70df92e7de80d56d237ffe33cd15ffcb0a80bc0d064ece8d169c6e38960f0d44e359a01cd7bcb987a08b3de1a19e94eb06&uin=NTk1NjM2Nzg3&key=b97b0a94956e0cb2b01167441267dc09f5e79cb852c323792d0040c1988b0c39913e23a2e43b0bccef725d2843e0da0d81c5164338260e6b932f25085936eec21b0e71ca66cd78582d2411b4737cb644&uin=MzM3MTk2NzAxNA%3D%3D".replaceAll("&key=[\\s\\S]+", ""));
		
	}

	public static List<Weixin> getExcel(String url) throws IOException {
		// String url="C:/Users/Administrator/Desktop/jd.xls";
		List<Weixin> list = new ArrayList<>();
		Workbook book = null;
		book = getExcelWorkbook(url);
		Sheet sheet = getSheetByNum(book, 0);
		int lastRowNum = sheet.getLastRowNum();
		log.info("last number is " + lastRowNum);
		for (int i = 1; i <= lastRowNum; i++) {
			Row row = null;
			row = sheet.getRow(i);
			if (row != null) {
				int lastCellNum = row.getLastCellNum();
				Cell cell = null;
				Weixin w = new Weixin();
				for (int j = 0; j < lastCellNum; j++) {
					cell = row.getCell(j);
					if (cell != null) {
						cell.setCellType(Cell.CELL_TYPE_STRING);
						String str = cell.getStringCellValue();
						if (str != null) {
							if (sheet.getRow(0).getCell(j) == null) {
								continue;
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("来源")) {
								w.setSource(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("时间")) {
								w.setTime(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("标题")) {
								w.setTitle(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("地址")) {
								w.setUrl(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("内容")) {
								w.setConetent(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("关键词")) {
								w.setKeyValue(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("阅读数")) {
								w.setRead(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("点赞数")) {
								w.setZan(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("用户昵称")) {
								w.setNickname(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("简介")) {
								w.setJianjie(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("历史标注情况")) {
								w.setHistory(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("长地址")) {
								w.setLongUrl(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("mark")) {
								w.setMark(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("mark_time")) {
								w.setMark_time(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("markname")) {
								w.setMarkname(str == null ? "null" : str);
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("真实阅读数")) {
								w.setTrueRead(str == null ? "null" : str);
							}
						}
					}
				}
				if (w != null)
					list.add(w);
			}
		}
		return list;
	}

	public static List<String> getZhUrls(String url) throws IOException {
		List<String> list = new ArrayList<>();
		Workbook book = null;
		book = getExcelWorkbook(url);
		Sheet sheet = getSheetByNum(book, 0);
		int lastRowNum = sheet.getLastRowNum();
		log.info("last number is " + lastRowNum);
		for (int i = 1; i <= lastRowNum; i++) {
			Row row = null;
			row = sheet.getRow(i);
			if (row != null) {
				int lastCellNum = row.getLastCellNum();
				Cell cell = null;
				for (int j = 0; j < lastCellNum; j++) {
					cell = row.getCell(j);
					if (cell != null) {
						cell.setCellType(Cell.CELL_TYPE_STRING);
						String str = cell.getStringCellValue();
						if (str != null) {
							if (sheet.getRow(0).getCell(j) == null) {
								continue;
							}
							if (sheet.getRow(0).getCell(j).getStringCellValue().equals("链接")) {
								list.add(str);
							}
						}
					}
				}
			}
		}
		return list;
	}
	public static Sheet getSheetByNum(Workbook book, int number) {
		Sheet sheet = null;
		try {
			sheet = book.getSheetAt(number);
			// if(sheet == null){
			// sheet = book.createSheet("Sheet"+number);
			// }
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
		return sheet;
	}

	public static Workbook getExcelWorkbook(String filePath) throws IOException {
		Workbook book = null;
		File file = null;
		FileInputStream fis = null;
		try {
			file = new File(filePath);
			if (!file.exists()) {
				throw new RuntimeException("文件不存在");
			} else {
				fis = new FileInputStream(file);
				book = WorkbookFactory.create(fis);
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (fis != null) {
				fis.close();
			}
		}
		return book;
	}
}
