IotServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package com.zswl.dataservice.service.iot;
  2. import com.zswl.dataservice.dao.UserDao;
  3. import com.zswl.dataservice.dao.iot.IotMainDao;
  4. import com.zswl.dataservice.dao.iot.IotTemplateDao;
  5. import com.zswl.dataservice.dao.iot.IotTopicDao;
  6. import com.zswl.dataservice.dataConfig.ResultMessage;
  7. import com.zswl.dataservice.domain.iot.IotMain;
  8. import com.zswl.dataservice.domain.iot.IotTemplate;
  9. import com.zswl.dataservice.domain.iot.IotTopic;
  10. import com.zswl.dataservice.domain.mqtt.DeviceInfo;
  11. import com.zswl.dataservice.model.iot.*;
  12. import com.zswl.dataservice.model.mqtt.DeviceInfoModel;
  13. import com.zswl.dataservice.model.mqtt.DeviceInfoSearchParam;
  14. import com.zswl.dataservice.service.base.SuperService;
  15. import com.zswl.dataservice.utils.bean.BeanUtil;
  16. import com.zswl.dataservice.utils.bean.BeanUtils;
  17. import com.zswl.dataservice.utils.page.PageEntityUtil;
  18. import com.zswl.dataservice.utils.result.ResultContent;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.commons.lang3.ObjectUtils;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.data.domain.Page;
  24. import org.springframework.data.domain.Pageable;
  25. import org.springframework.stereotype.Service;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import java.util.stream.Collectors;
  29. /**
  30. * @author TRX
  31. * @date 2024/6/20
  32. */
  33. @Slf4j
  34. @Service
  35. public class IotServiceImpl extends SuperService {
  36. @Autowired
  37. private IotTemplateDao iotTemplateDao;
  38. @Autowired
  39. private IotMainDao iotMainDao;
  40. @Autowired
  41. private IotTopicDao iotTopicDao;
  42. @Autowired
  43. UserDao userDao;
  44. //----------------------------- 模版 start----------------------------
  45. public ResultContent addIotTemplate(IotTemplateParam param) {
  46. IotTemplate template = null;
  47. initDefaultUser(param);
  48. if (StringUtils.isNotEmpty(param.getId())) {
  49. template = iotTemplateDao.findTopById(param.getId());
  50. if (ObjectUtils.isEmpty(template)) {
  51. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
  52. }
  53. IotTemplate temp = iotTemplateDao.findTopByName(param.getName());
  54. if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
  55. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
  56. }
  57. } else {
  58. template = new IotTemplate();
  59. IotTemplate temp = iotTemplateDao.findTopByName(param.getName());
  60. if (ObjectUtils.isNotEmpty(temp)) {
  61. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
  62. }
  63. }
  64. BeanUtils.copyProperties(param, template);
  65. iotTemplateDao.save(template);
  66. return ResultContent.buildSuccess();
  67. }
  68. public ResultContent deleteTemplate(String id) {
  69. IotTemplate template = iotTemplateDao.findTopById(id);
  70. if (ObjectUtils.isEmpty(template)) {
  71. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  72. }
  73. iotTemplateDao.delete(template);
  74. return ResultContent.buildSuccess();
  75. }
  76. public ResultContent<IotTemplateModel> getIotTemplate(String id) {
  77. IotTemplate template = iotTemplateDao.findTopById(id);
  78. if (ObjectUtils.isEmpty(template)) {
  79. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  80. }
  81. return ResultContent.buildSuccess(toModel(template));
  82. }
  83. public ResultContent<Page<IotTemplateModel>> pageTemplate(Pageable pageable, IotTemplateSearch param) {
  84. initSearchParam(param);
  85. Page<IotTemplate> page = iotTemplateDao.page(pageable, param);
  86. return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
  87. }
  88. //----------------------------- 模版 end------------------------------
  89. //----------------------------- Topic start----------------------------
  90. public ResultContent addIotTopic(IotTopicParam param) {
  91. initDefaultUser(param);
  92. String iotTemplateId = param.getIotTemplateId();
  93. IotTemplate iotTemplate = iotTemplateDao.findTopById(iotTemplateId);
  94. if (ObjectUtils.isEmpty(iotTemplate)) {
  95. return ResultContent.buildFail(String.format("模版不存在:%s", iotTemplateId));
  96. }
  97. IotTopic entity = null;
  98. if (StringUtils.isNotEmpty(param.getId())) {
  99. entity = iotTopicDao.findTopById(param.getId());
  100. if (ObjectUtils.isEmpty(entity)) {
  101. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
  102. }
  103. IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getTopic(), iotTemplate);
  104. if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
  105. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getTopic()));
  106. }
  107. } else {
  108. entity = new IotTopic();
  109. IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getTopic(), iotTemplate);
  110. if (ObjectUtils.isNotEmpty(temp)) {
  111. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getTopic()));
  112. }
  113. }
  114. BeanUtils.copyProperties(param, entity);
  115. entity.setIotTemplate(iotTemplate);
  116. iotTopicDao.save(entity);
  117. return ResultContent.buildSuccess();
  118. }
  119. public ResultContent deleteIotTopic(String id) {
  120. IotTopic entity = iotTopicDao.findTopById(id);
  121. if (ObjectUtils.isEmpty(entity)) {
  122. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  123. }
  124. iotTopicDao.delete(entity);
  125. return ResultContent.buildSuccess();
  126. }
  127. public ResultContent<IotTopicModel> getIotTopic(String id) {
  128. IotTopic entity = iotTopicDao.findTopById(id);
  129. if (ObjectUtils.isEmpty(entity)) {
  130. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  131. }
  132. return ResultContent.buildSuccess(toModel(entity));
  133. }
  134. /**
  135. * 分页查询 模版的Topic管理数据
  136. * @param pageable
  137. * @param param
  138. * @return
  139. */
  140. public ResultContent<Page<IotTopicModel>> pageIotTopic(Pageable pageable, IotTopicSearch param) {
  141. initSearchParam(param);
  142. Page<IotTopic> page = iotTopicDao.page(pageable, param);
  143. return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
  144. }
  145. //----------------------------- Topic end------------------------------
  146. //----------------------------- 物模型 start----------------------------
  147. public ResultContent addIotMain(IotMainParam param) {
  148. initDefaultUser(param);
  149. String iotTemplateId = param.getIotTemplateId();
  150. IotTemplate iotTemplate = iotTemplateDao.findTopById(iotTemplateId);
  151. if (ObjectUtils.isEmpty(iotTemplate)) {
  152. return ResultContent.buildFail(String.format("模版不存在:%s", iotTemplateId));
  153. }
  154. IotMain entity = null;
  155. if (StringUtils.isNotEmpty(param.getId())) {
  156. entity = iotMainDao.findTopById(param.getId());
  157. if (ObjectUtils.isEmpty(entity)) {
  158. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
  159. }
  160. IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getName(), iotTemplate);
  161. if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
  162. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
  163. }
  164. } else {
  165. entity = new IotMain();
  166. IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getName(), iotTemplate);
  167. if (ObjectUtils.isNotEmpty(temp)) {
  168. return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
  169. }
  170. }
  171. BeanUtils.copyProperties(param, entity);
  172. entity.setIotTemplate(iotTemplate);
  173. iotMainDao.save(entity);
  174. return ResultContent.buildSuccess();
  175. }
  176. public ResultContent deleteIotMain(String id) {
  177. IotMain entity = iotMainDao.findTopById(id);
  178. if (ObjectUtils.isEmpty(entity)) {
  179. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  180. }
  181. iotMainDao.delete(entity);
  182. return ResultContent.buildSuccess();
  183. }
  184. public ResultContent<IotMainModel> getIotMain(String id) {
  185. IotMain entity = iotMainDao.findTopById(id);
  186. if (ObjectUtils.isEmpty(entity)) {
  187. return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
  188. }
  189. return ResultContent.buildSuccess(toModel(entity));
  190. }
  191. public ResultContent<Page<IotMainModel>> pageIotMain(Pageable pageable, IotMainSearch param) {
  192. initSearchParam(param);
  193. Page<IotMain> page = iotMainDao.page(pageable, param);
  194. return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
  195. }
  196. /**
  197. * 得到模版物模型的顶用数据
  198. *
  199. * @param templateId
  200. * @return
  201. */
  202. public ResultContent<List<IotMainModel>> getTemplateIotMainList(String templateId) {
  203. List<IotMain> list = iotMainDao.findByIotTemplateOrderByCreateTimeAsc(IotTemplate.build(templateId));
  204. List<IotMainModel> models = new ArrayList<>();
  205. if (ObjectUtils.isNotEmpty(list)) {
  206. models = list.stream().map(this::toModel).collect(Collectors.toList());
  207. }
  208. return ResultContent.buildSuccess(models);
  209. }
  210. //----------------------------- 物模型 end------------------------------
  211. public IotTemplateModel toModel(IotTemplate entity) {
  212. IotTemplateModel model = null;
  213. if (ObjectUtils.isNotEmpty(entity)) {
  214. model = new IotTemplateModel();
  215. BeanUtils.copyProperties(entity, model);
  216. }
  217. return model;
  218. }
  219. public IotTopicModel toModel(IotTopic entity) {
  220. IotTopicModel model = null;
  221. if (ObjectUtils.isNotEmpty(entity)) {
  222. model = new IotTopicModel();
  223. BeanUtils.copyProperties(entity, model);
  224. }
  225. return model;
  226. }
  227. public IotMainModel toModel(IotMain entity) {
  228. IotMainModel model = null;
  229. if (ObjectUtils.isNotEmpty(entity)) {
  230. model = new IotMainModel();
  231. BeanUtils.copyProperties(entity, model);
  232. }
  233. return model;
  234. }
  235. }