소스 검색

河南郸城调度计划 定时扫描逻辑初版 测试中

1037015548@qq.com 11 달 전
부모
커밋
cf4f07e91d

+ 109 - 2
src/main/java/com/shkpr/service/aimodelpower/bizmgr/WODispatchPlanTmDoBizFun.java

@@ -19,7 +19,7 @@ import com.shkpr.service.aimodelpower.dto.*;
 import com.shkpr.service.aimodelpower.dto.woDispatchPlanModel.*;
 import com.shkpr.service.aimodelpower.globalmgr.AsyncTaskQueueMgr;
 import com.shkpr.service.aimodelpower.jsonbean.woDispatchPlan.*;
-import javafx.beans.binding.ObjectExpression;
+import com.shkpr.service.aimodelpower.services.ServiceMgrProxy;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
@@ -68,11 +68,118 @@ public class WODispatchPlanTmDoBizFun {
         return code;
     }
 
+    //TODO 判断差值是否小于等于五分钟
+    private static boolean isWithinFiveMinutes(long timestamp1, long timestamp2) {
+        return Math.abs(timestamp1 - timestamp2) <= 5 * 60 * 1000;
+    }
+
     private static ResponseCode handleProduceCmd(String planId, long checkTm){
         ResponseCode code = ResponseCode.RESULT_BAD;
         //通过planId在计划视图中查询指定计划基本信息,进行如下比对:
         //若:status==1 && checkTm == next_task_time && batch_total > 0
         //则对排班调用接口生成指令工单,否则直接返回
+        Map<String,Object> mapSel = getThisDBService().getOne(planId);
+        if(mapSel!=null&&mapSel.size()>0){
+            DispatchPlanInfoSSModel model = FastJsonUtil.map2Obj(mapSel,DispatchPlanInfoSSModel.class,true);
+            Map<String, Object> andWheres = new HashMap<>();
+            andWheres.put("plan_id", planId);
+            List<DispatchCmdBatchItemModel> itemModels = getItemDBService()
+                    .listAllWithsExByDispoal("", "", andWheres, new HashMap<>(), "handle_start_time", "");
+            if(model.getNextTakeTime()!=null&&model.getNextTakeTime()>0L
+                    &&model.getPlanStatus()==1
+                    &&(checkTm <=model.getNextTakeTime()&&isWithinFiveMinutes(checkTm,model.getNextTakeTime()))
+                    &&!CollectionUtils.isEmpty(itemModels)){
+                //TODO 满足以上条件就派发调用远程接口生成指令工单
+                for (DispatchCmdBatchItemModel item:itemModels) {
+                    Map<String, Object> planCommandParam = new HashMap<>();
+                    planCommandParam.put("type", 1);
+                    planCommandParam.put("title", model.getTitle());
+                    planCommandParam.put("zoneId", model.getZoneId());
+                    planCommandParam.put("zoneType", model.getZoneType());
+                    planCommandParam.put("zone", model.getZone());
+                    planCommandParam.put("sender", model.getCreatorId());
+                    planCommandParam.put("receiver", item.getHeadUserId());
+
+                    Long sugBeginTime = 0L;
+
+                    if(model.getDispatchLeadTime()<0){
+                        sugBeginTime = Instant.ofEpochMilli(model.getNextTakeTime())
+                                .plus(Math.abs(model.getDispatchLeadTime()),ChronoUnit.MINUTES).toEpochMilli();
+                    }else if(model.getDispatchLeadTime()==0){
+                        sugBeginTime = model.getNextTakeTime();
+                    }else if(model.getDispatchLeadTime()>0){
+                        sugBeginTime = Instant.ofEpochMilli(model.getNextTakeTime())
+                                .minus(Math.abs(model.getDispatchLeadTime()),ChronoUnit.MINUTES).toEpochMilli();
+                    }
+                    if (sugBeginTime<=0L){
+                        //TODO 说明时间错误,将错误拦截在此处不再进行派单动作
+                        continue;
+                    }
+                    planCommandParam.put("sugBeginTime",sugBeginTime);
+                    String endTimeStr = item.getHandleEndTime();
+                    planCommandParam.put("sugEndTime", LocalDateTime.now()
+                            .withHour(Integer.valueOf(endTimeStr.split(":")[0]))
+                            .withMinute(Integer.valueOf(endTimeStr.split(":")[1]))
+                            .withSecond(Integer.valueOf(endTimeStr.split(":")[2])));
+                    List<Map<String,Object>> suggestions = new ArrayList<>();
+                    if(!CollectionUtils.isEmpty(item.getDispoalModels())){
+                        for(DispatchCmdBatchDispoalModel dispoalModel:item.getDispoalModels()) {
+                            Map<String, Object> sug = new HashMap<>();
+                            sug.put("key", dispoalModel.getKey());
+                            sug.put("data", dispoalModel.getData());
+                            sug.put("ordering", dispoalModel.getOrdering());
+                            sug.put("did", dispoalModel.getDid());
+                            sug.put("dname", dispoalModel.getDname());
+                            suggestions.add(sug);
+                        }
+                    }
+                    planCommandParam.put("suggestions",FastJsonUtil.toJSON(suggestions));
+                    planCommandParam.put("fromSource",planId);
+                    planCommandParam.put("level",model.getUrgentLevel());
+                    planCommandParam.put("sendway",model.getSendway());
+                    planCommandParam.put("remark",model.getDispatchRemark());
+                    planCommandParam.put("gis",item.getGis());
+                    planCommandParam.put("address",item.getHandleAddress());
+
+                    //TODO 调用数据接口
+                    try {
+                        ResponseRes res = ServiceMgrProxy.getInstance().applyTaskServiceApi().postPlanCommand(planCommandParam);
+                        if(ResponseCode.RESULT_NORMAL.equals(res.getRescode())){
+                            //TODO 再计算下一次的时间,并且更新至调度计划的数据库
+                            Map<String,Object> upPlanUpWhere = new HashMap<>();//修改计划的条件
+                            upPlanUpWhere.put("plan_id",model.getPlanId());
+                            Map<String,Object> upPlan = new HashMap<>();//要修改的计划
+                            DispatchPlanInfoNextTimeModel nextTimeModel = new DispatchPlanInfoNextTimeModel();
+                            List<DispatchCmdBatchItemNextTimeModel> itemModelNextTimes = new ArrayList<>();
+                            if(!CollectionUtils.isEmpty(itemModels)){
+                                for(DispatchCmdBatchItemModel jpItem:itemModels){
+                                    DispatchCmdBatchItemNextTimeModel dispatchCmdBatchItemNextTimeModel =new DispatchCmdBatchItemNextTimeModel();
+                                    dispatchCmdBatchItemNextTimeModel.setHandleStartTime(jpItem.getHandleStartTime());
+                                    itemModelNextTimes.add(dispatchCmdBatchItemNextTimeModel);
+                                }
+                            }
+                            nextTimeModel.setItemsList(itemModelNextTimes);
+                            nextTimeModel.setPlanType(model.getPlanType());
+                            nextTimeModel.setDispatchLeadTime(model.getDispatchLeadTime());
+                            nextTimeModel.setCycles(model.getCycles());
+                            if(model.getPlanType()==0){
+                                upPlan.put("next_take_time",0L);
+                            }else {
+                                upPlan.put("next_take_time", returnNextTakeTime(nextTimeModel));
+                                upPlan.put("last_take_time", model.getNextTakeTime());
+                            }
+                            getThisDBService().updateWiths(upPlan, upPlanUpWhere);
+
+                            code = ResponseCode.RESULT_NORMAL;
+                        }
+                    }catch(Exception ex){
+                        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mStrClassName, mStrClassName
+                                ,String.format("check planInfo errorMsg:%s end<====",
+                                        ex.getLocalizedMessage()));
+                    }
+                }
+            }
+        }
         return code;
     }
 
