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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.persagy.core.enumeration.EMSOrder;
import com.persagy.core.mvc.service.CoreService;
import com.persagy.core.service.BaseBusinessService;
import com.persagy.core.service.BusinessService;
import com.persagy.ems.pojo.holiday.HolidayGoseek;
import com.persagy.framework.util.DateUtils;
import com.persagy.framework.util.HolidaySearchUtil;
import com.persagy.functions.weather.constant.EnumHolidayDayType;
import com.persagy.functions.weather.dto.DayHolidayDTO;

@Service("QueryHolidayOfYear")
@Scope("prototype")
@SuppressWarnings({"unchecked", "rawtypes" })
public class QueryHolidayOfYear extends BaseBusinessService<Map> implements BusinessService {
	
	@Resource(name = "coreService")
	private CoreService coreService;

	@Override
	public void handle(Map dto, List content) throws Exception {
		final Integer year = (Integer) dto.get("year");
		if(year < HolidaySearchUtil.START_YEAR || year > DateUtils.getYear(new Date())) {
			throw new Exception("不支持年份" + year);
		}
		
		Integer dayType = (Integer) dto.get("dayType");
		if(dayType != null && EnumHolidayDayType.getEnum(dayType) == null) {
			dayType = null;
		}
		
		HolidayGoseek queryObj = new HolidayGoseek();
		queryObj.setYear(year);
		queryObj.setSort("dateTime", EMSOrder.Asc);
		queryObj.setCode(dayType);
		
		List<HolidayGoseek> resultList = coreService.query(queryObj);
		if(resultList == null || resultList.size() == 0) {
			final String threadName = "RequestHolidayInfo";
			
			boolean threadRunning = false;
			Map<Thread, StackTraceElement[]> allThreadMap = Thread.getAllStackTraces();
			for(Thread threadObj : allThreadMap.keySet()) {
				if(threadName.equals(threadObj.getName())) {
					threadRunning = true;
				}
			}
			
			if(!threadRunning) {
				new Thread(threadName) {
					@Override
					public void run() {
						List<DayHolidayDTO> dataList = HolidaySearchUtil.queryHolidayCodeForOneYear(year);
						if(null != dataList && !dataList.isEmpty()) {
							List<HolidayGoseek> saveList = new ArrayList<HolidayGoseek>();
							for(DayHolidayDTO obj : dataList) {
								HolidayGoseek addObj = new HolidayGoseek(year, obj.getDayTime(), obj.getCode());
								addObj.setId(UUID.randomUUID().toString().replaceAll("-", ""));
								saveList.add(addObj);
							}
							
							HolidayGoseek delObj = new HolidayGoseek();
							delObj.setYear(year);
							try {
								coreService.remove(delObj);
								coreService.save(saveList);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}.start();
			}
			
			throw new Exception("服务繁忙，请1分钟后再试！");
		}else {
			for(HolidayGoseek obj : resultList) {
				content.add(new DayHolidayInfo(DateUtils.date2Str(obj.getDateTime(), DateUtils.SDFDAY), obj.getCode()));
			}
		}
	}
}

class DayHolidayInfo {
	private String dayTime;
	private Integer code;
	
	public DayHolidayInfo(String dayTime, Integer code) {
		super();
		this.dayTime = dayTime;
		this.code = code;
	}
	public String getDayTime() {
		return dayTime;
	}
	public void setDayTime(String dayTime) {
		this.dayTime = dayTime;
	}
	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
}
