package cn.cerc.mis.queue;

import cn.cerc.core.DataSet;
import cn.cerc.core.Record;
import cn.cerc.core.TDateTime;
import cn.cerc.mis.client.AutoService;
import cn.cerc.mis.core.LocalService;
import cn.cerc.mis.message.MessageProcess;
import cn.cerc.mis.rds.StubHandle;
import cn.cerc.mis.task.AbstractTask;
import lombok.extern.slf4j.Slf4j;

/**
 * 处理后台异步任务
 *
 * @author ZhangGong
 */
@Slf4j
public class ProcessService extends AbstractTask {

    // 手动执行所有的预约服务
    public static void main(String[] args) {
        StubHandle handle = new StubHandle();
        ProcessService ps = new ProcessService();
        ps.setHandle(handle);
        ps.run();
    }

    @Override
    public void execute() {
        LocalService svr = new LocalService(this, "SvrUserMessages.getWaitList");
        if (!svr.exec()) {
            throw new RuntimeException(svr.getMessage());
        }
        DataSet ds = svr.getDataOut();
        while (ds.fetch()) {
            log.info("开始处理异步任务，UID=" + ds.getString("UID_"));
            processService(ds.getString("UID_"));
        }
    }

    /**
     * 处理一个服务
     */
    private void processService(String msgId) {
        // 此任务可能被其它主机抢占
        LocalService svrMsg = new LocalService(this, "SvrUserMessages.readAsyncService");
        if (!svrMsg.exec("msgId", msgId)) {
            return;
        }
        Record ds = svrMsg.getDataOut().getHead();
        String corpNo = ds.getString("corpNo");
        String userCode = ds.getString("userCode");
        String content = ds.getString("content");
        String subject = ds.getString("subject");

        // 读取并标识为工作中，以防被其它用户抢占
        AsyncService svrSync = new AsyncService();
        svrSync.read(content);
        svrSync.setProcess(MessageProcess.working.ordinal());
        updateMessage(svrSync, msgId, subject);
        try {
            AutoService svrAuto = new AutoService(corpNo, userCode, svrSync.getService());
            svrAuto.getDataIn().appendDataSet(svrSync.getDataIn(), true);
            if (svrAuto.exec()) {
                svrSync.getDataOut().appendDataSet(svrAuto.getDataOut(), true);
                svrSync.setProcess(MessageProcess.ok.ordinal());
            } else {
                svrSync.getDataOut().appendDataSet(svrAuto.getDataOut(), true);
                svrSync.setProcess(MessageProcess.error.ordinal());
            }
            svrSync.getDataOut().getHead().setField("_message_", svrAuto.getMessage());
            updateMessage(svrSync, msgId, subject);
        } catch (Throwable e) {
            e.printStackTrace();
            svrSync.setProcess(MessageProcess.error.ordinal());
            svrSync.getDataOut().getHead().setField("_message_", e.getMessage());
            updateMessage(svrSync, msgId, subject);
        }
    }

    /**
     * 保存到数据库
     */
    private void updateMessage(AsyncService task, String msgId, String subject) {
        task.setProcessTime(TDateTime.Now().toString());
        LocalService svr = new LocalService(this, "SvrUserMessages.updateAsyncService");
        if (!svr.exec("msgId", msgId, "content", task.toString(), "process", task.getProcess())) {
            throw new RuntimeException("更新任务队列进度异常：" + svr.getMessage());
        }
        log.debug(task.getService() + ":" + subject + ":" + AsyncService.getProcessTitle(task.getProcess()));
    }
}

@@ -1641,20 +1641,18 @@
 Service 
+a
 s
-vrS
 ync = ne
@@ -1673,28 +1673,26 @@
 ();%0A        
+a
 s
-vrS
 ync.read(con
@@ -1698,36 +1698,34 @@
 ntent);%0A        
+a
 s
-vrS
 ync.setProcess(M
@@ -1772,36 +1772,34 @@
   updateMessage(
+a
 s
-vrS
 ync, msgId, subj
@@ -1886,20 +1886,18 @@
 erCode, 
+a
 s
-vrS
 ync.getS
@@ -1953,20 +1953,18 @@
 DataSet(
+a
 s
-vrS
 ync.getD
@@ -2021,36 +2021,34 @@
                 
+a
 s
-vrS
 ync.getDataOut()
@@ -2099,36 +2099,34 @@
                 
+a
 s
-vrS
 ync.setProcess(M
@@ -2183,36 +2183,34 @@
                 
+a
 s
-vrS
 ync.getDataOut()
@@ -2261,36 +2261,34 @@
                 
+a
 s
-vrS
 ync.setProcess(M
@@ -2337,36 +2337,34 @@
   %7D%0A            
+a
 s
-vrS
 ync.getDataOut()
@@ -2437,36 +2437,34 @@
   updateMessage(
+a
 s
-vrS
 ync, msgId, subj
@@ -2542,28 +2542,26 @@
             
+a
 s
-vrS
 ync.setProce
@@ -2608,20 +2608,18 @@
         
+a
 s
-vrS
 ync.getD
@@ -2706,12 +2706,10 @@
 age(
+a
 s
-vrS
 ync,
@@ -2817,20 +2817,21 @@
 Service 
-t
 as
-k
+ync
 , String
@@ -2864,20 +2864,21 @@
         
-t
 as
-k
+ync
 .setProc
@@ -3052,20 +3052,21 @@
 ntent%22, 
-t
 as
-k
+ync
 .toStrin
@@ -3081,20 +3081,21 @@
 ocess%22, 
-t
 as
-k
+ync
 .getProc
@@ -3206,20 +3206,21 @@
 g.debug(
-t
 as
-k
+ync
 .getServ
@@ -3278,20 +3278,21 @@
 ssTitle(
-t
 as
-k
+ync
 .getProc

Update ProcessService.java