|
@@ -0,0 +1,354 @@
|
|
|
+package com.szwl.service.es;
|
|
|
+
|
|
|
+import cn.com.crbank.ommo.esclient.EsBaseService;
|
|
|
+import cn.com.crbank.ommo.exception.MyException;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.szwl.model.bean.ChartBean;
|
|
|
+import com.szwl.model.bean.ChartColumn;
|
|
|
+import com.szwl.model.entity.TAdmin;
|
|
|
+import com.szwl.model.entity.TEquipment;
|
|
|
+import com.szwl.model.entity.TEquipmentExample;
|
|
|
+import com.szwl.model.query.StatisticsParam;
|
|
|
+import com.szwl.model.query.TCoinOrderParam;
|
|
|
+import com.szwl.model.query.TEquipmentParam;
|
|
|
+import com.szwl.model.query.TOrderParam;
|
|
|
+import com.szwl.model.utils.DateUtils;
|
|
|
+import com.szwl.service.TEquipmentService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.elasticsearch.action.search.SearchRequest;
|
|
|
+import org.elasticsearch.action.search.SearchResponse;
|
|
|
+import org.elasticsearch.client.RequestOptions;
|
|
|
+import org.elasticsearch.client.indices.GetIndexRequest;
|
|
|
+import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
+import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
+import org.elasticsearch.search.aggregations.Aggregations;
|
|
|
+import org.elasticsearch.search.aggregations.BucketOrder;
|
|
|
+import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
|
|
+import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
|
|
+import org.elasticsearch.search.aggregations.metrics.ParsedSum;
|
|
|
+import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
|
|
|
+import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class EsTEquipmentService extends EsBaseService<TEquipment, TEquipmentParam> {
|
|
|
+ private static boolean InitEsTableStatus = false; // 是否正常完成 es 初始化
|
|
|
+ public static final int MAX_ROW = 10000;
|
|
|
+ @Autowired
|
|
|
+ TEquipmentService tEquipmentService;
|
|
|
+ @Autowired
|
|
|
+ EsTCoinOrderService esTCoinOrderService;
|
|
|
+ @Autowired
|
|
|
+ EsTOrderService esTOrderService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getTableName() {
|
|
|
+ return "es_t_equipment";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TEquipment getInstanceOfEntity() {
|
|
|
+ return new TEquipment();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setInitTableStatus(boolean flag) {
|
|
|
+ InitEsTableStatus = flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean getInitTableStatus() {
|
|
|
+ return InitEsTableStatus;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initTableFun() {
|
|
|
+ try{
|
|
|
+ String tableName = getTableName();
|
|
|
+ GetIndexRequest request = new GetIndexRequest(tableName);
|
|
|
+// DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(this.getTableName());
|
|
|
+// restHighLevelClient.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
|
|
|
+ boolean isExists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
|
|
|
+ if (!isExists) {
|
|
|
+ log.info("es 索引 开始创建"+tableName);
|
|
|
+ createTable();
|
|
|
+ // 初始化旧流水
|
|
|
+ int num = 0;
|
|
|
+ while (true) {
|
|
|
+ int limit = MAX_ROW;
|
|
|
+// int offset= num * MAX_ROW;
|
|
|
+ int offset= num;
|
|
|
+
|
|
|
+ LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
|
|
|
+ Page<TEquipment> page = new Page<>(offset, limit, true);
|
|
|
+ IPage<TEquipment> iPage = tEquipmentService.page(page, query);
|
|
|
+ List<TEquipment> list = iPage.getRecords();
|
|
|
+// TEquipmentExample example = new TEquipmentExample();
|
|
|
+// example.setLimit(limit);
|
|
|
+// example.setOffset(offset);
|
|
|
+// List<TEquipment> list = tEquipmentService.selectByOption(example);
|
|
|
+ insertBatch(list);
|
|
|
+ num++;
|
|
|
+ if(list.size()< MAX_ROW){ // 数据小于 最大值 ,证明后面已无数据,则跳出
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("es 索引 "+tableName+" 已存在不再创建");
|
|
|
+ }
|
|
|
+ InitEsTableStatus = true;
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("ElasticsearchRunner InitEsTEquipmentThread 发生错误:{}" , e);
|
|
|
+ throw new MyException("ElasticsearchRunner InitEsTEquipmentThread 发生错误:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getEntityPrimaryKey(TEquipment tEquipment) {
|
|
|
+ return String.valueOf(tEquipment.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TEquipment setEntityPrimaryKey(TEquipment tEquipment, String value) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+// @Override
|
|
|
+// public TEquipment setEntityPrimaryKey(TEquipment tEquipment, String value) {
|
|
|
+// return tEquipment.setId(Long.parseLong(value));
|
|
|
+// }
|
|
|
+ /**
|
|
|
+ * 根据时间 重新同步es
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void updateEsByDate(String startTime, String endTime) {
|
|
|
+ try {
|
|
|
+ startTime = startTime.replace("/", "-");
|
|
|
+ endTime = endTime.replace("/", "-");
|
|
|
+ Date start = DateUtils.parseDate(startTime, DateUtils.PATTERN_yyyy_MM_dd_HH_mm_ss, new Date());
|
|
|
+ Date end = DateUtils.parseDate(endTime, DateUtils.PATTERN_yyyy_MM_dd_HH_mm_ss, new Date());
|
|
|
+ // 查询es 现有的数据,删除
|
|
|
+ TEquipmentParam param = new TEquipmentParam();
|
|
|
+ param.setCreateDate_start(start);
|
|
|
+ param.setCreateDate_end(end);
|
|
|
+ List<TEquipment> list_es = this.selectEntityByEqualToOption(param);
|
|
|
+ for (TEquipment entity : list_es) {
|
|
|
+ this.deleteTableById(String.valueOf(entity.getId()));
|
|
|
+ }
|
|
|
+ // 插入 新的
|
|
|
+// TEquipmentExample equipmentExample = new TEquipmentExample();
|
|
|
+// TEquipmentExample.Criteria criteria =equipmentExample.createCriteria();
|
|
|
+// criteria.andCreateDateBetween(start,end);
|
|
|
+// List<TEquipment> list = tEquipmentService.selectByOption(equipmentExample);
|
|
|
+ LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
|
|
|
+ query.gt(TEquipment::getCreateDate,start);
|
|
|
+ query.lt(TEquipment::getCreateDate,end);
|
|
|
+ List<TEquipment> list = tEquipmentService.list(query);
|
|
|
+ if (CollectionUtils.isNotEmpty(list)) {
|
|
|
+ insertBatch(list);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(this.getTableName()+" updateEsByDate 发生错误:{}", e);
|
|
|
+ throw new MyException(this.getTableName()+" updateEsByDate 发生错误:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public ChartColumn getEquipmentStatistics(StatisticsParam param) {
|
|
|
+ if(StringUtils.equals("0",param.getIfForeign())) { // 国内用户
|
|
|
+ return getEquipmentStatistics_0(param);
|
|
|
+ }else{ // 国外用户
|
|
|
+ return getEquipmentStatistics_1(param);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计 国内
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ChartColumn getEquipmentStatistics_0(StatisticsParam param) {
|
|
|
+ BoolQueryBuilder boolQueryBuilder;
|
|
|
+ String startDate = param.getStartDate().replace("/","-");
|
|
|
+ String endDate = param.getEndDate().replace("/","-");
|
|
|
+ Date start = DateUtils.parseDate(startDate+" 00:00:00",DateUtils.PATTERN_yyyy_MM_dd_HH_mm_ss,new Date());
|
|
|
+ Date end = DateUtils.parseDate(endDate+" 23:59:59",DateUtils.PATTERN_yyyy_MM_dd_HH_mm_ss,new Date());
|
|
|
+ TOrderParam tOrderParam = new TOrderParam();
|
|
|
+ tOrderParam.setStatus(1);
|
|
|
+
|
|
|
+ tOrderParam.setCreateDate_start(start);
|
|
|
+ tOrderParam.setCreateDate_end(end);
|
|
|
+ if(StringUtils.isNotEmpty(param.getAdminId())){ // 所属商家id
|
|
|
+ tOrderParam.setAdminId(Long.parseLong(param.getAdminId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ boolQueryBuilder = esTOrderService.getParam2QueryBuilder(tOrderParam);
|
|
|
+ if("1".equals(param.getChangeType())){
|
|
|
+ // 花型统计
|
|
|
+ return getEquipmentStatistics(boolQueryBuilder,esTOrderService.getTableName(),"productName.keyword");
|
|
|
+ }else{
|
|
|
+ log.info("1");
|
|
|
+ ChartColumn chartColumn = getEquipmentStatistics(boolQueryBuilder,esTOrderService.getTableName(),"equipmentId");
|
|
|
+
|
|
|
+ // 设置categories
|
|
|
+ ArrayList<String> equipmentIdList = chartColumn.getCategories();
|
|
|
+ List<Long> list = new ArrayList<>();
|
|
|
+ for(String e:equipmentIdList){
|
|
|
+ list.add(Long.valueOf(e));
|
|
|
+ }
|
|
|
+// TEquipmentParam tEquipmentParam = new TEquipmentParam();
|
|
|
+// tEquipmentParam.setId_inList(equipmentIdList);
|
|
|
+// List<TEquipment> equipmentList = this.selectEntityByEqualToOption(tEquipmentParam);
|
|
|
+// TEquipmentExample exampe = new TEquipmentExample();
|
|
|
+// TEquipmentExample.Criteria criteria = exampe.createCriteria();
|
|
|
+// criteria.andIdIn(list);
|
|
|
+// List<TEquipment> tEquipments = tEquipmentService.selectByOption(exampe);
|
|
|
+ LambdaQueryWrapper<TEquipment> query = Wrappers.lambdaQuery();
|
|
|
+ query.in(TEquipment::getId,list);
|
|
|
+ List<TEquipment> tEquipments = tEquipmentService.list(query);
|
|
|
+ ArrayList<String> categories_final = new ArrayList<>();
|
|
|
+ for (String equipmentId : equipmentIdList) {
|
|
|
+ String finalCategories = equipmentId;
|
|
|
+// Optional<TEquipment> op = equipmentList.stream().filter(
|
|
|
+ Optional<TEquipment> op = tEquipments.stream().filter(
|
|
|
+ e ->{
|
|
|
+ return StringUtils.equals(String.valueOf(e.getId()),equipmentId);
|
|
|
+ }
|
|
|
+ ).findFirst();
|
|
|
+ if(op.isPresent()){
|
|
|
+ TEquipment equipment = op.get();
|
|
|
+ if(StringUtils.isNotEmpty(equipment.getName())){
|
|
|
+ finalCategories = equipment.getName();
|
|
|
+ }else{
|
|
|
+ finalCategories = equipment.getClientId().substring(equipment.getClientId().length()-6,equipment.getClientId().length());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ categories_final.add(finalCategories);
|
|
|
+ }
|
|
|
+ chartColumn.setCategories(categories_final);
|
|
|
+
|
|
|
+ return chartColumn;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 统计 国外
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ChartColumn getEquipmentStatistics_1(StatisticsParam param) {
|
|
|
+ BoolQueryBuilder boolQueryBuilder;
|
|
|
+ String startDate = param.getStartDate().replace("/","-");
|
|
|
+ String endDate = param.getEndDate().replace("/","-");
|
|
|
+ Date start = DateUtils.parseDate(startDate+" 00:00:00",DateUtils.PATTERN_yyyy_MM_dd_HH_mm_ss,new Date());
|
|
|
+ Date end = DateUtils.parseDate(endDate+" 23:59:59",DateUtils.PATTERN_yyyy_MM_dd_HH_mm_ss,new Date());
|
|
|
+ TCoinOrderParam tCoinOrderParam = new TCoinOrderParam();
|
|
|
+
|
|
|
+// tCoinOrderParam.setCreateDate_start(start);
|
|
|
+ tCoinOrderParam.setPayDate_start(start);
|
|
|
+// tCoinOrderParam.setCreateDate_end(end);
|
|
|
+ tCoinOrderParam.setCreateDate_end(end);
|
|
|
+ if(StringUtils.isNotEmpty(param.getAdminId())){ // 所属商家id
|
|
|
+ tCoinOrderParam.setAdminId(Long.parseLong(param.getAdminId()));
|
|
|
+ }
|
|
|
+
|
|
|
+ boolQueryBuilder = esTCoinOrderService.getParam2QueryBuilder(tCoinOrderParam);
|
|
|
+ if("1".equals(param.getChangeType())){
|
|
|
+ // 花型统计
|
|
|
+ return getEquipmentStatistics(boolQueryBuilder,esTCoinOrderService.getTableName(),"productName.keyword");
|
|
|
+ }else{
|
|
|
+ log.info("1");
|
|
|
+ ChartColumn chartColumn = getEquipmentStatistics(boolQueryBuilder,esTCoinOrderService.getTableName(),"clientId.keyword");
|
|
|
+
|
|
|
+ // 设置categories
|
|
|
+ ArrayList<String> clientIdList = chartColumn.getCategories();
|
|
|
+ TEquipmentParam tEquipmentParam = new TEquipmentParam();
|
|
|
+ tEquipmentParam.setClientId_inList(clientIdList);
|
|
|
+ List<TEquipment> equipmentList = this.selectEntityByEqualToOption(tEquipmentParam);
|
|
|
+
|
|
|
+ ArrayList<String> categories_final = new ArrayList<>();
|
|
|
+ for (String clientId : clientIdList) {
|
|
|
+ String finalCategories = clientId.substring(0,4);
|
|
|
+ Optional<TEquipment> op = equipmentList.stream().filter(
|
|
|
+ e ->{
|
|
|
+ return StringUtils.equals(e.getClientId(),clientId);
|
|
|
+ }
|
|
|
+ ).findFirst();
|
|
|
+ if(op.isPresent()){
|
|
|
+ if(StringUtils.isNotEmpty(op.get().getName())){
|
|
|
+ finalCategories = op.get().getName();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ categories_final.add(finalCategories);
|
|
|
+ }
|
|
|
+ chartColumn.setCategories(categories_final);
|
|
|
+ return chartColumn;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public ChartColumn getEquipmentStatistics(BoolQueryBuilder queryBuilder,String tableName ,String termField) {
|
|
|
+ try {
|
|
|
+ String aggregationResultName = "aggregationResult";// 仅为名称,用以获取返回结果
|
|
|
+ String aggSumName = "sales"; // 仅为名称,用以获取返回结果
|
|
|
+
|
|
|
+ // 根据字段 termField 聚合
|
|
|
+ TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms(aggregationResultName).field(termField);
|
|
|
+ termsAggregationBuilder.size(30); // 聚合后 返回前30条 记录
|
|
|
+ termsAggregationBuilder.order(BucketOrder.compound(
|
|
|
+ BucketOrder.aggregation(aggSumName,false)//先按sales,降序排
|
|
|
+ ));
|
|
|
+ // 根据字段price 求和 sum, sales 仅为名称,用以获取返回结果
|
|
|
+ SumAggregationBuilder sumAggregationBuilder = AggregationBuilders.sum(aggSumName).field("price");
|
|
|
+ termsAggregationBuilder.subAggregation(sumAggregationBuilder);
|
|
|
+ SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
|
|
+ // 指定size为0,不返回文档 因为只需要数量
|
|
|
+ sourceBuilder.query(queryBuilder).aggregation(termsAggregationBuilder).size(0);
|
|
|
+ SearchRequest searchRequest = new SearchRequest(tableName);
|
|
|
+ searchRequest.source(sourceBuilder);
|
|
|
+ log.debug("sourceBuilder:{}", sourceBuilder);
|
|
|
+ SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
+ Aggregations aggregations = searchResponse.getAggregations();
|
|
|
+ Terms terms = aggregations.get(aggregationResultName);
|
|
|
+
|
|
|
+ List<ChartBean> chartBeanList = new ArrayList<>();
|
|
|
+ for (Terms.Bucket bucket : terms.getBuckets()) {
|
|
|
+ ChartBean chartBean = new ChartBean();
|
|
|
+ chartBean.setCategorie(bucket.getKeyAsString());
|
|
|
+// chartBean.setSaleNum(Long.valueOf(bucket.getDocCount()).intValue());
|
|
|
+ chartBean.setSaleNum(Float.valueOf(bucket.getDocCount()));
|
|
|
+ ParsedSum sum = bucket.getAggregations().get(aggSumName);
|
|
|
+// chartBean.setSalePrice(Double.valueOf(sum.getValue()).intValue());
|
|
|
+ String s = format1((float)sum.getValue());
|
|
|
+ chartBean.setSalePrice(Float.valueOf(s));
|
|
|
+ chartBeanList.add(chartBean);
|
|
|
+ }
|
|
|
+ log.debug("chartBeanList:{}",chartBeanList);
|
|
|
+ ChartColumn chartColumn = new ChartColumn(chartBeanList);
|
|
|
+ return chartColumn;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(getTableName() + " es根据条件 聚合查询 报错:{}", e);
|
|
|
+ throw new MyException(getTableName() + " es根据条件 聚合查询 报错:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static String format1(Float value){
|
|
|
+ BigDecimal bd = new BigDecimal(value);//创建一个bd对象,将要转换的值value传入构造函数
|
|
|
+ bd = bd.setScale(2, RoundingMode.HALF_UP);//调用setScale方法进行数据格式化,保留两位小数,采用四舍五入规则
|
|
|
+ return bd.toString(); //返回bd对象的值(转化为string形式)
|
|
|
+ }
|
|
|
+
|
|
|
+}
|