Explorar o código

增加批量建井接口

chenwen %!s(int64=2) %!d(string=hai) anos
pai
achega
d8c9c50a7f

+ 38 - 0
src/main/java/com/hb/proj/base/controller/WellController.java

@@ -1,14 +1,20 @@
 package com.hb.proj.base.controller;
 
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import com.hb.proj.allconfig.AccessToken;
 import com.hb.proj.base.service.DeviceService;
 import com.hb.proj.base.service.WellService;
+import com.hb.proj.model.BatchWell;
 import com.hb.proj.model.DevicePO;
 import com.hb.proj.model.Well;
 import com.hb.proj.model.WellVO;
@@ -28,6 +34,8 @@ public class WellController {
 	@Autowired
 	private DeviceService deviceService;
 	
+	
+	
 	/**
 	 * 获得单个井数据
 	 * @param wellId  井id
@@ -89,4 +97,34 @@ public class WellController {
 		service.delete(wellId);
 		return RespVOBuilder.ok();
 	}
+	
+	
+	/**
+	 * 批量加井
+	 * @param tempWellId  模板井号
+	 * @param orgId  新井所属井站
+	 * @param newWellJson  
+	 * @param token 登录人信息,自动从请求获取
+	 * @return
+	 * @throws Exception 
+	 */
+	@PostMapping("/addBatch")
+	public RespVO<String> addBatch(@RequestBody BatchWell batchWell,AccessToken token) throws Exception{
+		if(batchWell.getWells().size()==0) {
+			return RespVOBuilder.error("未提供待建井数据");
+		}
+		Set<String>  wellNames=new HashSet<String>();
+		for(WellVO well : batchWell.getWells()) {
+			well.setOrgId(batchWell.getOrgId());
+			well.setModifyBy(token.getUsName());
+			wellNames.add(well.getWellName());
+		}
+		wellNames=service.getSameWellName(wellNames);
+		if(wellNames!=null) {
+			return RespVOBuilder.error("井名已存在:"+StringUtils.join(wellNames,"、"));
+		}
+		
+		service.addBatch(batchWell.getWells(), batchWell.getTempWellId());
+		return RespVOBuilder.ok();
+	}
 }

+ 10 - 0
src/main/java/com/hb/proj/base/service/AlarmDefineService.java

@@ -30,6 +30,16 @@ public class AlarmDefineService {
 		return dao.queryForList(sql, AlarmDefineVO.class, holderId);
 	}
 	
+	public List<AlarmDefinePO> loadByWell(String wellId){
+		String sql="""
+				select alarm_id,alarm_desc,alarm_source,alarm_express,express_desc,alarm_mode,alarm_grade,alm.using_if,alm.del_if
+				from tzl_alarm alm
+				left join tzl_well_param wp on alm.alarm_source=wp.param_id
+				where alm.del_if=false and (alm.alarm_source=? or wp.well_id=?)
+				""";
+		return dao.queryForList(sql, AlarmDefinePO.class, wellId,wellId);
+	}
+	
 	
 	public String add(AlarmDefinePO alarm) {
 		UUIDHexGenerator uuid=UUIDHexGenerator.getInstance();

+ 7 - 1
src/main/java/com/hb/proj/base/service/DeviceService.java

@@ -23,7 +23,13 @@ public class DeviceService {
 	private SpringJdbcDAO  dao;
 	
 	public List<DeviceVO> loadByType(String devType){
-		return dao.queryForList("select device_name,device_code from tzl_gather_device where del_if=false and device_type=?", DeviceVO.class, devType);
+		String sql="""
+				select device_name,device_code,well_name 
+				from tzl_gather_device d
+				left join tzl_well w on d.well_id=w.well_id and w.del_if=false
+				where d.del_if=false and device_type=?
+				""";
+		return dao.queryForList(sql, DeviceVO.class, devType);
 	}
 	
 	

+ 16 - 0
src/main/java/com/hb/proj/base/service/WellParamService.java

@@ -4,6 +4,7 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -116,4 +117,19 @@ public class WellParamService {
 		
 	}
 	
+	/**
+	 * 查询指定井的某类参数的显示单位
+	 * @param wellIds
+	 * @param paramCode
+	 * @return
+	 */
+	public Map<String,Object>  loadSortParamDisUnit(Set<String> wellIds,String paramCode){
+		if(wellIds==null||wellIds.size()==0||StringUtils.isBlank(paramCode)) {
+			return null;
+		}
+		String ids="'"+StringUtils.join(wellIds,"','")+"'";
+		String sql="select well_id,display_unit from tzl_well_param where del_if=false and well_id in ("+ids+") and param_code=?";
+		
+		return dao.queryForMapping(sql, "well_id", "display_unit", paramCode);
+	}
 }

+ 193 - 6
src/main/java/com/hb/proj/base/service/WellService.java

@@ -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;
+   }
 
 }

+ 18 - 0
src/main/java/com/hb/proj/model/BatchWell.java

@@ -0,0 +1,18 @@
+package com.hb.proj.model;
+
+import java.util.List;
+
+import jakarta.validation.constraints.NotBlank;
+import lombok.Data;
+
+@Data
+public class BatchWell {
+
+	@NotBlank(message = "模板井号不能为空")
+	private String tempWellId;
+	
+	@NotBlank(message = "所属井站不能为空")
+	private String orgId;
+	
+	private List<WellVO> wells;
+}

+ 10 - 0
src/main/java/com/hb/proj/model/WellVO.java

@@ -5,6 +5,8 @@ public class WellVO extends Well {
 	private String belongOrgName;
 	
 	private String deviceCode;
+	
+	private String patrolStdTempName;
 
 	public String getBelongOrgName() {
 		return belongOrgName;
@@ -21,4 +23,12 @@ public class WellVO extends Well {
 	public void setDeviceCode(String deviceCode) {
 		this.deviceCode = deviceCode;
 	}
+
+	public String getPatrolStdTempName() {
+		return patrolStdTempName;
+	}
+
+	public void setPatrolStdTempName(String patrolStdTempName) {
+		this.patrolStdTempName = patrolStdTempName;
+	}
 }