DeviceService.java 4.5 KB

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