DeviceInfoDataImpl.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. package cc.iotkit.data.service;
  2. import cc.iotkit.common.api.PageRequest;
  3. import cc.iotkit.common.api.Paging;
  4. import cc.iotkit.common.utils.MapstructUtils;
  5. import cc.iotkit.common.utils.ReflectUtil;
  6. import cc.iotkit.data.dao.*;
  7. import cc.iotkit.data.manager.ICategoryData;
  8. import cc.iotkit.data.manager.IDeviceInfoData;
  9. import cc.iotkit.data.manager.IProductData;
  10. import cc.iotkit.data.model.*;
  11. import cc.iotkit.data.util.PageBuilder;
  12. import cc.iotkit.model.device.DeviceInfo;
  13. import cc.iotkit.model.device.message.DevicePropertyCache;
  14. import cc.iotkit.model.product.Category;
  15. import cc.iotkit.model.product.Product;
  16. import cc.iotkit.model.stats.DataItem;
  17. import com.querydsl.core.Tuple;
  18. import com.querydsl.core.types.Projections;
  19. import com.querydsl.jpa.impl.JPAQuery;
  20. import com.querydsl.jpa.impl.JPAQueryFactory;
  21. import lombok.RequiredArgsConstructor;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.springframework.beans.factory.annotation.Qualifier;
  24. import org.springframework.context.annotation.Primary;
  25. import org.springframework.data.domain.Page;
  26. import org.springframework.data.jpa.repository.JpaRepository;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.transaction.annotation.Transactional;
  29. import java.util.*;
  30. import java.util.stream.Collectors;
  31. import static cc.iotkit.data.model.QTbDeviceGroupMapping.tbDeviceGroupMapping;
  32. import static cc.iotkit.data.model.QTbDeviceInfo.tbDeviceInfo;
  33. import static cc.iotkit.data.model.QTbDeviceSubUser.tbDeviceSubUser;
  34. import static cc.iotkit.data.model.QTbProduct.tbProduct;
  35. @Primary
  36. @Service
  37. @RequiredArgsConstructor
  38. public class DeviceInfoDataImpl implements IDeviceInfoData, IJPACommData<DeviceInfo, String> {
  39. private final DeviceInfoRepository deviceInfoRepository;
  40. private final DeviceSubUserRepository deviceSubUserRepository;
  41. private final DeviceGroupMappingRepository deviceGroupMappingRepository;
  42. private final DeviceGroupRepository deviceGroupRepository;
  43. private final DeviceTagRepository deviceTagRepository;
  44. @Qualifier("productDataCache")
  45. private final IProductData productData;
  46. @Qualifier("categoryDataCache")
  47. private final ICategoryData categoryData;
  48. private final JPAQueryFactory jpaQueryFactory;
  49. private List<String> deviceStateList = Arrays.asList("online", "offline", "unactivated");
  50. @Override
  51. public JpaRepository getBaseRepository() {
  52. return deviceInfoRepository;
  53. }
  54. @Override
  55. public Class getJpaRepositoryClass() {
  56. return TbDeviceInfo.class;
  57. }
  58. @Override
  59. public Class getTClass() {
  60. return DeviceInfo.class;
  61. }
  62. @Override
  63. public void saveProperties(String deviceId, Map<String, DevicePropertyCache> properties) {
  64. }
  65. @Override
  66. public Map<String, DevicePropertyCache> getProperties(String deviceId) {
  67. return new HashMap<>();
  68. }
  69. @Override
  70. public DeviceInfo findByDeviceId(String deviceId) {
  71. TbDeviceInfo tbDeviceInfo = deviceInfoRepository.findByDeviceId(deviceId);
  72. DeviceInfo dto = MapstructUtils.convert(tbDeviceInfo, DeviceInfo.class);
  73. fillDeviceInfo(deviceId, tbDeviceInfo, dto);
  74. return dto;
  75. }
  76. /**
  77. * 填充设备其它信息
  78. */
  79. private void fillDeviceInfo(String deviceId, TbDeviceInfo vo, DeviceInfo dto) {
  80. if (vo == null || dto == null) {
  81. return;
  82. }
  83. //取子关联用户
  84. dto.setSubUid(deviceSubUserRepository.findByDeviceId(deviceId).stream()
  85. .map(TbDeviceSubUser::getUid).collect(Collectors.toList()));
  86. //取设备所属分组
  87. List<TbDeviceGroupMapping> groupMappings = deviceGroupMappingRepository.findByDeviceId(deviceId);
  88. Map<String, DeviceInfo.Group> groups = new HashMap<>();
  89. for (TbDeviceGroupMapping mapping : groupMappings) {
  90. TbDeviceGroup deviceGroup = deviceGroupRepository.findById(mapping.getGroupId()).orElse(null);
  91. if (deviceGroup == null) {
  92. continue;
  93. }
  94. groups.put(deviceGroup.getId(), new DeviceInfo.Group(deviceGroup.getId(), deviceGroup.getName()));
  95. }
  96. dto.setGroup(groups);
  97. //取设备标签
  98. List<TbDeviceTag> deviceTags = deviceTagRepository.findByDeviceId(deviceId);
  99. Map<String, DeviceInfo.Tag> tagMap = new HashMap<>();
  100. for (TbDeviceTag tag : deviceTags) {
  101. tagMap.put(tag.getCode(), new DeviceInfo.Tag(tag.getCode(), tag.getName(), tag.getValue()));
  102. }
  103. dto.setTag(tagMap);
  104. //将设备状态从vo转为dto的
  105. parseStateToDto(vo, dto);
  106. }
  107. /**
  108. * 将设备状态从vo转为dto的
  109. */
  110. private void parseStateToDto(TbDeviceInfo vo, DeviceInfo dto) {
  111. dto.setState(new DeviceInfo.State("online".equals(vo.getState()),
  112. vo.getOnlineTime(), vo.getOfflineTime()));
  113. dto.setLocate(new DeviceInfo.Locate(vo.getLongitude(), vo.getLatitude()));
  114. }
  115. /**
  116. * 将设备状态从dto转vo
  117. */
  118. private void parseStateToVo(DeviceInfo dto, TbDeviceInfo vo) {
  119. DeviceInfo.State state = dto.getState();
  120. vo.setState(state.isOnline() ? "online" : "offline");
  121. vo.setOfflineTime(state.getOfflineTime());
  122. vo.setOnlineTime(state.getOnlineTime());
  123. DeviceInfo.Locate locate = dto.getLocate();
  124. vo.setLongitude(locate.getLongitude());
  125. vo.setLatitude(locate.getLatitude());
  126. }
  127. /**
  128. * 将数据库中查出来的vo转为dto
  129. */
  130. private DeviceInfo parseVoToDto(TbDeviceInfo vo) {
  131. if (vo == null) {
  132. return null;
  133. }
  134. DeviceInfo dto = MapstructUtils.convert(vo, DeviceInfo.class);
  135. fillDeviceInfo(vo.getDeviceId(), vo, dto);
  136. return dto;
  137. }
  138. /**
  139. * 将数据库中查出来的vo列表转为dto列表
  140. */
  141. private List<DeviceInfo> parseVoToDto(List<TbDeviceInfo> vos) {
  142. return vos.stream().map(d -> {
  143. DeviceInfo dto = MapstructUtils.convert(d, DeviceInfo.class);
  144. fillDeviceInfo(d.getDeviceId(), d, dto);
  145. return dto;
  146. }).collect(Collectors.toList());
  147. }
  148. @Override
  149. public DeviceInfo findByDeviceName(String deviceName) {
  150. return parseVoToDto(deviceInfoRepository.findByDeviceName(deviceName));
  151. }
  152. @Override
  153. public List<DeviceInfo> findByParentId(String parentId) {
  154. return parseVoToDto(deviceInfoRepository.findByParentId(parentId));
  155. }
  156. @Override
  157. public List<String> findSubDeviceIds(String parentId) {
  158. return jpaQueryFactory.select(tbDeviceInfo.deviceId).from(tbDeviceInfo)
  159. .where(tbDeviceInfo.parentId.eq(parentId)).fetch();
  160. }
  161. @Override
  162. public List<DeviceInfo> findByProductNodeType(String uid) {
  163. List<TbDeviceInfo> devices = jpaQueryFactory.select(tbDeviceInfo).from(tbDeviceInfo)
  164. .join(tbProduct).on(tbProduct.nodeType.eq(0).and(tbDeviceInfo.productKey.eq(tbProduct.productKey))).fetch();
  165. return MapstructUtils.convert(devices, DeviceInfo.class);
  166. }
  167. @Override
  168. public boolean existByProductKey(String productKey) {
  169. return Optional.ofNullable(jpaQueryFactory.selectOne().from(tbDeviceInfo).where(tbDeviceInfo.productKey.eq(productKey)).fetchOne()).orElse(0) > 0;
  170. }
  171. @Override
  172. public Paging<DeviceInfo> findByConditions(String uid, String subUid,
  173. String productKey, String groupId,
  174. String state, String keyword,
  175. int page, int size) {
  176. JPAQuery<TbDeviceInfo> query = jpaQueryFactory.selectFrom(tbDeviceInfo);
  177. if (StringUtils.isNotBlank(uid)) {
  178. query.where(tbDeviceInfo.uid.eq(uid));
  179. }
  180. if (StringUtils.isNotBlank(subUid)) {
  181. query.join(tbDeviceSubUser).on(tbDeviceSubUser.deviceId.eq(tbDeviceInfo.deviceId));
  182. query.where(tbDeviceSubUser.uid.eq(subUid));
  183. }
  184. if (StringUtils.isNotBlank(productKey)) {
  185. query.where(tbDeviceInfo.productKey.eq(productKey));
  186. }
  187. if (StringUtils.isNotBlank(state)) {
  188. query.where(tbDeviceInfo.state.eq(state));
  189. }
  190. if (StringUtils.isNotBlank(keyword)) {
  191. query.where(tbDeviceInfo.deviceId.like("%" + keyword + "%")
  192. .or(tbDeviceInfo.deviceName.like("%" + keyword + "%")));
  193. }
  194. query.orderBy(tbDeviceInfo.createAt.desc());
  195. query.offset((page - 1) * size).limit(size);
  196. List<TbDeviceInfo> tbDeviceInfos = query.fetch();
  197. long total = query.fetchCount();
  198. List<DeviceInfo> deviceInfos = new ArrayList<>(tbDeviceInfos.size());
  199. for (TbDeviceInfo tbDeviceInfo : tbDeviceInfos) {
  200. DeviceInfo deviceInfo = MapstructUtils.convert(tbDeviceInfo, DeviceInfo.class);
  201. fillDeviceInfo(tbDeviceInfo.getDeviceId(), tbDeviceInfo, deviceInfo);
  202. deviceInfos.add(deviceInfo);
  203. }
  204. return new Paging<>(total, deviceInfos);
  205. }
  206. @Override
  207. public void updateTag(String deviceId, DeviceInfo.Tag tag) {
  208. TbDeviceTag deviceTag = deviceTagRepository.findByDeviceIdAndCode(deviceId, tag.getId());
  209. if (deviceTag != null) {
  210. deviceTag.setName(tag.getName());
  211. deviceTag.setValue(tag.getValue());
  212. deviceTagRepository.save(deviceTag);
  213. } else {
  214. deviceTagRepository.save(
  215. TbDeviceTag.builder()
  216. .id(UUID.randomUUID().toString())
  217. .code(tag.getId())
  218. .deviceId(deviceId)
  219. .name(tag.getName())
  220. .value(tag.getValue())
  221. .build()
  222. );
  223. }
  224. }
  225. @Override
  226. public List<DataItem> getDeviceStatsByCategory(String uid) {
  227. //先按产品统计设备数量
  228. JPAQuery<DataItem> query = jpaQueryFactory.select(Projections.bean(DataItem.class,
  229. tbDeviceInfo.productKey,
  230. tbDeviceInfo.count()))
  231. .from(tbDeviceInfo)
  232. .groupBy(tbDeviceInfo.productKey);
  233. if (StringUtils.isNotBlank(uid)) {
  234. query.where(tbDeviceInfo.uid.eq(uid));
  235. }
  236. List<DataItem> stats = new ArrayList<>();
  237. List<DataItem> rst = query.fetch();
  238. for (DataItem item : rst) {
  239. //找到产品对应的品类取出品类名
  240. Product product = productData.findByProductKey(item.getName());
  241. String cateId = product.getCategory();
  242. Category category = categoryData.findById(cateId);
  243. if (category == null) {
  244. continue;
  245. }
  246. //将数据替换成按品类的数据
  247. item.setName(category.getName());
  248. }
  249. //按品类分组求合
  250. rst.stream().collect(Collectors.groupingBy(DataItem::getName,
  251. Collectors.summarizingLong(item -> (long) item.getValue())))
  252. .forEach((key, sum) -> stats.add(new DataItem(key, sum.getSum())));
  253. return stats;
  254. }
  255. @Override
  256. public long countByGroupId(String groupId) {
  257. return deviceGroupMappingRepository.countByGroupId(groupId);
  258. }
  259. @Override
  260. @Transactional
  261. public void addToGroup(String deviceId, DeviceInfo.Group group) {
  262. String groupId = UUID.randomUUID().toString();
  263. deviceGroupMappingRepository.save(new TbDeviceGroupMapping(groupId, deviceId, group.getId()));
  264. //更新设备数量
  265. updateGroupDeviceCount(groupId);
  266. }
  267. private void updateGroupDeviceCount(String groupId) {
  268. //更新设备数量
  269. TbDeviceGroup deviceGroup = deviceGroupRepository.findById(groupId).orElse(null);
  270. if (deviceGroup != null) {
  271. deviceGroup.setDeviceQty((int) countByGroupId(groupId));
  272. deviceGroupRepository.save(deviceGroup);
  273. }
  274. }
  275. @Override
  276. public void updateGroup(String groupId, DeviceInfo.Group group) {
  277. //更新设备信息中的分组信息,关系数据库中不需要实现
  278. }
  279. @Override
  280. @Transactional
  281. public void removeGroup(String deviceId, String groupId) {
  282. jpaQueryFactory.delete(tbDeviceGroupMapping)
  283. .where(tbDeviceGroupMapping.deviceId.eq(deviceId)
  284. .and(tbDeviceGroupMapping.groupId.eq(groupId)))
  285. .execute();
  286. //更新设备数量
  287. updateGroupDeviceCount(groupId);
  288. }
  289. @Override
  290. @Transactional
  291. public void removeGroup(String groupId) {
  292. jpaQueryFactory.delete(tbDeviceGroupMapping)
  293. .where(tbDeviceGroupMapping.groupId.eq(groupId))
  294. .execute();
  295. //更新设备数量
  296. updateGroupDeviceCount(groupId);
  297. }
  298. @Override
  299. public List<DeviceInfo> findByUid(String uid) {
  300. return new ArrayList<>();
  301. }
  302. @Override
  303. public Paging<DeviceInfo> findByUid(String uid, int page, int size) {
  304. return new Paging<>();
  305. }
  306. @Override
  307. public long countByUid(String uid) {
  308. return 0;
  309. }
  310. @Override
  311. public DeviceInfo findById(String s) {
  312. return MapstructUtils.convert(
  313. deviceInfoRepository.findById(s).orElse(null), DeviceInfo.class);
  314. }
  315. @Override
  316. public List<DeviceInfo> findByIds(Collection<String> ids) {
  317. return MapstructUtils.convert(deviceInfoRepository.findAllById(ids), DeviceInfo.class);
  318. }
  319. @Override
  320. @Transactional
  321. public DeviceInfo save(DeviceInfo data) {
  322. TbDeviceInfo vo = deviceInfoRepository.findByDeviceId(data.getDeviceId());
  323. if (StringUtils.isBlank(data.getId())) {
  324. data.setId(UUID.randomUUID().toString());
  325. }
  326. if (vo == null) {
  327. vo = new TbDeviceInfo();
  328. }
  329. ReflectUtil.copyNoNulls(data, vo);
  330. //状态转换
  331. parseStateToVo(data, vo);
  332. //保存设备信息
  333. deviceInfoRepository.save(vo);
  334. //设备分组转换
  335. Map<String, DeviceInfo.Group> groupMap = data.getGroup();
  336. groupMap.forEach((id, group) -> {
  337. TbDeviceGroupMapping mapping = deviceGroupMappingRepository.findByDeviceIdAndGroupId(data.getDeviceId(), id);
  338. if (mapping == null) {
  339. //保存设备分组与设备对应关系
  340. deviceGroupMappingRepository.save(new TbDeviceGroupMapping(
  341. UUID.randomUUID().toString(),
  342. data.getDeviceId(),
  343. id
  344. ));
  345. }
  346. });
  347. return data;
  348. }
  349. @Override
  350. public void batchSave(List<DeviceInfo> data) {
  351. }
  352. @Override
  353. public void deleteById(String s) {
  354. deviceInfoRepository.deleteById(s);
  355. }
  356. @Override
  357. public void deleteByIds(Collection<String> ids) {
  358. deviceInfoRepository.deleteAllById(ids);
  359. }
  360. @Override
  361. public long count() {
  362. return deviceInfoRepository.count();
  363. }
  364. @Override
  365. public List<DeviceInfo> findAll() {
  366. return new ArrayList<>();
  367. }
  368. @Override
  369. public Paging<DeviceInfo> findAll(PageRequest<DeviceInfo> pageRequest) {
  370. Page<TbDeviceInfo> ret = deviceInfoRepository.findAll(PageBuilder.toPageable(pageRequest));
  371. return new Paging<>(ret.getTotalElements(), MapstructUtils.convert(ret.getContent(), DeviceInfo.class));
  372. }
  373. @Override
  374. public List<DeviceInfo> findAllByCondition(DeviceInfo data) {
  375. return Collections.emptyList();
  376. }
  377. @Override
  378. public DeviceInfo findOneByCondition(DeviceInfo data) {
  379. return null;
  380. }
  381. @Override
  382. public Map countStateMap(String tenantId) {
  383. Map<String, Long> resultMap = new HashMap();
  384. if (StringUtils.isNotBlank(tenantId) && tenantId.equals("000000")) {
  385. resultMap = countStateMapWithoutTenantId();
  386. } else {
  387. if (StringUtils.isNotBlank(tenantId)) {
  388. resultMap = countStateMapWithTenantId(tenantId);
  389. }
  390. }
  391. for(String item : deviceStateList){
  392. Long itemData = resultMap.get(item);
  393. if(Objects.isNull(itemData)){
  394. resultMap.put(item,0L);
  395. }
  396. }
  397. return resultMap;
  398. }
  399. private Map<String, Long> countStateMapWithTenantId(String tenantId) {
  400. Map<String, Long> resultMap = new HashMap();
  401. List<Tuple> result = jpaQueryFactory.select(tbDeviceInfo.id.count(), tbDeviceInfo.state).from(tbDeviceInfo)
  402. .leftJoin(tbProduct).on(tbProduct.productKey.eq(tbDeviceInfo.productKey))
  403. .where(tbProduct.tenantId.eq(tenantId))
  404. .groupBy(tbDeviceInfo.state).fetch();
  405. for (Tuple tuple : result) {
  406. resultMap.put(tuple.get(1, String.class), tuple.get(0, Long.class));
  407. }
  408. Long noRegistResult = jpaQueryFactory.select(tbDeviceInfo.id.count()).from(tbDeviceInfo).where(tbDeviceInfo.state.eq("offline")
  409. .and(tbDeviceInfo.onlineTime.isNull()))
  410. .leftJoin(tbProduct).on(tbProduct.productKey.eq(tbDeviceInfo.productKey))
  411. .where(tbProduct.tenantId.eq(tenantId))
  412. .fetchOne();
  413. resultMap.put("unactivated", noRegistResult);
  414. //
  415. Long offlineNum = resultMap.get("offline");
  416. if (Objects.nonNull(offlineNum)) {
  417. resultMap.put("offline", offlineNum - noRegistResult);
  418. }
  419. return resultMap;
  420. }
  421. private Map<String, Long> countStateMapWithoutTenantId() {
  422. Map<String, Long> resultMap = new HashMap();
  423. List<Tuple> result = jpaQueryFactory.select(tbDeviceInfo.id.count(), tbDeviceInfo.state).from(tbDeviceInfo).groupBy(tbDeviceInfo.state).fetch();
  424. for (Tuple tuple : result) {
  425. resultMap.put(tuple.get(1, String.class), tuple.get(0, Long.class));
  426. }
  427. Long noRegistResult = jpaQueryFactory.select(tbDeviceInfo.id.count()).from(tbDeviceInfo).where(tbDeviceInfo.state.eq("offline")
  428. .and(tbDeviceInfo.onlineTime.isNull())).fetchOne();
  429. resultMap.put("unactivated", noRegistResult);
  430. //
  431. Long offlineNum = resultMap.get("offline");
  432. if (Objects.nonNull(offlineNum)) {
  433. resultMap.put("offline", offlineNum - noRegistResult);
  434. }
  435. return resultMap;
  436. }
  437. @Override
  438. public Map countCategoryMap() {
  439. Map<String, Long> resultMap = new HashMap();
  440. List<Tuple> result = jpaQueryFactory.select(tbDeviceInfo.id.count(), tbDeviceInfo.productKey).from(tbDeviceInfo).groupBy(tbDeviceInfo.productKey).fetch();
  441. for (Tuple tuple : result) {
  442. resultMap.put(tuple.get(1, String.class), tuple.get(0, Long.class));
  443. }
  444. return resultMap;
  445. }
  446. @Override
  447. public Long countTodayAdd(String tenantId) {
  448. Calendar calendar = Calendar.getInstance();
  449. calendar.set(Calendar.HOUR_OF_DAY, 0);
  450. calendar.set(Calendar.MINUTE, 0);
  451. calendar.set(Calendar.HOUR, 0);
  452. calendar.set(Calendar.MILLISECOND, 0);
  453. return jpaQueryFactory.select(tbDeviceInfo.id.count()).from(tbDeviceInfo).where(tbDeviceInfo.createAt.gt(calendar.getTime().getTime())).fetchOne();
  454. }
  455. @Override
  456. public Paging<DeviceInfo> findByConditionsAndTenantId(String uid, String subUid, String productKey, String group, String state, String keyword, Integer page, Integer size, String tenantId) {
  457. JPAQuery<TbDeviceInfo> query = jpaQueryFactory.selectFrom(tbDeviceInfo);
  458. if (StringUtils.isNotBlank(uid)) {
  459. query.where(tbDeviceInfo.uid.eq(uid));
  460. }
  461. if (StringUtils.isNotBlank(subUid)) {
  462. query.join(tbDeviceSubUser).on(tbDeviceSubUser.deviceId.eq(tbDeviceInfo.deviceId));
  463. query.where(tbDeviceSubUser.uid.eq(subUid));
  464. }
  465. if (StringUtils.isNotBlank(productKey)) {
  466. query.where(tbDeviceInfo.productKey.eq(productKey));
  467. }
  468. if (StringUtils.isNotBlank(state)) {
  469. query.where(tbDeviceInfo.state.eq(state));
  470. }
  471. if (StringUtils.isNotBlank(keyword)) {
  472. query.where(tbDeviceInfo.deviceId.like("%" + keyword + "%")
  473. .or(tbDeviceInfo.deviceName.like("%" + keyword + "%")));
  474. }
  475. if (tenantId != null && !tenantId.equals("000000")) {
  476. query.leftJoin(tbProduct).on(tbProduct.productKey.eq(tbDeviceInfo.productKey)).where(tbProduct.tenantId.eq(tenantId));
  477. }
  478. query.orderBy(tbDeviceInfo.createAt.desc());
  479. query.offset((page - 1) * size).limit(size);
  480. List<TbDeviceInfo> tbDeviceInfos = query.fetch();
  481. long total = query.fetchCount();
  482. List<DeviceInfo> deviceInfos = new ArrayList<>(tbDeviceInfos.size());
  483. for (TbDeviceInfo tbDeviceInfo : tbDeviceInfos) {
  484. DeviceInfo deviceInfo = MapstructUtils.convert(tbDeviceInfo, DeviceInfo.class);
  485. fillDeviceInfo(tbDeviceInfo.getDeviceId(), tbDeviceInfo, deviceInfo);
  486. deviceInfos.add(deviceInfo);
  487. }
  488. return new Paging<>(total, deviceInfos);
  489. }
  490. @Override
  491. public Long countByTenantId(String tenantId) {
  492. if (StringUtils.isNotBlank(tenantId) && tenantId.equals("000000")) {
  493. return this.count();
  494. } else {
  495. if (StringUtils.isNotBlank(tenantId)) {
  496. return jpaQueryFactory.select(tbDeviceInfo.id.count()).from(tbDeviceInfo).leftJoin(tbProduct)
  497. .on(tbDeviceInfo.productKey.eq(tbProduct.productKey)).where(tbProduct.tenantId.eq(tenantId)
  498. ).fetchOne();
  499. } else {
  500. return 0L;
  501. }
  502. }
  503. }
  504. }