Browse Source

上传文件至 'log'

elasticsearch 相关方法的 acud操作。
沉锋 5 years ago
parent
commit
5567394c31
3 changed files with 1042 additions and 0 deletions
  1. 399 0
      log/AppHandler.java
  2. 221 0
      log/AppServiceImpl.java
  3. 422 0
      log/ElasticSearchDao.java

+ 399 - 0
log/AppHandler.java

@@ -0,0 +1,399 @@
+package com.pcitc.imp.bizlog.service.handler;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+
+import com.pcitc.imp.bizlog.bll.itf.AppService;
+import com.pcitc.imp.bizlog.exception.BusiException;
+import com.pcitc.imp.bizlog.service.model.App;
+import com.pcitc.imp.bizlog.util.CheckPrompt;
+import com.pcitc.imp.bizlog.util.CheckUtil;
+import com.pcitc.imp.bizlog.util.ErrorCodeEnum;
+import com.pcitc.imp.common.handler.BaseHandler;
+import com.pcitc.imp.common.model.Pagination;
+
+import io.vertx.core.Vertx;
+import io.vertx.ext.web.RoutingContext;
+import pcitc.imp.common.ettool.utils.ObjectConverter;
+import pcitc.imp.common.ettool.utils.RestfulTool;
+
+/**
+ * Created by pcitc on 2016/12/27.
+ */
+@Controller
+public class AppHandler extends BaseHandler {
+	
+	@Autowired
+	private AppService appService;
+
+    /**
+     * 新增应用
+     *
+     * @param routingContext
+     */
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+	public void addApp(RoutingContext routingContext) {
+        try {
+            List<com.pcitc.imp.bizlog.service.model.App> listApp = RestfulTool.toResourceRep(routingContext.getBodyAsString(),
+            		com.pcitc.imp.bizlog.service.model.App.class);
+            // 空格校验
+            List<App> apps = getStringNoBlank(listApp);
+            // 非空校验
+//            checkInput(apps);
+            for (App app : apps) {
+                app.setHref(routingContext.normalisedPath() + "/" + app.getCode());
+            }
+            List appEntitys = ObjectConverter.listConverter(apps, com.pcitc.imp.bizlog.bll.entity.App.class);
+            // 创建app
+            Vertx vertx = routingContext.vertx();
+            vertx.executeBlocking(future -> {
+                try {
+                	Integer addApp = appService.addApp(appEntitys);
+                    future.complete(addApp.toString());
+                } catch (Exception e) {
+                    future.fail(e);
+                }
+            }, res -> {
+                String collecion = null;
+                if (res.failed()) {
+                    collecion = buildErrorCollection(routingContext, (BusiException) res.cause());
+                } else if (res.succeeded()) {
+                	try {
+                		Object result = res.result();
+                        returnCollection(routingContext, result.toString());
+                    } catch (Exception e) {
+                        collecion = buildErrorCollection(routingContext, e);
+                        returnCollection(routingContext, collecion);
+                    }
+                }
+                returnCollection(routingContext, collecion);
+            });
+        } catch (BusiException e) {
+            String collecion = buildErrorCollection(routingContext, e);
+            returnCollection(routingContext, collecion);
+        } catch (Exception e) {
+            String collecion = buildErrorCollection(routingContext, e);
+            returnCollection(routingContext, collecion);
+        }
+    }
+
+    /**
+     * 查询应用
+     *
+     * @param routingContext
+     */
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+	public void getApps(RoutingContext routingContext) {
+
+        String skip = (routingContext.request().getParam("$skip") == null ? null : routingContext.request().getParam("$skip").trim());
+        String top = (routingContext.request().getParam("$top") == null ? null : routingContext.request().getParam("$top").trim());
+        String codeList = (routingContext.request().getParam("$codeList") == null ? null : routingContext.request().getParam("$codeList").trim());
+        try {
+            // 输入校验
+            String codes = checkInput(skip, top, codeList);
+            Pagination pagination = new Pagination();
+            // 查询app
+            Vertx vertx = routingContext.vertx(); 
+            vertx.executeBlocking(future -> {
+                List<com.pcitc.imp.bizlog.bll.entity.App> appList = null;
+                try {
+                	List<Integer> countList = new ArrayList();
+                    
+                    if(routingContext.request().getParam("$skip") != null && routingContext.request().getParam("$top") != null){
+                    	appList = appService.getAppsByPage(countList,skip, top, codes);
+                    	if (!appList.isEmpty()) {
+                    		pagination.setRecordCount(Long.valueOf(countList.get(0).toString()));
+                    	}
+                    }else{
+                    	appList = appService.getAppsByPage(skip, top, codes);
+                    }
+                    future.complete(appList);
+                } catch (BusiException e) {
+                    future.fail(e);
+                }
+            }, res -> {
+                String collecion = null;
+                if (res.failed()) {
+                    collecion = buildErrorCollection(routingContext, (BusiException) res.cause());
+                    returnCollection(routingContext, collecion);
+                } else if (res.succeeded()) {
+                    try {
+                        List<App> listApp = ObjectConverter.listConverter((List<com.pcitc.imp.bizlog.bll.entity.App>) res.result(), App.class);
+                        collecion = RestfulTool.buildCollection(listApp, pagination, routingContext.request().absoluteURI(), App.class);
+                        returnCollection(routingContext, collecion);
+                    } catch (Exception e) {
+                        collecion = buildErrorCollection(routingContext, e);
+                        returnCollection(routingContext, collecion);
+                    }
+                }
+            });
+        } catch (BusiException e) {
+            String collecion = buildErrorCollection(routingContext, e);
+            returnCollection(routingContext, collecion);
+        }
+    }
+
+    /**
+     * 根据编码查询应用
+     *
+     * @param routingContext
+     */
+    @SuppressWarnings("unchecked")
+	public void getAppByCode(RoutingContext routingContext) {
+        try {
+            String codetrim = routingContext.request().getParam("code").trim();
+            // 输入校验
+            checkInput(codetrim);
+            // 查询app
+            Vertx vertx = routingContext.vertx();
+            vertx.executeBlocking(future -> {
+                List<com.pcitc.imp.bizlog.bll.entity.App> appList = null;
+                try {
+                    appList = appService.getAppByCode(codetrim);
+                    future.complete(appList);
+                } catch (BusiException e) {
+                    future.fail(e);
+                }
+            }, res -> {
+                String collecion = null;
+                if (res.failed()) {
+                    collecion = buildErrorCollection(routingContext, (BusiException) res.cause());
+                    returnCollection(routingContext, collecion);
+                } else if (res.succeeded()) {
+                    try {
+                        List<App> listApp = ObjectConverter.listConverter((List<com.pcitc.imp.bizlog.bll.entity.App>) res.result(), App.class);
+                        collecion = RestfulTool.buildCollection(listApp, routingContext.request().absoluteURI(), App.class);
+                        returnCollection(routingContext, collecion);
+                    } catch (Exception e) {
+                        collecion = buildErrorCollection(routingContext, e);
+                        returnCollection(routingContext, collecion);
+                    }
+                }
+            });
+        } catch (BusiException e) {
+            String collecion = buildErrorCollection(routingContext, e);
+            returnCollection(routingContext, collecion);
+        }
+    }
+
+    /**
+     * 更新应用的名称
+     * 不支持批更新
+     *
+     * @param routingContext
+     */
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+	public void updateAppName(RoutingContext routingContext) {
+        try {
+            List<com.pcitc.imp.bizlog.service.model.App> listApp = RestfulTool.toResourceRep(routingContext.getBodyAsString(),
+            		com.pcitc.imp.bizlog.service.model.App.class);
+            // 空格校验
+            listApp = getStringNoBlank(listApp);
+            // 非空校验
+            checkInputUp(listApp);
+            List appEntity = ObjectConverter.listConverter(listApp, com.pcitc.imp.bizlog.bll.entity.App.class);
+            // 更新app
+            Vertx vertx = routingContext.vertx();
+            vertx.executeBlocking(future -> {
+                try {
+                	appService.updateAppName(routingContext.request().getParam("code"), appEntity);
+                    future.complete();
+                } catch (BusiException e) {
+                    future.fail(e);
+                }
+            }, res -> {
+                String collecion = null;
+                if (res.failed()) {
+                    collecion = buildErrorCollection(routingContext, (BusiException) res.cause());
+                } else if (res.succeeded()) {
+//                    collecion = "ok";
+                }
+                returnCollection(routingContext, collecion);
+            });
+        } catch (BusiException e) {
+            String collecion = buildErrorCollection(routingContext, e);
+            returnCollection(routingContext, collecion);
+        } catch (Exception e) {
+            String collecion = buildErrorCollection(routingContext, e);
+            returnCollection(routingContext, collecion);
+        }
+    }
+
+    /**
+     * 删除应用
+     *
+     * @param routingContext
+     */
+    public void deleteApp(RoutingContext routingContext) {
+        String code = routingContext.request().getParam("code");
+        String codeVal = (code == null ? null : code.trim());
+        try {
+            checkInput(codeVal);
+            // 更新app
+            Vertx vertx = routingContext.vertx();
+            vertx.executeBlocking(future -> {
+                try {
+                	appService.deleteApp(codeVal);
+                    future.complete();
+                } catch (BusiException e) {
+                    future.fail(e);
+                }
+            }, res -> {
+                String collecion = null;
+                if (res.failed()) {
+                    collecion = buildErrorCollection(routingContext, (BusiException) res.cause());
+                } else if (res.succeeded()) {
+//                    collecion = "ok";
+                }
+                returnCollection(routingContext, collecion);
+            });
+        } catch (BusiException e) {
+            String collecion = buildErrorCollection(routingContext, e);
+            returnCollection(routingContext, collecion);
+        }
+    }
+
+    /**
+     * @param code
+     * @return void    返回类型
+     * @throws BusiException
+     * @Title: checkInput
+     * @Description: 查询校验code
+     */
+    public void checkInput(String code) throws BusiException {
+        if (CheckUtil.checkStringIsNull(code)) {
+            throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_CODE_NULL);
+        }
+        Matcher checkMatcher = CheckUtil.checkMatcher(code);
+        if (checkMatcher.find() == false) {
+            throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.CHECKMATCHER);
+        }
+    }
+
+    /**
+     * 新增前数据校验
+     *
+     * @param appModels
+     * @throws BusiException
+     */
+    public void checkInput(List<App> appModels) throws BusiException {
+        for (App appModel : appModels) {
+            if (CheckUtil.checkStringIsNull(appModel.getCode())) {
+                throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_CODE_NULL+appModel.getCode());
+            }
+            if (CheckUtil.checkStringIsNull(appModel.getName())) {
+                throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_NAME_NULL+appModel.getName());
+            }
+            Matcher checkMatcher = CheckUtil.checkMatcher(appModel.getCode());
+            if (checkMatcher.find() == false) {
+                throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.CHECKMATCHER+appModel.getCode());
+            }
+            if (CheckUtil.characterFilter(appModel.getName())) {
+                throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_NAME_CHECKMATCHER+appModel.getName());
+            }
+        }
+    }
+
+    /**
+     * @param appModel
+     * @return App    返回类型
+     * @Title: getStringNoBlank
+     * @Description: 校验前后空格
+     */
+    public List<App> getStringNoBlank(List<App> appModel) {
+        for (App app : appModel) {
+            String code = null;
+            String name = null;
+            if (app.getCode() != null && !"".equals(app.getCode())) {
+                Matcher m = CheckUtil.checkNoBlank(app.getCode());
+                code = m.replaceAll("");
+            }
+            if (app.getName() != null && !"".equals(app.getName())) {
+                Matcher m = CheckUtil.checkNoBlank(app.getName());
+                name = m.replaceAll("");
+            }
+            app.setCode(code);
+            app.setName(name);
+        }
+        return appModel;
+    }
+
+    /**
+     * @param appModel
+     * @throws BusiException
+     * @Title: checkInputUp
+     * @Description: 修改时非空校验
+     */
+    public static void checkInputUp(List<App> appModel) throws BusiException {
+        for (App app : appModel) {
+            if (CheckUtil.checkStringIsNull(app.getName())) {
+                throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_NAME_NULL);
+            }
+            if (CheckUtil.characterFilter(app.getName())) {
+                throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_NAME_CHECKMATCHER);
+            }
+        }
+    }
+
+    /**
+     * @return
+     * @Title: checkCodeList
+     * @Description: 校验codeList
+     */
+    public static String checkCodeList(String codeList) throws BusiException {
+        String resultCodeList = "";
+        String[] strs = codeList.split(",");
+        for (int i = 0; i < strs.length; i++) {
+            // 空格校验
+            String codeLists = strs[i].trim();
+            // 非空校验
+            boolean checkStringIsNull = CheckUtil.checkStringIsNull(codeLists);
+            if (checkStringIsNull) {
+                continue;
+            }
+            // 特殊字符判断
+            if (CheckUtil.characterFilter(codeLists)) {
+                continue;
+            }
+            resultCodeList += codeLists;
+            if (i < strs.length - 1) {
+                resultCodeList = resultCodeList + ",";
+            }
+        }
+        return resultCodeList;
+    }
+
+    /**
+     * @Title: checkInput
+     */
+    public static String checkInput(String skip, String top, String codeList) throws BusiException {
+        String checkCodeList = null;
+        // 特殊字符判断
+        if (CheckUtil.checkStringIsNotNull(top) && CheckUtil.characterFilter(top)) {
+            throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.TOP);
+        }
+        if (CheckUtil.checkStringIsNotNull(top) && !CheckUtil.checkDigit(top)) {
+            throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.TOP_HIGHER);
+        }
+        if (CheckUtil.checkStringIsNotNull(top) && Integer.parseInt(top) == 0) {
+            throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.TOP);
+        }
+        // 特殊字符判断
+        if (CheckUtil.checkStringIsNotNull(skip) && CheckUtil.characterFilter(skip)) {
+            throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.SKIP);
+        }
+        if (CheckUtil.checkStringIsNotNull(skip) && !CheckUtil.checkDigit(skip)) {
+            throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.SKIP);
+        }
+
+        if (CheckUtil.checkStringIsNotNull(codeList)) {
+            //校验codeList
+            checkCodeList = checkCodeList(codeList);
+        }
+        return checkCodeList;
+    }
+}

+ 221 - 0
log/AppServiceImpl.java

@@ -0,0 +1,221 @@
+package com.pcitc.imp.bizlog.bll.itf.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+
+import com.pcitc.imp.bizlog.bll.itf.AppService;
+import com.pcitc.imp.bizlog.dal.dao.AppDao;
+import com.pcitc.imp.bizlog.dal.dao.LogDao;
+import com.pcitc.imp.bizlog.exception.BusiException;
+import com.pcitc.imp.bizlog.service.model.Condition;
+import com.pcitc.imp.bizlog.util.CheckPrompt;
+import com.pcitc.imp.bizlog.util.CheckUtil;
+import com.pcitc.imp.bizlog.util.EntityUtil;
+import com.pcitc.imp.bizlog.util.ErrorCodeEnum;
+
+import pcitc.imp.common.ettool.utils.ObjectConverter;
+
+/**
+ * @ClassName: AppServiceImpl
+ * @Description: 业务应用集合
+ * @author
+ * @date 2016年9月5日 上午8:49:30
+ */
+@Service
+@Component
+public class AppServiceImpl implements AppService{
+
+	@Autowired
+	private AppDao appDao;
+	@Autowired
+	private LogDao logDao;
+
+	/**
+	 * @Title: getAppByCode
+	 * @Description: app查询-条件查询code
+	 * @throws BusiException
+	 * @return List<App> 返回类型
+	 */
+	@SuppressWarnings("unchecked")
+	public List<com.pcitc.imp.bizlog.bll.entity.App> getAppByCode(String code) throws BusiException {
+		List<com.pcitc.imp.bizlog.dal.pojo.App> appList = null;
+		List<com.pcitc.imp.bizlog.bll.entity.App> applists = new ArrayList<>();
+		try {
+			appList = appDao.getAppByCode(code);
+			applists = ObjectConverter.listConverter(appList, com.pcitc.imp.bizlog.dal.pojo.App.class);
+		} catch (BusiException e) {
+			throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.QUERY, e);
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return applists;
+	}
+
+	/**
+	 * @Title: addApp
+	 * @Description: 日志操作记录-添加
+	 * @return void 返回类型
+	 * @param appEntitys
+	 * @throws Exception 
+	 */
+	public int addApp(List<com.pcitc.imp.bizlog.bll.entity.App> etAppList) throws BusiException {
+		List<com.pcitc.imp.bizlog.dal.pojo.App> pojoList = new ArrayList<>();
+		List<BusiException> errDatas = new ArrayList<>();
+		int count = etAppList.size();
+		for(com.pcitc.imp.bizlog.bll.entity.App appEntity : etAppList){
+			// 校验appcode是否已经存在
+			if (appDao.queryByParam(appEntity.getCode())) {
+				throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_CODE_REPEAT);
+			}
+			if(etAppList != null && etAppList.size()>0){
+				count--;
+				checkInput(appEntity,count,errDatas);
+			}
+			// entity转换为pojo
+			com.pcitc.imp.bizlog.dal.pojo.App appPojo;
+			try {
+				appPojo = EntityUtil.entity2Pojo(appEntity);
+				pojoList.add(appPojo);
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+		// 创建app
+		return appDao.insertApp(pojoList);
+	}
+
+	/**
+	 * @Title: deleteApp @Description: 日志操作记录-删除-根据条件删除 @param code 唯一条件 @return
+	 *         void 返回类型 @throws
+	 */
+	public void deleteApp(String code) throws BusiException {
+		List<com.pcitc.imp.bizlog.dal.pojo.Log> logPojoList = null;
+		Condition condition = new Condition();
+		boolean logIndexExist = true;
+		try {
+			logPojoList = logDao.getLogsByCon(code, condition);
+		} catch (BusiException e) {
+			logIndexExist = false;
+		}
+		if (logIndexExist && !logPojoList.isEmpty()) {
+			throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_CODE_REPEAT_LOG);
+		}
+		if (logIndexExist && logPojoList.isEmpty()) {
+			logDao.deleteLogs(code);
+		}
+		// 校验appcode是否已经存在
+		if (!appDao.queryByParam(code)) {
+			throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_CODE_NOT_REPEAT);
+		}
+		appDao.deleteApp(code);
+	}
+
+	/**
+	 * @Title: updateAppName
+	 * @Description: 日志操作记录-修改
+	 * @param code
+	 * @param appEntitys
+	 * @throws BusiException
+	 * @return String 返回类型
+	 * @throws BusiException
+	 */
+	@SuppressWarnings({ "unchecked", "unused" })
+	public void updateAppName(String code, List<com.pcitc.imp.bizlog.bll.entity.App> appEntitys) throws BusiException {
+		// 校验appcode是否已经存在
+		for (com.pcitc.imp.bizlog.bll.entity.App appEntity:appEntitys) {
+			if (!appDao.queryByParam(code)) {
+				throw new BusiException(ErrorCodeEnum.M000, CheckPrompt.APP_CODE_NOT_REPEAT);
+			}
+		}
+		List<com.pcitc.imp.bizlog.dal.pojo.App> appPojoList = null;
+		try {
+			appPojoList = ObjectConverter.listConverter(appEntitys, com.pcitc.imp.bizlog.dal.pojo.App.class);
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, e.getMessage());
+		}
+		appDao.updateAppName(code, appPojoList.get(0));
+	}
+
+	/**
+	 * @Title: getAppsByPage
+	 * @Description:分页查询 +codeList 查询
+	 * @param skip
+	 * @param top
+	 * @throws BusiException
+	 * @return List<App> 返回类型
+	 */
+	@SuppressWarnings("unchecked")
+	public List<com.pcitc.imp.bizlog.bll.entity.App> getAppsByPage(List<Integer> countList,String skip, String top, String codes) throws BusiException {
+		List<com.pcitc.imp.bizlog.dal.pojo.App> appPojo = null;
+		List<com.pcitc.imp.bizlog.bll.entity.App> appEntity = new ArrayList<>();
+		try {
+			appPojo = appDao.getAppsByPage(countList,skip, top, codes);
+			appEntity = ObjectConverter.listConverter(appPojo, com.pcitc.imp.bizlog.bll.entity.App.class);
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, e.getMessage());
+		}
+		return appEntity;
+	}
+	
+	@SuppressWarnings("unchecked")
+	public List<com.pcitc.imp.bizlog.bll.entity.App> getAppsByPage(String skip, String top, String codes) throws BusiException {
+		List<com.pcitc.imp.bizlog.dal.pojo.App> appPojo = null;
+		List<com.pcitc.imp.bizlog.bll.entity.App> appEntity = new ArrayList<>();
+		try {
+			appPojo = appDao.getAppsByPage(skip, top, codes);
+			appEntity = ObjectConverter.listConverter(appPojo, com.pcitc.imp.bizlog.bll.entity.App.class);
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, e.getMessage());
+		}
+		return appEntity;
+	}
+
+	/**
+	 * 创建appregister表
+	 */
+	public void creatAppRegister() throws BusiException{
+		appDao.creatAppRegister();
+	}
+	
+	/**
+	 * @Title: checkInput 
+	 * @Description: 应用批量验证新增返回信息
+	 * @param appEntity
+	 * @param integer
+	 * @param errList
+	 * @throws BusiException
+	 * @return void    返回类型 
+	 * @throws BusiException
+	 */
+	public void checkInput(com.pcitc.imp.bizlog.bll.entity.App appEntity,Integer integer,List<BusiException> errList) throws BusiException {
+
+		if (CheckUtil.checkStringIsNull(appEntity.getCode())) {
+			errList.add(new BusiException(ErrorCodeEnum.M000, appEntity.getCode()+":"+CheckPrompt.APP_CODE_NULL));
+		}
+		if (CheckUtil.checkStringIsNull(appEntity.getName())) {
+			errList.add(new BusiException(ErrorCodeEnum.M000, appEntity.getName()+":"+CheckPrompt.APP_NAME_NULL));
+		}
+		Matcher checkMatcher = CheckUtil.checkMatcher(appEntity.getCode());
+		if (checkMatcher.find() == false) {
+			errList.add(new BusiException(ErrorCodeEnum.M000, appEntity.getCode()+":"+CheckPrompt.CHECKMATCHER));
+		}
+		if (CheckUtil.characterFilter(appEntity.getName())) {
+			errList.add(new BusiException(ErrorCodeEnum.M000, appEntity.getName()+":"+CheckPrompt.APP_NAME_CHECKMATCHER));
+		}
+		if(integer ==0){
+			StringBuffer sb = new StringBuffer();
+			if(errList != null && errList.size()>0){
+				for (BusiException busiException : errList) {
+					sb.append(busiException.getMessage()+" ");
+				}
+				throw new BusiException(ErrorCodeEnum.M000,sb.toString());
+			}
+		}
+	}
+
+}

+ 422 - 0
log/ElasticSearchDao.java

@@ -0,0 +1,422 @@
+package com.pcitc.imp.bizlog.dal.dao.base;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.StatusLine;
+import org.apache.http.entity.ContentType;
+import org.apache.http.nio.entity.NStringEntity;
+import org.apache.http.util.EntityUtils;
+import org.elasticsearch.client.Response;
+import org.elasticsearch.client.RestClient;
+import org.springframework.stereotype.Repository;
+
+import com.pcitc.imp.bizlog.dal.dao.impl.Param;
+import com.pcitc.imp.bizlog.dal.pojo.App;
+import com.pcitc.imp.bizlog.dal.pojo.Log;
+import com.pcitc.imp.bizlog.exception.BusiException;
+import com.pcitc.imp.bizlog.service.model.Condition;
+import com.pcitc.imp.bizlog.util.CheckPrompt;
+import com.pcitc.imp.bizlog.util.CheckUtil;
+import com.pcitc.imp.bizlog.util.ClientFactory;
+import com.pcitc.imp.bizlog.util.ErrorCodeEnum;
+
+import io.vertx.core.json.Json;
+import io.vertx.core.json.JsonArray;
+import io.vertx.core.json.JsonObject;
+
+/**
+ * 用ES实现的持久层
+ *
+ * @author haiwen.wang
+ */
+@Repository
+public class ElasticSearchDao {
+
+	public ClientFactory getClientFactory() {
+		return ClientFactory.getInstance();
+	}
+
+	/**
+	 * 创建appregister索引
+	 *
+	 * @throws BusiException
+	 */
+	public void creatIndex(String url, String body) throws BusiException {
+		RestClient client = null;
+		try {
+			client = getClientFactory().getClient();
+			Map<String, String> params = new HashMap<String, String>();
+			HttpEntity entity = new NStringEntity(body, ContentType.APPLICATION_JSON);
+			client.performRequest("PUT", url, params, entity);
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, e.getMessage(), e);
+		}
+	}
+
+	/**
+	 * @param indexName
+	 * @return
+	 * @throws IOException
+	 */
+	@SuppressWarnings({ "unchecked", "rawtypes" })
+	public List<String> queryAll(String indexName) throws IOException {
+		RestClient client = getClientFactory().getClient();
+		String url = "/" + indexName + "/_search";
+		Map params = new HashMap<>();
+		params.put("size", String.valueOf(getClientFactory().getConfig().getInteger("buzilog.size")));  
+		List<String> result = performGet(client, url, params, null);
+		return result;
+	}
+
+	/**
+	 * 从GET方法的响应中取到数据
+	 * @param client
+	 * @param url
+	 * @param params
+	 * @param entity
+	 * @return
+	 * @throws IOException
+	 */
+	@SuppressWarnings({ "unchecked", "rawtypes" })
+	private List<String> performGet(RestClient client, String url, Map params, HttpEntity entity) throws IOException {
+		List<String> result = new ArrayList<String>();
+		Response response;
+		if (entity == null) {
+			response = client.performRequest("GET", url, params);
+		} else {
+			response = client.performRequest("GET", url, params, entity);
+		}
+		String retStr = EntityUtils.toString(response.getEntity());
+		JsonObject retJson = new JsonObject(retStr);
+		Integer count = retJson.getJsonObject("hits").getInteger("total");
+		JsonArray hits = retJson.getJsonObject("hits").getJsonArray("hits");
+		if (hits == null || hits.isEmpty()) {
+			return result;
+		}
+		result.add(count.toString());
+		for (int i = 0; i < hits.size(); i++) {
+			result.add(hits.getJsonObject(i).getJsonObject("_source").toString());
+		}
+		return result;
+	}
+	
+	@SuppressWarnings({ "unchecked", "rawtypes" })
+	private List<String> performGetUpdate(RestClient client, String url, Map params, HttpEntity entity) throws IOException {
+		List<String> result = new ArrayList<String>();
+		Response response;
+		if (entity == null) {
+			response = client.performRequest("GET", url, params);
+		} else {
+			response = client.performRequest("GET", url, params, entity);
+		}
+		String retStr = EntityUtils.toString(response.getEntity());
+		JsonObject retJson = new JsonObject(retStr);
+		JsonArray hits = retJson.getJsonObject("hits").getJsonArray("hits");
+		if (hits == null || hits.isEmpty()) {
+			return result;
+		}
+		for (int i = 0; i < hits.size(); i++) {
+			result.add(hits.getJsonObject(i).getJsonObject("_source").toString());
+			result.add(hits.getJsonObject(i).getString("_id"));
+		}
+		return result;
+	}
+	
+	/**
+	 * 根据条件查询数据
+	 * @param indexName
+	 * @param paramMap
+	 * @param skip
+	 * @param top
+	 * @return
+	 */
+	public List<String> queryByCon(String indexName, Map<String, Param> paramMap, String skip, String top, Condition condition)
+			throws BusiException {
+		RestClient client = getClientFactory().getClient();
+		String url = "/" + indexName + "/_search";
+		Map<String,String> params = new HashMap<String,String>();
+		if (!CheckUtil.checkStringIsNull(skip)) {
+			params.put("from", skip);
+		}
+		if (!CheckUtil.checkStringIsNull(top)) {
+			params.put("size", top);
+		}
+		JsonArray should = new JsonArray();
+		JsonArray must = new JsonArray();
+		JsonArray filter = new JsonArray();
+		JsonObject range = new JsonObject();
+		JsonObject item_p = new JsonObject();
+		Set<String> keys = paramMap.keySet();
+		String keyV = null;
+		for ( String key : keys) {
+			keyV = key;
+			Param param = paramMap.get(key);
+			if(key != "timestamp"){
+				if (param.getQueryType().equals("should")) {
+					setParam(should, key, param);
+				} else if (param.getQueryType().equals("must")) {
+					setParam(must, key, param);
+				} else if (param.getQueryType().equals("filter")) {
+					setParam(filter, key, param);
+				}
+			}else{
+				param.getMatchType().equals("range");
+				JsonObject item = new JsonObject();
+					item.put("gte", condition.getStartTime());
+					item.put("lte", condition.getEndTime());
+					range.put("timestamp", item);
+					item_p.put(param.getMatchType(), range);
+			}
+		}
+		JsonObject query = new JsonObject();
+		if(keyV != "timestamp"){
+		JsonObject con_p = new JsonObject();
+		con_p.put("must", must);
+		con_p.put("should", should);
+		con_p.put("filter", filter);
+
+		JsonObject bool = new JsonObject();
+		bool.put("bool", con_p);
+		query.put("query", bool);
+		}else{
+		query.put("query", item_p);
+		
+		}
+		HttpEntity entity = new NStringEntity(query.toString(), ContentType.APPLICATION_JSON);
+		List<String> result = null;
+		try {
+			result = performGet(client, url, params, entity);
+		} catch (IOException e) {
+			throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.LOG_NOT_EXIST, e);
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, e.getMessage(), e);
+		} 
+		return result;
+	}
+
+	@SuppressWarnings("rawtypes")
+	private void setParam(JsonArray queryJson, String key, Param param) {
+		List values = param.getValue();
+		for (int i = 0; i < values.size(); i++) {
+			JsonObject item = new JsonObject();
+			item.put(key, values.get(i));
+			JsonObject item_p = new JsonObject();
+			item_p.put(param.getMatchType(), item);
+			queryJson.add(item_p);
+		}
+	}
+
+	/**
+	 * 根据某个字段查询数据
+	 */
+	@SuppressWarnings({ "unchecked", "rawtypes" })
+	public List<String> queryByField(String indexName, String fieldName, String value) throws BusiException {
+		try {
+			RestClient client = getClientFactory().getClient();
+			String url = "/" + indexName + "/_search";
+			Map params = new HashMap<>();
+			params.put("q", fieldName + ":" + value);
+			List<String> result = performGet(client, url, params, null);
+			return result;
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.QUERY + ":" + e.getMessage(), e);
+		}
+	}
+	
+	@SuppressWarnings({ "unchecked", "rawtypes" })
+	public List<String> queryByFieldUpdate(String indexName, String fieldName, String value) throws BusiException {
+		try {
+			RestClient client = getClientFactory().getClient();
+			String url = "/" + indexName + "/_search";
+			Map params = new HashMap<>();
+			params.put("q", fieldName + ":" + value);
+			List<String> result = performGetUpdate(client, url, params, null);
+			return result;
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.QUERY + ":" + e.getMessage(), e);
+		}
+	}
+
+	/**
+	 * @return void 返回类型
+	 * @throws BusiException
+	 * @Title: insert
+	 * @Description: 添加
+	 */
+	@SuppressWarnings("unused")
+	public <E> void insert(String tableName, String typeName, List<E> pojo) throws BusiException {
+		try {
+			RestClient client = getClientFactory().getClient();
+			StringBuilder bodyStr = new StringBuilder();
+			bodyStr.append("{ \"index\" : { \"_index\" : \"" + tableName + "\", \"_type\" : \"" + typeName + "\"} }"+"\r\n");
+			for (int i = 0; i < pojo.size(); i++) {
+				bodyStr.append(Json.encode(pojo.get(i))+"\r\n");
+			}
+			Map<String, String> params = new HashMap<String, String>();
+			HttpEntity entity = new NStringEntity(bodyStr.toString(), ContentType.APPLICATION_JSON);
+			Response indexResponse = client.performRequest("POST", "/_bulk",params,entity);
+		} catch (IOException e) {
+			throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.INSERT + ":" + e.getMessage(), e);
+		}
+	}
+	
+	/**
+	 * @Title: insertApp
+	 * @Description: 添加应用
+	 * @return void 返回类型
+	 * @throws BusiException
+	 */
+	@SuppressWarnings("unused")
+	public int insertApp(String tableName, String typeName, List<App> pojo) throws BusiException {
+		RestClient client = getClientFactory().getClient();
+		int i = 0;
+		try {
+			List<String> lists = new ArrayList<>();
+			StringBuilder bodyStr = new StringBuilder();
+			Map<String, String> params = new HashMap<String, String>();
+			for (App app : pojo) {
+				bodyStr.append("{ \"index\" : { \"_index\" : \"" + tableName + "\", \"_type\" : \"" + typeName + "\"} }"
+						+ "\r\n");
+				bodyStr.append(Json.encode(app) + "\r\n");
+				i++;
+			}
+			HttpEntity entity = new NStringEntity(bodyStr.toString(), ContentType.APPLICATION_JSON);
+			Response indexResponse = client.performRequest("POST", "/_bulk", params, entity);
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.INSERT + ":" + e.getMessage(), e);
+		}
+		return i;
+	}
+	
+	/**
+	 * @Title: insertLog
+	 * @Description: 添加日志
+	 * @return void 返回类型
+	 * @throws BusiException
+	 */
+	@SuppressWarnings("unused")
+	public int insertLog(String tableName, String typeName, List<Log> pojoList) throws BusiException {
+		RestClient client = getClientFactory().getClient();
+		int i = 0 ;
+		try {
+			List<String> lists = new ArrayList<>();
+			Map<String, String> params = new HashMap<String, String>();
+			StringBuilder bodyStr = new StringBuilder();
+			for (Log log : pojoList) {
+				bodyStr.append("{ \"index\" : { \"_index\" : \"" + tableName + "\", \"_type\" : \"" + typeName + "\"} }"
+						+ "\r\n");
+					bodyStr.append(Json.encode(log) + "\r\n");
+					i++;
+			}
+			HttpEntity entity = new NStringEntity(bodyStr.toString(), ContentType.APPLICATION_JSON);
+			Response indexResponse = client.performRequest("POST", "/_bulk",params,entity);
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.INSERT + ":" + e.getMessage(), e);
+		}
+		return i;
+	}
+	
+	/**
+	 * @Title:
+	 * @Description: 操作记录-删除-根据条件删除
+	 * @param param
+	 *            删除的条件
+	 * @return void 返回类型
+	 * @throws BusiException
+	 */
+	@SuppressWarnings({ "unused", "rawtypes" })
+	public void deleteByParam(String tableName, String typeName,String id, Map param) throws BusiException {
+		try {
+			RestClient client = getClientFactory().getClient();
+			//由于版本internal控制不支持将值0作为有效的版本号,因此版本等于零的文档无法使用删除, _delete_by_query并且将会使请求失败。
+//			String url = "/" + tableName + "/" + typeName + "/_delete_by_query";
+			String url = "/" + tableName + "/" + typeName + "/" + id;
+			String paramStr = Json.encode(param);
+			JsonObject match = new JsonObject();
+			match.put("match", paramStr).toString();
+			JsonObject query = new JsonObject();
+			query.put("query", match);
+			Map<String, String> params = new HashMap<String, String>();
+			HttpEntity entity = new NStringEntity(query.toString(), ContentType.APPLICATION_JSON);
+			Response response = client.performRequest("DELETE", url, params, entity);
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.DELETE + ":" + e.getMessage(), e);
+		}
+	}
+
+	/**
+	 * 删除索引
+	 *
+	 * @param indexName
+	 * @throws BusiException
+	 */
+	@SuppressWarnings("unused")
+	public void deleteTable(String indexName) throws BusiException {
+		if (!isIndexExists(indexName)) {
+		} else {
+			RestClient client = getClientFactory().getClient();
+			String url = "/" + indexName;
+			try {
+				Response response = client.performRequest("DELETE", url);
+			} catch (IOException e) {
+				throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.DELETE + ":" + e.getMessage(), e);
+			}
+		}
+	}
+
+	/**
+	 * 判断索引是否存在 传入参数为索引库名称
+	 *
+	 * @param indexName
+	 * @return
+	 * @throws BusiException
+	 */
+	public boolean isIndexExists(String indexName) throws BusiException {
+		boolean flag = false;
+		try {
+			RestClient client = getClientFactory().getClient();
+			String url = "/" + indexName;
+			Response response = client.performRequest("GET", url);
+			StatusLine status = response.getStatusLine();
+			if (status.getStatusCode() == 200) {
+				flag = true;
+			} else if (status.getStatusCode() == 404) {
+				flag = false;
+			}
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.QUERY + ":" + e.getMessage(), e);
+		}
+		return flag;
+	}
+
+	/**
+	 * @param tableName
+	 * @param typeName
+	 * @param id
+	 * @param pojo
+	 * @return String 返回类型
+	 * @throws BusiException
+	 * @throws BusiException
+	 * @Title: updateAppName
+	 * @Description: 日志操作记录-修改
+	 */
+	@SuppressWarnings("unused")
+	public <E> void update(String tableName, String typeName, String id, E pojo) throws BusiException {
+		try {
+			RestClient client = getClientFactory().getClient();
+			String url = "/" + tableName + "/" + typeName + "/" + id;
+			String bodyStr = Json.encode(pojo);
+			HttpEntity entity = new NStringEntity(bodyStr, ContentType.APPLICATION_JSON);
+			Map<String, String> params = new HashMap<String, String>();
+			Response indexResponse = client.performRequest("PUT", url, params, entity);
+		} catch (Exception e) {
+			throw new BusiException(ErrorCodeEnum.M001, CheckPrompt.UPDATE + ":" + e.getMessage(), e);
+		}
+	}
+}