|
@@ -1,18 +1,30 @@
|
|
|
package com.hb.proj.base.service;
|
|
|
|
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.hb.proj.base.controller.WellIdGenerator;
|
|
|
+import com.hb.proj.model.AlarmDefinePO;
|
|
|
+import com.hb.proj.model.DevicePO;
|
|
|
import com.hb.proj.model.SideTreeNode;
|
|
|
import com.hb.proj.model.Well;
|
|
|
+import com.hb.proj.model.WellParamPO;
|
|
|
import com.hb.proj.model.WellVO;
|
|
|
+import com.hb.xframework.dao.core.PreparedSQLArgs;
|
|
|
import com.hb.xframework.dao.core.SpringJdbcDAO;
|
|
|
+import com.hb.xframework.dao.core.UpdateHandler;
|
|
|
+import com.hb.xframework.dao.util.UUIDHexGenerator;
|
|
|
|
|
|
@Service
|
|
|
public class WellService {
|
|
@@ -20,9 +32,35 @@ public class WellService {
|
|
|
@Autowired
|
|
|
private SpringJdbcDAO dao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private WellParamService wpService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlarmDefineService alarmService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeviceService deviceService;
|
|
|
+
|
|
|
|
|
|
private String tabName="tzl_well";
|
|
|
|
|
|
+ /**
|
|
|
+ * 检测系统内是否有同名的井名
|
|
|
+ * @param wellNames
|
|
|
+ * @return 同名的井名
|
|
|
+ */
|
|
|
+ public Set<String> getSameWellName(Set<String> wellNames) {
|
|
|
+ if(wellNames==null||wellNames.size()==0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String wn="'"+StringUtils.join(wellNames,"','")+"'";
|
|
|
+ String sql="select well_name,well_id from tzl_well where del_if=false and well_name in ("+wn+")";
|
|
|
+
|
|
|
+ Map<String,Object> mapping=dao.queryForMapping(sql, "well_name", "well_id");
|
|
|
+
|
|
|
+ return mapping!=null&&mapping.size()>0?mapping.keySet():null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 查询井节点-数据权限分配
|
|
|
* @param usId
|
|
@@ -66,7 +104,11 @@ public class WellService {
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 新增井
|
|
|
+ * @param well
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public String insert(Well well) {
|
|
|
//UUIDHexGenerator uuid = UUIDHexGenerator.getInstance();
|
|
|
well.setWellId(WellIdGenerator.generate());
|
|
@@ -77,14 +119,21 @@ public class WellService {
|
|
|
return well.getWellId();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 更新井
|
|
|
+ * @param well
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public boolean update(Well well) {
|
|
|
well.setModifyTime(new Date());
|
|
|
well.setDelIf(false);
|
|
|
return dao.update(well, "tzl_well", "well_id")>0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 删除井,逻辑删除
|
|
|
+ * @param wellId
|
|
|
+ */
|
|
|
public void delete(String wellId) {
|
|
|
dao.exeUpdate("update tzl_well set del_if=true,modify_time=now() where well_id=?",wellId);
|
|
|
|
|
@@ -94,17 +143,24 @@ public class WellService {
|
|
|
public WellVO get(String wellId){
|
|
|
String sql="""
|
|
|
|
|
|
- select w.*,g.org_name belong_org_name,d.device_code
|
|
|
+ select w.*,g.org_name belong_org_name,d.device_code,
|
|
|
+ p.temp_name patrol_std_temp_name
|
|
|
from tzl_well w
|
|
|
left join tsys_org g on w.org_id=g.org_id and g.del_if=false
|
|
|
left join tzl_gather_device d on d.well_id=w.well_id and d.del_if=false
|
|
|
+ left join tzl_temp p on w.patrol_std_temp=p.temp_id and p.del_if=false
|
|
|
where w.well_id=?
|
|
|
|
|
|
""";
|
|
|
return dao.queryForPojo(sql,WellVO.class,wellId);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 检测是否存在同井名
|
|
|
+ * @param wellName
|
|
|
+ * @param wellId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public boolean existWellName(String wellName,String wellId) {
|
|
|
if(StringUtils.isBlank(wellName)) {
|
|
|
return false;
|
|
@@ -118,6 +174,137 @@ public class WellService {
|
|
|
return dao.queryForObject(sql, Integer.class, args)>0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据模板井批量增加新井
|
|
|
+ * @param wells
|
|
|
+ * @param tempWellId
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void addBatch(List<WellVO> wells,String tempWellId) throws Exception{
|
|
|
+ Well wpo=null;
|
|
|
+ for(WellVO well : wells) {
|
|
|
+ wpo=new Well();
|
|
|
+ BeanUtils.copyProperties(well, wpo);
|
|
|
+ this.insert(wpo);
|
|
|
+ well.setWellId(wpo.getWellId());
|
|
|
+ deviceService.updateBindWell(well.getDeviceCode(),DevicePO.GATHER_DEV, well.getWellId());
|
|
|
+ }
|
|
|
+ copyWellParam(wells,tempWellId);
|
|
|
+ copyWellAlarm(wells,tempWellId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 复制模板井的报警设置
|
|
|
+ * @param wells
|
|
|
+ * @param tempWellId
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private void copyWellAlarm(List<WellVO> wells,String tempWellId) throws Exception {
|
|
|
+ List<AlarmDefinePO> alarmDefs=alarmService.loadByWell(tempWellId);
|
|
|
+ if(alarmDefs==null||alarmDefs.size()==0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ PreparedSQLArgs batchArgs=UpdateHandler.getInsertPreparedSQL(alarmDefs.get(0), "tzl_alarm");
|
|
|
+
|
|
|
+ Map<String,Field> fieldMap=getFieldMap(AlarmDefinePO.class);
|
|
|
+
|
|
|
+ for(Well well: wells) {
|
|
|
+
|
|
|
+ addBatchWellAlarm(well.getWellId(),alarmDefs,fieldMap,tempWellId,batchArgs.getParamNames(),batchArgs.getSql());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addBatchWellAlarm(String wellId,List<AlarmDefinePO> alarmDefs,Map<String,Field> fieldMap,String tempWellId,List<String> paramNames,String sql) throws Exception {
|
|
|
+ List<Object> argVals=null;
|
|
|
+ List<Object[]> sqlArgs=new ArrayList<Object[]>(alarmDefs.size());
|
|
|
+
|
|
|
+ Date d=new Date();
|
|
|
+ UUIDHexGenerator uuid=UUIDHexGenerator.getInstance();
|
|
|
+ String tempVal=null;
|
|
|
+ Field fd=null;
|
|
|
+ for(AlarmDefinePO alarm : alarmDefs) {
|
|
|
+ alarm.setAlarmId(uuid.generate());
|
|
|
+ alarm.setModifyBy("batch");
|
|
|
+ alarm.setModifyTime(d);
|
|
|
+ tempVal=alarm.getAlarmSource();
|
|
|
+ if(tempWellId.equals(tempVal)) { //组合报警,替换报警定义中的报警源为新井或新井参数
|
|
|
+ alarm.setAlarmSource(wellId);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ alarm.setAlarmSource(tempVal.replace(tempWellId, wellId));
|
|
|
+ }
|
|
|
+ tempVal=alarm.getAlarmExpress();
|
|
|
+ alarm.setAlarmExpress(tempVal.replace(tempWellId, wellId)); //报警表达式中的参数(包含的井id部分)替换为新井id
|
|
|
+
|
|
|
+ argVals=new ArrayList<Object>();
|
|
|
+ for(String pn:paramNames) { //准备批量参数值
|
|
|
+ fd=fieldMap.get(pn);
|
|
|
+ fd.setAccessible(true);
|
|
|
+ argVals.add(fd.get(alarm));
|
|
|
+ }
|
|
|
+ sqlArgs.add(argVals.toArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ dao.getJdbcTemplate().batchUpdate(sql, sqlArgs);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 复制模板井的采集参数
|
|
|
+ * @param wells
|
|
|
+ * @param tempWellId
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private void copyWellParam(List<WellVO> wells,String tempWellId) throws Exception {
|
|
|
+ List<WellParamPO> wparams=wpService.loadParamByWell(tempWellId);
|
|
|
+ if(wparams==null||wparams.size()==0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ PreparedSQLArgs batchArgs=UpdateHandler.getInsertPreparedSQL(wparams.get(0), "tzl_well_param");
|
|
|
+
|
|
|
+ Map<String,Field> fieldMap=getFieldMap(WellParamPO.class);
|
|
|
+
|
|
|
+ for(Well well: wells) {
|
|
|
+
|
|
|
+ addBatchWellParam(well.getWellId(),wparams,fieldMap,batchArgs.getParamNames(),batchArgs.getSql());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addBatchWellParam(String wellId,List<WellParamPO> wparams,Map<String,Field> fieldMap,List<String> paramNames,String sql) throws Exception {
|
|
|
+ List<Object> argVals=null;
|
|
|
+ List<Object[]> sqlArgs=new ArrayList<Object[]>(wparams.size());
|
|
|
+
|
|
|
+ Date d=new Date();
|
|
|
+ Field fd=null;
|
|
|
+ for(WellParamPO wp : wparams) {
|
|
|
+ wp.setParamId(wpService.getParamId(wellId, wp.getParamCode())); //重置为新井的
|
|
|
+ wp.setWellId(wellId);
|
|
|
+ wp.setModifyBy("batch");
|
|
|
+ wp.setModifyTime(d);
|
|
|
+
|
|
|
+ argVals=new ArrayList<Object>();
|
|
|
+ for(String pn:paramNames) { //准备批量参数值
|
|
|
+ fd=fieldMap.get(pn);
|
|
|
+ fd.setAccessible(true);
|
|
|
+ argVals.add(fd.get(wp)); //fieldMap.get(pn).get(wp)
|
|
|
+ //fd.setAccessible(false);
|
|
|
+ }
|
|
|
+ sqlArgs.add(argVals.toArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ dao.getJdbcTemplate().batchUpdate(sql, sqlArgs);
|
|
|
+ }
|
|
|
+
|
|
|
+ private <T> Map<String,Field> getFieldMap(Class<T> cls){
|
|
|
+ Field[] fields=cls.getDeclaredFields();
|
|
|
+ Map<String,Field> fieldMap=new HashMap<String,Field>(fields.length);
|
|
|
+ for(Field field : fields) {
|
|
|
+ fieldMap.put(field.getName(), field);
|
|
|
+ }
|
|
|
+
|
|
|
+ return fieldMap;
|
|
|
+ }
|
|
|
|
|
|
}
|