7 changed files with 277 additions and 0 deletions
-
59storeroom/src/main/java/com/storeroom/modules/device/controller/DeviceSpecParamController.java
-
60storeroom/src/main/java/com/storeroom/modules/device/domain/DeviceSpecParam.java
-
29storeroom/src/main/java/com/storeroom/modules/device/repository/DeviceSpecParamRepository.java
-
29storeroom/src/main/java/com/storeroom/modules/device/service/DeviceSpecParamService.java
-
40storeroom/src/main/java/com/storeroom/modules/device/service/dto/DeviceSpecParamDto.java
-
49storeroom/src/main/java/com/storeroom/modules/device/service/impl/DeviceSpecParamImpl.java
-
11storeroom/src/main/java/com/storeroom/modules/device/service/mapstruct/DeviceSpecParamMapper.java
@ -0,0 +1,59 @@ |
|||||
|
package com.storeroom.modules.device.controller; |
||||
|
|
||||
|
|
||||
|
import com.storeroom.annotaion.rest.AnonymousDeleteMapping; |
||||
|
import com.storeroom.annotaion.rest.AnonymousGetMapping; |
||||
|
import com.storeroom.annotaion.rest.AnonymousPostMapping; |
||||
|
import com.storeroom.exception.BaseException; |
||||
|
import com.storeroom.modules.device.service.DeviceSpecParamService; |
||||
|
import com.storeroom.modules.device.service.dto.DeviceInfoDto; |
||||
|
import com.storeroom.modules.device.service.dto.DeviceSpecParamDto; |
||||
|
import com.storeroom.utils.ApiResponse; |
||||
|
import com.storeroom.utils.StringUtils; |
||||
|
import com.storeroom.utils.enums.ResponseStatus; |
||||
|
import io.swagger.annotations.Api; |
||||
|
import io.swagger.annotations.ApiOperation; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||
|
import org.springframework.web.bind.annotation.RequestParam; |
||||
|
import org.springframework.web.bind.annotation.RestController; |
||||
|
|
||||
|
@RestController |
||||
|
@RequiredArgsConstructor |
||||
|
@Api(tags = "设备参数绑定") |
||||
|
@RequestMapping("/api/spec/") |
||||
|
public class DeviceSpecParamController { |
||||
|
|
||||
|
private final DeviceSpecParamService deviceSpecParamService; |
||||
|
|
||||
|
@ApiOperation("绑定参数") |
||||
|
@AnonymousPostMapping("bind") |
||||
|
public ApiResponse<Object> create(@RequestBody DeviceSpecParamDto deviceSpecParamDto) { |
||||
|
if (!StringUtils.isEmpty(deviceSpecParamDto.getId())) { |
||||
|
throw new BaseException("id不为空"); |
||||
|
} |
||||
|
deviceSpecParamService.bind(deviceSpecParamDto); |
||||
|
return ApiResponse.success(ResponseStatus.SUCCESS); |
||||
|
} |
||||
|
|
||||
|
@ApiOperation("删除参数") |
||||
|
@AnonymousDeleteMapping("delete") |
||||
|
public ApiResponse<Object> delete(@RequestParam String id) { |
||||
|
if (StringUtils.isEmpty(id)) { |
||||
|
throw new BaseException("id不能为空"); |
||||
|
} |
||||
|
deviceSpecParamService.delete(id); |
||||
|
return ApiResponse.success(ResponseStatus.SUCCESS); |
||||
|
} |
||||
|
|
||||
|
@ApiOperation("查询参数") |
||||
|
@AnonymousGetMapping("list") |
||||
|
public ApiResponse<Object> list(@RequestParam String deviceInfoId) { |
||||
|
if (StringUtils.isEmpty(deviceInfoId)) { |
||||
|
throw new BaseException("设备id不能为空"); |
||||
|
} |
||||
|
return ApiResponse.success(deviceSpecParamService.findByDeviceId(deviceInfoId)); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,60 @@ |
|||||
|
package com.storeroom.modules.device.domain; |
||||
|
|
||||
|
import com.alibaba.fastjson.annotation.JSONField; |
||||
|
import com.storeroom.base.BaseEntity; |
||||
|
import io.swagger.annotations.ApiModelProperty; |
||||
|
import lombok.Getter; |
||||
|
import lombok.Setter; |
||||
|
|
||||
|
import javax.persistence.*; |
||||
|
import javax.validation.constraints.NotNull; |
||||
|
import java.io.Serializable; |
||||
|
import java.util.Objects; |
||||
|
|
||||
|
@Entity |
||||
|
@Getter |
||||
|
@Setter |
||||
|
@Table(name = "device_spec_param") |
||||
|
public class DeviceSpecParam extends BaseEntity implements Serializable { |
||||
|
|
||||
|
@Id |
||||
|
@Column(name = "id") |
||||
|
@NotNull(groups = Update.class) |
||||
|
@ApiModelProperty(value = "ID", hidden = true) |
||||
|
private String id; |
||||
|
|
||||
|
@ManyToOne |
||||
|
@JoinColumn(name = "device_info_id",referencedColumnName = "id") |
||||
|
@ApiModelProperty(value = "设备信息id") |
||||
|
private DeviceInfo deviceInfoId; |
||||
|
|
||||
|
@Column(name = "param_id") |
||||
|
@ApiModelProperty(value = "参数id") |
||||
|
private String paramId; |
||||
|
|
||||
|
|
||||
|
@Column(name = "param_name") |
||||
|
@ApiModelProperty(value = "参数名称") |
||||
|
private String paramName; |
||||
|
|
||||
|
@Column(name = "unit") |
||||
|
@ApiModelProperty(value = "值单位") |
||||
|
private String unit; |
||||
|
|
||||
|
@Column(name = "sequence") |
||||
|
@ApiModelProperty(value = "序号") |
||||
|
private Integer sequence; |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(Object o) { |
||||
|
if (this == o) return true; |
||||
|
if (o == null || getClass() != o.getClass()) return false; |
||||
|
DeviceSpecParam that = (DeviceSpecParam) o; |
||||
|
return Objects.equals(id, that.id) && Objects.equals(deviceInfoId, that.deviceInfoId) && Objects.equals(paramId, that.paramId) && Objects.equals(paramName, that.paramName) && Objects.equals(unit, that.unit) && Objects.equals(sequence, that.sequence); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int hashCode() { |
||||
|
return Objects.hash(id, deviceInfoId, paramId, paramName, unit, sequence); |
||||
|
} |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
package com.storeroom.modules.device.repository; |
||||
|
|
||||
|
import com.storeroom.modules.device.domain.DeviceInfo; |
||||
|
import com.storeroom.modules.device.domain.DeviceSpecParam; |
||||
|
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
|
||||
|
public interface DeviceSpecParamRepository extends JpaRepository<DeviceSpecParam, String>, JpaSpecificationExecutor<DeviceSpecParam> { |
||||
|
|
||||
|
/** |
||||
|
* 获取数据最大值 |
||||
|
* |
||||
|
* @return |
||||
|
*/ |
||||
|
@Query(value = "SELECT MAX(sequence) AS maxsque FROM device_spec_param WHERE device_info_id =''", nativeQuery = true) |
||||
|
Integer findByMax(String deviceInfoId); |
||||
|
|
||||
|
/** |
||||
|
* 根据设备id查询 |
||||
|
* @param deviceInfoId |
||||
|
* @return |
||||
|
*/ |
||||
|
@Query(value = "SELECT * FROM device_spec_param WHERE device_info_id =?1", nativeQuery = true) |
||||
|
List<DeviceSpecParam> findByDeviceInfoId(String deviceInfoId); |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
package com.storeroom.modules.device.service; |
||||
|
|
||||
|
import com.storeroom.modules.device.service.dto.DeviceSpecParamDto; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface DeviceSpecParamService { |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 绑定参数 |
||||
|
*/ |
||||
|
void bind(DeviceSpecParamDto deviceSpecParamDto); |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 删除参数 |
||||
|
* @param id 参数id |
||||
|
*/ |
||||
|
void delete(String id); |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 通过设备信息id 查询绑定参数列表 |
||||
|
* @param deviceInfoId |
||||
|
* @return |
||||
|
*/ |
||||
|
List<DeviceSpecParamDto> findByDeviceId(String deviceInfoId); |
||||
|
} |
@ -0,0 +1,40 @@ |
|||||
|
package com.storeroom.modules.device.service.dto; |
||||
|
|
||||
|
import com.alibaba.fastjson.annotation.JSONField; |
||||
|
import com.storeroom.base.BaseDTO; |
||||
|
import com.storeroom.modules.device.domain.DeviceInfo; |
||||
|
import lombok.Getter; |
||||
|
import lombok.Setter; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.util.Objects; |
||||
|
|
||||
|
@Getter |
||||
|
@Setter |
||||
|
public class DeviceSpecParamDto extends BaseDTO implements Serializable { |
||||
|
|
||||
|
private String id; |
||||
|
|
||||
|
private DeviceInfo deviceInfoId; |
||||
|
|
||||
|
private String paramId; |
||||
|
|
||||
|
private String paramName; |
||||
|
|
||||
|
private String unit; |
||||
|
|
||||
|
private Integer sequence; |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(Object o) { |
||||
|
if (this == o) return true; |
||||
|
if (o == null || getClass() != o.getClass()) return false; |
||||
|
DeviceSpecParamDto that = (DeviceSpecParamDto) o; |
||||
|
return Objects.equals(id, that.id) && Objects.equals(deviceInfoId, that.deviceInfoId) && Objects.equals(paramId, that.paramId) && Objects.equals(paramName, that.paramName) && Objects.equals(unit, that.unit) && Objects.equals(sequence, that.sequence); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int hashCode() { |
||||
|
return Objects.hash(id, deviceInfoId, paramId, paramName, unit, sequence); |
||||
|
} |
||||
|
} |
@ -0,0 +1,49 @@ |
|||||
|
package com.storeroom.modules.device.service.impl; |
||||
|
|
||||
|
import com.storeroom.modules.device.domain.DeviceSpecParam; |
||||
|
import com.storeroom.modules.device.repository.DeviceSpecParamRepository; |
||||
|
import com.storeroom.modules.device.service.DeviceSpecParamService; |
||||
|
import com.storeroom.modules.device.service.dto.DeviceSpecParamDto; |
||||
|
import com.storeroom.modules.device.service.mapstruct.DeviceSpecParamMapper; |
||||
|
import com.storeroom.utils.NanoIdUtils; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.util.ObjectUtils; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
|
||||
|
@Service |
||||
|
@RequiredArgsConstructor |
||||
|
public class DeviceSpecParamImpl implements DeviceSpecParamService { |
||||
|
|
||||
|
|
||||
|
private final DeviceSpecParamRepository deviceSpecParamRepository; |
||||
|
private final DeviceSpecParamMapper deviceSpecParamMapper; |
||||
|
|
||||
|
|
||||
|
@Override |
||||
|
public void bind(DeviceSpecParamDto deviceSpecParamDto) { |
||||
|
DeviceSpecParam deviceSpecParam = deviceSpecParamMapper.toEntity(deviceSpecParamDto); |
||||
|
|
||||
|
Integer num = deviceSpecParamRepository.findByMax(deviceSpecParam.getDeviceInfoId().getId()); |
||||
|
if (ObjectUtils.isEmpty(num)) { |
||||
|
deviceSpecParam.setSequence(1); |
||||
|
} else { |
||||
|
deviceSpecParam.setSequence(num + 1); |
||||
|
} |
||||
|
deviceSpecParam.setId(NanoIdUtils.randomNanoId()); |
||||
|
deviceSpecParamRepository.save(deviceSpecParam); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void delete(String id) { |
||||
|
deviceSpecParamRepository.deleteById(id); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<DeviceSpecParamDto> findByDeviceId(String deviceInfoId) { |
||||
|
List<DeviceSpecParam> deviceSpecParamList = deviceSpecParamRepository.findByDeviceInfoId(deviceInfoId); |
||||
|
return deviceSpecParamMapper.toDto(deviceSpecParamList); |
||||
|
} |
||||
|
} |
@ -0,0 +1,11 @@ |
|||||
|
package com.storeroom.modules.device.service.mapstruct; |
||||
|
|
||||
|
import com.storeroom.base.BaseMapper; |
||||
|
import com.storeroom.modules.device.domain.DeviceSpecParam; |
||||
|
import com.storeroom.modules.device.service.dto.DeviceSpecParamDto; |
||||
|
import org.mapstruct.Mapper; |
||||
|
import org.mapstruct.ReportingPolicy; |
||||
|
|
||||
|
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) |
||||
|
public interface DeviceSpecParamMapper extends BaseMapper<DeviceSpecParamDto, DeviceSpecParam> { |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue