|
|
|
@ -2,7 +2,6 @@ package com.thing.common.tskv.service.tskv; |
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import com.google.common.collect.Maps; |
|
|
|
import com.mybatisflex.annotation.UseDataSource; |
|
|
|
import com.mybatisflex.core.paginate.Page; |
|
|
|
import com.mybatisflex.core.query.QueryChain; |
|
|
|
@ -24,7 +23,6 @@ import com.thing.common.tskv.entity.TsKvMy; |
|
|
|
import com.thing.common.tskv.mapper.TsKvMyMapper; |
|
|
|
import com.thing.common.tskv.service.DatabaseType; |
|
|
|
import com.thing.common.tskv.service.TsKvNativeSQL; |
|
|
|
import jakarta.annotation.PostConstruct; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
|
|
import org.apache.commons.collections4.MapUtils; |
|
|
|
@ -36,7 +34,9 @@ import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.util.*; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import static com.thing.common.tskv.NativeSQLTool.*; |
|
|
|
import static com.thing.common.util.time.DaXiaUtils.getAmEndTime; |
|
|
|
@ -59,11 +59,11 @@ public class TsKvMyServiceImpl extends ServiceImpl<TsKvMyMapper, TsKvMy> impleme |
|
|
|
this.eventPublisher = eventPublisher; |
|
|
|
} |
|
|
|
|
|
|
|
// private final Map<TimeType, String> intervalMap = Maps.newHashMap(); |
|
|
|
// private final Map<TimeType, String> intervalMap = Maps.newHashMap(); |
|
|
|
|
|
|
|
@Override |
|
|
|
public Integer saveDTOTsKv(List<TsKvDTO> kvDTOList,String tableSuffix) { |
|
|
|
return TsKvNativeSQL.saveDTOTsKv(kvDTOList, DatabaseType.MYSQL,tableSuffix); |
|
|
|
public Integer saveDTOTsKv(List<TsKvDTO> kvDTOList, String tableSuffix) { |
|
|
|
return TsKvNativeSQL.saveDTOTsKv(kvDTOList, DatabaseType.MYSQL, tableSuffix); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -179,8 +179,8 @@ public class TsKvMyServiceImpl extends ServiceImpl<TsKvMyMapper, TsKvMy> impleme |
|
|
|
@Override |
|
|
|
public List<TsKvDTO> findTsKvAggByCodeAndAttrs(String code, Collection<String> attrList, Long startTime, Long endTime, Boolean isAsc, AggType agg) { |
|
|
|
try { |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "Wrappers", Collection.class, Collection.class, Long.class, Long.class, AggType.class,DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, Lists.newArrayList(code), attrList, startTime, endTime, agg,DataBaseType.MYSQL); |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "Wrappers", Collection.class, Collection.class, Long.class, Long.class, AggType.class, DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, Lists.newArrayList(code), attrList, startTime, endTime, agg, DataBaseType.MYSQL); |
|
|
|
return RowUtil.toEntityList(Db.selectListBySql(sql), TsKvDTO.class); |
|
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
|
|
|
throw new SysException(String.format("调用%s%s%s方法异常,请检查代码", "get", agg, "Wrappers"), e); |
|
|
|
@ -197,8 +197,8 @@ public class TsKvMyServiceImpl extends ServiceImpl<TsKvMyMapper, TsKvMy> impleme |
|
|
|
@Override |
|
|
|
public PageData<TsKvDTO> findPageTsKvAggByCodeAndAttrs(String code, Collection<String> attrList, Long startTime, Long endTime, Boolean isAsc, AggType agg, Integer page, Integer limit) { |
|
|
|
try { |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "Wrappers", Collection.class, Collection.class, Long.class, Long.class, AggType.class,DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, Lists.newArrayList(code), attrList, startTime, endTime, agg,DataBaseType.MYSQL); |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "Wrappers", Collection.class, Collection.class, Long.class, Long.class, AggType.class, DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, Lists.newArrayList(code), attrList, startTime, endTime, agg, DataBaseType.MYSQL); |
|
|
|
long count = Db.selectCount(NativeSQLTool.countSql(sql, true)); |
|
|
|
return NativeSQLTool.getTsKvDTOPageData(sql, count, isAsc, page, limit); |
|
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
|
|
|
@ -212,11 +212,16 @@ public class TsKvMyServiceImpl extends ServiceImpl<TsKvMyMapper, TsKvMy> impleme |
|
|
|
return this.listAs(queryWrapper, TsKvDTO.class); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<TsKvDTO> findOriginalTsKvByCodesAndAttrs(Collection<String> codeList, Collection<String> attrList, Long startTime, Long endTime, Boolean isAsc) { |
|
|
|
return findTsKvByCodesAndAttrs(codeList, attrList, startTime, endTime, isAsc); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<TsKvDTO> findTsKvAggByCodesAndAttrs(Collection<String> codeList, Collection<String> attrList, Long startTime, Long endTime, Boolean isAsc, AggType agg) { |
|
|
|
try { |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "Wrappers", Collection.class, Collection.class, Long.class, Long.class, AggType.class,DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, codeList, attrList, startTime, endTime, agg,DataBaseType.MYSQL); |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "Wrappers", Collection.class, Collection.class, Long.class, Long.class, AggType.class, DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, codeList, attrList, startTime, endTime, agg, DataBaseType.MYSQL); |
|
|
|
sql = sql + " ORDER BY ts "; |
|
|
|
if (!isAsc) { |
|
|
|
sql = sql + " DESC"; |
|
|
|
@ -237,8 +242,8 @@ public class TsKvMyServiceImpl extends ServiceImpl<TsKvMyMapper, TsKvMy> impleme |
|
|
|
@Override |
|
|
|
public PageData<TsKvDTO> findPageTsKvAggByCodesAndAttrs(Collection<String> codeList, Collection<String> attrList, Long startTime, Long endTime, Boolean isAsc, AggType agg, Integer page, Integer limit) { |
|
|
|
try { |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "Wrappers", Collection.class, Collection.class, Long.class, Long.class, AggType.class,DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, codeList, attrList, startTime, endTime, agg,DataBaseType.MYSQL); |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "Wrappers", Collection.class, Collection.class, Long.class, Long.class, AggType.class, DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, codeList, attrList, startTime, endTime, agg, DataBaseType.MYSQL); |
|
|
|
long count = Db.selectCount(NativeSQLTool.countSql(sql, true)); |
|
|
|
return NativeSQLTool.getTsKvDTOPageData(sql, count, isAsc, page, limit); |
|
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
|
|
|
@ -252,11 +257,16 @@ public class TsKvMyServiceImpl extends ServiceImpl<TsKvMyMapper, TsKvMy> impleme |
|
|
|
return this.listAs(queryChain.toQueryWrapper(), TsKvDTO.class); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<TsKvDTO> findOriginalTsKvByMultiMap(Map<String, Collection<String>> multiMap, Long startTime, Long endTime, Boolean isAsc) { |
|
|
|
return findTsKvByMultiMap(multiMap, startTime, endTime, isAsc); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<TsKvDTO> findTsKvAggByMultiMap(Map<String, Collection<String>> multiMap, Long startTime, Long endTime, Boolean isAsc, AggType agg) { |
|
|
|
try { |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "MapWrapper", Map.class, Long.class, Long.class, AggType.class,DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, multiMap, startTime, endTime, agg,DataBaseType.MYSQL); |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "MapWrapper", Map.class, Long.class, Long.class, AggType.class, DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, multiMap, startTime, endTime, agg, DataBaseType.MYSQL); |
|
|
|
return RowUtil.toEntityList(Db.selectListBySql(sql), TsKvDTO.class); |
|
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
|
|
|
throw new SysException(String.format("调用%s%s%s方法异常,请检查代码", "get", agg, "MapWrapper"), e); |
|
|
|
@ -273,8 +283,8 @@ public class TsKvMyServiceImpl extends ServiceImpl<TsKvMyMapper, TsKvMy> impleme |
|
|
|
@Override |
|
|
|
public PageData<TsKvDTO> findPageTsKvAggByMultiMap(Map<String, Collection<String>> multiMap, Long startTime, Long endTime, Boolean isAsc, AggType agg, Integer page, Integer limit) { |
|
|
|
try { |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "MapWrapper", Map.class, Long.class, Long.class, AggType.class,DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, multiMap, startTime, endTime, agg,DataBaseType.MYSQL); |
|
|
|
Method method = NativeSQLTool.class.getMethod("get" + agg + "MapWrapper", Map.class, Long.class, Long.class, AggType.class, DataBaseType.class); |
|
|
|
String sql = (String) method.invoke(NativeSQLTool.class, multiMap, startTime, endTime, agg, DataBaseType.MYSQL); |
|
|
|
long count = Db.selectCount(NativeSQLTool.countSql(sql, true)); |
|
|
|
return NativeSQLTool.getTsKvDTOPageData(sql, count, isAsc, page, limit); |
|
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
|
|
|
@ -335,33 +345,33 @@ public class TsKvMyServiceImpl extends ServiceImpl<TsKvMyMapper, TsKvMy> impleme |
|
|
|
QueryWrapper queryWrapper = QueryWrapper.create(); |
|
|
|
queryWrapper.in(TsKvMy::getAttrKey, attrList, CollectionUtils.isNotEmpty(attrList)) |
|
|
|
.in(TsKvMy::getThingCode, codeList, CollectionUtils.isNotEmpty(codeList)) |
|
|
|
.in(TsKvMy::getTs,timeList,CollectionUtils.isNotEmpty(timeList)); |
|
|
|
.in(TsKvMy::getTs, timeList, CollectionUtils.isNotEmpty(timeList)); |
|
|
|
queryWrapper.orderBy(TsKvMy::getTs, isAsc); |
|
|
|
return mapper.selectListByQueryAs(queryWrapper,TsKvDTO.class); |
|
|
|
return mapper.selectListByQueryAs(queryWrapper, TsKvDTO.class); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Integer deleteTskv(TsKvDTO kvDTO) { |
|
|
|
String delSql = NativeSQLTool.deleteTskvSql(kvDTO); |
|
|
|
return Db.deleteBySql(delSql); |
|
|
|
return Db.deleteBySql(delSql); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Integer deleteLastTskv(TsKvDTO kvDTO) { |
|
|
|
String delSql = NativeSQLTool.deleteLastTskvSql(kvDTO); |
|
|
|
return Db.deleteBySql(delSql); |
|
|
|
return Db.deleteBySql(delSql); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<TsKvDTO> findTsKvByCodesAndAttrsWithTableSuffix(Collection<String> codeList, Collection<String> attrList, Long startTime, Long endTime, Boolean isAsc, String tableSuffix) { |
|
|
|
StringBuilder sql = new StringBuilder(" SELECT thing_code,attr_key,ts,val FROM thing_ts_kv"); |
|
|
|
sql.append(tableSuffix); |
|
|
|
NativeSQLTool.spliceCodesAndAttrsSql(codeList,attrList,sql,startTime,endTime); |
|
|
|
NativeSQLTool.spliceCodesAndAttrsSql(codeList, attrList, sql, startTime, endTime); |
|
|
|
sql.append(" ORDER BY ts "); |
|
|
|
if(!isAsc){ |
|
|
|
if (!isAsc) { |
|
|
|
sql.append(" DESC "); |
|
|
|
} |
|
|
|
return RowUtil.toEntityList(Db.selectListBySql(sql.toString()),TsKvDTO.class); |
|
|
|
return RowUtil.toEntityList(Db.selectListBySql(sql.toString()), TsKvDTO.class); |
|
|
|
} |
|
|
|
|
|
|
|
private PageData<TsKvDTO> getTsKvDTOPageIntervalData(Integer page, Integer limit, long count, String sql) { |
|
|
|
|