package com.persagy.functions.weather.business.v2020.maintain;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.functions.weather.service.HourRecordService;
import com.persagy.functions.weather.thread.FileDataTransThread;

/**
 * 城市小时历史文件数据入库
 *
 */
@Service("FileData2DB")
@SuppressWarnings({"unchecked","rawtypes" })
public class FileData2DB extends BaseBusinessService<Map> implements BusinessService {
	@Resource
	HourRecordService hourRecordService;
	
	@Override
	public void handle(Map dto, List content) throws Exception {
		String dirPath = (String) dto.get("dirPath");
		List<String> cityList = (List<String>) dto.get("cityList");
		String timeFrom = (String) dto.get("timeFrom");
		String timeTo = (String) dto.get("timeTo");
		
		File dir = new File(dirPath);
		if(!dir.exists() || !dir.isDirectory()) {
			throw new Exception("dirPath不是有效的目录");
		}
		
		if(null == cityList || cityList.isEmpty()) {
			throw new Exception("cityList不能为空");
		}
		
		Set<String> cityIds = new HashSet<>(cityList);
		
		if(timeFrom == null || timeFrom.length() != 19 || !timeFrom.contains("-")) {
			timeFrom = "2000-01-01 00:00:00";
		}
		
		if(timeTo == null || timeTo.length() != 19 || !timeTo.contains("-")) {
			timeTo = "2030-01-01 00:00:00";
		}
		
		if(FileDataTransThread.runningLogic.contains(dirPath)) {
			content.add("Task is Running");
		}else {
			new Thread(new FileDataTransThread(dirPath, hourRecordService, cityIds, timeFrom, timeTo)).start();
			content.add("Task start Running");
		}
		
	}
}
