소스 검색

增加设备管理接口

chenwen 2 년 전
부모
커밋
8ca618a435

+ 3 - 0
src/main/java/com/hb/proj/allconfig/SpringMvcConfigurer.java

@@ -22,6 +22,8 @@ public class SpringMvcConfigurer implements WebMvcConfigurer {
 	@Value("${api.filter.exclude}") 
 	private String excludePath;
 	
+	private static final long MAX_AGE=24*60*60;  //跨域预请求最大有效期(有效期内【只对同一请求?】不再预请求)
+	
 	/**
 	 * 静态资源的处理
 	 */
@@ -61,6 +63,7 @@ public class SpringMvcConfigurer implements WebMvcConfigurer {
         configuration.setAllowedOrigins(Arrays.asList("*"));
         configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "HEAD", "DELETE", "OPTION"));
         configuration.setAllowedHeaders(Arrays.asList("*"));
+        configuration.setMaxAge(MAX_AGE);
         UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
         source.registerCorsConfiguration("/**", configuration);
 

+ 106 - 0
src/main/java/com/hb/proj/base/controller/DeviceController.java

@@ -0,0 +1,106 @@
+package com.hb.proj.base.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import com.hb.proj.allconfig.AccessToken;
+import com.hb.proj.allconfig.SysLog;
+import com.hb.proj.base.service.DeviceService;
+import com.hb.proj.model.DevicePO;
+import com.hb.proj.model.DeviceVO;
+import com.hb.proj.utils.RequestParams;
+import com.hb.proj.utils.RespVO;
+import com.hb.proj.utils.RespVOBuilder;
+import com.hb.xframework.dao.util.PageModel;
+
+import jakarta.validation.constraints.NotBlank;
+
+@RestController
+@RequestMapping("/base/device")
+@Validated
+public class DeviceController {
+
+	@Autowired
+	private DeviceService  service;
+	
+	
+	
+	/**
+	 * 查询设备列表
+	 * @apiNote 查询参数类型列表
+	 * @param params  查询参数
+	 * @param pageNum  页码
+	 * @param pageSize 每页记录数
+	 * @return
+	 */
+	@RequestMapping("/query")
+	public RespVO<PageModel<DeviceVO>> query(RequestParams params,
+			@RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum,
+			@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
+		return RespVOBuilder.ok(service.query(params.getObjectMap(), pageNum, pageSize));
+	}
+	
+	
+	/**
+	 * 获得单个设备数据
+	 * @param deviceId 设备记录id
+	 * @return
+	 */
+	@RequestMapping("/get")
+	public RespVO<DeviceVO> get(@NotBlank(message = "缺少记录号") String deviceId){
+		return RespVOBuilder.ok(service.get(deviceId));
+	}
+	
+	
+	/**
+	 * 删除单个设备
+	 * @param id  待删除记录id
+	 * @return
+	 */
+	@RequestMapping("/delete")
+	@SysLog("删除设备信息:{arg0}")
+	public RespVO<Object> delete(String deviceId){
+		service.delete(deviceId);
+		return RespVOBuilder.ok();
+	}
+	
+
+	/**
+	 * 增加新设备
+	 * @param device 设备实体对象
+	 * @param token  当前登录实体
+	 * @return
+	 */
+	@RequestMapping("/add")
+	public RespVO<String> add(DevicePO device,AccessToken token){
+		 if(service.existDevice(device.getDeviceCode(),null)) {
+			 return RespVOBuilder.error("该设备编码已存在,勿重复添加");
+		 }
+		 device.setModifyBy(token.getTokenId());
+		 return RespVOBuilder.ok(service.add(device));
+		
+	}
+	
+	
+	/**
+	 * 更新设备信息
+	 * @param device 设备实体对象
+	 * @param token  当前登录实体
+	 * @return
+	 */
+	@RequestMapping("/update")
+	public RespVO<Object> update(DevicePO device,AccessToken token){
+		if(service.existDevice(device.getDeviceCode(), device.getDeviceId())) {
+			 return RespVOBuilder.error("该设备编码已存在,勿重复添加");
+		 }
+		device.setModifyBy(token.getTokenId());
+		service.update(device);
+		
+		return RespVOBuilder.ok();
+	  
+		
+	}
+}

+ 93 - 0
src/main/java/com/hb/proj/base/service/DeviceService.java

