杨宗理
This commit is contained in:
parent
8512691fa0
commit
29fbb83ff5
|
|
@ -1,6 +1,6 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 9200
|
||||
port: 9100
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
|
|
|
|||
|
|
@ -40,12 +40,21 @@
|
|||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Actuator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- redis-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<!-- Swagger UI -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import com.ruoyi.common.security.annotation.EnableRyFeignClients;
|
|||
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* 系统模块
|
||||
|
|
@ -15,6 +16,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
@EnableCustomSwagger2
|
||||
@EnableRyFeignClients
|
||||
@SpringBootApplication
|
||||
@EnableScheduling //定时任务
|
||||
public class RuoYiProductApplication
|
||||
{
|
||||
public static void main(String[] args)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
package com.ruoyi.product.config;
|
||||
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
||||
import org.springframework.data.elasticsearch.client.RestClients;
|
||||
|
||||
@Configuration
|
||||
public class ElasticsearchConfig {
|
||||
@Bean
|
||||
RestHighLevelClient elasticsearchClient() {
|
||||
ClientConfiguration configuration = ClientConfiguration.builder()
|
||||
.connectedTo("localhost:9200")
|
||||
//.withConnectTimeout(Duration.ofSeconds(5))
|
||||
//.withSocketTimeout(Duration.ofSeconds(3))
|
||||
//.useSsl()
|
||||
//.withDefaultHeaders(defaultHeaders)
|
||||
//.withBasicAuth(username, password)
|
||||
// ... other options
|
||||
.build();
|
||||
RestHighLevelClient client = RestClients.create(configuration).rest();
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.ruoyi.product.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/**
|
||||
* 新的分页插件,一缓和二缓遵循mybatis的规则,
|
||||
* 需要设置 MybatisConfiguration#useDeprecatedExecutor = false
|
||||
* 避免缓存出现问题(该属性会在旧插件移除后一同移除)
|
||||
*/
|
||||
// 最新版
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
|
|
@ -69,5 +69,15 @@ public class ProductController {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id查询数据
|
||||
* @param productInfoVo
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/selectProduct")
|
||||
public AjaxResult selectProduct(@RequestBody ProductInfoVo productInfoVo){
|
||||
return tbFinancialProductService.selectProduct(productInfoVo);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,14 @@ import com.baomidou.mybatisplus.annotation.TableId;
|
|||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
import org.springframework.data.elasticsearch.annotations.DateFormat;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.Field;
|
||||
import org.springframework.data.elasticsearch.annotations.FieldType;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
/**
|
||||
* 金融产品表
|
||||
|
|
@ -14,6 +21,7 @@ import lombok.Data;
|
|||
*/
|
||||
@TableName(value ="tb_financial_product")
|
||||
@Data
|
||||
@Document(indexName = "tb_financial_product")
|
||||
public class TbFinancialProduct implements Serializable {
|
||||
/**
|
||||
*
|
||||
|
|
@ -34,6 +42,7 @@ public class TbFinancialProduct implements Serializable {
|
|||
/**
|
||||
* 产品名称
|
||||
*/
|
||||
@Field(type = FieldType.Text,analyzer = "ik_max_word")
|
||||
private String productName;
|
||||
|
||||
/**
|
||||
|
|
@ -109,6 +118,9 @@ public class TbFinancialProduct implements Serializable {
|
|||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
|
||||
@Field(type = FieldType.Date,format = DateFormat. custom,pattern = "yyy-MM-dd HH:mm: ss")
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
|
|
@ -119,6 +131,9 @@ public class TbFinancialProduct implements Serializable {
|
|||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
|
||||
@Field(type = FieldType.Date,format = DateFormat. custom,pattern = "yyy-MM-dd HH:mm: ss")
|
||||
private Date updateTime;
|
||||
|
||||
@TableField(exist = false)
|
||||
|
|
|
|||
|
|
@ -21,4 +21,6 @@ public interface TbFinancialProductService extends IService<TbFinancialProduct>
|
|||
AjaxResult deleteById(IdVo idVo);
|
||||
|
||||
AjaxResult updateId(ProductInfoVo productInfoVo);
|
||||
|
||||
AjaxResult selectProduct(ProductInfoVo productInfoVo);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,9 +11,22 @@ import com.ruoyi.product.pojo.vo.ProductInfoVo;
|
|||
import com.ruoyi.product.service.TbFinancialProductService;
|
||||
import com.ruoyi.product.mapper.TbFinancialProductMapper;
|
||||
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.index.query.TermQueryBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
|
@ -26,6 +39,8 @@ public class TbFinancialProductServiceImpl extends ServiceImpl<TbFinancialProduc
|
|||
implements TbFinancialProductService{
|
||||
@Autowired
|
||||
private TbFinancialProductMapper tbFinancialProductMapper;
|
||||
@Autowired
|
||||
private ElasticsearchRestTemplate elasticsearchRestTemplate;
|
||||
|
||||
/**
|
||||
* 产品数据查询
|
||||
|
|
@ -34,21 +49,64 @@ public class TbFinancialProductServiceImpl extends ServiceImpl<TbFinancialProduc
|
|||
*/
|
||||
@Override
|
||||
public AjaxResult listPage(PageInfoVo pageInfoVo) {
|
||||
Page<TbFinancialProduct> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
|
||||
Page<TbFinancialProduct> financialProductPage = page(page, new QueryWrapper<TbFinancialProduct>().lambda().eq(TbFinancialProduct::getFirmName, pageInfoVo.getKeyword()).last("limit 1"));
|
||||
List<ProductInfoVo> collect = financialProductPage.getRecords().stream().map((item) -> {
|
||||
ProductInfoVo productInfoVo = new ProductInfoVo();
|
||||
BeanUtils.copyProperties(item, productInfoVo);
|
||||
return productInfoVo;
|
||||
if(pageInfoVo.getKeyword()==null){
|
||||
Page<TbFinancialProduct> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
|
||||
//.lambda().eq(TbFinancialProduct::getFirmName, pageInfoVo.getKeyword()).last("limit 1")
|
||||
Page<TbFinancialProduct> financialProductPage = page(page, new QueryWrapper<>());
|
||||
List<ProductInfoVo> collect = financialProductPage.getRecords().stream().map((item) -> {
|
||||
ProductInfoVo productInfoVo = new ProductInfoVo();
|
||||
BeanUtils.copyProperties(item, productInfoVo);
|
||||
return productInfoVo;
|
||||
|
||||
}).collect(Collectors.toList());
|
||||
Page<ProductInfoVo> infoVoPage = new Page<>();
|
||||
infoVoPage.setRecords(collect);
|
||||
infoVoPage.setCurrent(financialProductPage.getCurrent());
|
||||
infoVoPage.setSize(financialProductPage.getSize());
|
||||
infoVoPage.setTotal(financialProductPage.getTotal());
|
||||
|
||||
return AjaxResult.success(infoVoPage);
|
||||
}
|
||||
//获取分页数据
|
||||
Pageable page = PageRequest.of(pageInfoVo.getPageNum() - 1, pageInfoVo.getPageSize());
|
||||
TermQueryBuilder termQuery = QueryBuilders.termQuery("productName", pageInfoVo.getKeyword());
|
||||
SortBuilder sortBuilder = new FieldSortBuilder("createTime").order(SortOrder.DESC);
|
||||
//高亮查询数据
|
||||
HighlightBuilder highlightBuilder = new HighlightBuilder();
|
||||
highlightBuilder.preTags("<strong style='color:red'>")
|
||||
.postTags("</strong>")
|
||||
.field("productName");
|
||||
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(termQuery)
|
||||
.withPageable(page)
|
||||
.withSort(sortBuilder)
|
||||
.withHighlightBuilder(highlightBuilder)
|
||||
// .withQuery(queryBuilder)
|
||||
.build();
|
||||
long total = elasticsearchRestTemplate.count(searchQuery, TbFinancialProduct.class);
|
||||
SearchHits<TbFinancialProduct> search = elasticsearchRestTemplate.search(searchQuery, TbFinancialProduct.class);
|
||||
List<ProductInfoVo> productInfoVoList = search.getSearchHits().stream().map((item) -> {
|
||||
TbFinancialProduct content = item.getContent();
|
||||
List<String> productName = item.getHighlightField("productName");
|
||||
if (productName != null && productName.size() > 0) {
|
||||
String name = productName.get(0);
|
||||
//设置高亮查询
|
||||
content.setProductName(name);
|
||||
|
||||
}
|
||||
ProductInfoVo productInfoVo = new ProductInfoVo();
|
||||
BeanUtils.copyProperties(content, productInfoVo);
|
||||
return productInfoVo;
|
||||
}).collect(Collectors.toList());
|
||||
Page<ProductInfoVo> infoVoPage = new Page<>();
|
||||
infoVoPage.setRecords(collect);
|
||||
infoVoPage.setCurrent(financialProductPage.getCurrent());
|
||||
infoVoPage.setSize(financialProductPage.getSize());
|
||||
infoVoPage.setTotal(financialProductPage.getTotal());
|
||||
infoVoPage.setRecords(productInfoVoList);
|
||||
infoVoPage.setCurrent(pageInfoVo.getPageNum()-1);
|
||||
infoVoPage.setSize(pageInfoVo.getPageSize());
|
||||
infoVoPage.setTotal(total);
|
||||
|
||||
return AjaxResult.success(infoVoPage);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -57,6 +115,7 @@ public class TbFinancialProductServiceImpl extends ServiceImpl<TbFinancialProduc
|
|||
* @return
|
||||
*/
|
||||
@Override
|
||||
@Transactional //分布式事务
|
||||
public AjaxResult add(ProductInfoVo productInfoVo) {
|
||||
String firmName = productInfoVo.getFirmName();
|
||||
TbFinancialProduct one = getOne(new QueryWrapper<TbFinancialProduct>().lambda().eq(TbFinancialProduct::getFirmName, firmName));
|
||||
|
|
@ -66,8 +125,7 @@ public class TbFinancialProductServiceImpl extends ServiceImpl<TbFinancialProduc
|
|||
TbFinancialProduct financialProduct = new TbFinancialProduct();
|
||||
BeanUtils.copyProperties(productInfoVo,financialProduct);
|
||||
save(financialProduct);
|
||||
|
||||
|
||||
elasticsearchRestTemplate.save(financialProduct);
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
|
|
@ -113,6 +171,17 @@ public class TbFinancialProductServiceImpl extends ServiceImpl<TbFinancialProduc
|
|||
tbFinancialProductMapper.updateById(product);
|
||||
return AjaxResult.success("修改成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public AjaxResult selectProduct(ProductInfoVo productInfoVo) {
|
||||
Integer productId = productInfoVo.getProductId();
|
||||
TbFinancialProduct one = getOne(new QueryWrapper<TbFinancialProduct>().lambda().eq(TbFinancialProduct::getProductId, productId));
|
||||
if(one==null){
|
||||
return AjaxResult.error(402,"产品Id不存在");
|
||||
}
|
||||
TbFinancialProduct financialProduct = tbFinancialProductMapper.selectById(productId);
|
||||
return AjaxResult.success(financialProduct);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,85 @@
|
|||
package com.ruoyi.product.task;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.ruoyi.product.mapper.TbFinancialProductMapper;
|
||||
import com.ruoyi.product.pojo.TbFinancialProduct;
|
||||
import com.ruoyi.product.var.ConstantVars;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.core.document.Document;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @description: TODO
|
||||
* @author杨宗理
|
||||
* @date 2023/1/18 15:16
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class TaskQuartz {
|
||||
@Autowired
|
||||
ElasticsearchRestTemplate elasticsearchTemplate;
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
@Autowired
|
||||
private TbFinancialProductMapper tbFinancialProductMapper;
|
||||
@Scheduled(fixedDelay = 1000*60)
|
||||
public void ProductQuartz(){
|
||||
//判断索引
|
||||
if(!elasticsearchTemplate.indexOps(TbFinancialProduct.class).exists()){
|
||||
elasticsearchTemplate.indexOps(TbFinancialProduct.class).create();
|
||||
Document mapping = elasticsearchTemplate.indexOps(TbFinancialProduct.class).createMapping();
|
||||
elasticsearchTemplate.indexOps(TbFinancialProduct.class).putMapping(mapping);
|
||||
|
||||
}
|
||||
String strTime = (String) redisTemplate.opsForValue().get(ConstantVars.SYNC_PRODUCT_KEY);
|
||||
|
||||
if(strTime==null){
|
||||
//全量同步
|
||||
List<TbFinancialProduct> list = tbFinancialProductMapper.selectList(new QueryWrapper<TbFinancialProduct>().lambda().orderByDesc(TbFinancialProduct::getCreateTime));
|
||||
if(list!=null && list.size()>0){
|
||||
elasticsearchTemplate.save(list);
|
||||
TbFinancialProduct product = list.get(0);
|
||||
Date updateTime = product.getUpdateTime();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String format = sdf.format(updateTime);
|
||||
//存入redis中
|
||||
redisTemplate.opsForValue().set(ConstantVars.SYNC_PRODUCT_KEY,format);
|
||||
|
||||
}
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
//增量同步
|
||||
//全量同步
|
||||
List<TbFinancialProduct> list = tbFinancialProductMapper.selectList(new QueryWrapper<TbFinancialProduct>().lambda().gt(TbFinancialProduct::getUpdateTime,strTime)
|
||||
.orderByDesc(TbFinancialProduct::getCreateTime));
|
||||
if(list!=null && list.size()>0){
|
||||
elasticsearchTemplate.save(list);
|
||||
TbFinancialProduct product = list.get(0);
|
||||
Date updateTime = product.getUpdateTime();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String format = sdf.format(updateTime);
|
||||
//存入redis中
|
||||
redisTemplate.opsForValue().set(ConstantVars.SYNC_PRODUCT_KEY,format);
|
||||
|
||||
}
|
||||
return;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.ruoyi.product.var;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @description: TODO
|
||||
* @author杨宗理
|
||||
* @date 2023/1/19 21:45
|
||||
*/
|
||||
public interface ConstantVars {
|
||||
//作为Key值
|
||||
final String SYNC_PRODUCT_KEY="SYNC_PRODUCT_KEY";
|
||||
}
|
||||
Loading…
Reference in New Issue