package com.persagy.functions.weather.thread;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.persagy.ems.pojo.wz.HourRecordData;
import com.persagy.framework.util.CsvUtil;
import com.persagy.functions.weather.business.v2020.maintain.WRecord;
import com.persagy.functions.weather.service.HourRecordService;

/**
 * 文件数据转存至数据库
 */
public class FileDataTransThread implements Runnable {
	private static final Logger LOGGER = Logger.getLogger(FileDataTransThread.class);
	public static Set<String> runningLogic = new HashSet<>();
	
	private String dirPath;
	private HourRecordService hourRecordService;
	private Set<String> cityIds;
	private String timeFrom;
	private String timeTo;
	
	public FileDataTransThread(String dirPath, HourRecordService hourRecordService) {
		super();
		this.dirPath = dirPath;
		this.hourRecordService = hourRecordService;
	}
	
	public FileDataTransThread(String dirPath, HourRecordService hourRecordService, Set<String> cityIds) {
		super();
		this.dirPath = dirPath;
		this.hourRecordService = hourRecordService;
		this.cityIds = cityIds;
	}
	
	public FileDataTransThread(String dirPath, HourRecordService hourRecordService, Set<String> cityIds, 
			String timeFrom, String timeTo) {
		super();
		this.dirPath = dirPath;
		this.hourRecordService = hourRecordService;
		this.cityIds = cityIds;
		this.timeFrom = timeFrom;
		this.timeTo = timeTo;
	}

	@Override
	public void run() {
		LOGGER.info("FileDataTransThread start...");
		
		Set<String> doCities = new HashSet<>();
		if(null != cityIds && !cityIds.isEmpty()) {
			doCities.addAll(cityIds);
			LOGGER.info("需要处理城市数量：" + doCities.size() + "； 时间段：["+timeFrom+","+timeTo+")");
		}
		
		if(doCities.isEmpty()) {
			LOGGER.warn("no city to do");
			return;
		}
		
		File dir = new File(dirPath);
		if(dir.exists() && dir.isDirectory()) {
			File[] files = dir.listFiles();
			if(null != files && files.length > 0) {
				LOGGER.info("需要读取的文件数量：" + files.length + "，开始处理...");
				
				String CITY_ID = null, TIME = null;
				StringBuilder sbFiles = new StringBuilder();
				
				
				boolean ifBreak = false;
				for(int i=0; i<files.length; i++) {
					LOGGER.info("处理第"+(i+1)+"/"+files.length+"个文件...");
					
					File file = files[i];
					StringBuilder sb = new StringBuilder();
					String filePath = file.getAbsolutePath();
					String fileName = file.getName();
					sb.append(fileName+"\n");
					
					List<String[]> dataList = CsvUtil.readToStrList(filePath, true);
					
					if(dataList.size() > 0) {
						try {
							int recordNum = 0;
							Set<String> cityrecord = new HashSet<>();
							List<WRecord> records = new ArrayList<>();
							for(String[] dataArray : dataList) {
								if(dataArray.length != 16) {
									sb.append("RecordNum=" + recordNum + ",DataShort\n");
								}else {
									CITY_ID = dataArray[1];
									if(!doCities.contains(CITY_ID)) {
										continue;
									}
									TIME = dataArray[5];
									if(null == CITY_ID || "".equals(CITY_ID.trim()) || null == TIME || "".equals(TIME.trim())) {
										sb.append("RecordNum=" + recordNum + ",DataError=no CITY_ID or TIME\n");
									}else {
										if(TIME.length()<17) {
											TIME += ":00";
										}
										
										if(TIME.compareTo(timeFrom) < 0 || TIME.compareTo(timeTo) >= 0) {
											continue;
										}
										
										WRecord record = new WRecord();
										record.CITY_ID = CITY_ID;
										record.TIME = TIME;
										
										if(cityrecord.contains(CITY_ID+TIME)) {
											continue;
										}else {
											cityrecord.add(CITY_ID+TIME);
										}
										
										record.PRE = dataArray[6];
										record.PRS = dataArray[7];
										record.RHU = dataArray[8];
										record.TEM = dataArray[9];
										record.VIS = dataArray[10];
										record.WNS = dataArray[11];
										record.WNS_SCALE = dataArray[12];
										record.WND = dataArray[13];
										record.WND_DIRECTION = dataArray[14];
										record.WEATHER = dataArray[15];
										String validStr = record.validField();
										if(null != validStr) {
											sb.append("RecordNum=" + recordNum + ",DataValidWarn=" + validStr + "\n");
										}
										records.add(record);
									}
								}
								
								recordNum ++;
							}
							
							if(records.size() > 0) {
								LOGGER.info(fileName + "有效数据记录数：" + records.size() + ", 准备入库...");
								int successNum = 0;
								List<HourRecordData> saveList = new ArrayList<>();
								int logIndex = 1;
								for(int j=0; j<records.size(); j++) {
									WRecord record = records.get(j);
									HourRecordData obj = record.getDaoObj();
									if(null != obj) {
										saveList.add(obj);
									}
									
									if(saveList.size() == 240 || j == records.size()-1) {
										hourRecordService.saveBatch(saveList);
										successNum += saveList.size();
										saveList.clear();
										if(logIndex % 5 == 0) {
											LOGGER.info("FileDataTransThread--Alive--1200 Records saved");
										}
										logIndex ++;
									}
								}
								LOGGER.info("入库结果：total-success=" + records.size() + "-" + successNum);
							}else {
								LOGGER.error(fileName + "无有效数据！！！");
							}
						}catch (Exception e) {
							ifBreak = true;
							e.printStackTrace();
						}
						
					}else {
						sb.append("no data\n");
						LOGGER.error(sb.toString());
					}
					
					String sbStr = sb.toString();
					if(sbStr.length() > 30) {
						sbFiles.append(sbStr+"\n");
					}
					
					if(ifBreak) {
						break;
					}
				}
				
				try {
					System.out.println("处理完成。警告信息："+sbFiles.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}else {
				LOGGER.error(dirPath + "目录下无csv文件！");
			}
		}else {
			LOGGER.error(dirPath + "不是有效的目录地址");
		}
		
		runningLogic.remove(dirPath);
	}
	
}
