TRX преди 1 година
родител
ревизия
5f452f9b6c

+ 90 - 21
src/main/java/com/zswl/dataservice/controller/iot/IotController.java

@@ -1,5 +1,6 @@
 package com.zswl.dataservice.controller.iot;
 
+import com.zswl.dataservice.model.iot.*;
 import com.zswl.dataservice.model.mqtt.DeviceInfoAddParam;
 import com.zswl.dataservice.model.mqtt.DeviceInfoModel;
 import com.zswl.dataservice.model.mqtt.DeviceInfoSearchParam;
@@ -33,39 +34,107 @@ import org.springframework.web.bind.annotation.RestController;
 @Tag(name = "模版-物模型管理")
 public class IotController {
 
-    @Autowired
-    DeviceInfoService deviceInfoService;
-
     @Autowired
     IotServiceImpl iotService;
 
+    //-------------------------- 模版 start------------------------------
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "添加-编辑模版")
+    @RequestMapping(value = "addIotTemplate", method = {RequestMethod.POST})
+    public ResultContent addIotTemplate(@RequestBody IotTemplateParam param) {
+        Assert.hasText(param.getName(), "name不能为空");
+        return iotService.addIotTemplate(param);
+    }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "模版列表-分页查询")
+    @RequestMapping(value = {"pageTemplate"}, method = {RequestMethod.POST})
+    public ResultContent<Page<IotTemplateModel>> pageTemplate(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
+            @Parameter(required = false) IotTemplateSearch param) {
+        return iotService.pageTemplate(pageable, param);
+    }
+
     @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
-    @Operation(summary = "添加设备")
-    @RequestMapping(value = "addDeviceInfo", method = {RequestMethod.POST})
-    public ResultContent addDeviceInfo(@RequestBody DeviceInfoAddParam param) {
-        Assert.hasText(param.getDeviceId(), "设备ID不能为空");
-        Assert.hasText(param.getDeviceName(), "设备名称不能为空");
-        return deviceInfoService.addDeviceInfo(param);
+    @Operation(summary = "删除模版")
+    @RequestMapping(value = "deleteTemplate", method = {RequestMethod.GET})
+    public ResultContent deleteTemplate(@Parameter(name = "id", description = "数据id") String id) {
+        return iotService.deleteTemplate(id);
     }
 
     @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
-    @Operation(summary = "设备列表-分页查询")
-    @RequestMapping(value = {"pageActivity"}, method = {RequestMethod.POST})
-    public ResultContent<Page<DeviceInfoModel>> pageActivity(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) DeviceInfoSearchParam param) {
-        return deviceInfoService.pageDevice(pageable, param);
+    @Operation(summary = "查询模版")
+    @RequestMapping(value = "getIotTemplate", method = {RequestMethod.GET})
+    public ResultContent<IotTemplateModel> getIotTemplate(@Parameter(name = "id", description = "数据id") String id) {
+        return iotService.getIotTemplate(id);
+    }
+
+    //-------------------------- 模版 end--------------------------------
+
+    //-------------------------- Topic start------------------------------
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "添加-编辑Topic")
+    @RequestMapping(value = "addIotTopic", method = {RequestMethod.POST})
+    public ResultContent addIotTopic(@RequestBody IotTopicParam param) {
+        Assert.hasText(param.getTopic(), "topic不能为空");
+        return iotService.addIotTopic(param);
     }
 
     @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
-    @Operation(summary = "删除设备")
-    @RequestMapping(value = "deleteDeviceInfo", method = {RequestMethod.GET})
-    public ResultContent deleteDeviceInfo(String deviceId) {
-        return deviceInfoService.deleteDeviceInfo(deviceId);
+    @Operation(summary = "Topic列表-分页查询")
+    @RequestMapping(value = {"pageIotTopic"}, method = {RequestMethod.POST})
+    public ResultContent<Page<IotTopicModel>> pageIotTopic(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
+            @Parameter(required = false) IotTopicSearch param) {
+        return iotService.pageIotTopic(pageable, param);
     }
 
     @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
-    @Operation(summary = "查询设备")
-    @RequestMapping(value = "getDeviceById", method = {RequestMethod.GET})
-    public ResultContent<DeviceInfoModel> getDeviceById(String deviceId) {
-        return deviceInfoService.getDeviceById(deviceId);
+    @Operation(summary = "删除Topic")
+    @RequestMapping(value = "deleteIotTopic", method = {RequestMethod.GET})
+    public ResultContent deleteIotTopic(@Parameter(name = "id", description = "数据id") String id) {
+        return iotService.deleteIotTopic(id);
     }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "查询Topic")
+    @RequestMapping(value = "getIotTopic", method = {RequestMethod.GET})
+    public ResultContent<IotTopicModel> getIotTopic(@Parameter(name = "id", description = "数据id") String id) {
+        return iotService.getIotTopic(id);
+    }
+
+    //-------------------------- Topic end--------------------------------
+
+    //-------------------------- 物模型 start------------------------------
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "添加-编辑物模型")
+    @RequestMapping(value = "addIotMain", method = {RequestMethod.POST})
+    public ResultContent addIotMain(@RequestBody IotMainParam param) {
+        Assert.hasText(param.getName(), "name不能为空");
+        return iotService.addIotMain(param);
+    }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "物模型列表-分页查询")
+    @RequestMapping(value = {"pageIotMain"}, method = {RequestMethod.POST})
+    public ResultContent<Page<IotMainModel>> pageIotMain(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
+            @Parameter(required = false) IotMainSearch param) {
+        return iotService.pageIotMain(pageable, param);
+    }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "删除物模型")
+    @RequestMapping(value = "deleteIotMain", method = {RequestMethod.GET})
+    public ResultContent deleteIotMain(@Parameter(name = "id", description = "数据id") String id) {
+        return iotService.deleteIotMain(id);
+    }
+
+    @Parameter(in = ParameterIn.HEADER, name = "accessToken", required = false, description = "accessToken")
+    @Operation(summary = "查询物模型")
+    @RequestMapping(value = "getIotMain", method = {RequestMethod.GET})
+    public ResultContent<IotMainModel> getIotMain(@Parameter(name = "id", description = "数据id") String id) {
+        return iotService.getIotMain(id);
+    }
+
+    //-------------------------- 物模型 end--------------------------------
+
+
 }

