/// <summary> /// 更新实体记录 /// </summary> /// <param name="entity"></param> /// <returns></returns> public int Update(BaseEntity entity) { return(this.ExecuteTransaction(() => { #region 更新前 Plugin var context = new PersistBrokerPluginContext() { Broker = this, Entity = entity, EntityName = entity.EntityName, Action = EntityAction.PreUpdate }; ServiceContainer.ResolveAll <IPersistBrokerBeforeCreateOrUpdate>()? .Each(item => item.Execute(context)); ServiceContainer.ResolveAll <IPersistBrokerPlugin>(item => item.StartsWith(entity.EntityName.Replace("_", ""), StringComparison.OrdinalIgnoreCase)) .Each(item => item.Execute(context)); #endregion var sql = @" UPDATE {0} SET {1} WHERE {2} = @id; "; var paramList = new Dictionary <string, object>() { { "@id", entity.Id } }; #region 处理属性 var attributes = new List <string>(); int count = 0; foreach (var item in entity.GetAttributes()) { if (item.Key != "Id" && item.Key != entity.EntityName + "Id") { var keyValue = ParseSqlUtil.GetSpecialValue($"@param{count}", item.Value); paramList.Add($"@param{count}", keyValue.value); attributes.Add($"{ item.Key} = {keyValue.name}"); count++; } } #endregion sql = string.Format(sql, entity.EntityName, string.Join(",", attributes), entity.MainKeyName); var result = this.Execute(sql, paramList); #region 更新后 Plugin context.Action = EntityAction.PostUpdate; ServiceContainer.ResolveAll <IPersistBrokerPlugin>(item => item.StartsWith(entity.EntityName.Replace("_", ""), StringComparison.OrdinalIgnoreCase)) .Each(item => item.Execute(context)); #endregion return result; })); }
/// <summary> /// 创建实体记录 /// </summary> /// <param name="entity"></param> /// <returns></returns> public string Create(BaseEntity entity, bool usePlugin = true) { return(this.ExecuteTransaction(() => { #region 创建前 Plugin var context = new PersistBrokerPluginContext() { Entity = entity, Broker = this, Action = EntityAction.PreCreate, EntityName = entity.EntityName }; ServiceContainer.ResolveAll <IPersistBrokerBeforeCreateOrUpdate>()? .Each(item => item.Execute(context)); if (usePlugin) { ServiceContainer.ResolveAll <IPersistBrokerPlugin>(item => item.StartsWith(entity.EntityName.Replace("_", ""), StringComparison.OrdinalIgnoreCase)) .Each(item => item.Execute(context)); } #endregion var sql = "INSERT INTO {0}({1}) Values({2})"; var attrs = new List <string>(); var values = new List <object>(); var paramList = new Dictionary <string, object>(); foreach (var attr in entity.GetAttributes()) { var attrName = attr.Key == "Id" ? entity.MainKeyName : attr.Key; var keyValue = ParseSqlUtil.GetSpecialValue($"@{attrName}", attr.Value); attrs.Add(attrName); values.Add(keyValue.name); paramList.Add(attrName, keyValue.value); } sql = string.Format(sql, entity.EntityName, string.Join(",", attrs), string.Join(",", values)); this.Execute(sql, paramList); #region 创建后 Plugin if (usePlugin) { context.Action = EntityAction.PostCreate; ServiceContainer.ResolveAll <IPersistBrokerPlugin>(item => item.StartsWith(entity.EntityName.Replace("_", ""), StringComparison.OrdinalIgnoreCase)) .Each(item => item.Execute(context)); } #endregion return entity.Id; })); }
/// <summary> /// 重复字段检查 /// </summary> /// <param name="entity"></param> /// <param name="broker"></param> private void CheckDuplicate(BaseEntity entity, IPersistBroker broker) { var attrs = entity.GetType().GetCustomAttributes(typeof(KeyAttributesAttribute), false); if (attrs.Length == 0) { return; } attrs.Select(item => item as KeyAttributesAttribute) .Each(item => { if (item.AttributeList == null || item.AttributeList.Count == 0) { return; } var paramList = new Dictionary <string, object>() { { "@id", entity.Id } }; var sqlParam = new List <string>() { $" AND {entity.EntityName}Id <> @id" }; // 排除自身 item.AttributeList.Distinct().Each(attr => { var keyValue = ParseSqlUtil.GetSpecialValue($"@{attr}", entity[attr]); sqlParam.Add($" AND {attr} = {keyValue.name}"); paramList.Add(keyValue.name, keyValue.value); }); var sql = string.Format(@"SELECT {0}Id FROM {0} WHERE 1 = 1 ", entity.EntityName) + string.Join("", sqlParam); AssertUtil.CheckBoolean <SpException>(broker.Query <string>(sql, paramList)?.Count() > 0, item.RepeatMessage, "7293452C-AFCA-408D-9EBD-B1CECD206A7D"); }); }