Browse Source

ADD req fields

kerry 5 years ago
parent
commit
a8277e5d7d
25 changed files with 468 additions and 36 deletions
  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 View File

9
 https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-delete.html
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 View File


+ 103 - 4
elastic-publish-service/pom.xml View File

25
         </dependency>
25
         </dependency>
26
 
26
 
27
         <dependency>
27
         <dependency>
28
+            <groupId>org.springframework.boot</groupId>
29
+            <artifactId>spring-boot-starter-aop</artifactId>
30
+        </dependency>
31
+
32
+        <dependency>
28
             <groupId>org.mybatis.spring.boot</groupId>
33
             <groupId>org.mybatis.spring.boot</groupId>
29
             <artifactId>mybatis-spring-boot-starter</artifactId>
34
             <artifactId>mybatis-spring-boot-starter</artifactId>
30
             <version>2.1.1</version>
35
             <version>2.1.1</version>
82
 
87
 
83
     </dependencies>
88
     </dependencies>
84
 
89
 
90
+
85
     <build>
91
     <build>
86
         <plugins>
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
         </plugins>
190
         </plugins>
92
     </build>
191
     </build>
93
 
192
 

+ 6 - 6
elastic-publish-service/req/main-controller.http View File

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

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

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 View File

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 View File

1
 package com.vcarecity.publish.constants;
1
 package com.vcarecity.publish.constants;
2
 
2
 
3
+import java.time.ZoneId;
4
+import java.time.format.DateTimeFormatter;
5
+
3
 /**
6
 /**
4
  * @author VcKerry on 12/18/19
7
  * @author VcKerry on 12/18/19
5
  */
8
  */
11
 
14
 
12
 
15
 
13
     public static final int QUERY_PAGE_COUNT = 2000;
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 View File

1
 package com.vcarecity.publish.controller;
1
 package com.vcarecity.publish.controller;
2
 
2
 
3
+import com.vcarecity.publish.annotation.SkipMethod;
3
 import com.vcarecity.publish.api.ApiResult;
4
 import com.vcarecity.publish.api.ApiResult;
4
 import com.vcarecity.publish.api.ApiStatus;
5
 import com.vcarecity.publish.api.ApiStatus;
5
 import com.vcarecity.publish.constants.ElasticConstant;
6
 import com.vcarecity.publish.constants.ElasticConstant;
7
+import com.vcarecity.publish.elastic.service.ElasticLoadDataService;
6
 import com.vcarecity.publish.pojo.dto.UnitAgencyDTO;
8
 import com.vcarecity.publish.pojo.dto.UnitAgencyDTO;
9
+import com.vcarecity.publish.pojo.dto.UnitIdDTO;
7
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
10
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
8
 import com.vcarecity.publish.sql.service.UnitService;
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
 import javax.validation.Valid;
14
 import javax.validation.Valid;
14
 import java.io.IOException;
15
 import java.io.IOException;
15
 import java.util.List;
16
 import java.util.List;
16
-import java.util.stream.Collectors;
17
 
17
 
18
 /**
18
 /**
19
  * @author Kerry on 19/12/11
19
  * @author Kerry on 19/12/11
20
  */
20
  */
21
 
21
 
22
 @RestController
22
 @RestController
23
-public class MainController {
23
+public class MainController implements PrepareController {
24
+
25
+    private int status = -1;
24
 
26
 
25
     private final UnitService unitService;
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
         this.unitService = unitService;
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
     @PostMapping("/query-unit-agency")
56
     @PostMapping("/query-unit-agency")
32
     public ApiResult queryUnitAgency(@RequestBody @Valid UnitAgencyQuery unitAgencyQuery) throws IOException {
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
         return ApiResult.builder().status(ApiStatus.SUCCESS_CODE).data(data).build();
61
         return ApiResult.builder().status(ApiStatus.SUCCESS_CODE).data(data).build();
37
     }
62
     }
38
 
63
 
39
     @PostMapping("/test/query-unit-agency")
64
     @PostMapping("/test/query-unit-agency")
40
     public ApiResult testQueryUnitAgency(@RequestBody @Valid UnitAgencyQuery unitAgencyQuery) throws IOException {
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
         return ApiResult.builder().status(ApiStatus.SUCCESS_CODE).data(data).build();
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 View File

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 View File

25
      * create index
25
      * create index
26
      *
26
      *
27
      * @param indices
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
      * delete index
34
      * delete index

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

5
 import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
5
 import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
6
 import org.elasticsearch.client.RequestOptions;
6
 import org.elasticsearch.client.RequestOptions;
7
 import org.elasticsearch.client.RestHighLevelClient;
7
 import org.elasticsearch.client.RestHighLevelClient;
8
+import org.elasticsearch.client.indices.CreateIndexRequest;
9
+import org.elasticsearch.client.indices.CreateIndexResponse;
8
 import org.elasticsearch.client.indices.GetIndexRequest;
10
 import org.elasticsearch.client.indices.GetIndexRequest;
11
+import org.elasticsearch.common.xcontent.XContentType;
9
 import org.springframework.stereotype.Service;
12
 import org.springframework.stereotype.Service;
10
 
13
 
11
 import java.io.IOException;
14
 import java.io.IOException;
32
     }
35
     }
33
 
36
 
34
     @Override
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
     public List<String> indices() {
81
     public List<String> indices() {
48
         return null;
82
         return null;
49
     }
83
     }
84
+
85
+
50
 }
86
 }

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

1
 package com.vcarecity.publish.entity;
1
 package com.vcarecity.publish.entity;
2
 
2
 
3
+import com.fasterxml.jackson.annotation.JsonFormat;
4
+import com.vcarecity.publish.constants.ElasticConstant;
3
 import lombok.Data;
5
 import lombok.Data;
4
 
6
 
7
+import java.util.Date;
8
+
5
 /**
9
 /**
6
  * @author VcKerry on 12/17/19
10
  * @author VcKerry on 12/17/19
7
  */
11
  */
15
 
19
 
16
     private String puserCode;
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
     private Long agencyId;
25
     private Long agencyId;
19
 
26
 
20
     private String agencyPath;
27
     private String agencyPath;

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

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

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

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 View File

1
 package com.vcarecity.publish.sql.service;
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
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
4
 import com.vcarecity.publish.pojo.query.UnitAgencyQuery;
5
 
5
 
6
 import java.io.IOException;
6
 import java.io.IOException;
16
      * test
16
      * test
17
      *
17
      *
18
      * @param queryParam
18
      * @param queryParam
19
+     * @param cls
19
      * @param indices
20
      * @param indices
20
      * @return
21
      * @return
21
      * @throws IOException
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 View File

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

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

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 View File

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 View File

26
 debug: true
26
 debug: true
27
 app:
27
 app:
28
   scheduling:
28
   scheduling:
29
-    enable: false
29
+    enable: true

+ 2 - 1
elastic-publish-service/src/main/resources/application.yml View File

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

+ 14 - 0
elastic-publish-service/src/main/resources/logback-spring.xml View File

21
 
21
 
22
     <springProfile name="dev">
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
         <logger name="com.vcarecity" level="DEBUG" additivity="false">
39
         <logger name="com.vcarecity" level="DEBUG" additivity="false">
26
             <appender-ref ref="CONSOLE"/>
40
             <appender-ref ref="CONSOLE"/>

+ 2 - 0
elastic-publish-service/src/main/resources/mapper/AgencyMapper.xml View File

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

+ 4 - 0
elastic-publish-service/src/main/resources/mapper/UnitMapper.xml View File

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

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

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 View File

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