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 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> pageTemplate(Pageable pageable, IotTemplateSearch param) { initSearchParam(param); Page 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 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> pageIotTopic(Pageable pageable, IotTopicSearch param) { initSearchParam(param); Page 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 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> pageIotMain(Pageable pageable, IotMainSearch param) { initSearchParam(param); Page page = iotMainDao.page(pageable, param); return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel)); } /** * 得到模版物模型的顶用数据 * * @param templateId * @return */ public ResultContent> getTemplateIotMainList(String templateId) { List list = iotMainDao.findByIotTemplateOrderByCreateTimeAsc(IotTemplate.build(templateId)); List 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; } }