DeviceService.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.hb.proj.base.service;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import com.hb.proj.model.DevicePO;
  10. import com.hb.proj.model.DeviceParamPO;
  11. import com.hb.proj.model.DeviceVO;
  12. import com.hb.xframework.dao.core.SpringJdbcDAO;
  13. import com.hb.xframework.dao.util.PageModel;
  14. import com.hb.xframework.dao.util.UUIDHexGenerator;
  15. @Service
  16. public class DeviceService {
  17. @Autowired
  18. private SpringJdbcDAO dao;
  19. public List<DeviceVO> loadByType(String devType){
  20. String sql="""
  21. select device_name,device_code,well_name
  22. from tzl_gather_device d
  23. left join tzl_well w on d.well_id=w.well_id and w.del_if=false
  24. where d.del_if=false and device_type=?
  25. """;
  26. return dao.queryForList(sql, DeviceVO.class, devType);
  27. }
  28. public PageModel<DeviceVO> query(Map<String,Object> args,int pageNo,int pageSize){
  29. List<Object> sqlParams=new ArrayList<Object>();
  30. StringBuilder sql=new StringBuilder(100);
  31. sql.append("select w.well_name,d.* ");
  32. sql.append(" from tzl_gather_device d ");
  33. sql.append(" left join tzl_well w on d.well_id=w.well_id and w.del_if=false");
  34. sql.append(" where d.del_if=false ");
  35. if(args!=null&&StringUtils.isNotBlank((String)args.get("deviceKey"))) {
  36. sql.append(" and (device_name like ? or device_code like ?)");
  37. sqlParams.add("%"+args.get("deviceKey")+"%");
  38. sqlParams.add("%"+args.get("deviceKey")+"%");
  39. }
  40. sql.append(" order by device_code");
  41. Object[] sqlArgs=sqlParams.size()>0?sqlParams.toArray():null;
  42. return dao.queryForPagedData(sql.toString(),pageNo, pageSize,DeviceVO.class ,sqlArgs);
  43. }
  44. public String add(DevicePO device) {
  45. UUIDHexGenerator uuid=UUIDHexGenerator.getInstance();
  46. device.setDeviceId(uuid.generate());
  47. device.setModifyTime(new Date());
  48. device.setDelIf(false);
  49. dao.insert(device,"tzl_gather_device");
  50. return device.getDeviceId();
  51. }
  52. public boolean delete(String deviceId) {
  53. String sql="update tzl_gather_device set del_if=true,modify_time=now() where device_id=?";
  54. dao.exeUpdate(sql,deviceId);
  55. return true;
  56. }
  57. public DeviceVO get(String deviceId) {
  58. String sql="""
  59. select w.well_name,d.* from tzl_gather_device d
  60. left join tzl_well w on d.well_id=w.well_id and w.del_if=false
  61. where d.device_id=?
  62. """;
  63. return dao.queryForPojo(sql,DeviceVO.class,deviceId);
  64. }
  65. public boolean update(DevicePO device) {
  66. device.setModifyTime(new Date());
  67. device.setDelIf(false);
  68. return dao.update(device, "tzl_gather_device", "device_id")>0;
  69. }
  70. public boolean existDevice(String deviceCode,String deviceId) {
  71. if(StringUtils.isBlank(deviceId)) {
  72. String sql="select count(1) from tzl_gather_device where del_if=false and device_code=?";
  73. return dao.queryForObject(sql, Integer.class,deviceCode)>0;
  74. }
  75. else {
  76. String sql="select count(1) from tzl_gather_device where del_if=false and device_code=? and device_id!=?";
  77. return dao.queryForObject(sql, Integer.class, deviceCode,deviceId)>0;
  78. }
  79. }
  80. /**
  81. * 更新设备的相关设置(标定、采集单位)
  82. * @param param
  83. * @return
  84. */
  85. public boolean updateDeviceParamConfig(DeviceParamPO param) {
  86. if(param==null) {
  87. return false;
  88. }
  89. dao.exeUpdate("delete from tzl_gather_device_param where device_code=? and param_code=?", param.getDeviceCode(),param.getParamCode());
  90. UUIDHexGenerator uuid=UUIDHexGenerator.getInstance();
  91. param.setDevParamId(uuid.generate());
  92. param.setModifyTime(new Date());
  93. dao.insert(param,"tzl_gather_device_param");
  94. return true;
  95. }
  96. /**
  97. * 对设备进行井的绑定(同类设备只能绑定一个)
  98. * @param deviceCode
  99. * @param deviceType
  100. * @param wellId
  101. * @return
  102. */
  103. public boolean updateBindWell(String deviceCode,String deviceType,String wellId) {
  104. dao.exeUpdate("update tzl_gather_device set well_id=null where well_id=? and device_type=?", wellId,deviceType); //先解绑原有的绑定
  105. dao.exeUpdate("update tzl_gather_device set well_id=? where device_code=?", wellId,deviceCode);
  106. return true;
  107. }
  108. /**
  109. * 查询井的某类设备信息
  110. * @param wellId
  111. * @param devType
  112. * @return
  113. */
  114. public DeviceVO getByWell(String wellId,String devType) {
  115. String sql="""
  116. select * from tzl_gather_device
  117. where del_if=false and well_id=? and device_type=?
  118. limit 1
  119. """;
  120. return dao.queryForObject(sql, DeviceVO.class, wellId,wellId);
  121. }
  122. }