短信搜索,完成后台管理的搜索功能和页面展示

master
Administrator 11 months ago
parent 383d04babd
commit fe16add083

@ -0,0 +1,31 @@
package com.mashibing.search.controller;
import com.mashibing.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.Map;
/**
* @author zjw
* @description
*/
@RestController
public class SmsSearchController {
@Autowired
private SearchService searchService;
@PostMapping("/search/sms/list")
public Map<String,Object> findSmsByParameters(@RequestBody Map<String,Object> parameters) throws IOException {
//1、调用搜索模块完成查询,因为ES在查询数据时会返回总条数
Map<String,Object> result = searchService.findSmsByParameters(parameters);
//2、返回数据
return result;
}
}

@ -35,4 +35,10 @@ public interface SearchService {
*/
void update(String index, String id, Map<String,Object> doc)throws IOException;
/**
*
* @param parameters
* @return
*/
Map<String, Object> findSmsByParameters(Map<String, Object> parameters) throws IOException;
}

@ -8,20 +8,30 @@ import com.mashibing.common.model.StandardReport;
import com.mashibing.search.service.SearchService;
import com.mashibing.search.utils.SearchUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.io.IOException;
import java.util.Map;
import java.util.*;
/**
* @author zjw
@ -60,12 +70,12 @@ public class ElasticsearchServiceImpl implements SearchService {
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
//4、校验添加是否成功
String result = response.getResult().getLowercase();
if(!CREATED.equals(result)){
if (!CREATED.equals(result)) {
// 添加失败!!
log.error("【搜索模块-写入数据失败】 index = {},id = {},json = {},result = {}",index,id,json,result);
log.error("【搜索模块-写入数据失败】 index = {},id = {},json = {},result = {}", index, id, json, result);
throw new SearchException(ExceptionEnums.SEARCH_INDEX_ERROR);
}
log.info("【搜索模块-写入数据成功】 索引添加成功index = {},id = {},json = {},result = {}",index,id,json,result);
log.info("【搜索模块-写入数据成功】 索引添加成功index = {},id = {},json = {},result = {}", index, id, json, result);
}
@Override
@ -88,17 +98,17 @@ public class ElasticsearchServiceImpl implements SearchService {
public void update(String index, String id, Map<String, Object> doc) throws IOException {
//1、基于exists方法查询当前文档是否存在
boolean exists = exists(index, id);
if(!exists){
if (!exists) {
// 当前文档不存在
StandardReport report = SearchUtils.get();
if(report.getReUpdate()){
if (report.getReUpdate()) {
// 第二次获取投递的消息到这已经是延迟20s了。
log.error("【搜索模块-修改日志】 修改日志失败report = {}",report);
}else{
log.error("【搜索模块-修改日志】 修改日志失败report = {}", report);
} else {
// 第一次投递可以再次将消息仍会MQ中
// 开始第二次消息的投递了
report.setReUpdate(true);
rabbitTemplate.convertAndSend(RabbitMQConstants.SMS_GATEWAY_NORMAL_QUEUE,report);
rabbitTemplate.convertAndSend(RabbitMQConstants.SMS_GATEWAY_NORMAL_QUEUE, report);
}
SearchUtils.remove();
return;
@ -112,36 +122,125 @@ public class ElasticsearchServiceImpl implements SearchService {
UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT);
String result = update.getResult().getLowercase();
if(!UPDATED.equals(result)){
if (!UPDATED.equals(result)) {
// 添加失败!!
log.error("【搜索模块-修改日志失败】 index = {},id = {},doc = {}",index,id,doc);
log.error("【搜索模块-修改日志失败】 index = {},id = {},doc = {}", index, id, doc);
throw new SearchException(ExceptionEnums.SEARCH_UPDATE_ERROR);
}
log.info("【搜索模块-修改日志成功】 文档修改成功index = {},id = {},doc = {}",index,id,doc);
log.info("【搜索模块-修改日志成功】 文档修改成功index = {},id = {},doc = {}", index, id, doc);
}
@Override
public Map<String, Object> findSmsByParameters(Map<String, Object> parameters) throws IOException {
//1、声明SearchRequest(后期需要根据传递的时间指定查询哪些索引,如果没传,可以指定默认查询前三个月)
SearchRequest request = new SearchRequest(SearchUtils.getCurrYearIndex(), "");
//2、封装查询条件
//2.1 参数全部取出来
Object fromObj = parameters.get("from");
Object sizeObj = parameters.get("size");
Object contentObj = parameters.get("content");
Object mobileObj = parameters.get("mobile");
Object startTimeObj = parameters.get("starttime");
Object stopTimeObj = parameters.get("stoptime");
Object clientIDObj = parameters.get("clientID");
//2.2 clientID需要单独操作一下。
List<Long> clientIDList = null;
if (clientIDObj instanceof List) {
// 传递的是个集合
clientIDList = (List) clientIDObj;
} else if (!ObjectUtils.isEmpty(clientIDObj)) {
clientIDList = Collections.singletonList(Long.parseLong(clientIDObj + ""));
}
//2.3 条件封装
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// ========================封装查询条件到boolQuery========================================
//2.3.1、关键字
if (!ObjectUtils.isEmpty(contentObj)) {
boolQuery.must(QueryBuilders.matchQuery("text", contentObj));
// 高亮。设置给sourceBuilder
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("text");
highlightBuilder.preTags("<span style='color: red'>");
highlightBuilder.postTags("</span>");
highlightBuilder.fragmentSize(100);
sourceBuilder.highlighter(highlightBuilder);
}
//2.3.2、手机号
if (!ObjectUtils.isEmpty(mobileObj)) {
boolQuery.must(QueryBuilders.prefixQuery("mobile", (String) mobileObj));
}
//2.3.3、开始时间
if(!ObjectUtils.isEmpty(startTimeObj)){
boolQuery.must(QueryBuilders.rangeQuery("sendTime").gte(startTimeObj));
}
//2.3.4、结束时间
if(!ObjectUtils.isEmpty(stopTimeObj)){
boolQuery.must(QueryBuilders.rangeQuery("sendTime").lte(stopTimeObj));
}
//2.3.5、客户id
if(clientIDList != null){
boolQuery.must(QueryBuilders.termsQuery("clientId",clientIDList.toArray(new Long[]{})));
}
//2.3.6 分页查询
sourceBuilder.from(Integer.parseInt(fromObj + ""));
sourceBuilder.size(Integer.parseInt(sizeObj + ""));
// ========================封装查询条件到boolQuery========================================
sourceBuilder.query(boolQuery);
request.source(sourceBuilder);
//3、执行查询
SearchResponse resp = restHighLevelClient.search(request, RequestOptions.DEFAULT);
//4、封装数据
long total = resp.getHits().getTotalHits().value;
List<Map> rows = new ArrayList<>();
for (SearchHit hit : resp.getHits().getHits()) {
Map<String, Object> row = hit.getSourceAsMap();
List sendTime = (List) row.get("sendTime");
String sendTimeStr = listToDateString(sendTime);
row.put("sendTimeStr",sendTimeStr);
row.put("corpname",row.get("sign"));
// 高亮结果的处理
HighlightField highlightField = hit.getHighlightFields().get("text");
if(highlightField != null){
String textHighLight = highlightField.getFragments()[0].toString();
row.put("text",textHighLight);
}
rows.add(row);
}
//5、返回数据
Map<String, Object> result = new HashMap<>();
result.put("total",total);
result.put("rows",rows);
return result;
}
private String listToDateString(List sendTime) {
String year = sendTime.get(0) + "";
Integer monthInt = (Integer) sendTime.get(1);
Integer dayInt = (Integer) sendTime.get(2);
Integer hourInt = (Integer) sendTime.get(3);
Integer minuteInt = (Integer) sendTime.get(4);
Integer secondInt = (Integer) sendTime.get(5);
String month = monthInt / 10 == 0 ? "0" + monthInt : monthInt + "";
String day = dayInt / 10 == 0 ? "0" + dayInt : dayInt + "";
String hour = hourInt / 10 == 0 ? "0" + hourInt : hourInt + "";
String minute = minuteInt / 10 == 0 ? "0" + minuteInt : minuteInt + "";
String second = secondInt / 10 == 0 ? "0" + secondInt : secondInt + "";
return year + "-" + month + "-" + day + " " + hour + ":" + month + ":" + second;
}
}

@ -24,6 +24,10 @@ public class SearchUtils {
return LocalDateTime.now().getYear() + "";
}
public static String getCurrYearIndex(){
return INDEX + getYear();
}
// ThreadLocal操作
private static ThreadLocal<StandardReport> reportThreadLocal = new ThreadLocal<>();

@ -62,10 +62,15 @@
<version>6.1.5.Final</version>
</dependency>
<!-- 需要调用其他模块-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>

@ -1,12 +1,12 @@
package com.mashibing.webmaster;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import java.util.UUID;
import java.util.Date;
/**
* @author zjw
@ -15,10 +15,11 @@ import java.util.UUID;
@SpringBootApplication
@MapperScan(basePackages = "com.mashibing.webmaster.mapper")
@EnableFeignClients
@EnableDiscoveryClient
public class WebMasterStarterApp {
public static void main(String[] args) {
SpringApplication.run(WebMasterStarterApp.class,args);
SpringApplication.run(WebMasterStarterApp.class, args);
}
}

@ -1,6 +1,8 @@
package com.mashibing.webmaster.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Map;
@ -11,7 +13,14 @@ import java.util.Map;
@FeignClient(value = "beacon-search")
public interface SearchClient {
Map<String,Object> findSmsByParameters(Map<String,Object> parameters);
/**
*
* @param parameters
* @return Map total: rowslist
*/
@PostMapping("/search/sms/list")
Map<String,Object> findSmsByParameters(@RequestBody Map<String,Object> parameters);

@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -96,15 +97,20 @@ public class SearchController {
//3、判断返回的total如果total为0正常返回
Long total = Long.parseLong(data.get("total") + "");
if (total == 0) {
return R.ok();
return R.ok(0L,null);
}
//4、如果数据正常做返回数据的封装声明SearchSmsVO的实体类
List<Map> list = (List<Map>) data.get("rows");
List<SearchSmsVO> rows = new ArrayList<>();
try {
BeanUtils.copyProperties(list, rows);
} catch (Exception e) {
e.printStackTrace();
// 遍历集合,封装数据
for (Map row : list) {
SearchSmsVO vo = new SearchSmsVO();
try {
BeanUtils.copyProperties(vo,row);
} catch (Exception e) {
e.printStackTrace();
}
rows.add(vo);
}
//5、响应数据

@ -12,11 +12,11 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
public class SearchSmsVO {
// ok
// ok 公司名称
private String corpname;
// 格式待定
private Long sendTimeStr;
private String sendTimeStr;
// 修改页面
private Integer reportState;

@ -6,6 +6,23 @@ spring:
username: root
password: ZhengJinWei123!
type: com.alibaba.druid.pool.DruidDataSource
# 服务名称
application:
name: beacon-webmaster
# 多环境
profiles:
active: dev
# nacos注册中心地址
cloud:
nacos:
discovery:
server-addr: 114.116.226.76:8848
# nacos配置中心地址:
config:
server-addr: 114.116.226.76:8848
file-extension: yml
# beacon-search-dev.yml
# MyBatis
mybatis:
mapper-locations: classpath:mapper/*.xml

@ -30,13 +30,7 @@ $(function () {
},
/*{field: 'totalRepTime', title: '请求次数'},*/
{field: 'corpname', title: '客户名称'},
{field: 'sendTimeStr', title: '发送时间',formatter: function (value, row, index) {
if (value){
return formatDate(value)
}
}},
{field: 'sendTimeStr', title: '发送时间'},
{field: 'reportState', title: '状态', formatter: function (v, r, i) {
if (v == 0) {
return "等待";
@ -59,7 +53,7 @@ $(function () {
}
}
},
{field: 'errorCode', title: '错误码'},
{field: 'errorMsg', title: '错误码'},
{field: 'srcNumber', title: '发送号'},
{field: 'mobile', title: '手机号'},
{field: 'text', title: '短信内容'}

Loading…
Cancel
Save