/// <summary> /// 得到可正确拼接的SQL条件语句(可能是没有) /// </summary> /// <param name="table">当前数据操作对象</param> /// <param name="conditions">附加的条件集合</param> /// <returns></returns> public static void ConditionSqlCode <TEntity>(IDataTable <TEntity> table, List <string> conditions) where TEntity : EditDataObject, new() { foreach (var trigger in DependencyHelper.GetServices <IDataTrigger>()) { trigger.ConditionSqlCode(table, conditions); } }
/// <summary> /// 与更新同时执行的SQL(更新之后立即执行) /// </summary> /// <param name="table">当前数据操作对象</param> /// <param name="condition">当前场景的执行条件</param> /// <param name="code">写入SQL的文本构造器</param> /// <returns></returns> public static void AfterUpdateSql <TEntity>(IDataTable <TEntity> table, StringBuilder code, string condition) where TEntity : EditDataObject, new() { foreach (var trigger in DependencyHelper.GetServices <IDataTrigger>().Where(p => p.DataBaseType.HasFlag(table.DataBaseType))) { trigger.AfterUpdateSql(table, condition, code); } }
/// <summary> /// 更新语句后处理(单个实体操作不引发) /// </summary> /// <param name="table">当前数据操作对象</param> /// <param name="condition">执行条件</param> /// <param name="args">参数值</param> /// <param name="operatorType">操作类型</param> public static void OnOperatorExecuted <TEntity>(IDataTable <TEntity> table, string condition, IEnumerable <DbParameter> args, DataOperatorType operatorType) where TEntity : EditDataObject, new() { foreach (var trigger in DependencyHelper.GetServices <IDataTrigger>()) { trigger.OnOperatorExecuted(table, condition, args, operatorType); } }
/// <summary> /// 保存完成后期处理 /// </summary> /// <param name="data">保存的对象</param> /// <param name="operatorType">操作类型</param> public static void OnDataSaved <TEntity>(TEntity data, DataOperatorType operatorType) where TEntity : EditDataObject, new() { foreach (var trigger in DependencyHelper.GetServices <IDataTrigger>()) { trigger.OnDataSaved(data, operatorType); } }
/// <summary> /// 处理 /// </summary> /// <returns></returns> async Task Handle() { Message.RealState = MessageState.Processing; //参数处理 if (!ArgumentPrepare(action)) { return; } Message.ResultSerializer = action.ResultSerializer; Message.ResultCreater = action.ResultCreater; //扩展检查 var checkers = DependencyHelper.GetServices <IApiActionChecker>(); foreach (var checker in checkers) { if (!checker.Check(action, Message)) { return; } } GlobalContext.Current.RequestTask = new ActionTask(); GlobalContext.Current.ActionTask = new TaskCompletionSource <bool>(); try { action.Execute(Message, Service.Serialize); var(state, result) = await GlobalContext.Current.RequestTask.Task; Message.State = state; Message.ResultData = result; } //catch (TaskCanceledException) //{ // Message.State = MessageState.Cancel; // var status = DependencyHelper.GetService<IOperatorStatus>(); // status.Code = OperatorStatusCode.TimeOut; // status.Message = "操作被取消"; // Message.ResultData = status; //} catch (FormatException fe) { FlowTracer.MonitorError(() => $"参数转换出错误, 请检查调用参数是否合适:{fe.Message}"); Message.RealState = MessageState.FormalError; var status = DependencyHelper.GetService <IOperatorStatus>(); status.Code = OperatorStatusCode.ArgumentError; status.Message = "参数转换出错误, 请检查调用参数是否合适"; Message.ResultData = status; } catch (MessageArgumentNullException b) { var msg = $"参数{b.ParamName}不能为空"; FlowTracer.MonitorDetails(msg); Message.RealState = MessageState.FormalError; var status = DependencyHelper.GetService <IOperatorStatus>(); status.Code = OperatorStatusCode.ArgumentError; status.Message = msg; Message.ResultData = status; } }
/// <summary> /// 状态修改事件 /// </summary> /// <param name="database">数据库</param> /// <param name="entity">实体</param> /// <param name="oType">操作</param> /// <param name="valueType">值类型</param> /// <param name="value">内容</param> /// <remarks> /// 如果内容为实体,使用JSON序列化, /// 如果使用主键内容为#:[key](如:#:123)样式, /// 如果为批量操作,内容为QueryCondition的JSON序列化 /// </remarks> public static async Task OnStatusChanged(string database, string entity, DataOperatorType oType, EntityEventValueType valueType, string value) { var services = DependencyHelper.GetServices <IEntityEventProxy>(); if (services == null) { return; } foreach (var service in services) { await service.OnStatusChanged(database, entity, oType, valueType, value); } }
/// <summary> /// 准备 /// </summary> /// <param name="service">当前服务</param> /// <param name="message">当前消息</param> /// <param name="tag">扩展信息</param> /// <returns></returns> async Task <bool> IMessageMiddleware.Prepare(IService service, IInlineMessage message, object tag) { if (message.Service != "_health_") { return(true); } var col = Collection; Collection = new ApiCollection { Start = DateTime.Now, End = DateTime.Now }; var checkers = DependencyHelper.GetServices <IHealthCheck>(); var res = new NameValue <Dictionary <string, HealthInfo> > { Name = ZeroAppOption.Instance.HostName, Value = new Dictionary <string, HealthInfo>(StringComparer.OrdinalIgnoreCase) }; res.Value.Add("ApiCollection", new HealthInfo { Value = (col.End - col.Start).TotalSeconds, Details = col.ToJson() }); foreach (var checker in checkers) { if (res.Value.ContainsKey(checker.Name)) { continue; } DateTime start = DateTime.Now; try { var info = await checker.Check(); info.Value = (DateTime.Now - start).TotalMilliseconds; if (info.Value < 10) { info.Level = 5; } else if (info.Value < 100) { info.Level = 4; } else if (info.Value < 500) { info.Level = 3; } else if (info.Value < 3000) { info.Level = 2; } else { info.Level = 1; } res.Value.Add(checker.Name, info); } catch (Exception ex) { res.Value.Add(checker.Name, new HealthInfo { Level = -1, ItemName = checker.Name, Details = ex.Message }); } } message.RealState = MessageState.Success; message.ResultData = ApiResultHelper.Succees(res); return(false); }