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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.com.usky.iot.entity.YtiotTCompany;
import cn.com.usky.iot.entity.YtiotTCompanyVer;
import cn.com.usky.iot.company.dao.YtiotTCompanyDao;
import cn.com.usky.iot.company.dao.YtiotTCompanyVerDao;
import cn.com.usky.utils.DaoResult;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class YtiotTCompanyServicesImpl implements YtiotTCompanyService {
	private YtiotTCompanyDao ytiotTCompanyDao;
	private YtiotTCompanyVerDao ytiotTCompanyVerDao;

	public YtiotTCompanyDao getYtiotTCompanyDao() {
		return ytiotTCompanyDao;
	}

	public void setYtiotTCompanyDao(YtiotTCompanyDao ytiotTCompanyDao) {
		this.ytiotTCompanyDao = ytiotTCompanyDao;
	}

	public YtiotTCompanyVerDao getYtiotTCompanyVerDao() {
		return ytiotTCompanyVerDao;
	}

	public void setYtiotTCompanyVerDao(YtiotTCompanyVerDao ytiotTCompanyVerDao) {
		this.ytiotTCompanyVerDao = ytiotTCompanyVerDao;
	}
	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("I_COMPANYVER_ID"))
			return "ICompanyverId";
		if(field.equals("UQ_COMPANY_ID"))
			return "uqCompanyId";
		if(field.equals("I_ORDER_IDX"))
			return "IOrderIdx";
		if(field.equals("UQ_PARENT_ID"))
			return "uqParentId";
		if(field.equals("UQ_PARENT_COMPANY_ID"))
			return "uqParentCompanyId";
		if(field.equals("V_COMPANY_NAME"))
			return "VCompanyName";
		if(field.equals("V_COMPANY_ADDRESS_0"))
			return "VCompanyAddress0";
		if(field.equals("V_COMPANY_ADDRESS_1"))
			return "VCompanyAddress1";
		if(field.equals("V_COMPANY_ADDRESS_2"))
			return "VCompanyAddress2";
		if(field.equals("E_STATUS"))
			return "EStatus";
		if(field.equals("UQ_ADMIN_ID"))
			return "uqAdminId";
		if(field.equals("I_VERSION"))
			return "IVersion";
		if(field.equals("D_STORETIME"))
			return "DStoretime";
		if(field.equals("V_COMMENT"))
			return "VComment";
		return null;
	}
	@Override
	public String appendCompany(String queryJson) throws Exception {
		// TODO Auto-generated method stub		
		String UQ_COMPANY_ID = null;
		String UQ_PARENT_ID = null;//上级部门ID
		//String UQ_PARENT_COMPANY_ID = null;//所属单位的ID
		String V_COMPANY_NAME = null;//单位/部门名称
		String V_COMPANY_ADDRESS_0 = null;//单位/部门地址_1
		String V_COMPANY_ADDRESS_1  = null;//单位/部门地址_2
		String V_COMPANY_ADDRESS_2  = null;//单位/部门地址_3
		String E_STATUS  = null;//当前状态，0为待开通服务，1为已开通服务，2为服务锁定，3为已注销，4、5留用
		String UQ_ADMIN_ID = null;//信息录入人ID
		String V_COMMENT  = null;//注册/修改说明
		JSONObject q_json = null;
		JSONArray jSONArray = new JSONArray();
		JSONObject json = new JSONObject();
		json.put("action", "appendCompany");
		if(queryJson!=null) {
			q_json = JSONObject.fromObject(queryJson);
			UQ_PARENT_ID = q_json.has("UQ_PARENT_ID")?q_json.getString("UQ_PARENT_ID"):null;
			//UQ_PARENT_COMPANY_ID = q_json.has("UQ_PARENT_COMPANY_ID")?q_json.getString("UQ_PARENT_COMPANY_ID"):null;			
			V_COMPANY_NAME = q_json.has("V_COMPANY_NAME")?q_json.getString("V_COMPANY_NAME"):null;
			V_COMPANY_ADDRESS_0 = q_json.has("V_COMPANY_ADDRESS_0")?q_json.getString("V_COMPANY_ADDRESS_0"):null;
			V_COMPANY_ADDRESS_1 = q_json.has("V_COMPANY_ADDRESS_1")?q_json.getString("V_COMPANY_ADDRESS_1"):null;			
			V_COMPANY_ADDRESS_2 = q_json.has("V_COMPANY_ADDRESS_2")?q_json.getString("V_COMPANY_ADDRESS_2"):null;
//			E_STATUS = q_json.has("E_STATUS")?q_json.getString("E_STATUS"):null;
			UQ_ADMIN_ID = q_json.has("UQ_ADMIN_ID")?q_json.getString("UQ_ADMIN_ID"):null;
			V_COMMENT = q_json.has("V_COMMENT")?q_json.getString("V_COMMENT"):"";
			json.put("query", queryJson);
		}
		if(chk_string_param(UQ_PARENT_ID)//&&chk_string_param(UQ_PARENT_COMPANY_ID)
				&&chk_string_param(V_COMPANY_NAME)&&chk_string_param(V_COMPANY_ADDRESS_0)
				&&chk_string_param(V_COMPANY_ADDRESS_1)&&chk_string_param(V_COMPANY_ADDRESS_2)//&&chk_string_param(E_STATUS)
				&&chk_string_param(UQ_ADMIN_ID)&&chk_string_param(V_COMMENT)) {
			YtiotTCompany o = new YtiotTCompany();
			o.setUqParentId(UQ_PARENT_ID);
			//o.setUqParentCompanyId(UQ_PARENT_COMPANY_ID);			
			o.setVCompanyName(V_COMPANY_NAME);
			o.setVCompanyAddress0(V_COMPANY_ADDRESS_0);
			o.setVCompanyAddress1(V_COMPANY_ADDRESS_1);
			o.setVCompanyAddress2(V_COMPANY_ADDRESS_2);
			o.setEStatus("0");
			o.setUqAdminId(UQ_ADMIN_ID);			
			o.setDStoretime(new Date());
			DaoResult ret = ytiotTCompanyDao.add(o, V_COMMENT);
			json.put("success", ret.isSuccess());
			json.put("Msg", ret.getResult());
		}else {
			json.put("success", false);
			json.put("Msg", "参数验证错误");
		}
		return json.toString();
	}

	@Override
	public String updateCompany(String queryJson) throws Exception {
		// TODO Auto-generated method stub
		String UQ_COMPANY_ID = null;
		String V_COMPANY_NAME = null;//单位/部门名称
		String V_COMPANY_ADDRESS_0 = null;//单位/部门地址_1
//		String V_COMPANY_ADDRESS_1  = null;//单位/部门地址_2
//		String V_COMPANY_ADDRESS_2  = null;//单位/部门地址_3
		String V_COMMENT  = null;//注册/修改说明
		String UQ_ADMIN_ID = null;//信息录入人ID
//		String UQ_PARENT_ID = null;//上级部门ID
//		String UQ_PARENT_COMPANY_ID = null;//所属单位的ID
		String E_STATUS  = null;//当前状态，0为待开通服务，1为已开通服务，2为服务锁定，3为已注销，4、5留用
		JSONObject q_json = null;
		JSONArray jSONArray = new JSONArray();
		JSONObject json = new JSONObject();
		json.put("action", "updateCompany");
		if(queryJson!=null) {
			q_json = JSONObject.fromObject(queryJson);
			UQ_COMPANY_ID = q_json.has("UQ_COMPANY_ID")?q_json.getString("UQ_COMPANY_ID"):null;
			V_COMPANY_NAME = q_json.has("V_COMPANY_NAME")?q_json.getString("V_COMPANY_NAME"):null;
			V_COMPANY_ADDRESS_0 = q_json.has("V_COMPANY_ADDRESS_0")?q_json.getString("V_COMPANY_ADDRESS_0"):null;
//			V_COMPANY_ADDRESS_1 = q_json.has("V_COMPANY_ADDRESS_1")?q_json.getString("V_COMPANY_ADDRESS_1"):null;			
//			V_COMPANY_ADDRESS_2 = q_json.has("V_COMPANY_ADDRESS_2")?q_json.getString("V_COMPANY_ADDRESS_2"):null;
			V_COMMENT = q_json.has("V_COMMENT")?q_json.getString("V_COMMENT"):"";
//			UQ_PARENT_ID = q_json.has("UQ_PARENT_ID")?q_json.getString("UQ_PARENT_ID"):null;
			UQ_ADMIN_ID = q_json.has("UQ_ADMIN_ID")?q_json.getString("UQ_ADMIN_ID"):null;			
//			UQ_PARENT_COMPANY_ID = q_json.has("UQ_PARENT_COMPANY_ID")?q_json.getString("UQ_PARENT_COMPANY_ID"):null;			
			E_STATUS = q_json.has("E_STATUS")?q_json.getString("E_STATUS").toUpperCase():null;
			json.put("query", queryJson);
		}
		if(chk_string_param(UQ_COMPANY_ID)&&chk_string_param(V_COMPANY_NAME)&&chk_string_param(V_COMPANY_ADDRESS_0)
//				&&chk_string_param(V_COMPANY_ADDRESS_1)&&chk_string_param(V_COMPANY_ADDRESS_2)
				&&(V_COMMENT!=null)&&chk_string_param(UQ_ADMIN_ID)//&&chk_string_param(UQ_PARENT_ID)
				&&chk_string_param(E_STATUS)) {
			if(UQ_COMPANY_ID!=null) {
				List list = ytiotTCompanyDao.query(" where u."+fieldname("UQ_COMPANY_ID")+" ='"+UQ_COMPANY_ID+"' ", null, null, null, null);
				if(list.size()>0) {
					YtiotTCompany o = (YtiotTCompany) list.get(0);
					if(V_COMPANY_NAME!=null)
						o.setVCompanyName(V_COMPANY_NAME);
					if(V_COMPANY_ADDRESS_0!=null)
						o.setVCompanyAddress0(V_COMPANY_ADDRESS_0);
//					if(UQ_PARENT_ID!=null)
//						o.setUqParentId(UQ_PARENT_ID);
//					if(UQ_PARENT_COMPANY_ID!=null)
//						o.setUqParentId(UQ_PARENT_COMPANY_ID);
//					o.setUqParentId(UQ_PARENT_ID);
//					o.setVCompanyAddress1(V_COMPANY_ADDRESS_1);
//					o.setVCompanyAddress2(V_COMPANY_ADDRESS_2);
					o.setUqAdminId(UQ_ADMIN_ID);
					o.setEStatus(E_STATUS);
					DaoResult ret = ytiotTCompanyDao.update(o, V_COMMENT);
					json.put("success", ret.isSuccess());
					json.put("MSG", ret.getResult());
				}else {
					json.put("success", false);
					json.put("MSG", "需要编辑的权限记录不存在");
				}
			}else {
				json.put("success", false);
				json.put("MSG", "记录ID不能为空");
			}
		}else {
			json.put("success", false);
			json.put("MSG", "参数验证错误");
		}
		return json.toString();
	}

	@Override
	public String deleteCompany(String queryJson) throws Exception {
		// TODO Auto-generated method stub
		//String UQ_COMPANY_ID = null;
		JSONObject q_json = null;
		JSONArray jSONArray = new JSONArray();
		JSONArray jsonCompanyList = new JSONArray();
		JSONObject json = new JSONObject();
		json.put("action", "deleteCompany");	
		if(queryJson!=null) {
			q_json = JSONObject.fromObject(queryJson);
			if(q_json.has("COMPANY_LIST")) {
				jsonCompanyList = q_json.getJSONArray("COMPANY_LIST");
				for(int i=0;i<jsonCompanyList.size();i++) {
					JSONObject j_temp = jsonCompanyList.getJSONObject(i);
					if(j_temp.has("UQ_COMPANY_ID")) {
						List list = ytiotTCompanyDao.query(" where u."+fieldname("UQ_COMPANY_ID")+" ='"+j_temp.getString("UQ_COMPANY_ID")+"' ", null, null, null, null);
						
						if(list.size()>0) {
							
							for(int j=0;j<list.size();j++) {
								YtiotTCompany o = (YtiotTCompany) list.get(j);
								o.setEStatus("3");
								ytiotTCompanyDao.delect(o, "注销");
								System.out.println("测试：111111111111111");
							}
						}
					}
				}
			}
		}	
		
//		if(queryJson!=null) {
//			q_json = JSONObject.fromObject(queryJson);
//			UQ_COMPANY_ID = q_json.has("UQ_COMPANY_ID")?q_json.getString("UQ_COMPANY_ID"):null;
//			json.put("query", queryJson);
//		}
//		
//		if(chk_string_param(UQ_COMPANY_ID)) {
//			if(UQ_COMPANY_ID!=null) {
//				List list = ytiotTCompanyDao.query(" where u."+fieldname("UQ_COMPANY_ID")+" ='"+UQ_COMPANY_ID+"' ", null, null, null, null);
//				if(list.size()>0) {
//					YtiotTCompany o = (YtiotTCompany) list.get(0);
//					o.setEStatus("3");
//					DaoResult ret = ytiotTCompanyDao.delect(o, V_COMMENT);
//					json.put("success", ret.isSuccess());
//					json.put("MSG", ret.getResult());
//				}else {
//					json.put("success", false);
//					json.put("MSG", "记录不存在");
//				}
//			}else {
//				json.put("success", false);
//				json.put("MSG", "记录ID不能为空");
//			}
//		}else {
//			json.put("success", false);
//			json.put("MSG", "参数验证错误");
//		}
		return json.toString();
		
	}

	@Override
	public String getCompanyList(String queryJson, String page, String start, String limit, String sort) throws Exception {
		// TODO Auto-generated method stub
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
		String UQ_COMPANY_ID = null;
		
		String V_COMPANY_NAME=null;//单位名称
		String V_COMPANY_ADDRESS_0=null;//单位地址_1
		String I_VERSION=null;//当前记录版本
		String D_STORETIME=null;//记录存储时间
