// 定义一个定时器类(类名格式:自定义功能名 + _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");//计划完成时间
AttendanceManagement AttendanceManagement = new AttendanceManagement();
bool boole = AttendanceManagement.judgeWorkDay(now, engine);
//定时上班打卡
if(startTime < now && endTime > now && !boole)
{
AttendanceManagement.AttendanceRecord(engine);
}
//下班消息提醒
DateTime startTime1 = Convert.ToDateTime("19:30:00");//计划开始时间
DateTime endTime1 = Convert.ToDateTime("21:00:00");//计划完成时间
if(startTime1 < now && endTime1 > now && !boole)
{
ClockinMessage ClockinMessage = new ClockinMessage();
ClockinMessage.projectPerson(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());//执行发送
}
}