kerry vor 5 Jahren
Ursprung
Commit
a8277e5d7d
25 geänderte Dateien mit 468 neuen und 36 gelöschten Zeilen
  1. 7 1
      README.md
  2. 0 0
      elastic-publish-service/config/logback-my.xml
  3. 103 4
      elastic-publish-service/pom.xml
  4. 6 6
      elastic-publish-service/req/main-controller.http
  5. 20 0
      elastic-publish-service/src/main/java/com/vcarecity/publish/annotation/SkipMethod.java
  6. 42 0
      elastic-publish-service/src/main/java/com/vcarecity/publish/aop/PrepareAopController.java
  7. 13 0
      elastic-publish-service/src/main/java/com/vcarecity/publish/constants/ElasticConstant.java
  8. 39 10
      elastic-publish-service/src/main/java/com/vcarecity/publish/controller/MainController.java
  9. 20 0
      elastic-publish-service/src/main/java/com/vcarecity/publish/controller/PrepareController.java
  10. 3 1
      elastic-publish-service/src/main/java/com/vcarecity/publish/elastic/service/ElasticIndexService.java
  11. 37 1
      elastic-publish-service/src/main/java/com/vcarecity/publish/elastic/service/impl/ElasticIndexServiceImpl.java
  12. 7 0
      elastic-publish-service/src/main/java/com/vcarecity/publish/entity/UnitAgencyMergeEntity.java
  13. 7 2
      elastic-publish-service/src/main/java/com/vcarecity/publish/pojo/dto/UnitAgencyDTO.java
  14. 12 0
      elastic-publish-service/src/main/java/com/vcarecity/publish/pojo/dto/UnitIdDTO.java
  15. 3 2
      elastic-publish-service/src/main/java/com/vcarecity/publish/sql/service/UnitService.java
  16. 20 7
      elastic-publish-service/src/main/java/com/vcarecity/publish/sql/service/impl/UnitServiceImpl.java
  17. 41 0
      elastic-publish-service/src/main/java/com/vcarecity/publish/util/ObjectFieldToMap.java
  18. 28 0
      elastic-publish-service/src/main/resources/application-beta.yml
  19. 1 1
      elastic-publish-service/src/main/resources/application-dev.yml
  20. 2 1
      elastic-publish-service/src/main/resources/application.yml
  21. 14 0
      elastic-publish-service/src/main/resources/logback-spring.xml
  22. 2 0
      elastic-publish-service/src/main/resources/mapper/AgencyMapper.xml
  23. 4 0
      elastic-publish-service/src/main/resources/mapper/UnitMapper.xml
  24. 33 0
      elastic-publish-service/src/test/java/com/vcarecity/publish/elastic/service/impl/ElasticIndexServiceImplTest.java
  25. 4 0
      elastic-publish-service/src/test/java/com/vcarecity/publish/elastic/service/impl/ElasticLoadDataServiceImplTest.java

+ 7 - 1
README.md Datei anzeigen

@@ -9,4 +9,10 @@ https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest
9 9
 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-delete.html
10 10
 
11 11
 
12
-https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-update-by-query.html
12
+https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-update-by-query.html
13
+
14
+
15
+
16
+```
17
+
18
+```

+ 0 - 0
elastic-publish-service/config/logback-my.xml Datei anzeigen


+ 103 - 4
elastic-publish-service/pom.xml Datei anzeigen

@@ -25,6 +25,11 @@
25 25
         </dependency>
26 26
 
27 27
         <dependency>
28
+            <groupId>org.springframework.boot</groupId>
29
+            <artifactId>spring-boot-starter-aop</artifactId>
30
+        </dependency>
31
+
32
+        <dependency>
28 33
             <groupId>org.mybatis.spring.boot</groupId>
29 34
             <artifactId>mybatis-spring-boot-starter</artifactId>
30 35
             <version>2.1.1</version>
@@ -82,12 +87,106 @@
82 87
 
83 88
     </dependencies>
84 89
 
90
+
85 91
     <build>
86 92
         <plugins>
87
-            <!--            <plugin>-->
88
-            <!--                <groupId>org.springframework.boot</groupId>-->
89
-            <!--                <artifactId>spring-boot-maven-plugin</artifactId>-->
90
-            <!--            </plugin>-->
93
+            <plugin>
94
+                <groupId>org.springframework.boot</groupId>
95
+                <artifactId>spring-boot-maven-plugin</artifactId>
96
+            </plugin>
97
+
98
+            <plugin>
99
+                <groupId>org.apache.maven.plugins</groupId>
100
+                <artifactId>maven-jar-plugin</artifactId>
101
+                <configuration>
102
+                    <archive>
103
+                        <manifest>
104
+                            <addClasspath>true</addClasspath>
105
+                            <classpathPrefix>lib/</classpathPrefix>
106
+                            <mainClass>com.vcarecity.publish.ElasticPublishApplication</mainClass>
107
+                        </manifest>
108
+                    </archive>
109
+                    <excludes>
110
+                        <exclude>rebel.xml</exclude>
111
+                    </excludes>
112
+                </configuration>
113
+            </plugin>
114
+
115
+
116
+            <plugin>
117
+                <artifactId>maven-resources-plugin</artifactId>
118
+                <configuration>
119
+                    <encoding>UTF-8</encoding>
120
+                </configuration>
121
+
122
+                <executions>
123
+                    <execution>
124
+                        <id>copy-resource-config</id>
125
+                        <phase>package</phase>
126
+                        <goals>
127
+                            <goal>copy-resources</goal>
128
+                        </goals>
129
+
130
+                        <configuration>
131
+                            <outputDirectory>${project.build.directory}/config</outputDirectory>
132
+                            <resources>
133
+                                <resource>
134
+                                    <directory>${project.basedir}/src/main/resources</directory>
135
+                                    <include>*.yml</include>
136
+                                    <include>*.properties</include>
137
+                                </resource>
138
+                            </resources>
139
+                        </configuration>
140
+                    </execution>
141
+                    <execution>
142
+                        <id>copy-resource-logback</id>
143
+                        <phase>package</phase>
144
+                        <goals>
145
+                            <goal>copy-resources</goal>
146
+                        </goals>
147
+
148
+                        <configuration>
149
+                            <outputDirectory>${project.build.directory}/config</outputDirectory>
150
+                            <resources>
151
+                                <resource>
152
+                                    <directory>${project.basedir}/config</directory>
153
+                                    <include>logback-my.xml</include>
154
+                                </resource>
155
+                            </resources>
156
+                        </configuration>
157
+                    </execution>
158
+                </executions>
159
+            </plugin>
160
+            <plugin>
161
+                <groupId>org.apache.maven.plugins</groupId>
162
+                <artifactId>maven-surefire-plugin</artifactId>
163
+                <version>2.21.0</version>
164
+                <configuration>
165
+                    <skipTests>true</skipTests>
166
+                </configuration>
167
+            </plugin>
168
+
169
+
170
+            <plugin>
171
+                <groupId>org.apache.maven.plugins</groupId>
172
+                <artifactId>maven-dependency-plugin</artifactId>
173
+                <version>3.0.1</version>
174
+                <executions>
175
+
176
+                    <execution>
177
+                        <id>copy-dependencies</id>
178
+                        <phase>package</phase>
179
+                        <goals>
180
+                            <goal>copy-dependencies</goal>
181
+                        </goals>
182
+                        <configuration>
183
+                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
184
+                        </configuration>
185
+                    </execution>
186
+                </executions>
187
+            </plugin>
188
+
189
+
91 190
         </plugins>
92 191
     </build>
93 192
 

+ 6 - 6
elastic-publish-service/req/main-controller.http Datei anzeigen

@@ -3,10 +3,10 @@ Content-Type: application/json
3 3
 
