Browse Source

添加一个关键词搜索的接口,只能应用于少量的关键词

tuonina 5 years ago
parent
commit
58c1394828

+ 2 - 1
tuon-core/src/main/java/cn/tonyandmoney/tuon/core/error/BaseResp.java

@@ -27,8 +27,9 @@ public class BaseResp<T> {
         return message;
     }
 
-    public void setMessage(String message) {
+    public BaseResp<T> setMessage(String message) {
         this.message = message;
+        return this;
     }
 
     public T getData() {

+ 43 - 0
tuon-qywx/src/main/java/cn/tonyandmoney/tuon/qywx/search/SearchResult.java

@@ -0,0 +1,43 @@
+package cn.tonyandmoney.tuon.qywx.search;
+
+/**
+ * 搜索结果返回
+ */
+public class SearchResult {
+
+    private String title;
+    private Object meta;
+    private String desc;
+    private String url;
+
+    public SearchResult(String title){
+        this.title =title;
+    }
+
+    public SearchResult setDesc(String desc) {
+        this.desc = desc;
+        return this;
+    }
+
+    public SearchResult setUrl(String url) {
+        this.url = url;
+        return this;
+    }
+
+    public SearchResult setMeta(Object meta) {
+        this.meta = meta;
+        return this;
+    }
+
+    public void setTitle(String title) {
+        this.title = title;
+    }
+
+    public Object getMeta() {
+        return meta;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+}

+ 30 - 0
tuon-qywx/src/main/kotlin/cn/tonyandmoney/tuon/qywx/controller/GlobalSearchController.kt

@@ -0,0 +1,30 @@
+package cn.tonyandmoney.tuon.qywx.controller
+
+import cn.tonyandmoney.tuon.core.error.BaseResp
+import cn.tonyandmoney.tuon.qywx.service.ISearchService
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.web.bind.annotation.GetMapping
+import org.springframework.web.bind.annotation.RequestMapping
+import org.springframework.web.bind.annotation.RequestParam
+import org.springframework.web.bind.annotation.RestController
+import reactor.core.publisher.Flux
+
+/**
+ * 设定关键词,进行搜索,返回指定的数据
+ */
+@RestController
+@RequestMapping("/wx/search")
+class GlobalSearchController {
+
+    @Autowired
+    private lateinit var searchHandlers: List<ISearchService>
+
+    /**
+     * 搜索接口,设定关键词,
+     */
+    @GetMapping
+    fun search(@RequestParam("keywords") keywords: String): Flux<BaseResp<*>> {
+        return Flux.concat(searchHandlers.filter { it.apply(keywords) }.map { it.search(keywords) })
+    }
+
+}

+ 1 - 1
tuon-qywx/src/main/kotlin/cn/tonyandmoney/tuon/qywx/service/IDutyService.kt

@@ -4,7 +4,7 @@ import cn.tonyandmoney.tuon.qywx.entity.TDuty
 import reactor.core.publisher.Mono
 import java.util.*
 
-interface IDutyService {
+interface IDutyService :ISearchService{
 
     /**
      * 添加值班人员

+ 22 - 0
tuon-qywx/src/main/kotlin/cn/tonyandmoney/tuon/qywx/service/ISearchService.kt

@@ -0,0 +1,22 @@
+package cn.tonyandmoney.tuon.qywx.service
+
+import cn.tonyandmoney.tuon.core.error.BaseResp
+import cn.tonyandmoney.tuon.qywx.search.SearchResult
+import reactor.core.publisher.Mono
+
+/**
+ * 指定一个搜索的服务,能提供服务的service都实现该接口
+ */
+interface ISearchService {
+
+
+    /**
+     * 判断是否响应该关键词,
+     * 如果能,则返回true
+     */
+    fun apply(keywords: String):Boolean
+    /**
+     * 进行搜索操作,返回数据
+     */
+    fun search(keywords:String): Mono<BaseResp<List<SearchResult>>>
+}

+ 31 - 0
tuon-qywx/src/main/kotlin/cn/tonyandmoney/tuon/qywx/service/impl/DutyServiceImpl.kt

@@ -1,8 +1,10 @@
 package cn.tonyandmoney.tuon.qywx.service.impl
 
+import cn.tonyandmoney.tuon.core.error.BaseResp
 import cn.tonyandmoney.tuon.core.utils.DateUtils
 import cn.tonyandmoney.tuon.qywx.dao.IDutyDao
 import cn.tonyandmoney.tuon.qywx.entity.TDuty
+import cn.tonyandmoney.tuon.qywx.search.SearchResult
 import cn.tonyandmoney.tuon.qywx.service.IDutyService
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
 import org.springframework.beans.factory.annotation.Autowired
@@ -16,9 +18,38 @@ import java.util.*
 @Service
 class DutyServiceImpl : IDutyService {
 
+    companion object {
+        val KEYWORDS = setOf("值班", "今日值班")
+        const val DESC = "今日值班信息"
+    }
+
     @Autowired
     private lateinit var dutyDao: IDutyDao
 
+
+    override fun apply(keywords: String): Boolean {
+        return KEYWORDS.contains(keywords)
+    }
+
+    /**
+     * 符合关键词,返回响应的信息
+     * 比如关键词为:值班,则返回今日的值班信息,
+     * 暂时只实现这两个关键词
+     */
+    override fun search(keywords: String): Mono<BaseResp<List<SearchResult>>> {
+        return if (keywords.contains(keywords)) {
+            queryByDate(DateUtils.today())
+                    .map {
+                        BaseResp.ok(it.map { duty -> SearchResult(duty.userName).setMeta(duty) })
+                                .setMessage(DESC)
+                    }
+        } else {
+            Mono.just(BaseResp<List<SearchResult>>()
+                    .setMessage(DESC))
+        }
+    }
+
+
     override fun addDuty(duty: TDuty): Mono<Void> {
         return Mono.create {
             duty.createTime = DateUtils.today()

+ 29 - 0
tuon-qywx/src/main/kotlin/cn/tonyandmoney/tuon/qywx/service/impl/SearchHandler.kt

@@ -0,0 +1,29 @@
+package cn.tonyandmoney.tuon.qywx.service.impl
+
+import cn.tonyandmoney.tuon.core.error.BaseResp
+import cn.tonyandmoney.tuon.qywx.search.SearchResult
+import cn.tonyandmoney.tuon.qywx.service.ISearchService
+import org.springframework.stereotype.Service
+import reactor.core.publisher.Mono
+
+/**
+ * 允许多个搜索
+ */
+@Service
+class SearchHandler : ISearchService {
+
+    companion object {
+        const val DESC = "关键词搜索"
+    }
+
+
+    override fun apply(keywords: String): Boolean {
+        return true
+    }
+
+    override fun search(keywords: String): Mono<BaseResp<List<SearchResult>>> {
+        return Mono.just(BaseResp<List<SearchResult>>()
+                .setData(emptyList())
+                .setMessage(DESC))
+    }
+}