多单位版国产化地质资料管理系统
zhai
2025-12-13 fc0cc9fff4b4cbdc7cbb52b4a96c947530fcbba0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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();
    }
 
}