DeviceInfoDataImpl.java 23 KB

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