Commit f15db100 authored by huangcb's avatar huangcb

增加物模型add/list功能

parent 29485f98
......@@ -112,7 +112,7 @@ public class ReqUtils {
public static void checkParamsNotBlank(JSONObject reqJson, String[] params) throws EException {
for (int i = 0; i < params.length; i++) {
if (StringUtils.isBlank(reqJson.getString(params[i]))) {
throw new EException(ECode.PARAM_ERROR.code(), "参数"+params[i]+"不能为空");
throw new EException(ECode.PARAM_ERROR.code(), "参数["+params[i]+"]不能为空");
}
}
}
......
package com.esv.datacenter.iot.module.omodel.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.esv.datacenter.iot.common.exception.EException;
import com.esv.datacenter.iot.common.response.EResponse;
import com.esv.datacenter.iot.common.util.ReqUtils;
import com.esv.datacenter.iot.common.validator.groups.ValidatorInsert;
import com.esv.datacenter.iot.common.validator.groups.ValidatorList;
import com.esv.datacenter.iot.module.omodel.form.ModelPropertyForm;
import com.esv.datacenter.iot.module.omodel.form.ObjectModelForm;
import com.esv.datacenter.iot.module.omodel.service.ObjectModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @description: 物模型Controller
* @author: huangchaobin@esvtek.com
* @createTime: 2020/07/31 14:52
* @version:1.0
*/
@Slf4j
@RestController
@RequestMapping("/model")
@Validated
public class ObjectModelController {
private ObjectModelService objectModelService;
public ObjectModelController(ObjectModelService objectModelService) {
this.objectModelService = objectModelService;
}
/**
* @description 创建模型
* @param form:
* @return com.esv.datacenter.iot.common.response.EResponse
* @author huangChaobin@esvtek.com
* @createTime 2020/07/31 14:53
**/
@PostMapping("/add")
public EResponse add(@RequestBody @Validated(ValidatorInsert.class) ObjectModelForm form) throws EException {
// 参数校验:模型属性
String[] notBlandParams = new String[]{"propertyCode", "propertyName", "propertyType"};
List<ModelPropertyForm> propertyList = form.getPropertyList();
propertyList.forEach(property -> {
ReqUtils.checkParamsNotBlank(JSON.parseObject(property.toString()), notBlandParams);
});
Long id = objectModelService.createObjectModel(form);
JSONObject data = new JSONObject();
data.put("id", id);
return EResponse.ok(data);
}
/**
* @description 分页查询模型列表
* @param form:
* @return com.esv.datacenter.iot.common.response.EResponse
* @author huangChaobin@esvtek.com
* @createTime 2020/07/31 16:31
**/
@PostMapping("/list")
public EResponse list(@RequestBody @Validated(ValidatorList.class) ObjectModelForm form) throws EException {
return EResponse.ok(objectModelService.getModel4Page(form));
}
}
package com.esv.datacenter.iot.module.omodel.dao;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.esv.datacenter.iot.module.omodel.entity.ObjectModelEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.esv.datacenter.iot.module.omodel.form.ObjectModelForm;
import org.apache.ibatis.annotations.Mapper;
/**
......@@ -13,5 +15,15 @@ import org.apache.ibatis.annotations.Mapper;
*/
@Mapper
public interface ObjectModelDao extends BaseMapper<ObjectModelEntity> {
/**
* @description 分页查询
* @param page:
* @param queryObj:
* @return com.baomidou.mybatisplus.core.metadata.IPage
* @author huangChaobin@esvtek.com
* @createTime 2020/07/31 16:24
**/
IPage select4Page(IPage page, ObjectModelForm queryObj);
}
package com.esv.datacenter.iot.module.omodel.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
......@@ -20,10 +22,20 @@ public class InstanceTopicEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*
*/
@TableId
private Long id;
/**
* 租户ID
*/
@TableField(fill = FieldFill.INSERT)
private Long tenantId;
/**
* 部门ID
*/
@TableField(fill = FieldFill.INSERT)
private Long departmentId;
/**
* 模型ID
*/
......
......@@ -23,6 +23,16 @@ public class ObjectModelEntity implements Serializable {
*/
@TableId
private Long id;
/**
* 租户ID
*/
@TableField(fill = FieldFill.INSERT)
private Long tenantId;
/**
* 部门ID
*/
@TableField(fill = FieldFill.INSERT)
private Long departmentId;
/**
* 模型名称
*/
......
......@@ -19,10 +19,20 @@ public class ObjectModelInstanceEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*
*/
@TableId
private Long id;
/**
* 租户ID
*/
@TableField(fill = FieldFill.INSERT)
private Long tenantId;
/**
* 部门ID
*/
@TableField(fill = FieldFill.INSERT)
private Long departmentId;
/**
* 模型ID
*/
......
......@@ -19,10 +19,20 @@ public class ObjectModelPropertyEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*
*/
@TableId
private Long id;
/**
* 租户ID
*/
@TableField(fill = FieldFill.INSERT)
private Long tenantId;
/**
* 部门ID
*/
@TableField(fill = FieldFill.INSERT)
private Long departmentId;
/**
* 模型ID
*/
......
package com.esv.datacenter.iot.module.omodel.form;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
/**
* @description:
* @author: huangchaobin@esvtek.com
* @createTime: 2020/07/31 14:44
* @version:1.0
*/
@Data
public class ModelInstanceForm {
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
}
package com.esv.datacenter.iot.module.omodel.form;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
/**
* @description:
* @author: huangchaobin@esvtek.com
* @createTime: 2020/07/31 14:45
* @version:1.0
*/
@Data
public class ModelPropertyForm {
/**
* 属性代码
*/
private String propertyCode;
/**
* 属性名称
*/
private String propertyName;
/**
* 属性类型(字典表)
*/
private Integer propertyType;
/**
* 属性默认值
*/
private String propertyDefaultValue;
/**
* 属性单位
*/
private String propertyUnit;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
}
package com.esv.datacenter.iot.module.omodel.form;
import com.esv.datacenter.iot.common.validator.groups.ValidatorInsert;
import com.esv.datacenter.iot.common.validator.groups.ValidatorList;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* @description:
......@@ -24,7 +28,7 @@ public class ObjectModelForm {
/**
* 模型名称
*/
@Length(max = 50, message = "[模型名称]长度不合法", groups = {ValidatorInsert.class})
@Length(max = 50, message = "[模型名称]长度不合法", groups = {ValidatorInsert.class, ValidatorList.class})
@NotBlank(message = "[模型名称]不能为空", groups = {ValidatorInsert.class})
private String name;
/**
......@@ -34,6 +38,25 @@ public class ObjectModelForm {
@NotBlank(message = "[模型描述]不能为空", groups = {ValidatorInsert.class})
private String description;
/**
* 模型属性
*/
@NotNull(message = "[模型属性]不能为空", groups = {ValidatorInsert.class})
private List<ModelPropertyForm> propertyList;
/**
* 页码
**/
@Range(min = 1, max = 65535, message = "无效的pageNum", groups = {ValidatorList.class})
@NotNull(message = "参数pageNum不能为空", groups = {ValidatorList.class})
private Integer pageNum;
/**
* 每页记录条数
**/
@Range(min = 1, max = 100, message = "pageSize", groups = {ValidatorList.class})
@NotNull(message = "参数pageSize不能为空", groups = {ValidatorList.class})
private Integer pageSize;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
......
package com.esv.datacenter.iot.module.omodel.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.esv.datacenter.iot.common.vo.PageResultVO;
import com.esv.datacenter.iot.module.omodel.entity.ObjectModelEntity;
import com.esv.datacenter.iot.module.omodel.form.ObjectModelForm;
......@@ -22,5 +23,14 @@ public interface ObjectModelService extends IService<ObjectModelEntity> {
**/
Long createObjectModel(ObjectModelForm form);
/**
* @description 分页查询模型列表
* @param queryObj:
* @return com.esv.datacenter.iot.common.vo.PageResultVO
* @author huangChaobin@esvtek.com
* @createTime 2020/07/31 16:26
**/
PageResultVO getModel4Page(ObjectModelForm queryObj);
}
package com.esv.datacenter.iot.module.omodel.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.esv.datacenter.iot.common.exception.EException;
import com.esv.datacenter.iot.common.response.ECode;
import com.esv.datacenter.iot.common.util.ReqUtils;
import com.esv.datacenter.iot.common.vo.PageResultVO;
import com.esv.datacenter.iot.module.omodel.dao.ObjectModelDao;
import com.esv.datacenter.iot.module.omodel.entity.ObjectModelEntity;
import com.esv.datacenter.iot.module.omodel.entity.ObjectModelPropertyEntity;
import com.esv.datacenter.iot.module.omodel.form.ModelPropertyForm;
import com.esv.datacenter.iot.module.omodel.form.ObjectModelForm;
import com.esv.datacenter.iot.module.omodel.service.ObjectModelPropertyService;
import com.esv.datacenter.iot.module.omodel.service.ObjectModelService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@Service("objectModelService")
public class ObjectModelServiceImpl extends ServiceImpl<ObjectModelDao, ObjectModelEntity> implements ObjectModelService {
private ObjectModelPropertyService objectModelPropertyService;
public ObjectModelServiceImpl(ObjectModelPropertyService objectModelPropertyService) {
this.objectModelPropertyService = objectModelPropertyService;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Long createObjectModel(ObjectModelForm form) {
return null;
// 1.创建模型
// 模型名称校验:不能重复
int modelCount = this.getBaseMapper().selectCount(new LambdaQueryWrapper<ObjectModelEntity>()
.eq(ObjectModelEntity::getName, form.getName())
.eq(ObjectModelEntity::getTenantId, ReqUtils.getRequestTenantId()));
if (0 < modelCount) {
throw new EException(ECode.PARAM_ERROR.code(), "模型[" + form.getName() + "]已存在");
}
ObjectModelEntity modelEntity = new ObjectModelEntity();
modelEntity.setName(form.getName());
modelEntity.setDescription(form.getDescription());
this.getBaseMapper().insert(modelEntity);
Long modelId = modelEntity.getId();
// 2.添加模型字段
List<ModelPropertyForm> propertyList = form.getPropertyList();
List<ObjectModelPropertyEntity> modelPropertyEntityList = new ArrayList<>();
for (ModelPropertyForm propertyForm : propertyList) {
ObjectModelPropertyEntity propertyEntity = new ObjectModelPropertyEntity();
BeanUtils.copyProperties(propertyForm, propertyEntity);
propertyEntity.setModelId(modelId);
modelPropertyEntityList.add(propertyEntity);
}
this.objectModelPropertyService.saveBatch(modelPropertyEntityList);
for (ObjectModelPropertyEntity modelPropertyEntity : modelPropertyEntityList) {
ObjectModelPropertyEntity entity = new ObjectModelPropertyEntity();
entity.setId(modelPropertyEntity.getId());
entity.setSortNum(modelPropertyEntity.getId());
this.objectModelPropertyService.updateById(entity);
}
// 3.创建时序数据库表
return modelId;
}
}
\ No newline at end of file
@Override
public PageResultVO getModel4Page(ObjectModelForm queryObj) {
IPage<ObjectModelEntity> page = new Page<>(queryObj.getPageNum(), queryObj.getPageSize());
this.baseMapper.select4Page(page, queryObj);
return new PageResultVO(page, page.getRecords());
}
}
package com.esv.datacenter.iot.module.omodel.vo;
import lombok.Data;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
/**
* @description:
* @author: huangchaobin@esvtek.com
* @createTime: 2020/07/31 16:12
* @version:1.0
*/
@Data
public class ObjectModelVO {
/**
*
*/
private Long id;
/**
* 模型名称
*/
private String name;
/**
* 模型描述
*/
private String description;
/**
* 创建者
*/
private String createUser;
/**
* 修改者
*/
private String updateUser;
/**
* 创建时间
*/
private Long createTime;
/**
* 修改时间
*/
private Long updateTime;
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
}
......@@ -48,6 +48,11 @@
<appender-ref ref="FILE_APPENDER" />
</logger>
<logger name="com.esv.datacenter.iot.config.mybatis" level="INFO" additivity="false">
<appender-ref ref="CONSOLE_APPENDER" />
<appender-ref ref="FILE_APPENDER" />
</logger>
<!--控制台和日志文件输出级别-->
<root level="INFO" additivity="false">
<appender-ref ref="CONSOLE_APPENDER" />
......
......@@ -6,6 +6,8 @@
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.esv.datacenter.iot.module.omodel.entity.InstanceTopicEntity" id="instanceTopicMap">
<result property="id" column="id"/>
<result property="tenantId" column="tenant_id"/>
<result property="departmentId" column="department_id"/>
<result property="modelId" column="model_id"/>
<result property="instanceId" column="instance_id"/>
<result property="topic" column="topic"/>
......
......@@ -6,6 +6,8 @@
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.esv.datacenter.iot.module.omodel.entity.ObjectModelEntity" id="objectModelMap">
<result property="id" column="id"/>
<result property="tenantId" column="tenant_id"/>
<result property="departmentId" column="department_id"/>
<result property="name" column="name"/>
<result property="description" column="description"/>
<result property="deleted" column="deleted"/>
......@@ -15,5 +17,16 @@
<result property="updateTime" column="update_time"/>
</resultMap>
<!-- 分页查询 -->
<select id="select4Page" parameterType="com.esv.datacenter.iot.module.omodel.form.ObjectModelForm"
resultType="com.esv.datacenter.iot.module.omodel.entity.ObjectModelEntity">
select *
from base_object_model
where 1=1
<if test="queryObj.name != null">
and name = #{queryObj.name}
</if>
ORDER BY name ASC
</select>
</mapper>
\ No newline at end of file
......@@ -6,6 +6,8 @@
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.esv.datacenter.iot.module.omodel.entity.ObjectModelInstanceEntity" id="objectModelInstanceMap">
<result property="id" column="id"/>
<result property="tenantId" column="tenant_id"/>
<result property="departmentId" column="department_id"/>
<result property="modelId" column="model_id"/>
<result property="instanceName" column="instance_name"/>
<result property="description" column="description"/>
......
......@@ -6,6 +6,8 @@
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.esv.datacenter.iot.module.omodel.entity.ObjectModelPropertyEntity" id="objectModelPropertyMap">
<result property="id" column="id"/>
<result property="tenantId" column="tenant_id"/>
<result property="departmentId" column="department_id"/>
<result property="modelId" column="model_id"/>
<result property="propertyCode" column="property_code"/>
<result property="propertyName" column="property_name"/>
......
package com.esv.datacenter.iot;
import com.esv.datacenter.iot.common.constants.CommonConstants;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.UUID;
/**
* @description:
* @author: 黄朝斌
......@@ -30,6 +36,7 @@ public class BaseTestController {
@Before
public void before() {
this.initLogTraceId();
log.info("=================================== Test Start ===================================");
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
TEST_START_TIME = System.currentTimeMillis();
......@@ -42,4 +49,27 @@ public class BaseTestController {
log.info("=================================== Test End ===================================");
}
public HttpHeaders getDefaultHttpHeaders() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("gateway_traceid", UUID.randomUUID().toString().replace("-", ""));
httpHeaders.add("esv_system", "cc");
httpHeaders.add("esv_data_perm", "0");
httpHeaders.add("esv_tenant", "1");
httpHeaders.add("esv_department", "1");
httpHeaders.add("esv_department_children", "1,2,3");
httpHeaders.add("esv_user", "1");
httpHeaders.add("esv_account", "admin");
httpHeaders.add("Source-Type", "1");
httpHeaders.add(CommonConstants.REQ_SOURCE_TYPE_KEY, CommonConstants.REQ_SOURCE_TYPE_WEB);
return httpHeaders;
}
private void initLogTraceId() {
String traceId = MDC.get(CommonConstants.LOG_TRACE_ID);
if (StringUtils.isBlank(traceId)) {
traceId = UUID.randomUUID().toString().replace("-", "");
MDC.put(CommonConstants.LOG_TRACE_ID, traceId);
}
}
}
package com.esv.datacenter.iot.module.omodel.controller;
import com.alibaba.fastjson.JSONObject;
import com.esv.datacenter.iot.BaseTestController;
import com.esv.datacenter.iot.common.response.ECode;
import com.esv.datacenter.iot.module.omodel.form.ModelPropertyForm;
import com.esv.datacenter.iot.module.omodel.form.ObjectModelForm;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
* @description:
* @author: huangchaobin@esvtek.com
* @createTime: 2020/07/31 14:54
* @version:1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Transactional
@Rollback(false)
public class ObjectModelControllerTest extends BaseTestController {
/**
* 新增个人承运商
**/
@Test
public void a1_add_success_test() throws Exception {
String url = "/model/add";
// 构造数据
ObjectModelForm form = new ObjectModelForm();
form.setName("测试模型");
form.setDescription("这是一个测试模型");
List<ModelPropertyForm> propertyList = new ArrayList<>();
ModelPropertyForm modelPropertyForm = new ModelPropertyForm();
modelPropertyForm.setPropertyCode("id");
modelPropertyForm.setPropertyName("主键");
modelPropertyForm.setPropertyType(1);
propertyList.add(modelPropertyForm);
form.setPropertyList(propertyList);
MvcResult mvcResult = this.getMockMvc().perform(MockMvcRequestBuilders.post(url)
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.headers(this.getDefaultHttpHeaders())
.content(form.toString()))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
String responseStr = mvcResult.getResponse().getContentAsString();
log.info(responseStr);
JSONObject result = JSONObject.parseObject(responseStr);
Assert.assertEquals(ECode.SUCCESS.code(), result.getIntValue("code"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment