/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-05-10
 * 描述            ：温湿度采集表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.tcp.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.tcp.mapper.TcpTempHumMapper;
import market.tcp.service.TcpTempHumService;
import market.tcp.vo.TcpTempHum;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@SuppressWarnings("rawtypes")
@Service("TcpTempHumService")
@RemoteService(path="TcpTempHumService")
public class TcpTempHumServiceImpl extends BaseServiceImpl<TcpTempHum,Object> implements TcpTempHumService {

	/**
	 * 温湿度采集仪器：通过采集时间查询
	 * @param cp startTime查询开始时间  endTime查询结束时间
	 * @return
	 */
	public ResultModel doQueryTcpTempHum(CommonPage<TcpTempHum> cp) {
		ResultModel rm=new ResultModel();		
		String startTime = cp.getStartTime();
		String endTime = cp.getEndTime();
		try
		{
			String swhere="";
			if (startTime != null && !"".equals(startTime)) {
				swhere += " and to_char(mydate, 'yyyy-mm-dd hh24:mi:ss') >='" + startTime + "'";
			}
			if (endTime != null && !"".equals(endTime)) {
				swhere += " and to_char(mydate, 'yyyy-mm-dd hh24:mi:ss') <= '" + endTime + "'";
			}
			List<TcpTempHum> list = this.getMapper(TcpTempHumMapper.class).findWithCondition(null, swhere, "mydate desc", 0, 1000);
			rm.setData(list);;
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 温湿度采集数据到数据库
	 */
	public void doInsertTempHum(String url){
		try {
			List<TcpTempHum> lists = this.getMapper(TcpTempHumMapper.class).findWithCondition(null, null, "mydate desc", 0, 1000);
			if (lists!=null && lists.size()>0) {
				Date mydate = lists.get(0).getMydate();
				long recentTime = mydate.getTime();
				long curTime = new Date().getTime();
				if (curTime-recentTime < 5*60*1000) {
					//5分钟内不进行数据的采集
					return;
				}
			}			
			URLConnection connection = new URL(url).openConnection();
			InputStream is = connection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = br.readLine())!= null) {
                builder.append(line);
            }
            br.close();
            isr.close();
            is.close();
            Date now = new Date();
            //采集到的json {"humidity":"55.5","temperature":"24.5"}           
            JSONObject js = JSONObject.fromObject(builder.toString());
            TcpTempHum tth = new TcpTempHum();
            tth.setMydate(now);//采集时间
            tth.setTemp(Double.parseDouble(js.get("temperature").toString()));//温度
            tth.setHum(Double.parseDouble(js.get("humidity").toString()));//湿度
            //如果存在相同的数据，则不继续新增
            List<TcpTempHum> list = this.getMapper(TcpTempHumMapper.class).find(tth, 0, 0);
            if (list!=null && list.size()>0) {
				return;
			}
            this.getMapper(TcpTempHumMapper.class).insert(tth);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取当前的温湿度
	 * @param cp memo1：url
	 * @return
	 */
	public ResultModel doQueryCurrentTempHum(CommonPage<TcpTempHum> cp){
		ResultModel rm=new ResultModel();		
		String url = cp.getMemo1();
		try {
			URLConnection connection = new URL(url).openConnection();
			InputStream is = connection.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String line;
			StringBuilder builder = new StringBuilder();
			while ((line = br.readLine())!= null) {
				builder.append(line);
			}
			br.close();
			isr.close();
			is.close();
			//采集到的json {"humidity":"55.5","temperature":"24.5"}           
			JSONObject js = JSONObject.fromObject(builder.toString());
			TcpTempHum tth = new TcpTempHum();
			tth.setMydate(new Date());//采集时间
			tth.setTemp(Double.parseDouble(js.get("temperature").toString()));//温度
			tth.setHum(Double.parseDouble(js.get("humidity").toString()));//湿度
			rm.setData(tth);;
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		} catch (MalformedURLException e) {
			rm.setState("200");
			rm.setMsgInfo("查询失败!");
			e.printStackTrace();
		} catch (IOException e) {
			rm.setState("200");// 为了不报错先写成200：20240722刘侣
			rm.setMsgInfo("查询失败!");
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 获取小时实绩
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryRtdEnergyHour(CommonPage<TcpTempHum> cp) {
		ResultModel rm=new ResultModel();		
		try {
			String swhere="";
			if (cp.getStartTime()!=null && !"".equals(cp.getStartTime())) {
				swhere += " and t.start_time >= to_date('"+cp.getStartTime()+"', 'yyyy-mm-dd hh24:mi:ss')";
				swhere += " and t.end_time >= to_date('"+cp.getStartTime()+"', 'yyyy-mm-dd hh24:mi:ss')";
			}
			if (cp.getMemo1()!=null && !"".equals(cp.getMemo1())) {
				//swhere += " and t.prd_line = '"+cp.getMemo1()+"'";
			}			
			List<HashMap> list = this.getMapper(TcpTempHumMapper.class).doQueryRtdEnergyHour(swhere);
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		}
		catch(Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 获取采集实绩
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryRtdPingZheng(CommonPage<TcpTempHum> cp) {
		ResultModel rm=new ResultModel();		
		try {
			String swhere="";
			if (cp.getStartTime()!=null && !"".equals(cp.getStartTime())) {
				swhere += " and t.systime >= to_date('"+cp.getStartTime()+"', 'yyyy-mm-dd hh24:mi:ss')";
				//swhere += " and t.systime >= to_date('"+cp.getStartTime()+"', 'yyyy-mm-dd hh24:mi:ss')";
			}
			if (cp.getMemo1()!=null && !"".equals(cp.getMemo1())) {
				//swhere += " and t.prd_line = '"+cp.getMemo1()+"'";
			}			
			List<HashMap> list = this.getMapper(TcpTempHumMapper.class).doQueryRtdPingZheng(swhere);
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		}
		catch(Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 退火炉
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryRtdTuihuo(CommonPage<TcpTempHum> cp) {
		ResultModel rm=new ResultModel();		
		try {
			String swhere="";
			if (cp.getStartTime()!=null && !"".equals(cp.getStartTime())) {
				swhere += " and t.create_time >= to_date('"+cp.getStartTime()+"', 'yyyy-mm-dd hh24:mi:ss')";
				//swhere += " and t.create_time >= to_date('"+cp.getStartTime()+"', 'yyyy-mm-dd hh24:mi:ss')";
			}
			if (cp.getMemo1()!=null && !"".equals(cp.getMemo1())) {
				//swhere += " and t.prd_line = '"+cp.getMemo1()+"'";
			}			
			List<HashMap> list = this.getMapper(TcpTempHumMapper.class).doQueryRtdTuihuo(swhere);
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		}
		catch(Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 退火炉
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryRtdSuanxi(CommonPage<TcpTempHum> cp) {
		ResultModel rm=new ResultModel();		
		try {
			String swhere="";
			if (cp.getStartTime()!=null && !"".equals(cp.getStartTime())) {
				swhere += " and t.systime >= to_date('"+cp.getStartTime()+"', 'yyyy-mm-dd hh24:mi:ss')";
			}
						
			List<HashMap> list = this.getMapper(TcpTempHumMapper.class).doQueryRtdSuanxi(swhere);
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");
		}
		catch(Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
}
