Bläddra i källkod

项目集成es, 故障案例功能,使用es实现

wanggaokun 1 år sedan
förälder
incheckning
c97033f78e

+ 2 - 0
PHM-admin/phm-admin/src/main/java/com/phm/PHMApplication.java

@@ -1,5 +1,6 @@
 package com.phm;
 
+import org.dromara.easyes.starter.register.EsMapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@@ -10,6 +11,7 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  * @author phm
  */
 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}, scanBasePackages = {"com.phm"})
+@EsMapperScan("com.phm.manage.es.mapper")
 public class PHMApplication {
     public static void main(String[] args) {
         // System.setProperty("spring.devtools.restart.enabled", "false");

+ 5 - 0
PHM-admin/phm-admin/src/main/resources/application-dev.yml

@@ -1,3 +1,8 @@
+easy-es:
+  address: 127.0.0.1:9200
+  banner: false # 默认为true 打印banner 若您不期望打印banner,可配置为false
+  username: #es用户名,若无则删去此行配置
+  password: #es密码,若无则删去此行配置
 # 数据源配置
 spring:
   redis:

+ 5 - 0
PHM-admin/phm-admin/src/main/resources/application-local.yml

@@ -1,3 +1,8 @@
+easy-es:
+  address: 127.0.0.1:9200
+  banner: false # 默认为true 打印banner 若您不期望打印banner,可配置为false
+  username: #es用户名,若无则删去此行配置
+  password: #es密码,若无则删去此行配置
 # 数据源配置
 spring:
   redis:

+ 5 - 0
PHM-admin/phm-admin/src/main/resources/application-pro.yml

@@ -1,3 +1,8 @@
+easy-es:
+  address: 127.0.0.1:9200
+  banner: false # 默认为true 打印banner 若您不期望打印banner,可配置为false
+  username: #es用户名,若无则删去此行配置
+  password: #es密码,若无则删去此行配置
 # 数据源配置
 spring:
   redis:

+ 14 - 0
PHM-admin/phm-framework/pom.xml

@@ -21,6 +21,20 @@
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
+             <exclusions>
+                 <exclusion>
+                     <groupId>org.elasticsearch.client</groupId>
+                     <artifactId>elasticsearch-rest-high-level-client</artifactId>
+                 </exclusion>
+                 <exclusion>
+                     <groupId>org.elasticsearch.client</groupId>
+                     <artifactId>elasticsearch-rest-client</artifactId>
+                 </exclusion>
+                 <exclusion>
+                     <groupId>org.elasticsearch</groupId>
+                     <artifactId>elasticsearch</artifactId>
+                 </exclusion>
+             </exclusions>
         </dependency>
 
         <!-- SpringBoot 拦截器 -->

+ 17 - 0
PHM-admin/phm-manage/pom.xml

@@ -25,6 +25,23 @@
             <groupId>com.phm</groupId>
             <artifactId>phm-system</artifactId>
         </dependency>
+        <!--<dependency>-->
+        <!--    <groupId>org.elasticsearch.client</groupId>-->
+        <!--    <artifactId>elasticsearch-rest-high-level-client</artifactId>-->
+        <!--</dependency>-->
+        <dependency>
+            <groupId>org.elasticsearch.client</groupId>
+            <artifactId>elasticsearch-rest-client</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.elasticsearch</groupId>
+            <artifactId>elasticsearch</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.dromara.easy-es</groupId>
+            <artifactId>easy-es-boot-starter</artifactId>
+        </dependency>
     </dependencies>
 
 </project>

+ 21 - 6
PHM-admin/phm-manage/src/main/java/com/phm/manage/controller/FaultCaseController.java

@@ -1,5 +1,20 @@
 package com.phm.manage.controller;
 
+import java.util.List;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
 import com.phm.common.annotation.Log;
 import com.phm.common.core.controller.BaseController;
 import com.phm.common.core.domain.AjaxResult;
@@ -8,12 +23,6 @@ import com.phm.common.enums.BusinessType;
 import com.phm.common.utils.poi.ExcelUtil;
 import com.phm.manage.domain.FaultCase;
 import com.phm.manage.service.IFaultCaseService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.security.access.prepost.PreAuthorize;
-import org.springframework.web.bind.annotation.*;
-
-import javax.servlet.http.HttpServletResponse;
-import java.util.List;
 
 /**
  * 故障案例信息Controller
@@ -88,4 +97,10 @@ public class FaultCaseController extends BaseController {
     public AjaxResult remove(@PathVariable Long[] ids) {
         return toAjax(faultCaseService.deleteFaultCaseByIds(ids));
     }
+
+    @PostMapping("/importTemplate")
+    public void importTemplate(HttpServletResponse response) {
+        ExcelUtil<FaultCase> util = new ExcelUtil<>(FaultCase.class);
+        util.importTemplateExcel(response, "故障数据");
+    }
 }

+ 80 - 16
PHM-admin/phm-manage/src/main/java/com/phm/manage/domain/FaultCase.java

@@ -1,16 +1,23 @@
 package com.phm.manage.domain;
 
+import java.io.Serializable;
+import java.util.Date;
+
+import org.dromara.easyes.annotation.HighLight;
+import org.dromara.easyes.annotation.IndexField;
+import org.dromara.easyes.annotation.IndexId;
+import org.dromara.easyes.annotation.IndexName;
+import org.dromara.easyes.annotation.rely.Analyzer;
+import org.dromara.easyes.annotation.rely.FieldStrategy;
+import org.dromara.easyes.annotation.rely.FieldType;
+import org.dromara.easyes.annotation.rely.IdType;
+
 import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
 import com.phm.common.annotation.Excel;
-import com.phm.common.core.domain.BaseEntity;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
 
-import java.util.Date;
+import lombok.Data;
 
 /**
  * 故障案例信息对象 phm_fault_case
@@ -18,55 +25,112 @@ import java.util.Date;
  * @author phm
  * @date 2023-08-22
  */
-@EqualsAndHashCode(callSuper = true)
 @Data
-public class FaultCase extends BaseEntity {
+@IndexName("fault_case")
+public class FaultCase implements Serializable {
     private static final long serialVersionUID = 1L;
 
     /**
      * 唯一ID
      */
     @JsonSerialize(using = ToStringSerializer.class)
+    @IndexField(exist = false)
     private long id;
 
+    @IndexField("id")
+    @IndexId(type = IdType.CUSTOMIZE)
+    private String esId;
+
     /**
-     * 案例案例名称
+     * 案例文件名称
      */
-    @Excel(name = "案例文件名称")
+    @IndexField(exist = false)
     private String caseFileName;
 
     /**
      * 案例案例名称
      */
     @Excel(name = "案例名称")
+    @IndexField(fieldType = FieldType.TEXT, strategy = FieldStrategy.NOT_EMPTY, analyzer = Analyzer.IK_MAX_WORD,
+        searchAnalyzer = Analyzer.IK_MAX_WORD)
     private String caseName;
 
-    /**
-     * 故障描述
-     */
-    @Excel(name = "故障描述")
-    private String description;
-
     /**
      * 案例编号
      */
     @Excel(name = "案例编号")
+    @IndexField(fieldType = FieldType.TEXT, strategy = FieldStrategy.NOT_EMPTY)
     private String caseNumber;
 
+    /**
+     * 故障描述
+     */
+    @Excel(name = "故障描述")
+    @IndexField(value = "content", fieldType = FieldType.TEXT, strategy = FieldStrategy.NOT_EMPTY,
+        analyzer = Analyzer.IK_MAX_WORD, searchAnalyzer = Analyzer.IK_MAX_WORD)
+    @HighLight(mappingField = "highlightContent", preTag = "<font color=\"red\" >", postTag = "</font>")
+    private String description;
+
     /**
      * 案例特征参数
      */
     @Excel(name = "案例特征参数")
+    @IndexField(fieldType = FieldType.TEXT, strategy = FieldStrategy.NOT_EMPTY, analyzer = Analyzer.IK_MAX_WORD,
+        searchAnalyzer = Analyzer.IK_MAX_WORD)
     private String parameter;
 
     /**
      * 故障定位
      */
     @Excel(name = "故障定位")
+    @IndexField(fieldType = FieldType.TEXT, strategy = FieldStrategy.NOT_EMPTY, analyzer = Analyzer.IK_MAX_WORD,
+        searchAnalyzer = Analyzer.IK_MAX_WORD)
     private String location;
     /**
      * 故障描述
      */
     @Excel(name = "故障描述")
+    @IndexField(fieldType = FieldType.TEXT, strategy = FieldStrategy.NOT_EMPTY, analyzer = Analyzer.IK_MAX_WORD,
+        searchAnalyzer = Analyzer.IK_MAX_WORD)
     private String solution;
+
+    private String highlightContent;
+
+    /**
+     * 创建者
+     */
+    @IndexField(fieldType = FieldType.TEXT)
+    private String createBy;
+
+    /**
+     * 创建时间
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @IndexField(fieldType = FieldType.DATE, dateFormat = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
+    private Date createTime;
+
+    /**
+     * 更新者
+     */
+    @IndexField(fieldType = FieldType.TEXT)
+    private String updateBy;
+
+    /**
+     * 更新时间
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @IndexField(fieldType = FieldType.DATE, dateFormat = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
+    private Date updateTime;
+
+    /**
+     * 备注
+     */
+    @IndexField(fieldType = FieldType.TEXT)
+    private String remark;
+
+    /**
+     * 数据是否删除(1:删除,0有效)
+     */
+    @IndexField(fieldType = FieldType.LONG)
+    private Integer isDelete;
 }

+ 12 - 0
PHM-admin/phm-manage/src/main/java/com/phm/manage/es/mapper/EsFaultCaseMapper.java

@@ -0,0 +1,12 @@
+package com.phm.manage.es.mapper;
+
+import org.dromara.easyes.core.core.BaseEsMapper;
+
+import com.phm.manage.domain.FaultCase;
+
+/**
+ * @Description EsFaultCaseMapper
+ * @Author WGK
+ * @Date 2023/11/6 23:43
+ */
+public interface EsFaultCaseMapper extends BaseEsMapper<FaultCase> {}

+ 10 - 2
PHM-admin/phm-manage/src/main/java/com/phm/manage/mapper/FaultCaseMapper.java

@@ -1,9 +1,9 @@
 package com.phm.manage.mapper;
 
-import com.phm.manage.domain.FaultCase;
-
 import java.util.List;
 
+import com.phm.manage.domain.FaultCase;
+
 /**
  * 故障案例信息Mapper接口
  *
@@ -27,6 +27,14 @@ public interface FaultCaseMapper {
      */
     public List<FaultCase> selectFaultCaseList(FaultCase faultCase);
 
+    /**
+     * 查询故障案例信息列表
+     *
+     * @param ids 故障案例信息Id
+     * @return 故障案例信息集合
+     */
+    public List<FaultCase> selectFaultCaseListByIds(Long[] ids);
+
     /**
      * 新增故障案例信息
      *

+ 43 - 12
PHM-admin/phm-manage/src/main/java/com/phm/manage/service/impl/FaultCaseServiceImpl.java

@@ -1,13 +1,18 @@
 package com.phm.manage.service.impl;
 
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
 import com.phm.manage.domain.FaultCase;
+import com.phm.manage.es.mapper.EsFaultCaseMapper;
 import com.phm.manage.mapper.FaultCaseMapper;
 import com.phm.manage.service.IFaultCaseService;
 import com.phm.manage.util.SnowFlakeIdGenerator;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Service;
-
-import java.util.List;
 
 /**
  * 故障案例信息Service业务层处理
@@ -22,6 +27,10 @@ public class FaultCaseServiceImpl implements IFaultCaseService {
 
     @Autowired
     private SnowFlakeIdGenerator snowFlakeIdGenerator;
+
+    @Autowired
+    private EsFaultCaseMapper esFaultCaseMapper;
+
     /**
      * 查询故障案例信息
      *
@@ -41,30 +50,50 @@ public class FaultCaseServiceImpl implements IFaultCaseService {
      */
     @Override
     public List<FaultCase> selectFaultCaseList(FaultCase faultCase) {
-        return faultCaseMapper.selectFaultCaseList(faultCase);
+        LambdaEsQueryWrapper<FaultCase> wrapper = new LambdaEsQueryWrapper<>();
+        wrapper.match(StringUtils.isNotBlank(faultCase.getDescription()), "content", faultCase.getDescription(), 5.0f)
+            .or()
+            .match(StringUtils.isNotBlank(faultCase.getCaseName()), FaultCase::getCaseName, faultCase.getCaseName())
+            .or()
+            .match(StringUtils.isNotBlank(faultCase.getCaseNumber()), FaultCase::getCaseNumber,
+                faultCase.getCaseNumber())
+            .or()
+            .match(StringUtils.isNotBlank(faultCase.getLocation()), FaultCase::getLocation, faultCase.getLocation())
+            .or()
+            .match(StringUtils.isNotBlank(faultCase.getParameter()), FaultCase::getParameter, faultCase.getParameter())
+            .or()
+            .match(StringUtils.isNotBlank(faultCase.getSolution()), FaultCase::getSolution, faultCase.getSolution());
+        if (!esFaultCaseMapper.existsIndex("fault_case")) {
+            esFaultCaseMapper.createIndex();
+        }
+        return esFaultCaseMapper.selectList(wrapper);
     }
 
     /**
-     * 新增故障案例信息
+     * 修改故障案例信息
      *
      * @param faultCase 故障案例信息
      * @return 结果
      */
     @Override
-    public int insertFaultCase(FaultCase faultCase) {
-        faultCase.setId(snowFlakeIdGenerator.nextId());
-        return faultCaseMapper.insertFaultCase(faultCase);
+    public int updateFaultCase(FaultCase faultCase) {
+        faultCase.setEsId(String.valueOf(faultCase.getId()));
+        esFaultCaseMapper.updateById(faultCase);
+        return faultCaseMapper.updateFaultCase(faultCase);
     }
 
     /**
-     * 修改故障案例信息
+     * 新增故障案例信息
      *
      * @param faultCase 故障案例信息
      * @return 结果
      */
     @Override
-    public int updateFaultCase(FaultCase faultCase) {
-        return faultCaseMapper.updateFaultCase(faultCase);
+    public int insertFaultCase(FaultCase faultCase) {
+        faultCase.setId(snowFlakeIdGenerator.nextId());
+        faultCase.setEsId(String.valueOf(faultCase.getId()));
+        esFaultCaseMapper.insert(faultCase);
+        return faultCaseMapper.insertFaultCase(faultCase);
     }
 
     /**
@@ -75,6 +104,7 @@ public class FaultCaseServiceImpl implements IFaultCaseService {
      */
     @Override
     public int deleteFaultCaseByIds(Long[] ids) {
+        esFaultCaseMapper.deleteBatchIds(Arrays.asList(ids));
         return faultCaseMapper.deleteFaultCaseByIds(ids);
     }
 
@@ -86,6 +116,7 @@ public class FaultCaseServiceImpl implements IFaultCaseService {
      */
     @Override
     public int deleteFaultCaseById(Long id) {
+        esFaultCaseMapper.deleteById(id);
         return faultCaseMapper.deleteFaultCaseById(id);
     }
 }

+ 33 - 10
PHM-admin/phm-manage/src/main/resources/mapper/manage/FaultCaseMapper.xml

@@ -37,20 +37,43 @@
         from PHM.PHM_FAULT_CASE
     </sql>
 
-    <select id="selectFaultCaseList" parameterType="FaultCase" resultMap="FaultCaseResult">
+    <select id="selectFaultCaseList" parameterType="FaultCase"
+            resultMap="FaultCaseResult">
         <include refid="selectFaultCaseVo"/>
         <where>
-            <if test="caseName != null  and caseName != ''">and case_name like concat('%', #{caseName}, '%')</if>
-            <if test="description != null  and description != ''">and description = #{description}</if>
-            <if test="caseNumber != null  and caseNumber != ''">and case_number = #{caseNumber}</if>
+            <if test="caseName != null  and caseName != ''">and case_name like concat('%',
+                #{caseName}, '%')
+            </if>
+            <if test="description != null  and description != ''">and description =
+                #{description}
+            </if>
+            <if test="caseNumber != null  and caseNumber != ''">and case_number =
+                #{caseNumber}
+            </if>
             <if test="isDelete != null ">and is_delete = #{isDelete}</if>
-            <if test="createBy != null  and createBy != ''">and create_by = #{createBy}</if>
+            <if test="createBy != null  and createBy != ''">and create_by = #{createBy}
+            </if>
             <if test="createTime != null ">and create_time = #{createTime}</if>
-            <if test="updateBy != null  and updateBy != ''">and update_by = #{updateBy}</if>
+            <if test="updateBy != null  and updateBy != ''">and update_by = #{updateBy}
+            </if>
             <if test="updateTime != null ">and update_time = #{updateTime}</if>
-            <if test="parameter != null  and parameter != ''">and parameter = #{parameter}</if>
-            <if test="location != null  and location != ''">and location = #{location}</if>
-            <if test="solution != null  and solution != ''">and solution = #{solution}</if>
+            <if test="parameter != null  and parameter != ''">and parameter =
+                #{parameter}
+            </if>
+            <if test="location != null  and location != ''">and location = #{location}
+            </if>
+            <if test="solution != null  and solution != ''">and solution = #{solution}
+            </if>
+        </where>
+    </select>
+    <select id="selectFaultCaseListByIds" parameterType="Long"
+            resultMap="FaultCaseResult">
+        <include refid="selectFaultCaseVo"/>
+        <where>
+            id in
+            <foreach item="id" collection="array" open="(" separator="," close=")">
+                #{id}
+            </foreach>
         </where>
     </select>
 
@@ -118,7 +141,7 @@
         where id = #{id}
     </delete>
 
-    <delete id="deleteFaultCaseByIds" parameterType="String">
+    <delete id="deleteFaultCaseByIds" parameterType="Long">
         delete from PHM.PHM_FAULT_CASE where id in
         <foreach item="id" collection="array" open="(" separator="," close=")">
             #{id}