@@ -0,0 +1,93 @@
+package com.hb.proj.base.service;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.hb.proj.model.DevicePO;
+import com.hb.proj.model.DeviceVO;
+import com.hb.xframework.dao.core.SpringJdbcDAO;
+import com.hb.xframework.dao.util.PageModel;
+import com.hb.xframework.dao.util.UUIDHexGenerator;
+
+@Service
+public class DeviceService {
+
+	@Autowired
+	private SpringJdbcDAO  dao;
+	
+	
+	
+	
+	public PageModel<DeviceVO> query(Map<String,Object> args,int pageNo,int pageSize){
+		List<Object> sqlParams=new ArrayList<Object>();
+		StringBuilder sql=new StringBuilder(100);
+		sql.append("select w.well_name,d.* ");
+		sql.append(" from tzl_gather_device d ");
+		sql.append(" left join tzl_well w on d.well_id=w.well_id and w.del_if=false");
+		sql.append(" where d.del_if=false ");
+		
+		if(args!=null&&StringUtils.isNotBlank((String)args.get("deviceKey"))) {
+			sql.append(" and (device_name like ? or device_code like ?)");
+			sqlParams.add("%"+args.get("deviceKey")+"%");
+			sqlParams.add("%"+args.get("deviceKey")+"%");
+		}
+		
+		
+		
+		sql.append(" order by device_code");
+		
+		Object[] sqlArgs=sqlParams.size()>0?sqlParams.toArray():null;
+		return dao.queryForPagedData(sql.toString(),pageNo, pageSize,DeviceVO.class ,sqlArgs);
+	}
+	
+	public String add(DevicePO device) {
+		UUIDHexGenerator uuid=UUIDHexGenerator.getInstance();
+		device.setDeviceId(uuid.generate());
+		device.setModifyTime(new Date());
+		device.setDelIf(false);
+		dao.insert(device,"tzl_gather_device");
+		return device.getDeviceId();
+	}
+	
+	public boolean delete(String deviceId) {
+		String sql="update  tzl_gather_device set del_if=true,modify_time=now() where device_id=?";
+		dao.exeUpdate(sql,deviceId);
+		return true;
+	}
+	
+	public DeviceVO get(String deviceId) {
+		String sql="""
+				 select w.well_name,d.* from tzl_gather_device d 
+				 left join tzl_well w on d.well_id=w.well_id and w.del_if=false
+				 where d.device_id=?
+				""";
+		return dao.queryForPojo(sql,DeviceVO.class,deviceId);
+	}
+	
+	
+	
+	public boolean update(DevicePO device) {
+		device.setModifyTime(new Date());
+		device.setDelIf(false);
+		return dao.update(device, "tzl_gather_device", "device_id")>0;
+	}
+	
+	public boolean existDevice(String deviceCode,String deviceId) {
+		if(StringUtils.isBlank(deviceId)) {
+			String sql="select count(1) from tzl_gather_device where del_if=false and device_code=?";
+			return dao.queryForObject(sql, Integer.class,deviceCode)>0;
+		}
+		else {
+			String sql="select count(1) from tzl_gather_device where del_if=false  and device_code=? and device_id!=?";
+			return dao.queryForObject(sql, Integer.class, deviceCode,deviceId)>0;
+		}
+		
+	}
+	
+}

+ 6 - 0
src/main/java/com/hb/proj/constant/SortCodeConstant.java

@@ -0,0 +1,6 @@
+package com.hb.proj.constant;
+
+public class SortCodeConstant {
+
+	
+}

+ 35 - 0
src/main/java/com/hb/proj/model/DevicePO.java

@@ -0,0 +1,35 @@
+package com.hb.proj.model;
+
+import java.util.Date;
+
+import lombok.Data;
+
+@Data
+public class DevicePO {
+
+	private String deviceId;
+	
+	private String deviceName;
+	
+	private String deviceModel;
+	
+	private String factory;
+	
+	private String serialNum;
+	
+	private String deviceCode;
+	
+	private String wellId;
+	
+	private String deviceType;
+	
+	private Boolean delIf;
+	
+	private Date modifyTime;
+	
+	private String modifyBy;
+	
+	private Date heartbeatLast;
+	
+	private Integer heartbeatCycleSec;
+}

+ 50 - 0
src/main/java/com/hb/proj/model/DeviceVO.java

@@ -0,0 +1,50 @@
+package com.hb.proj.model;
+
+import java.util.Date;
+
+public class DeviceVO extends DevicePO {
+
+	private String wellName;
+	
+	private String deviceTypeName;
+	
+	private Boolean onlineIf;
+	
+	public void setHeartbeatLast(Date heartbeatLast) {
+		super.setHeartbeatLast(heartbeatLast);
+		if(heartbeatLast!=null&&this.getHeartbeatCycleSec()!=null) {
+			this.onlineIf=	((new Date()).getTime()-heartbeatLast.getTime())<=(this.getHeartbeatCycleSec()*1000);
+		}
+	}
+	
+	public void setHeartbeatCycleSec(Integer sec) {
+		super.setHeartbeatCycleSec(sec);
+		if(sec!=null&&this.getHeartbeatLast()!=null) {
+			this.onlineIf=	((new Date()).getTime()-this.getHeartbeatLast().getTime())<=(sec*1000);
+		}
+	}
+
+	public String getWellName() {
+		return wellName;
+	}
+
+	public void setWellName(String wellName) {
+		this.wellName = wellName;
+	}
+
+	public String getDeviceTypeName() {
+		return deviceTypeName;
+	}
+
+	public void setDeviceTypeName(String deviceTypeName) {
+		this.deviceTypeName = deviceTypeName;
+	}
+
+	public Boolean getOnlineIf() {
+		return onlineIf;
+	}
+
+	public void setOnlineIf(Boolean onlineIf) {
+		this.onlineIf = onlineIf;
+	}
+}