package com.zbooksoft.gdmis.common;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.ruili.wcp.common.SpringContextUtil;
|
import com.ruili.wcp.common.TextExtractUtil;
|
import com.ruili.wcp.data.entity.config.Module;
|
import com.ruili.wcp.data.entity.config.TaskLog;
|
import com.ruili.wcp.data.entity.config.View;
|
import com.ruili.wcp.engine.FulltextIndexBuilder;
|
import com.ruili.wcp.service.config.ModuleService;
|
import com.ruili.wcp.service.config.TaskLogService;
|
import com.ruili.wcp.service.config.ViewService;
|
import org.quartz.*;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
|
@DisallowConcurrentExecution
|
public class EsAttachIndexBuildJob implements Job {
|
@Autowired
|
private JdbcTemplate jdbcTemplate;
|
@Autowired
|
private TaskLogService taskLogService;
|
@Autowired
|
private ModuleService moduleService;
|
@Autowired
|
private ViewService viewService;
|
private static final Logger logger = LoggerFactory.getLogger(EsAttachIndexBuildJob.class);
|
|
@Override
|
public void execute(JobExecutionContext context) throws JobExecutionException {
|
// TODO Auto-generated method stub
|
FulltextIndexBuilder fulltextIndexBuilder = (FulltextIndexBuilder) SpringContextUtil.getBean(FulltextIndexBuilder.class);
|
boolean isExists = false;
|
int ESIndexCount = 0;
|
int ESIndexFail = 0;
|
JobDataMap jobDetailDataMap = context.getJobDetail().getJobDataMap();
|
String taskName = jobDetailDataMap.getString("taskName");
|
Long id = jobDetailDataMap.getLong("id");
|
|
if (!fulltextIndexBuilder.conn()) {
|
TaskLog log = new TaskLog();
|
log.setTaskName(taskName);
|
log.setAction("任务调度运行");
|
log.setTaskId(id);
|
log.setOperateType("运行");
|
log.setMessage("ES服务器连接失败,请检查服务器配置是否正确!");
|
log.setOperateTime(new Date());
|
taskLogService.saveOrUpdate(log);
|
return;
|
}
|
isExists = fulltextIndexBuilder.isIndexLibraryExists("attach");
|
if (!isExists) {
|
boolean isCreate = fulltextIndexBuilder.createIndexAttachLibrary();
|
if (!isCreate) {
|
TaskLog log = new TaskLog();
|
log.setTaskName(taskName);
|
log.setAction("任务调度运行");
|
log.setTaskId(id);
|
log.setOperateType("运行");
|
log.setMessage("创建附件索引库失败");
|
log.setOperateTime(new Date());
|
taskLogService.saveOrUpdate(log);
|
return;
|
}
|
}
|
// 取得需要添加es索引的模块ID
|
QueryWrapper<Module> queryWrapper = new QueryWrapper<Module>();
|
queryWrapper.eq("enable_attach_fulltext", 1);
|
List<Module> modules = moduleService.list(queryWrapper);
|
|
for (Module module : modules) {
|
ESIndexCount = 0;
|
ESIndexFail = 0;
|
String moduleId = module.getModuleId().toString();
|
String moduleTableName = module.getMainTableName();
|
QueryWrapper<View> viewQueryWrapper = new QueryWrapper<View>();
|
viewQueryWrapper.like("view_identify", "Search");
|
viewQueryWrapper.eq("module_id", moduleId);
|
View view = viewService.getOne(viewQueryWrapper);
|
String sqlWhere = view.getSqlWhere();
|
// 一个模块ID的所有附件
|
String sql = "SELECT * FROM " + moduleTableName + " where " + sqlWhere;
|
//String sql = "SELECT * FROM sys_attach a," + moduleTableName + " b where a.fulltext_state=0 and a.module_id=" + moduleId + " and a.key_id=b.id and b.delete_state=0 and b."+sqlWhere;
|
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
|
for (Map<String, Object> map : list) {
|
try {
|
String attachPath = map.get("WJLJ").toString();
|
String esId = map.get("id").toString();
|
String attachType = map.get("WJGS").toString();
|
String content = TextExtractUtil.extract(attachPath, attachType);
|
if (content != null && !"".equals(content)) {
|
map.put("fullText", content);
|
String IsSucceed = fulltextIndexBuilder.createIndex("attach", "file", esId, map);
|
if (IsSucceed.equals("update") || IsSucceed.equals("created")) {
|
jdbcTemplate.execute("update " + moduleTableName + " set fulltext_state=1 where id=" + esId);
|
ESIndexCount++;
|
} else {
|
ESIndexFail++;
|
}
|
} else {
|
ESIndexFail++;
|
}
|
} catch (Exception ex) {
|
logger.error(ex.getMessage(), ex);
|
ESIndexFail++;
|
}
|
|
}
|
|
|
TaskLog log = new TaskLog();
|
log.setTaskName(taskName);
|
log.setAction("任务调度运行");
|
log.setTaskId(id);
|
log.setOperateType("运行");
|
log.setMessage("成功创建" + module.getModuleName() + "模块附件索引" + ESIndexCount + "条,失败" + ESIndexFail + "条");
|
log.setOperateTime(new Date());
|
taskLogService.saveOrUpdate(log);
|
}
|
|
fulltextIndexBuilder.closeConn();
|
}
|
|
}
|