| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- package com.zswl.dataservice.service.iot;
- import com.zswl.dataservice.dao.UserDao;
- import com.zswl.dataservice.dao.iot.IotMainDao;
- import com.zswl.dataservice.dao.iot.IotTemplateDao;
- import com.zswl.dataservice.dao.iot.IotTopicDao;
- import com.zswl.dataservice.dataConfig.ResultMessage;
- import com.zswl.dataservice.domain.iot.IotMain;
- import com.zswl.dataservice.domain.iot.IotTemplate;
- import com.zswl.dataservice.domain.iot.IotTopic;
- import com.zswl.dataservice.domain.mqtt.DeviceInfo;
- import com.zswl.dataservice.model.iot.*;
- import com.zswl.dataservice.model.mqtt.DeviceInfoModel;
- import com.zswl.dataservice.model.mqtt.DeviceInfoSearchParam;
- import com.zswl.dataservice.service.base.SuperService;
- import com.zswl.dataservice.utils.bean.BeanUtil;
- import com.zswl.dataservice.utils.bean.BeanUtils;
- import com.zswl.dataservice.utils.page.PageEntityUtil;
- import com.zswl.dataservice.utils.result.ResultContent;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.ObjectUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- import org.springframework.stereotype.Service;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- /**
- * @author TRX
- * @date 2024/6/20
- */
- @Slf4j
- @Service
- public class IotServiceImpl extends SuperService {
- @Autowired
- private IotTemplateDao iotTemplateDao;
- @Autowired
- private IotMainDao iotMainDao;
- @Autowired
- private IotTopicDao iotTopicDao;
- @Autowired
- UserDao userDao;
- //----------------------------- 模版 start----------------------------
- public ResultContent addIotTemplate(IotTemplateParam param) {
- IotTemplate template = null;
- initDefaultUser(param);
- if (StringUtils.isNotEmpty(param.getId())) {
- template = iotTemplateDao.findTopById(param.getId());
- if (ObjectUtils.isEmpty(template)) {
- return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
- }
- IotTemplate temp = iotTemplateDao.findTopByName(param.getName());
- if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
- return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
- }
- } else {
- template = new IotTemplate();
- IotTemplate temp = iotTemplateDao.findTopByName(param.getName());
- if (ObjectUtils.isNotEmpty(temp)) {
- return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
- }
- }
- BeanUtils.copyProperties(param, template);
- iotTemplateDao.save(template);
- return ResultContent.buildSuccess();
- }
- public ResultContent deleteTemplate(String id) {
- IotTemplate template = iotTemplateDao.findTopById(id);
- if (ObjectUtils.isEmpty(template)) {
- return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
- }
- iotTemplateDao.delete(template);
- return ResultContent.buildSuccess();
- }
- public ResultContent<IotTemplateModel> getIotTemplate(String id) {
- IotTemplate template = iotTemplateDao.findTopById(id);
- if (ObjectUtils.isEmpty(template)) {
- return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
- }
- return ResultContent.buildSuccess(toModel(template));
- }
- public ResultContent<Page<IotTemplateModel>> pageTemplate(Pageable pageable, IotTemplateSearch param) {
- initSearchParam(param);
- Page<IotTemplate> page = iotTemplateDao.page(pageable, param);
- return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
- }
- //----------------------------- 模版 end------------------------------
- //----------------------------- Topic start----------------------------
- public ResultContent addIotTopic(IotTopicParam param) {
- initDefaultUser(param);
- String iotTemplateId = param.getIotTemplateId();
- IotTemplate iotTemplate = iotTemplateDao.findTopById(iotTemplateId);
- if (ObjectUtils.isEmpty(iotTemplate)) {
- return ResultContent.buildFail(String.format("模版不存在:%s", iotTemplateId));
- }
- IotTopic entity = null;
- if (StringUtils.isNotEmpty(param.getId())) {
- entity = iotTopicDao.findTopById(param.getId());
- if (ObjectUtils.isEmpty(entity)) {
- return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
- }
- IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getTopic(), iotTemplate);
- if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
- return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getTopic()));
- }
- } else {
- entity = new IotTopic();
- IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getTopic(), iotTemplate);
- if (ObjectUtils.isNotEmpty(temp)) {
- return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getTopic()));
- }
- }
- BeanUtils.copyProperties(param, entity);
- entity.setIotTemplate(iotTemplate);
- iotTopicDao.save(entity);
- return ResultContent.buildSuccess();
- }
- public ResultContent deleteIotTopic(String id) {
- IotTopic entity = iotTopicDao.findTopById(id);
- if (ObjectUtils.isEmpty(entity)) {
- return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
- }
- iotTopicDao.delete(entity);
- return ResultContent.buildSuccess();
- }
- public ResultContent<IotTopicModel> getIotTopic(String id) {
- IotTopic entity = iotTopicDao.findTopById(id);
- if (ObjectUtils.isEmpty(entity)) {
- return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
- }
- return ResultContent.buildSuccess(toModel(entity));
- }
- /**
- * 分页查询 模版的Topic管理数据
- * @param pageable
- * @param param
- * @return
- */
- public ResultContent<Page<IotTopicModel>> pageIotTopic(Pageable pageable, IotTopicSearch param) {
- initSearchParam(param);
- Page<IotTopic> page = iotTopicDao.page(pageable, param);
- return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
- }
- //----------------------------- Topic end------------------------------
- //----------------------------- 物模型 start----------------------------
- public ResultContent addIotMain(IotMainParam param) {
- initDefaultUser(param);
- String iotTemplateId = param.getIotTemplateId();
- IotTemplate iotTemplate = iotTemplateDao.findTopById(iotTemplateId);
- if (ObjectUtils.isEmpty(iotTemplate)) {
- return ResultContent.buildFail(String.format("模版不存在:%s", iotTemplateId));
- }
- IotMain entity = null;
- if (StringUtils.isNotEmpty(param.getId())) {
- entity = iotMainDao.findTopById(param.getId());
- if (ObjectUtils.isEmpty(entity)) {
- return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
- }
- IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getName(), iotTemplate);
- if (ObjectUtils.isNotEmpty(temp) && !temp.getId().equals(param.getId())) {
- return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
- }
- } else {
- entity = new IotMain();
- IotTopic temp = iotTopicDao.findTopByTopicAndIotTemplate(param.getName(), iotTemplate);
- if (ObjectUtils.isNotEmpty(temp)) {
- return ResultContent.buildFail(String.format(ResultMessage.NAME_EXIT, param.getName()));
- }
- }
- BeanUtils.copyProperties(param, entity);
- entity.setIotTemplate(iotTemplate);
- iotMainDao.save(entity);
- return ResultContent.buildSuccess();
- }
- public ResultContent deleteIotMain(String id) {
- IotMain entity = iotMainDao.findTopById(id);
- if (ObjectUtils.isEmpty(entity)) {
- return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
- }
- iotMainDao.delete(entity);
- return ResultContent.buildSuccess();
- }
- public ResultContent<IotMainModel> getIotMain(String id) {
- IotMain entity = iotMainDao.findTopById(id);
- if (ObjectUtils.isEmpty(entity)) {
- return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
- }
- return ResultContent.buildSuccess(toModel(entity));
- }
- public ResultContent<Page<IotMainModel>> pageIotMain(Pageable pageable, IotMainSearch param) {
- initSearchParam(param);
- Page<IotMain> page = iotMainDao.page(pageable, param);
- return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
- }
- /**
- * 得到模版物模型的顶用数据
- *
- * @param templateId
- * @return
- */
- public ResultContent<List<IotMainModel>> getTemplateIotMainList(String templateId) {
- List<IotMain> list = iotMainDao.findByIotTemplateOrderByCreateTimeAsc(IotTemplate.build(templateId));
- List<IotMainModel> models = new ArrayList<>();
- if (ObjectUtils.isNotEmpty(list)) {
- models = list.stream().map(this::toModel).collect(Collectors.toList());
- }
- return ResultContent.buildSuccess(models);
- }
- //----------------------------- 物模型 end------------------------------
- public IotTemplateModel toModel(IotTemplate entity) {
- IotTemplateModel model = null;
- if (ObjectUtils.isNotEmpty(entity)) {
- model = new IotTemplateModel();
- BeanUtils.copyProperties(entity, model);
- }
- return model;
- }
- public IotTopicModel toModel(IotTopic entity) {
- IotTopicModel model = null;
- if (ObjectUtils.isNotEmpty(entity)) {
- model = new IotTopicModel();
- BeanUtils.copyProperties(entity, model);
- }
- return model;
- }
- public IotMainModel toModel(IotMain entity) {
- IotMainModel model = null;
- if (ObjectUtils.isNotEmpty(entity)) {
- model = new IotMainModel();
- BeanUtils.copyProperties(entity, model);
- }
- return model;
- }
- }
|