@@ -201,7 +308,7 @@ public class WODispatchPlanTmDoBizFun {
         }
         try{
             Map<String,Object> map = getThisDBService().getOne(oJsonParam.getPlanId());
-            if(map!=null||map.keySet().size()>0){
+            if(map!=null||map.size()>0){
                 DispatchPlanInfoDetailsModel model = FastJsonUtil.map2Obj(map,DispatchPlanInfoDetailsModel.class,true);
                 if (model!=null) {
                     //TODO 查询相关联规则和标签

+ 39 - 2
src/main/java/com/shkpr/service/aimodelpower/dbdao/services/WODispatchPlanDBServiceImpl.java

@@ -243,7 +243,13 @@ public class WODispatchPlanDBServiceImpl implements WODispatchPlanDBService,WODi
                 mapDispoal.put("key",StringUtils.isEmpty(dispoal.getKey())?CommTool.genPlanItemDispoalId():dispoal.getKey());
                 mapDispoal.put("create_time",TimeTool.getCurMsUTC());
                 mapDispoal.put(WODispatchCmdPatchDisposalTable.W_INFO.BATCH_ITEM_ID,map.get(WODispatchCmdPatchItemTable.W_INFO.PRIMARY_KEY));
-                woDispatchCmdPatchDisposalMapper.inserts(mapDispoal);
+                if(StringUtils.isEmpty(dispoal.getKey())){
+                    woDispatchCmdPatchDisposalMapper.inserts(mapDispoal);
+                }else{
+                    Map<String,Object> andWhereUpDis = new HashMap<>();
+                    andWhereUpDis.put("key",dispoal.getKey());
+                    woDispatchCmdPatchDisposalMapper.updateWiths(mapDispoal,andWhereUpDis,new HashMap<>(),"");
+                }
             }
             woDispatchCmdPatchItemMapper.inserts(map);
         }
@@ -316,7 +322,13 @@ public class WODispatchPlanDBServiceImpl implements WODispatchPlanDBService,WODi
                     mapDispoal.put("key", StringUtils.isEmpty(dispoal.getKey())?CommTool.genPlanItemDispoalId():dispoal.getKey());
                     mapDispoal.put("create_time", TimeTool.getCurMsUTC());
                     mapDispoal.put(WODispatchCmdPatchDisposalTable.W_INFO.BATCH_ITEM_ID, map.get(WODispatchCmdPatchItemTable.W_INFO.PRIMARY_KEY));
-                    woDispatchCmdPatchDisposalMapper.inserts(mapDispoal);
+                    if(StringUtils.isEmpty(dispoal.getKey())){
+                        woDispatchCmdPatchDisposalMapper.inserts(mapDispoal);
+                    }else{
+                        Map<String,Object> andWhereUpDis = new HashMap<>();
+                        andWhereUpDis.put("key",dispoal.getKey());
+                        woDispatchCmdPatchDisposalMapper.updateWiths(mapDispoal,andWhereUpDis,new HashMap<>(),"");
+                    }
                 }
                 woDispatchCmdPatchItemMapper.inserts(map);
             }