4 4
 {
5 5
   "agencyPath": "/1",
6
-  "keyword": "鱼峰区",
7
-  "page": 2,
6
+  "keyword": "",
7
+  "page": 1,
8 8
   "size": 10,
9
-  "regulatoryLevelId": "27,28",
9
+  "regulatoryLevelId": "",
10 10
   "property": "",
11 11
   "classify": "",
12 12
   "agencyAttributeClassId": ""
@@ -20,10 +20,10 @@ Content-Type: application/json
20 20
 
21 21
 {
22 22
   "agencyPath": "/1",
23
-  "keyword": "鱼峰区",
24
-  "page": 2,
23
+  "keyword": "",
24
+  "page": 1,
25 25
   "size": 10,
26
-  "regulatoryLevelId": "27,28",
26
+  "regulatoryLevelId": "",
27 27
   "property": "",
28 28
   "classify": "",
29 29
   "agencyAttributeClassId": ""

+ 20 - 0
elastic-publish-service/src/main/java/com/vcarecity/publish/annotation/SkipMethod.java Datei anzeigen

@@ -0,0 +1,20 @@
1
+package com.vcarecity.publish.annotation;
2
+
3
+/**
4
+ * @author VcKerry on 12/20/19
5
+ */
6
+
7
+
8
+import java.lang.annotation.ElementType;
9
+import java.lang.annotation.Retention;
10
+import java.lang.annotation.RetentionPolicy;
11
+import java.lang.annotation.Target;
12
+
13
+/**
14
+ * @author VcKerry
15
+ */
16
+@Retention(RetentionPolicy.RUNTIME)
17
+@Target(value = {ElementType.METHOD})
18
+public @interface SkipMethod {
19
+    String value() default "";
20
+}

+ 42 - 0
elastic-publish-service/src/main/java/com/vcarecity/publish/aop/PrepareAopController.java Datei anzeigen

@@ -0,0 +1,42 @@
1
+package com.vcarecity.publish.aop;
2
+
3
+import com.vcarecity.publish.api.ApiResult;
4
+import com.vcarecity.publish.controller.PrepareController;
5
+import org.aspectj.lang.ProceedingJoinPoint;
6
+import org.aspectj.lang.annotation.Around;
7
+import org.aspectj.lang.annotation.Aspect;
8
+import org.aspectj.lang.annotation.Pointcut;
9
+import org.springframework.stereotype.Component;
10
+
11
+/**
12
+ * @author VcKerry on 12/20/19
13
+ */
14
+
15
+@Aspect
16
+@Component
17
+public class PrepareAopController {
18
+
19
+
20
+    @Pointcut("execution(public * com.vcarecity.publish.controller.*.*(..))")
21
+    public void preparePointcut() {
22
+
23
+    }
24
+
25
+
26
+    @Around("preparePointcut()")
27
+    public Object check(ProceedingJoinPoint pjp) throws Throwable {
28
+        Object target = pjp.getTarget();
29
+
30
+        if (target instanceof PrepareController) {
31
+
32
+            if (((PrepareController) target).isPrepare()) {
33
+                return pjp.proceed();
34
+            }
35
+            // 数据没有准备好...
36
+            return ApiResult.builder().status(40000).message("data loading ").build();
37
+        }
38
+
39
+        return pjp.proceed();
40
+    }
41
+
42
+}

+ 13 - 0
elastic-publish-service/src/main/java/com/vcarecity/publish/constants/ElasticConstant.java Datei anzeigen

@@ -1,5 +1,8 @@
1 1
 package com.vcarecity.publish.constants;
2 2
 
3
+import java.time.ZoneId;
4
+import java.time.format.DateTimeFormatter;
5
+
3 6
 /**
4 7
  * @author VcKerry on 12/18/19
5 8
  */
@@ -11,4 +14,14 @@ public class ElasticConstant {
11 14
 
12 15
 
13 16
     public static final int QUERY_PAGE_COUNT = 2000;
17
+
18
+
19
+    /**
20
+     * ElasticSearch 查询时的时间格式,插入JSON时的时间格式
21
+     */
22
+    public static final String ES_DATETIME_PATTERN = "yyyy/MM/dd HH:mm:ss";
23
+    public static final DateTimeFormatter ES_DT_FORMATTER = DateTimeFormatter.ofPattern(ES_DATETIME_PATTERN);
24
+
25
+
26
+    public static final String SYSTEM_TIMEZONE = ZoneId.systemDefault().getId();
14 27
 }

+ 39 - 10
elastic-publish-service/src/main/java/com/vcarecity/publish/controller/MainController.java Datei anzeigen

@@ -1,46 +1,75 @@
1 1
 package com.vcarecity.publish.controller;
2 2
 
3
+import com.vcarecity.publish.annotation.SkipMethod;
3 4
 import com.vcarecity.publish.api.ApiResult;
4 5
 import com.vcarecity.publish.api.ApiStatus;
5 6
 import com.vcarecity.publish.constants.ElasticConstant;
7
+import com.vcarecity.publish.elastic.service.ElasticLoadDataService;
6 8
 import com.vcarecity.publish.pojo.dto.UnitAgencyDTO;
9
+import com.vcarecity.publish.pojo.dto.UnitIdDTO;
7 10
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
8 11
 import com.vcarecity.publish.sql.service.UnitService;
9
-import org.springframework.web.bind.annotation.PostMapping;
10
-import org.springframework.web.bind.annotation.RequestBody;
11
-import org.springframework.web.bind.annotation.RestController;
12
+import org.springframework.web.bind.annotation.*;
12 13
 
13 14
 import javax.validation.Valid;
14 15
 import java.io.IOException;
15 16
 import java.util.List;
16
-import java.util.stream.Collectors;
17 17
 
18 18
 /**
19 19
  * @author Kerry on 19/12/11
20 20
  */
21 21
 
22 22
 @RestController
23
-public class MainController {
23
+public class MainController implements PrepareController {
24
+
25
+    private int status = -1;
24 26
 
25 27
     private final UnitService unitService;
28
+    private final ElasticLoadDataService elasticLoadDataService;
26 29
 
27
-    public MainController(UnitService unitService) {
30
+    public MainController(UnitService unitService, ElasticLoadDataService elasticLoadDataService) {
28 31
         this.unitService = unitService;
32
+        this.elasticLoadDataService = elasticLoadDataService;
33
+    }
34
+
35
+
36
+    @RequestMapping(value = "/load", method = {RequestMethod.GET, RequestMethod.POST})
37
+    public ApiResult loadData() {
38
+        if (status == -1) {
39
+            status = 0;
40
+            synchronized (this) {
41
+                new Thread(() -> {
42
+                    try {
43
+                        elasticLoadDataService.loadData();
44
+                        status = 1;
45
+                    } catch (IOException e) {
46
+                        e.printStackTrace();
47
+                        status = -1;
48
+                    }
49
+                }).start();
50
+            }
51
+        }
52
+        return ApiResult.builder().status(40000).message("loading data").build();
29 53
     }
30 54
 
55
+
31 56
     @PostMapping("/query-unit-agency")
32 57
     public ApiResult queryUnitAgency(@RequestBody @Valid UnitAgencyQuery unitAgencyQuery) throws IOException {
33
-        final List<UnitAgencyDTO> unitAgencyByQuery = unitService.getUnitAgencyByQuery(unitAgencyQuery, ElasticConstant.UNIT_AGENCY_INDEX);
34 58
 
35
-        List<Long> data = unitAgencyByQuery.stream().map(UnitAgencyDTO::getUnitId).collect(Collectors.toList());
59
+        List<UnitIdDTO> data = unitService.getUnitAgencyByQuery(unitAgencyQuery, UnitIdDTO.class, ElasticConstant.UNIT_AGENCY_INDEX);
60
+
36 61
         return ApiResult.builder().status(ApiStatus.SUCCESS_CODE).data(data).build();
37 62
     }
38 63
 
39 64
     @PostMapping("/test/query-unit-agency")
40 65
     public ApiResult testQueryUnitAgency(@RequestBody @Valid UnitAgencyQuery unitAgencyQuery) throws IOException {
41
-        List<UnitAgencyDTO> data = unitService.getUnitAgencyByQuery(unitAgencyQuery, ElasticConstant.UNIT_AGENCY_INDEX);
66
+        List<UnitAgencyDTO> data = unitService.getUnitAgencyByQuery(unitAgencyQuery, UnitAgencyDTO.class, ElasticConstant.UNIT_AGENCY_INDEX);
42 67
         return ApiResult.builder().status(ApiStatus.SUCCESS_CODE).data(data).build();
43 68
     }
44 69
 
45
-
70
+    @SkipMethod
71
+    @Override
72
+    public boolean isPrepare() {
73
+        return false;
74
+    }
46 75
 }

+ 20 - 0
elastic-publish-service/src/main/java/com/vcarecity/publish/controller/PrepareController.java Datei anzeigen

@@ -0,0 +1,20 @@
1
+package com.vcarecity.publish.controller;
2
+
3
+import com.vcarecity.publish.annotation.SkipMethod;
4
+
5
+/**
6
+ * @author VcKerry on 12/20/19
7
+ */
8
+
9
+public interface PrepareController {
10
+
11
+
12
+    /**
13
+     * is success
14
+     *
15
+     * @return
16
+     */
17
+    @SkipMethod
18
+    boolean isPrepare();
19
+
20
+}

+ 3 - 1
elastic-publish-service/src/main/java/com/vcarecity/publish/elastic/service/ElasticIndexService.java Datei anzeigen

@@ -25,8 +25,10 @@ public interface ElasticIndexService {
25 25
      * create index
26 26
      *
27 27
      * @param indices
28
+     * @return
29
+     * @throws IOException
28 30
      */
29
-    void createIndex(String... indices);
31
+    boolean createIndex(String indices) throws IOException;
30 32
 
31 33
     /**
32 34
      * delete index

+ 37 - 1
elastic-publish-service/src/main/java/com/vcarecity/publish/elastic/service/impl/ElasticIndexServiceImpl.java Datei anzeigen

@@ -5,7 +5,10 @@ import lombok.extern.slf4j.Slf4j;
5 5
 import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
6 6
 import org.elasticsearch.client.RequestOptions;
7 7
 import org.elasticsearch.client.RestHighLevelClient;
8
+import org.elasticsearch.client.indices.CreateIndexRequest;
9
+import org.elasticsearch.client.indices.CreateIndexResponse;
8 10
 import org.elasticsearch.client.indices.GetIndexRequest;
11
+import org.elasticsearch.common.xcontent.XContentType;
9 12
 import org.springframework.stereotype.Service;
10 13
 
11 14
 import java.io.IOException;
@@ -32,7 +35,38 @@ public class ElasticIndexServiceImpl implements ElasticIndexService {
32 35
     }
33 36
 
34 37
     @Override
35
-    public void createIndex(String... indices) {
38
+    public boolean createIndex(String index) throws IOException {
39
+
40
+        CreateIndexRequest request = new CreateIndexRequest(index);
41
+
42
+        // mapping
43
+        //language=JSON
44
+        String mapping = "{\n" +
45
+                "  \"properties\": {\n" +
46
+                "    \"agencyName\": {\n" +
47
+                "      \"type\": \"text\",\n" +
48
+                "      \"analyzer\": \"ik_max_word\",\n" +
49
+                "      \"search_analyzer\": \"ik_smart\"\n" +
50
+                "    },\n" +
51
+                "    \"agencyDetail\": {\n" +
52
+                "      \"type\": \"text\",\n" +
53
+                "      \"analyzer\": \"ik_max_word\",\n" +
54
+                "      \"search_analyzer\": \"ik_smart\"\n" +
55
+                "    },\n" +
56
+                "    \"nameOfBuilding\": {\n" +
57
+                "      \"type\": \"text\",\n" +
58
+                "      \"analyzer\": \"ik_max_word\",\n" +
59
+                "      \"search_analyzer\": \"ik_smart\"\n" +
60
+                "    }\n" +
61
+                "  " +
62
+                "}\n" +
63
+                "}";
64
+
65
+        request.mapping(mapping, XContentType.JSON);
66
+
67
+        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
68
+
69
+        return createIndexResponse.isAcknowledged();
36 70
 
37 71
     }
38 72
 
@@ -47,4 +81,6 @@ public class ElasticIndexServiceImpl implements ElasticIndexService {
47 81
     public List<String> indices() {
48 82
         return null;
49 83
     }
84
+
85
+
50 86
 }

+ 7 - 0
elastic-publish-service/src/main/java/com/vcarecity/publish/entity/UnitAgencyMergeEntity.java Datei anzeigen

@@ -1,7 +1,11 @@
1 1
 package com.vcarecity.publish.entity;
2 2
 
3
+import com.fasterxml.jackson.annotation.JsonFormat;
4
+import com.vcarecity.publish.constants.ElasticConstant;
3 5
 import lombok.Data;
4 6
 
7
+import java.util.Date;
8
+
5 9
 /**
6 10
  * @author VcKerry on 12/17/19
7 11
  */
@@ -15,6 +19,9 @@ public class UnitAgencyMergeEntity {
15 19
 
16 20
     private String puserCode;
17 21
 
22
+    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = ElasticConstant.ES_DATETIME_PATTERN, timezone = "GMT+8")
23
+    private Date unitUpdateStamp;
24
+
18 25
     private Long agencyId;
19 26
 
20 27
     private String agencyPath;

+ 7 - 2
elastic-publish-service/src/main/java/com/vcarecity/publish/pojo/dto/UnitAgencyDTO.java Datei anzeigen

@@ -1,17 +1,22 @@
1 1
 package com.vcarecity.publish.pojo.dto;
2 2
 
3 3
 import lombok.Data;
4
+import lombok.EqualsAndHashCode;
5
+import lombok.ToString;
4 6
 
5 7
 /**
6 8
  * @author Kerry on 19/12/11
7 9
  */
8 10
 
11
+@ToString(callSuper = true)
12
+@EqualsAndHashCode(callSuper = true)
9 13
 @Data
10
-public class UnitAgencyDTO {
11
-    private Long unitId;
14
+public class UnitAgencyDTO extends UnitIdDTO {
12 15
     private String agencyName;
13 16
     private String agencyDetail;
14 17
     private String nameOfBuilding;
18
+    private String unitType;
19
+    private String unitUpdateStamp;
15 20
     private Long classify;
16 21
     private Long property;
17 22
     private Long regulatoryLevelId;

+ 12 - 0
elastic-publish-service/src/main/java/com/vcarecity/publish/pojo/dto/UnitIdDTO.java Datei anzeigen

@@ -0,0 +1,12 @@
1
+package com.vcarecity.publish.pojo.dto;
2
+
3
+import lombok.Data;
4
+
5
+/**
6
+ * @author VcKerry on 12/19/19
7
+ */
8
+
9
+@Data
10
+public class UnitIdDTO {
11
+    private Long unitId;
12
+}

+ 3 - 2
elastic-publish-service/src/main/java/com/vcarecity/publish/sql/service/UnitService.java Datei anzeigen

@@ -1,6 +1,6 @@
1 1
 package com.vcarecity.publish.sql.service;
2 2
 
3
-import com.vcarecity.publish.pojo.dto.UnitAgencyDTO;
3
+import com.vcarecity.publish.pojo.dto.UnitIdDTO;
4 4
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
5 5
 
6 6
 import java.io.IOException;
@@ -16,10 +16,11 @@ public interface UnitService {
16 16
      * test
17 17
      *
18 18
      * @param queryParam
19
+     * @param cls
19 20
      * @param indices
20 21
      * @return
21 22
      * @throws IOException
22 23
      */
23
-    List<UnitAgencyDTO> getUnitAgencyByQuery(UnitAgencyQuery queryParam, String... indices) throws IOException;
24
+    <T extends UnitIdDTO> List<T> getUnitAgencyByQuery(UnitAgencyQuery queryParam, Class<T> cls, String... indices) throws IOException;
24 25
 
25 26
 }

+ 20 - 7
elastic-publish-service/src/main/java/com/vcarecity/publish/sql/service/impl/UnitServiceImpl.java Datei anzeigen

@@ -2,9 +2,10 @@ package com.vcarecity.publish.sql.service.impl;
2 2
 
3 3
 import com.fasterxml.jackson.databind.ObjectMapper;
4 4
 import com.vcarecity.elastic.util.AgencyPathUtil;
5
-import com.vcarecity.publish.pojo.dto.UnitAgencyDTO;
5
+import com.vcarecity.publish.pojo.dto.UnitIdDTO;
6 6
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
7 7
 import com.vcarecity.publish.sql.service.UnitService;
8
+import com.vcarecity.publish.util.ObjectFieldToMap;
8 9
 import com.vcarecity.publish.util.Utils;
9 10
 import lombok.extern.slf4j.Slf4j;
10 11
 import org.elasticsearch.action.search.SearchRequest;
@@ -15,6 +16,7 @@ import org.elasticsearch.index.query.*;
15 16
 import org.elasticsearch.search.SearchHit;
16 17
 import org.elasticsearch.search.SearchHits;
17 18
 import org.elasticsearch.search.builder.SearchSourceBuilder;
19
+import org.elasticsearch.search.sort.SortOrder;
18 20
 import org.springframework.stereotype.Service;
19 21
 
20 22
 import java.io.IOException;
@@ -49,7 +51,6 @@ public class UnitServiceImpl implements UnitService {
49 51
             "classify"
50 52
     };
51 53
 
52
-    static String[] queryColumns = new String[]{"unitId", "nameOfBuilding", "agencyDetail", "agencyName", "agencyAttributeClassId", "regulatoryLevelId", "property", "classify"};
53 54
 
54 55
     public UnitServiceImpl(RestHighLevelClient restHighLevelClient,
55 56
                            ObjectMapper objectMapper) {
@@ -58,11 +59,16 @@ public class UnitServiceImpl implements UnitService {
58 59
     }
59 60
 
60 61
     @Override
61
-    public List<UnitAgencyDTO> getUnitAgencyByQuery(UnitAgencyQuery queryParam, String... indices) throws IOException {
62
+    public <T extends UnitIdDTO> List<T> getUnitAgencyByQuery(UnitAgencyQuery queryParam, Class<T> cls, String... indices) throws IOException {
63
+
64
+
62 65
         List<Integer> agencyIdList = AgencyPathUtil.splitAgencyId(queryParam.getAgencyPath());
63 66
         if (agencyIdList.isEmpty()) {
64 67
             return Collections.emptyList();
65 68
         }
69
+
70
+        String[] queryFields = ObjectFieldToMap.getField(cls);
71
+
66 72
         QueryBuilder agencyIdBoolQuery = agencyIdListCondition(agencyIdList);
67 73
         QueryBuilder keywordBoolQuery = multiInputParamKeywordBool(queryParam.getKeyword());
68 74
 
@@ -82,9 +88,14 @@ public class UnitServiceImpl implements UnitService {
82 88
         SearchRequest searchRequest = new SearchRequest();
83 89
         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
84 90
         searchSourceBuilder.query(queryBuilder);
85
-        searchSourceBuilder.fetchSource(queryColumns, new String[]{});
91
+        searchSourceBuilder.fetchSource(queryFields, new String[]{});
86 92
         searchSourceBuilder.from(from);
87 93
         searchSourceBuilder.size(queryParam.getSize());
94
+
95
+        searchSourceBuilder.sort("property", SortOrder.ASC);
96
+        searchSourceBuilder.sort("unitUpdateStamp", SortOrder.ASC);
97
+        searchSourceBuilder.sort("unitId", SortOrder.ASC);
98
+
88 99
         searchRequest.source(searchSourceBuilder);
89 100
         searchRequest.indices(indices);
90 101
 
@@ -94,10 +105,12 @@ public class UnitServiceImpl implements UnitService {
94 105
         SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
95 106
 
96 107
         final SearchHits hits = searchResponse.getHits();
97
-        List<UnitAgencyDTO> res = new ArrayList<>(hits.getHits().length);
108
+
109
+
110
+        List<T> res = new ArrayList<>(hits.getHits().length);
98 111
         for (SearchHit hit : hits.getHits()) {
99
-            UnitAgencyDTO unitAgencyDTO = objectMapper.readValue(hit.getSourceAsString(), UnitAgencyDTO.class);
100
-            res.add(unitAgencyDTO);
112
+            T t = objectMapper.readValue(hit.getSourceAsString(), cls);
113
+            res.add(t);
101 114
         }
102 115
         return res;
103 116
     }

+ 41 - 0
elastic-publish-service/src/main/java/com/vcarecity/publish/util/ObjectFieldToMap.java Datei anzeigen

@@ -0,0 +1,41 @@
1
+package com.vcarecity.publish.util;
2
+
3
+import java.lang.reflect.Field;
4
+import java.util.ArrayList;
5
+import java.util.HashMap;
6
+import java.util.List;
7
+import java.util.Map;
8
+
9
+/**
10
+ * @author VcKerry on 12/19/19
11
+ */
12
+
13
+public class ObjectFieldToMap {
14
+
15
+    private static Map<Class<?>, String[]> fieldCache = new HashMap<>();
16
+
17
+
18
+    public static String[] getField(Class<?> cls) {
19
+
20
+        String[] fields = fieldCache.get(cls);
21
+
22
+        if (fields != null) {
23
+            return fields;
24
+        }
25
+
26
+        Class<?> clazz = cls;
27
+
28
+        List<String> fieldList = new ArrayList<>();
29
+        while (clazz != Object.class) {
30
+            Field[] declaredFields = clazz.getDeclaredFields();
31
+            for (Field declaredField : declaredFields) {
32
+                fieldList.add(declaredField.getName());
33
+            }
34
+            clazz = clazz.getSuperclass();
35
+        }
36
+        String[] strings = fieldList.toArray(new String[]{});
37
+        fieldCache.put(cls, strings);
38
+        return strings;
39
+    }
40
+
41
+}

+ 28 - 0
elastic-publish-service/src/main/resources/application-beta.yml Datei anzeigen

@@ -0,0 +1,28 @@
1
+server:
2
+  port: 7080
3
+spring:
4
+  datasource:
5
+    url: jdbc:mysql:replication://192.168.10.210:6446,192.168.10.210:6447/fmmp?serverTimezone=Asia/Shanghai
6
+    username: root
7
+    password: R00T@mysql
8
+  elasticsearch:
9
+    rest:
10
+      uris:
11
+        - http://192.168.10.104:9200
12
+        - http://192.168.10.105:9200
13
+      username: kerry
14
+      password: abcd!234
15
+  redis:
16
+    database: 2
17
+    host: 192.168.10.112
18
+    port: 6379
19
+    password: admin~1(^-^)
20
+mybatis:
21
+  type-aliases-package: com.vcarecity.publish.entity
22
+  mapper-locations: classpath:mapper/*.xml
23
+  configuration:
24
+    map-underscore-to-camel-case: true
25
+debug: true
26
+app:
27
+  scheduling:
28
+    enable: true

+ 1 - 1
elastic-publish-service/src/main/resources/application-dev.yml Datei anzeigen

@@ -26,4 +26,4 @@ mybatis:
26 26
 debug: true
27 27
 app:
28 28
   scheduling:
29
-    enable: false
29
+    enable: true

+ 2 - 1
elastic-publish-service/src/main/resources/application.yml Datei anzeigen

@@ -1,3 +1,4 @@
1 1
 spring:
2 2
   profiles:
3
-    active: dev
3
+    active: dev
4
+    #active: beta

+ 14 - 0
elastic-publish-service/src/main/resources/logback-spring.xml Datei anzeigen

@@ -21,6 +21,20 @@
21 21
 
22 22
     <springProfile name="dev">
23 23
 
24
+        <logger name="com.vcarecity" level="DEBUG" additivity="false">
25
+            <appender-ref ref="CONSOLE"/>
26
+        </logger>
27
+
28
+        <logger name="com.vcarecity.publish.mapper.AgencyMapper" level="INFO" additivity="false">
29
+            <appender-ref ref="CONSOLE"/>
30
+        </logger>
31
+
32
+
33
+    </springProfile>
34
+
35
+
36
+    <springProfile name="beta">
37
+
24 38
 
25 39
         <logger name="com.vcarecity" level="DEBUG" additivity="false">
26 40
             <appender-ref ref="CONSOLE"/>

+ 2 - 0
elastic-publish-service/src/main/resources/mapper/AgencyMapper.xml Datei anzeigen

@@ -15,6 +15,7 @@
15 15
         <result column="UNIT_ID" property="unitId"/>
16 16
         <result column="DTU_NO" property="dtuNo"/>
17 17
         <result column="PUSER_CODE" property="puserCode"/>
18
+        <result column="UNIT_UPDATE_STAMP" property="unitUpdateStamp" javaType="java.util.Date"/>
18 19
         <result column="ISDELETED" property="isDeleted"/>
19 20
         <result column="AGENCY_ID" property="agencyId"/>
20 21
         <result column="AGENCY_PATH" property="agencyPath"/>
@@ -34,6 +35,7 @@
34 35
         SELECT TU.UNIT_ID,
35 36
                TU.DTU_NO,
36 37
                TU.PUSER_CODE,
38
+               TU.UPDATESTAMP UNIT_UPDATE_STAMP,
37 39
                TAG.AGENCY_ID,
38 40
                TAG.AGENCY_PATH,
39 41
                TAG.AGENCY_NAME,

+ 4 - 0
elastic-publish-service/src/main/resources/mapper/UnitMapper.xml Datei anzeigen

@@ -1,10 +1,12 @@
1 1
 <?xml version="1.0" encoding="UTF-8" ?>
2 2
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
3 3
 <mapper namespace="com.vcarecity.publish.mapper.UnitMapper">
4
+
4 5
     <resultMap id="unitAgencyMergeEntityMap" type="unitAgencyMergeEntity">
5 6
         <result column="UNIT_ID" property="unitId"/>
6 7
         <result column="DTU_NO" property="dtuNo"/>
7 8
         <result column="PUSER_CODE" property="puserCode"/>
9
+        <result column="UNIT_UPDATE_STAMP" property="unitUpdateStamp" javaType="java.util.Date"/>
8 10
         <result column="ISDELETED" property="isDeleted"/>
9 11
         <result column="AGENCY_ID" property="agencyId"/>
10 12
         <result column="AGENCY_PATH" property="agencyPath"/>
@@ -30,6 +32,7 @@
30 32
         SELECT TU.UNIT_ID,
31 33
                TU.DTU_NO,
32 34
                TU.PUSER_CODE,
35
+               TU.UPDATESTAMP UNIT_UPDATE_STAMP,
33 36
                TU.ISDELETED,
34 37
                TAG.AGENCY_ID,
35 38
                TAG.AGENCY_PATH,
@@ -56,6 +59,7 @@
56 59
         SELECT TU.UNIT_ID,
57 60
                TU.DTU_NO,
58 61
                TU.PUSER_CODE,
62
+               TU.UPDATESTAMP UNIT_UPDATE_STAMP,
59 63
                TAG.AGENCY_ID,
60 64
                TAG.AGENCY_PATH,
61 65
                TAG.AGENCY_NAME,

+ 33 - 0
elastic-publish-service/src/test/java/com/vcarecity/publish/elastic/service/impl/ElasticIndexServiceImplTest.java Datei anzeigen

@@ -0,0 +1,33 @@
1
+package com.vcarecity.publish.elastic.service.impl;
2
+
3
+import com.vcarecity.publish.elastic.service.ElasticIndexService;
4
+import org.junit.jupiter.api.Test;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.boot.test.context.SpringBootTest;
7
+
8
+import java.io.IOException;
9
+
10
+@SpringBootTest
11
+class ElasticIndexServiceImplTest {
12
+
13
+    @Autowired
14
+    private ElasticIndexService elasticIndexService;
15
+
16
+
17
+    @Test
18
+    void indexExist() {
19
+    }
20
+
21
+    @Test
22
+    void createIndex() throws IOException {
23
+        elasticIndexService.createIndex("test");
24
+    }
25
+
26
+    @Test
27
+    void deleteIndex() {
28
+    }
29
+
30
+    @Test
31
+    void indices() {
32
+    }
33
+}

+ 4 - 0
elastic-publish-service/src/test/java/com/vcarecity/publish/elastic/service/impl/ElasticLoadDataServiceImplTest.java Datei anzeigen

@@ -4,9 +4,12 @@ import com.vcarecity.publish.elastic.service.ElasticLoadDataService;
4 4
 import org.junit.jupiter.api.Test;
5 5
 import org.springframework.beans.factory.annotation.Autowired;
6 6
 import org.springframework.boot.test.context.SpringBootTest;
7
+import org.springframework.test.context.TestPropertySource;
7 8
 
8 9
 import java.io.IOException;
9 10
 
11
+
12
+@TestPropertySource(properties = "app.scheduling.enable=false")
10 13
 @SpringBootTest
11 14
 class ElasticLoadDataServiceImplTest {
12 15
 
@@ -15,6 +18,7 @@ class ElasticLoadDataServiceImplTest {
15 18
 
16 19
     @Test
17 20
     void loadData() throws IOException {
21
+
18 22
         elasticLoadDataService.loadData();
19 23
     }
20 24
 }