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

import java.util.Date;

import com.persagy.ems.pojo.wz.HourRecordData;
import com.persagy.framework.util.DateUtils;

public class WRecord {
	public String CITY_ID;
	public String TIME;
	public String TEM;
	public String RHU;
	public String PRE;
	public String PRS;
	public String WND;
	public String WND_DIRECTION;
	public String WNS;
	public String WNS_SCALE;
	public String VIS;
	public String WEATHER;
	@Override
	public String toString() {
		return "WRecord [CITY_ID=" + CITY_ID + ", TIME=" + TIME + ", TEM=" + TEM + ", RHU=" + RHU + ", PRE=" + PRE
				+ ", PRS=" + PRS + ", WND=" + WND + ", WND_DIRECTION=" + WND_DIRECTION + ", WNS=" + WNS + ", WNS_SCALE="
				+ WNS_SCALE + ", VIS=" + VIS + ", WEATHER=" + WEATHER + "]";
	}
	/**
	 * 校验数据
	 * @return null或异常信息
	 */
	public String validField() {
		String str = "";
		
		if(TEM == null || TEM.trim().length() == 0) {
			str = "TEM:null";
		}else {
			if(TEM.length() > 10) {
				TEM = TEM.substring(0, 10);
			}
			Double tem = Double.parseDouble(TEM);
			if(tem < -58 || tem > 45) {
				str = "TEM:" + TEM;
			}
		}
		
		if(RHU == null || RHU.trim().length() == 0) {
			str += "RHU:null;";
		}else {
			if(RHU.length() > 10) {
				RHU = RHU.substring(0, 10);
			}
			Double reh = Double.parseDouble(RHU);
			if(reh < 0 || reh > 100) {
				str += "RHU:" + RHU +";";
			}
		}
		
		if(PRS == null || PRS.trim().length() == 0) {
			str += "PRS:null;";
		}else {
			if(PRS.length() > 10) {
				PRS = PRS.substring(0, 10);
			}
			Double prs = Double.parseDouble(PRS);
			if(prs < 500 || prs > 1200) {
				str += "PRS:" + PRS +";";
			}
		}
		
		if(WND == null || WND.trim().length() == 0) {
			str += "WND:null;";
		}else {
			if(WND.length() > 10) {
				WND = WND.substring(0, 10);
			}
			Double prs = Double.parseDouble(WND);
			if(prs < 0 || prs > 360) {
				str += "WND:" + WND +";";
				WND = null;
			}
		}
		
		if(WNS == null || WNS.trim().length() == 0) {
			str += "WNS:null;";
		}else {
			if(WNS.length() > 10) {
				WNS = WNS.substring(0, 10);
			}
			Double prs = Double.parseDouble(WNS);
			if(prs < 0 || prs > 75) {
				str += "WNS:" + WNS +";";
				WNS = null;
			}
		}
		
		if(VIS == null || VIS.trim().length() == 0) {
			str += "VIS:null;";
		}else {
			if(VIS.length() > 10) {
				VIS = VIS.substring(0, 10);
			}
			Double prs = Double.parseDouble(VIS);
			if(prs < 0 || prs > 50000) {
				str += "VIS:" + VIS +";";
			}else {
				prs = prs / 1000.0;
				VIS = prs + "";
				VIS = VIS.length() > 10 ? VIS.substring(0, 6) : VIS;
			}
		}
		
		if("".equals(str)) {
			str = null;
		}else if(str.endsWith(";")) {
			str = str.substring(0, str.length()-1);
		}
		
		return str;
	}
	public HourRecordData getDaoObj() {
//		HourRecordData
		if(TIME != null) {
			Date hourTime = null;
			try {
				hourTime = DateUtils.str2Date(TIME, "yyyy-MM-dd HH:mm:ss");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(null != hourTime) {
				HourRecordData obj = new HourRecordData();
				obj.setCityId(CITY_ID);
				obj.setHourTime(hourTime);
				obj.setTemperature(TEM);
				obj.setHumidity(RHU);
				obj.setPressure(PRS);
				obj.setWind_direction_degree(WND);
				obj.setWind_direction(WND_DIRECTION);
				obj.setWind_speed(WNS);
				obj.setWind_scale(WNS_SCALE);
				obj.setVisibility(VIS);
				obj.setText(WEATHER);
				return obj;
			}
		}
		return null;
	}
	
}
