package org.mycat.web.service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.curator.utils.ZKPaths;
import org.hx.rainbow.common.context.RainbowContext;
import org.hx.rainbow.common.util.JavaBeanUtil;
import org.mycat.web.util.JavaBeanToMapUtil;
import org.mycat.web.util.ZookeeperCuratorHandler;

import com.alibaba.fastjson.JSON;

public abstract class AbstractConfigSevice {
	
	private ZookeeperCuratorHandler zkHander=  ZookeeperCuratorHandler.getInstance();
	
	@SuppressWarnings("unchecked")
	public <T> RainbowContext queryByPage(RainbowContext context,Class<T> entity) {
		String searchPath = (String)context.getAttr("search");
		String zkId = (String)context.getAttr("zkId");
		String guid = (String)context.getAttr("guid");
		String parentPath = getPath(zkId,guid);
		Map<String, Object> beanMap = new HashMap<String, Object>();
		if(searchPath != null && !searchPath.equals("")){
			String path = ZKPaths.makePath(parentPath, searchPath);                    
			T t = zkHander.getBeanData(path, entity);
		    beanMap = JavaBeanToMapUtil.beanToMap(t);
			if (beanMap != null && beanMap.size() > 0) {
				context.addRow(beanMap);
				context.setTotal(1);
			}
		}else{
			int offset =Integer.parseInt((String)context.getAttr("offset") == null ? "0" :(String)context.getAttr("offset"));
			int limit = Integer.parseInt((String) context.getAttr("limit") == null ? "10" :(String) context.getAttr("limit") );
			Map<String, Object> data = new HashMap<String, Object>();
			try {
				data = zkHander.getChildNodeDataByPage(parentPath, entity, limit, offset,beanMap);
			} catch (Exception e) {
				
			}
			if(data != null && data.size() > 0){
				context.setRows((List<Map<String, Object>>) data.get("rows"));
				context.setTotal((int) data.get("total"));
			}
		}
	    return context;
	}
	
	@SuppressWarnings("unchecked")
	public <T> RainbowContext queryAll(RainbowContext context,Class<T> entity){
		String zkId = (String)context.getAttr("zkId");
		String guid = (String)context.getAttr("guid");
		String parentPath = getPath(zkId,guid);
		Map<String,Object> data = zkHander.getChildNodeData(parentPath,entity);
		if(data != null && data.size() > 0){
			context.setRows((List<Map<String, Object>>) data.get("rows"));
		}
	    return context;
	}

	public <T> RainbowContext query(RainbowContext context,Class<T> entity){
		String searchPath = (String)context.getAttr("search");
		String zkId = (String)context.getAttr("zkId");
		String path = getPath(zkId, searchPath);
		T t = zkHander.getBeanData(path, entity);
		Map<String, Object> beanMap = JavaBeanToMapUtil.beanToMap(t);
		if (beanMap != null && beanMap.size() > 0) {
			context.addRow(beanMap);
			context.setTotal(1);
		}
	    return context;
	}
	
	public <T> RainbowContext insert (RainbowContext context,Class<T> entity) throws Exception{
		Map<String,Object> params = context.getAttr();
		String zkId = (String)context.getAttr("zkId");
		String json_new =  JSON.toJSONString(params);
		T t = JSON.parseObject(json_new, entity);
		Field fields[] = entity.getDeclaredFields();
		Field.setAccessible(fields,   true);
		String fieldValue="";
		for (int i = 0; i < fields.length; i++) {
			Field field = fields[i];
			String fieldName = field.getName();
			
			if(fieldName.equals("name")){
				fieldValue = (String)field.get(t);
				break;
			}
		}
		String parentPath = getPath(zkId,"");
		List<String> childrenPath =  zkHander.getChildNode(parentPath);
		for (String cpath : childrenPath) {
			if(fieldValue !="" && fieldValue.equals(cpath)){
				context.setSuccess(false);
				context.setMsg("名称已存在");
				return context;
			}
		}
		String creatPath = ZKPaths.makePath(parentPath, fieldValue);
		String data = JSON.toJSONString(t);
		zkHander.createNode(creatPath, data);
		return context;
	}
	
	public <T> RainbowContext update(RainbowContext context,Class<T> entity) throws Exception{
		Map<String,Object> params = context.getAttr();
		String zkId = (String)context.getAttr("zkId");
		String guid = (String)context.getAttr("guid");
		String path = getPath(zkId,guid);
		boolean exists = zkHander.existsNode(path);
		if(exists){
			String json_newStr =  JSON.toJSONString(params);
			T _newObj = JSON.parseObject(json_newStr, entity);
			String  json_oldStr = zkHander.getNodeData(path);
			T _oldObj = JSON.parseObject(json_oldStr, entity);
			JavaBeanUtil.copyProperties(_newObj, _oldObj);
			String data = JSON.toJSONString(_oldObj);
			zkHander.setNodeData(path, data);
		}else{
			context.setMsg("zk 路径不存在");
			context.setSuccess(false);
		}
			
		return context;
	}

	public RainbowContext delete(RainbowContext context) throws Exception {
		String zkId = (String)context.getAttr("zkId");
		String guid = (String)context.getAttr("guid");
		String path = getPath(zkId,guid);
		boolean exists = zkHander.existsNode(path);
		if(exists){
			zkHander.deleteNode(path);
		}else{
			context.setMsg("zk路径不存在");
			context.setSuccess(false);
		}
		return context;
	}
	
	public abstract String getPath(String zkId,String guid) ;
	
}

@@ -1317,57 +1317,8 @@
 et%22)
- == null ? %220%22 :(String)context.getAttr(%22offset%22)
 );%0A%09
@@ -1385,59 +1385,8 @@
 it%22)
- == null ? %2210%22 :(String) context.getAttr(%22limit%22) 
 );%0A%09
@@ -3586,36 +3586,27 @@
 creatPath = 
-ZKPaths.make
+get
 Path(parentP
@@ -3983,32 +3983,32 @@
 h = getPath(
-zkId,gui
+guid,zkI
 d);%0A%09%09boolea

Revert "优化配置页面"