@@ -348,6 +360,31 @@ public class WODispatchPlanDBServiceImpl implements WODispatchPlanDBService,WODi
         }
         return arrRes;
     }
+    @Override
+    public List<Map<String, Object>> selectAllPlanTags(String table, String filed, Map<String, Object> andWheres, Map<String, Object> orWheres, String orderBy, String extend) {
+        int nCode = 0;
+        String strMsg = "Success";
+        List<Map<String, Object>> arrRes = null;
+        try {
+            arrRes = woDispatchPlanTagsMapper.batchQueryWiths(table, filed, andWheres, orWheres, orderBy, extend);
+        }catch (Exception e){
+            nCode = LogLevelFlag.LOG_ERROR.ordinal();
+            strMsg = e.getLocalizedMessage();
+        }finally {
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.valueFromInt(nCode)
+                    , mBusinessType
+                    , mStrClassName
+                    , String.format("Batch Query %s:{andWhere.size=%d, orWhere.size=%d, orderBy=%s, extend=%s, back.size=%d} from database, code:{%d} msg:{%s} ..."
+                            , logTag
+                            , CommTool.mapSize(andWheres)
+                            , CommTool.mapSize(orWheres)
+                            , orderBy, extend
+                            , CommTool.listSize(arrRes)
+                            , nCode, strMsg));
+        }
+        return arrRes;
+    }
+    @Override
     public List<Map<String, Object>> selectAllRuls(String table, String filed, Map<String, Object> andWheres, Map<String, Object> orWheres, String orderBy, String extend) {
         int nCode = 0;
         String strMsg = "Success";

+ 6 - 0
src/main/java/com/shkpr/service/aimodelpower/dbdao/services/intef/WODispatchPlanDBService.java

@@ -52,6 +52,12 @@ public interface WODispatchPlanDBService extends BaseDBService {
             , String orderBy
             , String extend);
 
+    List<Map<String,Object>> selectAllPlanTags(String table, String filed
+            , Map<String, Object> andWheres
+            , Map<String, Object> orWheres
+            , String orderBy
+            , String extend);
+
     List<Map<String,Object>> selectAllRuls(String table, String filed
             , Map<String, Object> andWheres
             , Map<String, Object> orWheres

+ 4 - 2
src/main/java/com/shkpr/service/aimodelpower/jsonbean/woDispatchPlan/JPDispatchPlanInfoUpdate.java

@@ -57,8 +57,7 @@ public class JPDispatchPlanInfoUpdate {
     @StrNullOrSize(groups = {CommonParamValidReset.class})
     private String dispatchRemark = null;//调度计划描述
 
-    @IntInitOrRange(init = -1, groups = {CommonParamValidReset.class})
-    private Integer dispatchLeadTime = -1;//派单时间提前量设定(单位分钟)
+    private Integer dispatchLeadTime = null;//派单时间提前量设定(单位分钟)
                                     //    提前或推迟的校正时间(存储单位:分钟)
                                     //*) =0则不推迟也不提前
                                     //*) >0则推迟触发
@@ -131,6 +130,9 @@ public class JPDispatchPlanInfoUpdate {
                     (planType != 0 && planType != 1 && planType != 2 && planType != 3 && planType != 4)) {
                 return false;
             }
+            if(dispatchLeadTime==null){
+                return false;
+            }
             if(!CollectionUtils.isEmpty(itemsList)){
                 if(StringUtils.isEmpty(itemsList)){
                     return false;

+ 31 - 0
src/main/java/com/shkpr/service/aimodelpower/services/CloudTaskService.java

@@ -23,6 +23,7 @@ import org.springframework.util.StringUtils;
 import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.PostConstruct;
+import java.util.Map;
 
 @Service
 public class CloudTaskService {
@@ -42,6 +43,7 @@ public class CloudTaskService {
     private String mStrTaskAddress = "";
 
     private String mNotifyMsgPath = "";
+    private String mPlanCommandPath = "";
 
     @PostConstruct
     public void init(){
@@ -52,6 +54,7 @@ public class CloudTaskService {
         else if (!mStrTaskAddress.endsWith("/"))
             mStrTaskAddress += "/";
         mNotifyMsgPath = String.format("%s%s", mStrTaskAddress, "task/internal/notify");
+        mPlanCommandPath = String.format("%s%s", mStrTaskAddress, "task/wws-dispatch/cmd/new-info");
     }
 
     @Retryable(value = {RemoteAccessException.class}, maxAttempts = 3, backoff = @Backoff(delay = 200L, multiplier = 1))
@@ -81,4 +84,32 @@ public class CloudTaskService {
         }
         return resResult;
     }
+
+    @Retryable(value = {RemoteAccessException.class}, maxAttempts = 3, backoff = @Backoff(delay = 200L, multiplier = 1))
+    public ResponseRes<String> postPlanCommand(final Map<String,Object> jsonParam) throws Exception{
+        ResponseRes<String> resResult = new ResponseRes<String>();
+        resResult.setRescode(ResponseCode.RESULT_BAD.toStrCode());
+        resResult.setResmsg(MSG_FAILED);
+        resResult.setResdata("");
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
+        headers.add(ApiURI.HEADER_CLIENT_TYPE, GlobalData.getInstance().getServerId());
+        headers.add(ApiURI.HEADER_AUTH_VERIFY, mStrTaskCallPassword);
+
+        String body = FastJsonUtil.toJSON(jsonParam);
+        HttpEntity<String> request = new HttpEntity<>(body, headers);
+        try {
+            ResponseEntity<String> response = restTemplate.postForEntity(mPlanCommandPath, request, String.class);
+            if (response.getStatusCode() == HttpStatus.OK){
+                String strBody = response.getBody();
+                return FastJsonUtil.fromJSONByGson(strBody, ResponseRes.class);
+            }
+        }catch (Exception e){
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_WARN, LogFlagBusiType.BUSI_CALL_TASK_AS.toStrValue(), strClassName, String.format("notifyMsg %s to CloudTaskService(url:%s) failed(%s)...", body, mNotifyMsgPath, e.getMessage()));
+            //达到maxAttempts次数将返回RemoteAccessException
+            throw new RemoteAccessException("Retry...");
+        }
+        return resResult;
+    }
 }

+ 4 - 0
src/main/java/com/shkpr/service/aimodelpower/services/ServiceMgrProxy.java

@@ -6,6 +6,7 @@ public class ServiceMgrProxy {
     private volatile CloudBaseService baseService = null;
     private volatile CloudDataService dataService = null;
     private volatile CloudGWService gwService = null;
+    private volatile CloudTaskService taskService= null;
     private static volatile ServiceMgrProxy msInstance = null;
     public static ServiceMgrProxy getInstance(){
         if (msInstance == null){
@@ -27,6 +28,8 @@ public class ServiceMgrProxy {
             dataService = (CloudDataService) SpringContextUtil.getBean(CloudDataService.class);
         if (gwService == null)
             gwService = (CloudGWService) SpringContextUtil.getBean(CloudGWService.class);
+        if (taskService == null)
+            taskService = (CloudTaskService) SpringContextUtil.getBean(CloudTaskService.class);
     }
 
     public CloudBaseService applyBaseServiceApi() { return baseService; }
@@ -34,4 +37,5 @@ public class ServiceMgrProxy {
     public CloudDataService applyDataServiceApi() { return dataService; }
 
     public CloudGWService applyDataGWServiceApi() {return gwService;}
+    public CloudTaskService applyTaskServiceApi() {return taskService;}
 }