定时器调用消息通知

//定义一个定时器类(类名格式:自定义功能名 + _Timer),且继承H3.SmartForm.Timer类
// public class Supervision_Timer: H3.SmartForm.Timer
// {
//     //构造方法,跟类名保持一致,里面不必书写代码,但是必须存在
//     public Supervision_Timer() { }

//     //重写定时器引擎执行的方法,必须存在,且方法名必须为OnWork,改动
//     protected override void OnWork(H3.IEngine engine) {
//         TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
//         try
//         {

//             DateTime now = DateTime.Now;
//             DateTime startTime = Convert.ToDateTime("08:00:00");//计划开始时间
//             DateTime endTime = Convert.ToDateTime("12:00:00");//计划完成时间
//             if(startTime < now && endTime > now) 
//             {
//                 PDOsendMessage PDOsendMessage = new PDOsendMessage();
//                 PDOsendMessage.generatePDOrdersFood(engine);
//             }
//             TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
//             double usedTime = ts1.Subtract(ts2).Duration().TotalSeconds;
//             addLogFS(engine, null, usedTime); //写定时器日志
//         }
//         catch(Exception ex)
//         {
//             TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
//             double usedTime = ts1.Subtract(ts2).Duration().TotalSeconds;
//             addLogFS(engine, ex, usedTime); //写定时器日志
//             errorHandlerRemindFS(engine, ex); //定时处理异常提醒
//         }
//     }

//     /*
//         在业务代码执行完毕后使用
//         向定时器日志添加一行记录
//         入参: 氚云引擎, 流水号(定时器代号), 异常类(正常处理传空) , 运行时间
//         by: lins, date: 2020-12-02
//      */
//     private void addLogFS(H3.IEngine engine, Exception ex, double usedTime) {
//         H3.DataModel.BizObjectSchema schema = engine.BizObjectManager.GetPublishedSchema("D002033timerLog");
//         H3.DataModel.BizObject createBo = new H3.DataModel.BizObject(engine, schema, H3.Organization.User.SystemUserId);
//         // createBo["joinFormMenu"] = joinFormMenu;
//         // createBo["joinTimerDefinition"] = objectId;
//         createBo["usedTime"] = usedTime;
//         bool successFlag = true;
//         if(ex != null)
//         {
//             successFlag = false;
//             createBo["errorMessage"] = ex.Message;
//             createBo["errorStackTrace"] = ex.StackTrace;
//         }
//         createBo["successFlag"] = successFlag;
//         //设置业务对象数据为生效状态
//         createBo.Status = H3.DataModel.BizObjectStatus.Effective;
//         createBo.Create();
//     }

//     /* 
//         向定时器管理员发送定时器处理异常信息
//         入参: 氚云引擎, 流水号(定时器代号), 异常类
//         by: lins, date: 2021-02-25
//     */
//     private void errorHandlerRemindFS(H3.IEngine engine, Exception ex) {
//         // string[] handlers = (string[]) masterBo["errorHandler"];
//         List < H3.Notification.UserMessage > messageList=new List<H3.Notification.UserMessage>();

//         // for(int i = 0;i < handlers.Length; i++) {
//         string title = "定时器报错处理提醒";//标题
//         string context = "定时器: '项目部订餐消息通知'运行失败请前往定时器日志查看, 出错信息: -> " + ex.Message;//发送内容
//         string send = H3.Organization.User.SystemUserId;//发起人
//         string receiverid = "8b9de57e-2ebc-4d75-82cf-d891b0b7e09b";//handlers[i] + string.Empty;//接收人
//         H3.Notification.UserMessage message = new H3.Notification.UserMessage(H3.Notification.UserMessageType.DingTalkPlainText, send, receiverid, "", title, context, "");
//         messageList.Add(message);
//         // }
//         if(messageList.Count > 0) engine.Notifier.Send(messageList.ToArray());//执行发送
//     }
// }
消息通知

public class PDOsendMessage
{
    public void generatePDOrdersFood(H3.IEngine engine)
    {
        // string sql = "select a.objectid aid, b.objectid bid FROM i_D002033FSPlanTimeSlot a join i_D002033D002033FunctionSupervisionPlanList b on a.parentobjectid = b.objectid where b.state = '有效' and a.isTask = 0";
        string sql = "select objectid from i_D002033Orderingmeal where OrderingmealName = '中洞项目部' and state ='有效'";
        System.Data.DataTable dt = engine.Query.QueryTable(sql, null);
        if(dt != null && dt.Rows.Count > 0)
        {
            foreach(System.Data.DataRow row in dt.Rows)
            {
                H3.DataModel.BizObject sourceBo = H3.DataModel.BizObject.Load(H3.Organization.User.SystemUserId, engine, "D002033Orderingmeal", row["objectid"] + string.Empty, false);
                H3.DataModel.BizObjectSchema schema = engine.BizObjectManager.GetPublishedSchema("D002033PDOrdersFood");
                string[] person = (string[]) sourceBo["personnel"];
                for(int i = 0;i < person.Length; i++) {
                    H3.DataModel.BizObject targetBo = new H3.DataModel.BizObject(engine, schema, H3.Organization.User.SystemUserId);
                    targetBo["Orderingstaff"] = person[i];
                    DateTime now = DateTime.Now.AddDays(1);
                    targetBo["dinnerTime"] = now;
                    targetBo.Create();
                    sendMessage(engine, targetBo);
                }
            }
        }
    }

    public void sendMessage(H3.IEngine Engine, H3.DataModel.BizObject targetBo)
    {
        H3.Task.UserTask task = new H3.Task.UserTask();
        string appId = "D002033PDOrdersFood";   //应用编码
        task.ObjectId = Guid.NewGuid().ToString();//Guid码
        task.Sender = H3.Organization.User.SystemUserId;//任务的发起人id
        task.UserId = targetBo["Orderingstaff"] + string.Empty; //任务的接收人id
        task.AppCode = appId;//应用编码
        task.TaskType = H3.Task.TaskType.Reminder; //任务类型为提醒
        task.SchemaCode = targetBo.Schema.SchemaCode; //表单编码
        task.TargetType = H3.LinkTargetType.BizObject; //关联类型
        task.TargetId = targetBo.ObjectId; //关联业务对象的ID
        task.TargetName = targetBo.Name;  //关联业务对象的数据标题
        H3.Organization.User user = (H3.Organization.User) Engine.Organization.GetUnit(task.UserId);
        task.Name = user.Name + "有一份明日订餐需要您处理"; //提醒标题
        // task.Summary = "请在五个工作日内完成填报,逾期将按默认休假计划自动提交。"; //提醒的内容
        task.AlertTime = DateTime.Now;  // 提醒时间
        task.ReminderType = H3.Task.ReminderType.Once; //提醒类型
        task.StartTime = DateTime.Now; //开始时间
        task.EndTime = DateTime.Now;   //截止时间
        task.TaskState = H3.Task.TaskState.Unfinished; //任务状态
        task.AlertState = H3.Task.AlertState.Waiting; //提醒的状态
        Engine.TaskManager.AddTask(task);//添加任务
    }
}

 

posted on 2022-05-26 16:09  天涯何  阅读(45)  评论(0编辑  收藏  举报