Prechádzať zdrojové kódy

指定的产生在新的handleProduceCmd()中进行修改

andyliu 11 mesiacov pred
rodič
commit
da53be1531

+ 95 - 53
src/main/java/com/shkpr/service/aimodelpower/bizmgr/WODispatchPlanTmDoBizFun.java

@@ -13,6 +13,7 @@ import com.shkpr.service.aimodelpower.constants.CommFieldStatus;
 import com.shkpr.service.aimodelpower.constants.TaskQueueDataTypeEx;
 import com.shkpr.service.aimodelpower.dbdao.DBMgrProxy;
 import com.shkpr.service.aimodelpower.dbdao.services.intef.WODispatchCmdBatchItemDBService;
+import com.shkpr.service.aimodelpower.dbdao.services.intef.WODispatchCmdInfoDBService;
 import com.shkpr.service.aimodelpower.dbdao.services.intef.WODispatchPlanDBService;
 import com.shkpr.service.aimodelpower.dbdao.tables.WODispatchPlanInfoTable;
 import com.shkpr.service.aimodelpower.dto.*;
@@ -40,7 +41,7 @@ public class WODispatchPlanTmDoBizFun {
 
     private static WODispatchPlanDBService getThisDBService(){ return DBMgrProxy.getInstance().applyWODispatchPlanApi();}
     private static WODispatchCmdBatchItemDBService getItemDBService(){ return DBMgrProxy.getInstance().applyWODispatchItemApi();}
-
+    private static WODispatchCmdInfoDBService getCmdInfoDBService() {return DBMgrProxy.getInstance().applyWODispatchCmdApi();}
     public static ResponseCode produceCmd(String planId, long checkTm){
         ResponseCode code = ResponseCode.RESULT_BAD;
         if (StringUtils.isEmpty(planId))
@@ -76,28 +77,71 @@ public class WODispatchPlanTmDoBizFun {
 
     private static ResponseCode handleProduceCmd(String planId, long checkTm){
         ResponseCode code = ResponseCode.RESULT_BAD;
-        //通过planId在计划视图中查询指定计划基本信息,进行如下比对:
-        //若:status==1 && checkTm == next_task_time && batch_total > 0
-        //则对排班调用接口生成指令工单,否则直接返回
         Long nowLoc = TimeTool.getCurMsUTC();
         Map<String,Object> mapSel = getThisDBService().getOne(planId);
         if(mapSel!=null&&mapSel.size()>0){
             DispatchPlanInfoSSModel model = FastJsonUtil.map2Obj(mapSel,DispatchPlanInfoSSModel.class,true);
+            String errors = "";
+            List<String> cmdTaskIds = new ArrayList<>();
+            long oldNextTakeTime = model.getNextTakeTime();
+            long oldLimitNextTakeTime = model.getLimitNextTakeTime();
+            long newNextTakeTime = oldNextTakeTime;
+            long newLimitNextTakeTime = oldLimitNextTakeTime;
+            long newLastTakeTime = model.getLastTakeTime();
             Map<String, Object> andWheres = new HashMap<>();
-            andWheres.put("plan_id", planId);
-            List<DispatchCmdBatchItemModel> itemModels = getItemDBService()
-                    .listAllWithsExByDispoal("", "", andWheres, new HashMap<>(), "handle_start_time", "");
-            if(checkTm == model.getNextTakeTime()
-                    &&!CollectionUtils.isEmpty(itemModels)){
-                //TODO 满足以上条件就派发调用远程接口生成指令工单
-                JSONArray jsonArray = new JSONArray();
+            Map<String, Object> updates = new HashMap<>();
+            andWheres.put(WODispatchPlanInfoTable.W_INFO.UNIQUE_ID, planId);
+
+            do {
+                List<DispatchCmdBatchItemModel> itemModels = getItemDBService()
+                        .listAllWithsExByDispoal("", "", andWheres, new HashMap<>(), "handle_start_time", "");
+                if (checkTm != model.getNextTakeTime()){
+                    errors = "Next take time changed,goto next cycle";
+                    break;
+                }
+                if (model.getPlanStatus() != 1){
+                    errors = "Plan status changed,goto next cycle";
+                    break;
+                }
+                if (CollectionUtils.isEmpty(itemModels)){
+                    errors = "Plan batch empty,goto next cycle";
+                    break;
+                }
+
+                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());
+                newNextTakeTime = (model.getPlanType()==0)?0L:returnNextTakeTime(nextTimeModel);
+                newLimitNextTakeTime = (model.getPlanType()==0)?0L:0L;//returnNextTakeTime(nextTimeModel);
+
+                if (model.getPlanType() != 0 && newNextTakeTime <= oldNextTakeTime){
+                    newNextTakeTime = oldNextTakeTime;
+                    newLimitNextTakeTime = oldLimitNextTakeTime;
+                    errors = "Plan take time produce error,please check take time logic and goto next cycle";
+                    break;
+                }
+
+
+                if (model.getPlanType() != 0
+                        && !(nowLoc >= oldNextTakeTime && nowLoc <= oldLimitNextTakeTime)){
+                    errors = "Plan will use new take time,goto next cycle";
+                    break;
+                }
 
+                JSONArray jsonArray = new JSONArray();
                 for (DispatchCmdBatchItemModel item:itemModels) {
                     JSONObject planCommandParam = new JSONObject();
                     planCommandParam.put("type", 1);
-                    //"%s--第mmddhh批次任务"
-                    planCommandParam.put("title",String.format("%s--第%s批次任务",model.getTitle(),
-                            TimeTool.convertUTC2DateStr(TimeTool.getCurMsUTC(), TimeTool.TIMESTAMP_FORMAT_EX3)));
                     planCommandParam.put("zoneId", model.getZoneId());
                     planCommandParam.put("zoneType", model.getZoneType());
                     planCommandParam.put("zone", model.getZone());
@@ -105,7 +149,6 @@ public class WODispatchPlanTmDoBizFun {
                     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();
@@ -119,6 +162,9 @@ public class WODispatchPlanTmDoBizFun {
                         //TODO 说明时间错误,将错误拦截在此处不再进行派单动作
                         continue;
                     }
+
+                    planCommandParam.put("title",String.format("%s--第%s批次任务",model.getTitle(),
+                            TimeTool.convertUTC2DateStr(sugBeginTime, TimeTool.TIMESTAMP_FORMAT_EX3)));
                     planCommandParam.put("sugBeginTime",sugBeginTime);
                     String endTimeStr = item.getHandleEndTime();
                     planCommandParam.put("sugEndTime", TimeTool.convertDateStr2UTC(LocalDateTime.now()
@@ -147,58 +193,54 @@ public class WODispatchPlanTmDoBizFun {
                     planCommandParam.put("address",item.getHandleAddress());
                     jsonArray.add(planCommandParam);
                 }
-                //TODO 调用数据接口
+
                 if(jsonArray.size()>0) {
                     try {
                         JSONObject jsonParam = new JSONObject();
                         jsonParam.put("total",jsonArray.size());
                         jsonParam.put("data",jsonArray);
-                        ResponseRes res = ServiceMgrProxy.getInstance().applyTaskServiceApi().postPlanCommand(jsonParam);
-                        JSONObject resDataObj = JSONObject.parseObject(res.getResdata().toString());
-
-                        Map<String, Object> upPlanUpWhere = new HashMap<>();//修改计划的条件
-                        upPlanUpWhere.put("plan_id", model.getPlanId());
-                        Map<String, Object> upPlan = new HashMap<>();//要修改的计划
-                        if (ResponseCode.RESULT_NORMAL.toStrCode().equals(res.getRescode())
-                                &&!resDataObj.getJSONArray("data").isEmpty()
-                                &&"0".equals(resDataObj.getJSONArray("data").getJSONObject(0).getString("code"))) {
-                            //TODO 再计算下一次的时间,并且更新至调度计划的数据库
-                            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);
+                        ResponseRes<String> res = ServiceMgrProxy.getInstance().applyTaskServiceApi().postPlanCommand(jsonParam);
+                        if (ResponseCode.RESULT_NORMAL.toStrCode().equals(res.getRescode())) {
+                            DispatchCmdTaskRspGroup result = FastJsonUtil.fromJSONByGson(res.getResdata(), DispatchCmdTaskRspGroup.class);
+                            if (CommTool.listSize(result.getData()) > 0){
+                                for (DispatchCmdTaskRsp item:result.getData()){
+                                    if ("0".equals(item.getCode())){
+                                        cmdTaskIds.add(item.getUid());
+                                        newLastTakeTime = item.getOkTime();
+                                        code = ResponseCode.RESULT_NORMAL;
+                                    }
                                 }
                             }
-                            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("errors", "");
-                            upPlan.put("last_take_time", resDataObj.getJSONArray("data").getJSONObject(0).getLong("okTime"));
-
-                            code = ResponseCode.RESULT_NORMAL;
-                        }
-                        upPlan.put("takes",(model.getTakes()==null?0:model.getTakes())+jsonArray.size());
-                        if(res!=null&&!ResponseCode.RESULT_NORMAL.toStrCode().equals(res.getRescode())){
-                            //TODO 全失败,添加失败原因
-                            upPlan.put("errors",res.getRescode()+":"+res.getResmsg());
                         }
-                        getThisDBService().updateWiths(upPlan, upPlanUpWhere);
-
                     } catch (Exception ex) {
+                        code = ResponseCode.BUSINESS_BUSY;
                         LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mStrClassName, mStrClassName
                                 , String.format("check planInfo errorMsg:%s end<====",
                                         ex.getLocalizedMessage()));
                     }
+                    if (code != ResponseCode.RESULT_NORMAL)
+                        errors = "Produce cmd failed by remote api,goto next cycle";
+                }else
+                    errors = "Produce cmd failed by invalid param,goto next cycle";
+
+                if (CommTool.listSize(cmdTaskIds) <= 0 || code != ResponseCode.RESULT_NORMAL){
+                    newNextTakeTime = oldNextTakeTime;
+                    newLimitNextTakeTime = oldLimitNextTakeTime;
+                    break;
+                }
+            }while (false);
+
+            updates.put(WODispatchPlanInfoTable.W_INFO.ERRORS, errors);
+            updates.put(WODispatchPlanInfoTable.W_INFO.TAKES, model.getTakes()+1);
+            updates.put(WODispatchPlanInfoTable.W_INFO.UPDATE_TIME, nowLoc);
+            updates.put(WODispatchPlanInfoTable.W_INFO.LAST_TAKE_TIME, newLastTakeTime);
+            updates.put(WODispatchPlanInfoTable.W_INFO.NEXT_TAKE_TIME, newNextTakeTime);
+            updates.put(WODispatchPlanInfoTable.W_INFO.LIMIT_NEXT_TAKE_TIME, newLimitNextTakeTime);
+            if (getThisDBService().updateWiths(updates, andWheres) <= 0){
+                if (CommTool.listSize(cmdTaskIds) > 0){
+                    getCmdInfoDBService().batchDeleteIn(cmdTaskIds, "");
                 }
+                code = ResponseCode.BUSINESS_DB_REQ_FAILED;
             }
         }
         return code;

+ 5 - 0
src/main/java/com/shkpr/service/aimodelpower/dbdao/tables/WODispatchPlanInfoTable.java

@@ -30,5 +30,10 @@ public interface WODispatchPlanInfoTable {
         String UNIQUE_ID = "plan_id";
         String NEXT_TAKE_TIME = "next_take_time";
         String LAST_TAKE_TIME = "last_take_time";
+
+	    String LIMIT_NEXT_TAKE_TIME = "limit_next_take_time";
+        String TAKES = "takes";
+        String ERRORS = "errors";
+        String UPDATE_TIME = "update_time";
     }
 }

+ 1 - 1
src/main/java/com/shkpr/service/aimodelpower/dto/woDispatchPlanModel/DispatchPlanInfoSSModel.java

@@ -52,7 +52,7 @@ public class DispatchPlanInfoSSModel {
                                 //*) adjust
                                 //*) 调度指令排班表中的min(sug_begin_time)
                                 //    注:【计划新建/修改计划cycles/计划触发成功后】就得计算出该值,且临时计划触发成功后需置为0以便不再参与定时扫描
-
+    private long limitNextTakeTime = 0L;
     private String updaterId;//修改人
 
     private Integer takes;//总计触发次数(成功+未成功),用于后台查询分析