package cn.com.usky.iot.company.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Calendar;
import java.text.NumberFormat;
import cn.com.usky.iot.admin.dao.YtiotTAdminDao;
import cn.com.usky.iot.admin.dao.YtiotVUserCompanyDao;
import cn.com.usky.iot.company.dao.YtiotVCompanyDao;
import cn.com.usky.iot.entity.YtiotTAdmin;
import cn.com.usky.iot.entity.YtiotTInspectors;
import cn.com.usky.iot.entity.YtiotVCompany;
import cn.com.usky.iot.entity.YtiotVCompanyId;
import cn.com.usky.iot.entity.YtiotVUsercompany;
import cn.com.usky.iot.entity.YtiotVUsercompanyId;
import cn.com.usky.iot.entity.YtiotTProjectDetails;
import cn.com.usky.iot.entity.YtiotTProjectDevice;
import cn.com.usky.iot.entity.YtiotTProjectMonitor;
import cn.com.usky.iot.entity.YtiotVEfire;
import cn.com.usky.iot.entity.YtiotVEfireId;
import cn.com.usky.iot.entity.YtiotVHj;
import cn.com.usky.iot.entity.YtiotVHjId;
import cn.com.usky.iot.entity.YtiotVRtu;
import cn.com.usky.iot.entity.YtiotVRtuId;
import cn.com.usky.iot.entity.YtiotVSj;
import cn.com.usky.iot.entity.YtiotVSjId;
import cn.com.usky.iot.alarm.dao.YtiotVAlarmDao;
import cn.com.usky.iot.data.dao.YtiotVDataDao;
import cn.com.usky.iot.entity.YtiotVEfire;
import cn.com.usky.iot.entity.YtiotVEfireId;
import cn.com.usky.iot.entity.YtiotVHj;
import cn.com.usky.iot.entity.YtiotVHjId;
import cn.com.usky.iot.entity.YtiotVRtu;
import cn.com.usky.iot.entity.YtiotVRtuId;
import cn.com.usky.iot.entity.YtiotVSj;
import cn.com.usky.iot.entity.YtiotVSjId;
import cn.com.usky.utils.MD5Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class YtiotVCompanyServiceImpl implements YtiotVCompanyService {
	private YtiotVCompanyDao ytiotVCompanyDao;
	private YtiotTAdminDao ytiotTAdminDao;
	private YtiotVUserCompanyDao ytiotVUserCompanyDao;
	private YtiotVAlarmDao ytiotVAlarmDao;
	private YtiotVDataDao ytiotVDataDao;

	public YtiotVDataDao getYtiotVDataDao() {
		return ytiotVDataDao;
	}

	public void setYtiotVDataDao(YtiotVDataDao ytiotVDataDao) {
		this.ytiotVDataDao = ytiotVDataDao;
	}
	
	public YtiotVAlarmDao getYtiotVAlarmDao() {
		return ytiotVAlarmDao;
	}

	public void setYtiotVAlarmDao(YtiotVAlarmDao ytiotVAlarmDao) {
		this.ytiotVAlarmDao = ytiotVAlarmDao;
	}
	
	public YtiotVUserCompanyDao getYtiotVUserCompanyDao() {
		return ytiotVUserCompanyDao;
	}

	public void setYtiotVUserCompanyDao(YtiotVUserCompanyDao ytiotVUserCompanyDao) {
		this.ytiotVUserCompanyDao = ytiotVUserCompanyDao;
	}
	public YtiotVCompanyDao getYtiotVCompanyDao() {
		return ytiotVCompanyDao;
	}

	public void setYtiotVCompanyDao(YtiotVCompanyDao ytiotVCompanyDao) {
		this.ytiotVCompanyDao = ytiotVCompanyDao;
	}

	public YtiotTAdminDao getYtiotTAdminDao() {
		return ytiotTAdminDao;
	}

	public void setYtiotTAdminDao(YtiotTAdminDao ytiotTAdminDao) {
		this.ytiotTAdminDao = ytiotTAdminDao;
	}

	public boolean chk_string_param(String param) {
		if(param==null)
			return true;
		if(param.indexOf("=")>=0)
			return false;
		if(param.indexOf("\'")>=0)
			return false;
		if(param.indexOf("\"")>=0)
			return false;
		return true;
	}
	
	public String chkhw(String hqlwhere){
		if(hqlwhere==null)
			return " where ";
		else
			return hqlwhere+" and ";
	}
	
	public String fieldname(String field){
		if(field.equals("building_id"))
			return "buildingId";
		if(field.equals("building_name"))
			return "buildingName";
		if(field.equals("create_man"))
			return "createMan";
		if(field.equals("create_time"))
			return "createTime";
		if(field.equals("owner_address"))
			return "ownerAddress";
		if(field.equals("owner_id"))
			return "ownerId";
		if(field.equals("owner_man"))
			return "ownerMan";
		if(field.equals("owner_name"))
			return "ownerName";
		if(field.equals("owner_office"))
			return "ownerOffice";
		if(field.equals("company_code"))
			return "companyCode";
		return field;
	}
	@Override
	public String getVCompanyList(String queryJson, String page, String start, String limit, String sort)
			throws Exception {
		// TODO Auto-generated method stub
		String addr1=null, addr2=null, addr3=null;
		String addr1list=null, addr2list=null, addr3list=null;
		String address=null, building_id=null, building_name=null, create_man=null;
		String data1=null, data2=null, data3=null, fulladdress=null;
		String h5=null, hls=null, id=null, owner_address=null;
		String owner_id=null, owner_man=null, owner_name=null;
		String owner_id_list=null, building_id_list=null;
		String owner_office=null, rtmp=null, xmlx=null, xmlx_list=null;
		String hqlwhere = null;
		String order = null;
		String dir = null;
		String Start = null;
		String Limit = null;
		String Page = null;
		String order_str = null;
		JSONObject q_json = null;
		JSONArray Sort = null;
		JSONArray jSONArray = new JSONArray();
		JSONObject json = new JSONObject();
		json.put("action", "getVCompanyList");
		if(queryJson!=null) {
			q_json = JSONObject.fromObject(queryJson);
			if(q_json.has("addr1")) {
				if(((JSONObject) q_json.get("addr1")).isArray())
				{
					JSONArray j_tmp = q_json.getJSONArray("addr1");
					for(int i=0;i<j_tmp.size();i++)
					{
						String tmp = j_tmp.getString(i);
						if(i==0)
							addr1list = tmp;
						else
							addr1list += ","+tmp;
					}
				}else
					addr1 = q_json.getString("addr1");
			}
			if(q_json.has("addr2")) {
				if(((JSONObject) q_json.getJSONObject("addr2")).isArray())
				{
					JSONArray j_tmp = q_json.getJSONArray("addr2");
					for(int i=0;i<j_tmp.size();i++)
					{
						String tmp = j_tmp.getString(i);
						if(i==0)
							addr2list = tmp;
						else
							addr2list += ","+tmp;
					}
				}else
					addr2 = q_json.getString("addr2");
			}
			if(q_json.has("addr3")) {
				if(((JSONObject)q_json.getJSONObject("addr3")).isArray())
				{
					JSONArray j_tmp = q_json.getJSONArray("addr3");
					for(int i=0;i<j_tmp.size();i++)
					{
						String tmp = j_tmp.getString(i);
						if(i==0)
							addr3list = tmp;
						else
							addr3list += ","+tmp;
					}
				}else
					addr3 = q_json.getString("addr3");
			}
			address = q_json.has("address")?q_json.getString("address"):null;
			if(q_json.has("building_id")) {
				if(((JSONObject)q_json.getJSONObject("building_id")).isArray())
				{
					JSONArray j_tmp = q_json.getJSONArray("building_id");
					for(int i=0;i<j_tmp.size();i++)
					{
						String tmp = j_tmp.getString(i);
						if(i==0)
							building_id_list = tmp;
						else
							building_id_list += ","+tmp;
					}
				}else
					building_id = q_json.getString("building_id");
			}
			building_name = q_json.has("building_name")?q_json.getString("building_name"):null;
			create_man = q_json.has("create_man")?q_json.getString("create_man"):null;
			data1 = q_json.has("data1")?q_json.getString("data1"):null;
			data2 = q_json.has("data2")?q_json.getString("data2"):null;
			data3 = q_json.has("data3")?q_json.getString("data3"):null;
			fulladdress = q_json.has("fulladdress")?q_json.getString("fulladdress"):null;
			id = q_json.has("id")?q_json.getString("id"):null;
			if(q_json.has("owner_id"))
			{
				if(((JSONObject)q_json.getJSONObject("owner_id")).isArray())
				{
					JSONArray j_tmp = q_json.getJSONArray("owner_id");
					for(int i=0;i<j_tmp.size();i++)
					{
						String tmp = j_tmp.getString(i);
						if(i==0)
							owner_id_list = tmp;
						else
							owner_id_list += ","+tmp;
					}
				}else
					owner_id = q_json.getString("owner_id");
			}
			owner_address = q_json.has("owner_address")?q_json.getString("owner_address"):null;
			owner_office = q_json.has("owner_office")?q_json.getString("owner_office"):null;
			if(q_json.has("xmlx")) {
				if(((JSONObject)q_json.getJSONObject("xmlx")).isArray()) {
					JSONArray j_tmp = q_json.getJSONArray("xmlx");
					for(int i=0;i<j_tmp.size();i++) {
						String tmp = j_tmp.getString(i);
						if(i==0)
							xmlx_list = "'"+tmp+"'";
						else
							xmlx_list += ",'"+tmp+"'";
					}
				}else
					xmlx = q_json.getString("xmlx");
			}
			xmlx = q_json.has("xmlx")?q_json.getString("xmlx"):null;
			order = q_json.has("order")?q_json.getString("order"):null;
			dir = q_json.has("dir")?q_json.getString("dir"):null;
			Start = q_json.has("start")?q_json.getString("start"):(start!=null)?start:"0";
			Limit = q_json.has("limit")?q_json.getString("limit"):(limit!=null)?limit:null;
			Page = q_json.has("page")?q_json.getString("page"):(page!=null)?page:"0";
			json.put("query", queryJson);
		}
		if(sort!=null) {
			Sort = JSONArray.fromObject(sort);
			if((Sort.isArray())&&(Sort.size()>0)) {
				JSONObject s_json = Sort.getJSONObject(0);
				order = s_json.has("property")?s_json.getString("property"):order;
				dir = s_json.has("direction")?s_json.getString("direction"):dir;
			}
			json.put("sort", sort);
		}
		Start = start;
		Limit = limit;
		Page = page;
		if(addr1!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr1")+" like '%"+addr1+"%' ";
		if(addr1list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr1")+" in ("+addr1list+") ";
		if(addr2!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr2")+" like '%"+addr2+"%' ";
		if(addr2list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr2")+" in ("+addr2list+") ";
		if(addr3!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr3")+" like '%"+addr3+"%' ";
		if(addr3list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr3")+" in ("+addr3list+") ";
		if(address!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("address")+" like '%"+address+"%' ";
		if(building_id!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("building_id")+" like '%"+building_id+"%' ";
		if(building_id_list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("building_id")+" in ("+building_id_list+") ";
		if(building_name!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("building_name")+" like '%"+building_name+"%' ";
		if(create_man!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("create_man")+" like '%"+create_man+"%' ";
		if(data1!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("data1")+" like '%"+data1+"%' ";
		if(data2!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("data2")+" like '%"+data2+"%' ";
		if(data3!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("data3")+" like '%"+data3+"%' ";
		if(fulladdress!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("fulladdress")+" like '%"+fulladdress+"%' ";
		if(id!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("id")+" = '"+id+"' ";
		if(owner_id!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("owner_id")+" = "+owner_id+" ";
		if(owner_id_list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("owner_id")+" in ("+owner_id_list+") ";
		if(owner_address!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("owner_address")+" like '%"+owner_address+"%' ";
		if(owner_office!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("owner_office")+" like '%"+owner_office+"%' ";
		if(xmlx!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("xmlx")+" like '%"+xmlx+"%' ";
		if(xmlx_list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("xmlx")+" in ("+xmlx_list+") ";
		if(order!=null) 
			order_str = " order by u.id."+fieldname(order);
		else
			order_str = " order by u.id."+fieldname("id");
		int totalCount = ytiotVCompanyDao.getCount(hqlwhere);
		json.put("totalCount", totalCount);
		if(Start!=null){
			if(Integer.parseInt(Start)>totalCount){
				Start = "0";
				Page = "0";
			}
			if(page==null)
				Page="0";
		}else{
			Start = "0";
			Page = "0";
		}
		json.put("page", Page);
		json.put("start", Start);
		json.put("limit", Limit);
		if(totalCount>0) {
			List list = ytiotVCompanyDao.query(hqlwhere, order_str, dir, Start, Limit);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(list.size()>0) {
				for(int i=0;i<list.size();i++) {
					YtiotVCompany o = (YtiotVCompany)list.get(i);
					YtiotVCompanyId Id = o.getId();
					Map map = new HashMap();
					map.put("addr1", Id.getAddr1());
					map.put("addr2", Id.getAddr2());
					map.put("addr3", Id.getAddr3());
					map.put("address", Id.getAddress());
					map.put("building_id", Id.getBuildingId());
					map.put("building_name", Id.getBuildingName());
					map.put("create_man", Id.getCreateMan());
					map.put("create_time",  df.format(Id.getCreateTime()));
					map.put("data1", Id.getData1());
					map.put("data2", Id.getData2());
					map.put("data3", Id.getData3());
					map.put("fulladdress", Id.getFulladdress());
					map.put("h5", Id.getH5());
					map.put("hls", Id.getHls());
					map.put("id", Id.getId());
					map.put("owner_address", Id.getOwnerAddress());
					map.put("owner_id", Id.getOwnerId());
					map.put("owner_man", Id.getOwnerMan());
					map.put("owner_name", Id.getOwnerName());
					map.put("owner_office", Id.getOwnerOffice());
					map.put("position", Id.getPosition());
					map.put("rtmp", Id.getRtmp());
					map.put("xmlx", Id.getXmlx());
					JSONObject jSONObject = JSONObject.fromObject(map);
					jSONArray.add(jSONObject);
				}
				json.put("RESULT", jSONArray);
			}
		}
		return json.toString();
	}

	@Override
	public String getVCompanyNameList(String queryJson) throws Exception {
		// TODO Auto-generated method stub
		String addr1=null, addr2=null, addr3=null;
		String addr1list=null, addr2list=null, addr3list=null;
		String address=null, building_id=null, building_name=null, create_man=null;
		String data1=null, data2=null, data3=null, fulladdress=null;
		String h5=null, hls=null, id=null, owner_address=null;
		String owner_id=null, owner_man=null, owner_name=null;
		String owner_id_list=null, building_id_list=null;
		String owner_office=null, rtmp=null, xmlx=null, xmlx_list=null;
		String V_LOGINNAME = null;
		String V_PASSWORD = null;
		String owner_id_List = null;
		int E_STATUS=-1;
		String hqlwhere = null;
		JSONObject q_json = null;
		JSONArray Sort = null;
		JSONArray jSONArray = new JSONArray();
		JSONObject json = new JSONObject();
		json.put("action", "getVCompanyNameList");
		if(queryJson!=null) {
			q_json = JSONObject.fromObject(queryJson);
			if(q_json.has("addr1")) {
				if(((JSONObject) q_json.get("addr1")).isArray())
				{
					JSONArray j_tmp = q_json.getJSONArray("addr1");
					for(int i=0;i<j_tmp.size();i++)
					{
						String tmp = j_tmp.getString(i);
						if(i==0)
							addr1list = tmp;
						else
							addr1list += ","+tmp;
					}
				}else
					addr1 = q_json.getString("addr1");
			}
			if(q_json.has("addr2")) {
				if(((JSONObject) q_json.getJSONObject("addr2")).isArray())
				{
					JSONArray j_tmp = q_json.getJSONArray("addr2");
					for(int i=0;i<j_tmp.size();i++)
					{
						String tmp = j_tmp.getString(i);
						if(i==0)
							addr2list = tmp;
						else
							addr2list += ","+tmp;
					}
				}else
					addr2 = q_json.getString("addr2");
			}
			if(q_json.has("addr3")) {
				if(((JSONObject)q_json.getJSONObject("addr3")).isArray())
				{
					JSONArray j_tmp = q_json.getJSONArray("addr3");
					for(int i=0;i<j_tmp.size();i++)
					{
						String tmp = j_tmp.getString(i);
						if(i==0)
							addr3list = tmp;
						else
							addr3list += ","+tmp;
					}
				}else
					addr3 = q_json.getString("addr3");
			}
			address = q_json.has("address")?q_json.getString("address"):null;
			if(q_json.has("building_id")) {
				if(((JSONObject)q_json.getJSONObject("building_id")).isArray())
				{
					JSONArray j_tmp = q_json.getJSONArray("building_id");
					for(int i=0;i<j_tmp.size();i++)
					{
						String tmp = j_tmp.getString(i);
						if(i==0)
							building_id_list = tmp;
						else
							building_id_list += ","+tmp;
					}
				}else
					building_id = q_json.getString("building_id");
			}
			building_name = q_json.has("building_name")?q_json.getString("building_name"):null;
			create_man = q_json.has("create_man")?q_json.getString("create_man"):null;
			data1 = q_json.has("data1")?q_json.getString("data1"):null;
			data2 = q_json.has("data2")?q_json.getString("data2"):null;
			data3 = q_json.has("data3")?q_json.getString("data3"):null;
			fulladdress = q_json.has("fulladdress")?q_json.getString("fulladdress"):null;
			id = q_json.has("id")?q_json.getString("id"):null;
			if(q_json.has("owner_id"))
			{
				if(((JSONObject)q_json.getJSONObject("owner_id")).isArray())
				{
					JSONArray j_tmp = q_json.getJSONArray("owner_id");
					for(int i=0;i<j_tmp.size();i++)
					{
						String tmp = j_tmp.getString(i);
						if(i==0)
							owner_id_list = tmp;
						else
							owner_id_list += ","+tmp;
					}
				}else
					owner_id = q_json.getString("owner_id");
			}
			owner_address = q_json.has("owner_address")?q_json.getString("owner_address"):null;
			owner_office = q_json.has("owner_office")?q_json.getString("owner_office"):null;
			if(q_json.has("xmlx")) {
				if(((JSONObject)q_json.getJSONObject("xmlx")).isArray()) {
					JSONArray j_tmp = q_json.getJSONArray("xmlx");
					for(int i=0;i<j_tmp.size();i++) {
						String tmp = j_tmp.getString(i);
						if(i==0)
							xmlx_list = "'"+tmp+"'";
						else
							xmlx_list += ",'"+tmp+"'";
					}
				}else
					xmlx = q_json.getString("xmlx");
			}
			xmlx = q_json.has("xmlx")?q_json.getString("xmlx"):null;
			json.put("query", queryJson);
			V_LOGINNAME = q_json.has("V_LOGINNAME")?q_json.getString("V_LOGINNAME"):null;
			V_PASSWORD = q_json.has("V_PASSWORD")?q_json.getString("V_PASSWORD"):null;
		}
		if((V_LOGINNAME!=null)&&(V_PASSWORD!=null)) {
			List list = ytiotTAdminDao.query(" where u.VLoginname='"+V_LOGINNAME+"' and u.VPassword='"
					+MD5Util.EncoderByMd5(MD5Util.EncoderByMd5(V_PASSWORD))+"' ", null, null, null, null);
			if(list.size()>0) {
				YtiotTAdmin o = (YtiotTAdmin)list.get(0);
				E_STATUS = Integer.parseInt(o.getEStatus());
			}
			String where = " where u.id.agentid='"+V_LOGINNAME+"' ";
			List list1 = ytiotVUserCompanyDao.query(where, null, null, null, null);
			if(list1.size()>0) {
				for(int i=0;i<list1.size();i++) {
					YtiotVUsercompany o = (YtiotVUsercompany)list1.get(i);
					YtiotVUsercompanyId oid = o.getId();
					if(i==0)
						owner_id_List = oid.getOwnerCode();
					else
						owner_id_List += ","+oid.getOwnerCode();
				}
			}
		}

		if(addr1!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr1")+" like '%"+addr1+"%' ";
		if(addr1list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr1")+" in ("+addr1list+") ";
		if(owner_id_List!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("owner_id")+" in ("+owner_id_List+") ";
		if(addr2!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr2")+" like '%"+addr2+"%' ";
		if(addr2list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr2")+" in ("+addr2list+") ";
		if(addr3!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr3")+" like '%"+addr3+"%' ";
		if(addr3list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("addr3")+" in ("+addr3list+") ";
		if(address!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("address")+" like '%"+address+"%' ";
		if(building_id!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("building_id")+" like '%"+building_id+"%' ";
		if(building_id_list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("building_id")+" in ("+building_id_list+") ";
		if(building_name!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("building_name")+" like '%"+building_name+"%' ";
		if(create_man!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("create_man")+" like '%"+create_man+"%' ";
		if(data1!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("data1")+" like '%"+data1+"%' ";
		if(data2!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("data2")+" like '%"+data2+"%' ";
		if(data3!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("data3")+" like '%"+data3+"%' ";
		if(fulladdress!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("fulladdress")+" like '%"+fulladdress+"%' ";
		if(id!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("id")+" = '"+id+"' ";
		if(owner_id!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("owner_id")+" = "+owner_id+" ";
		if(owner_id_list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("owner_id")+" in ("+owner_id_list+") ";
		if(owner_address!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("owner_address")+" like '%"+owner_address+"%' ";
		if(owner_office!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("owner_office")+" like '%"+owner_office+"%' ";
		if(xmlx!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("xmlx")+" like '%"+xmlx+"%' ";
		if(xmlx_list!=null)
			hqlwhere = chkhw(hqlwhere)+"u.id."+fieldname("xmlx")+" in ("+xmlx_list+") ";
		int totalCount = ytiotVCompanyDao.getCount(hqlwhere);
		json.put("totalCount", totalCount);
		if(totalCount>0) {
			List list = ytiotVCompanyDao.query(hqlwhere, null, null, null, null);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(list.size()>0) {
				for(int i=0;i<list.size();i++) {
					YtiotVCompany o = (YtiotVCompany)list.get(i);
					YtiotVCompanyId Id = o.getId();
					Map map = new HashMap();
					map.put("owner_id", Id.getOwnerId());
					map.put("owner_name", Id.getOwnerName());
					map.put("xmlx", Id.getXmlx());
					JSONObject jSONObject = JSONObject.fromObject(map);
					jSONArray.add(jSONObject);
				}
				json.put("RESULT", jSONArray);
			}
		}
		return json.toString();
	}
	
	
	@Override
	public String getVBuildingNameList(String queryJson) throws Exception {
		String V_LOGINNAME = null;
		String company_code_list = null;
		
		String hqlwhere = null;

		JSONObject q_json = null;		
		JSONArray jSONArray = new JSONArray();
		JSONObject json = new JSONObject();
//		json.put("action", "getPatrolPointList");
		if(queryJson!=null) {
			q_json = JSONObject.fromObject(queryJson);
			V_LOGINNAME = q_json.has("V_LOGINNAME")?q_json.getString("V_LOGINNAME"):null;
			json.put("query", queryJson);
		}	
		
			if(V_LOGINNAME!=null) {
				String where1 = " where u.id.agentid='"+V_LOGINNAME+"' ";
				List list1 = ytiotVUserCompanyDao.query(where1, null, null, null, null);
				if(list1.size()>0) {
					for(int i=0;i<list1.size();i++) {
						YtiotVUsercompany o = (YtiotVUsercompany)list1.get(i);
						YtiotVUsercompanyId oid = o.getId();
						if(i==0)
							company_code_list = "'"+oid.getOwnerCode()+"'";
						else
							company_code_list += ",'"+oid.getOwnerCode()+"'";
					}
				}
			}
			

			if(company_code_list!=null)
				hqlwhere = " where u.id.ownerId in ("+company_code_list+")";
				List list = ytiotVCompanyDao.query(hqlwhere, null, null, null, null);
				if(list.size()>0) {
					for(int i=0;i<list.size();i++) {
						YtiotVCompany o = (YtiotVCompany)list.get(i);
						YtiotVCompanyId Id = o.getId();
						Map map = new HashMap();
						map.put("building_id", Id.getBuildingId());
						map.put("building_name", Id.getBuildingName());
						JSONObject jSONObject = JSONObject.fromObject(map);
						jSONArray.add(jSONObject);
					}
					json.put("RESULT", jSONArray);
				}
		return json.toString();
	}

	public static int getDates(Date date1,Date date2) {
		Calendar cal=Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