+ 3 - 0
src/main/java/com/zswl/dataservice/dao/iot/IotTopicDao.java

@@ -4,6 +4,7 @@ import com.zswl.dataservice.dao.MongoDao;
 import com.zswl.dataservice.dao.iot.extend.IotTopicDaoExtend;
 import com.zswl.dataservice.dao.mqtt.extend.DeviceInfoDaoExtend;
 import com.zswl.dataservice.domain.iot.IotMain;
+import com.zswl.dataservice.domain.iot.IotTemplate;
 import com.zswl.dataservice.domain.iot.IotTopic;
 
 /**
@@ -14,4 +15,6 @@ public interface IotTopicDao extends MongoDao<IotTopic>, IotTopicDaoExtend {
 
     IotTopic findTopById(String id);
 
+    IotTopic findTopByTopicAndIotTemplate(String topic, IotTemplate iotTemplate);
+
 }

+ 13 - 0
src/main/java/com/zswl/dataservice/dataConfig/ResultMessage.java

@@ -0,0 +1,13 @@
+package com.zswl.dataservice.dataConfig;
+
+/**
+ * @author TRX
+ * @date 2024/6/20
+ */
+public class ResultMessage {
+
+    public static final String DATA_NOT_EXIT = "数据不存在:%s";
+
+    public static final String NAME_EXIT = "名称存在:%s";
+
+}

+ 45 - 0
src/main/java/com/zswl/dataservice/service/base/SuperService.java

@@ -2,9 +2,18 @@ package com.zswl.dataservice.service.base;
 
 import com.zswl.dataservice.auth.UserContext;
 import com.zswl.dataservice.dao.UserDao;
+import com.zswl.dataservice.domain.base.SuperEntity;
 import com.zswl.dataservice.domain.user.User;
+import com.zswl.dataservice.model.baseParam.SuperParam;
+import com.zswl.dataservice.model.baseParam.SuperSearchParam;
+import com.zswl.dataservice.utils.DateUtils;
 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.util.Assert;
+
+import java.util.List;
 
 /**
  * @author TRX
@@ -40,4 +49,40 @@ public abstract class SuperService {
         }
         return null;
     }
+
+    public void initDefaultUser(SuperParam param) {
+        if (param.getId() != null && param.getId().trim().equals("")) {
+            param.setId(null);
+        }
+        User user = getCrrentUser();
+        if (user != null && StringUtils.isEmpty(param.getCreateUserId())) {
+            param.setCreateUserId(user.getId());
+            param.setCreateUserName(user.getUserName());
+            param.setCreatePhone(user.getPhone());
+        }
+    }
+
+    public void initEntity(SuperEntity param) {
+        if (param.getId() != null && param.getId().trim().equals("")) {
+            param.setId(null);
+        }
+        User user = getCrrentUser();
+        if (user != null && StringUtils.isEmpty(param.getCreateUserId())) {
+            param.setCreateUserId(user.getId());
+            param.setCreateUserName(user.getUserName());
+            param.setCreatePhone(user.getPhone());
+        }
+    }
+
+    public void initSearchParam(SuperSearchParam param) {
+        List<Long> times = param.getTimes();
+        if (ObjectUtils.isNotEmpty(times) && times.size() == 2) {
+            Long startTime = times.get(0);
+            startTime = DateUtils.getDayStartTime(startTime);
+            Long endTime = times.get(1);
+            endTime = DateUtils.getDayEndTime(endTime);
+            param.setStartTime(startTime);
+            param.setEndTime(endTime);
+        }
+    }
 }

+ 199 - 2
src/main/java/com/zswl/dataservice/service/iot/IotServiceImpl.java

@@ -4,12 +4,25 @@ 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.model.iot.IotTemplateParam;
+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;
 
 /**
@@ -32,9 +45,193 @@ public class IotServiceImpl extends SuperService {
     @Autowired
     UserDao userDao;
 
-    public ResultContent addIotTemplate(IotTemplateParam iotTemplate) {
+    //----------------------------- 模版 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_EXIT, 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_EXIT, 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_EXIT, 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_EXIT, 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_EXIT, 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_EXIT, id));
+        }
+        return ResultContent.buildSuccess(toModel(entity));
+    }
+
+    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_EXIT, 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_EXIT, 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_EXIT, 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));
+    }
+    //----------------------------- 物模型 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;
+    }
+
+
 }