/// <summary> /// 更新记录 /// </summary> /// <param name="param">ParamUpdate</param> /// <returns>int</returns> public int Update(ParamUpdate param) { var result = 0; db.UseTransaction(true); var rtnBefore = this.OnBeforeUpdate(new UpdateEventArgs() { db = db, data = param.GetData() }); if (!rtnBefore) { return(result); } string sql = BuilderParse(param).Data.Command.Data.Context.Data.FluentDataProvider.GetSqlForUpdateBuilder(BuilderParse(param).Data); LogHelper.WriteDb("(ParamUpdate)执行更新记录sql语句:" + sql); result = BuilderParse(param).Execute(); this.CommandResult.Set(true, APP.Msg_Update_Success); this.OnAfterUpdate(new UpdateEventArgs() { db = db, data = param.GetData(), executeValue = result }); db.Commit(); return(result); }
protected IUpdateBuilder BuilderParse(ParamUpdate param) { var data = param.GetData(); var updateBuilder = db.Update(data.Update.Length == 0 ? typeof(T).Name : data.Update); //除开编辑人和编辑日期字段外,其他data中的所有字段都加入参数内 var dict = GetCommonFieldValueForEdit(); foreach (var column in data.Columns.Where(column => !dict.ContainsKey(column.Key))) { updateBuilder.Column(column.Key, column.Value); } //如果实体中有编辑人和编辑日期字段,则加入参数内 var properties = JTS.Utils.ReflectionUtil.GetProperties(typeof(T)); foreach (var item in dict.Where(item => properties.ContainsKey(item.Key.ToLower()))) { updateBuilder.Column(item.Key, item.Value); } data.Where.ForEach(item => updateBuilder.Where(item.Data.Column, item.Data.Value)); //foreach (var item in data.Where) //{ // updateBuilder.Where(item.Data.Column, item.Data.Value); //} return(updateBuilder); }
/// <summary> /// 获取Update参数类 /// </summary> /// <param name="data">实体对象</param> /// <returns>ParamUpdate</returns> public ParamUpdate GetParamUpdate(T data) { var paramUpdate = ParamUpdate.Instance().Update(typeof(T).Name); PropertyInfo[] pis = typeof(T).GetProperties(); var tableAttribute = AttributeHelper.GetCustomAttribute <TableAttribute>(typeof(T)); foreach (PropertyInfo p in pis) { bool existsC = GetCommonFieldName().ContainsKey(p.Name);//排开所有公共字段 if (p.Name == "tempid" || p.Name == tableAttribute.PrimaryField || p.Name == tableAttribute.IdentityField || existsC == true) { continue; } if (tableAttribute.IgnoreUpdateFieldsList != null && tableAttribute.IgnoreUpdateFieldsList.Contains(p.Name) == true) { continue; } paramUpdate.Column(p.Name, data.GetValue(p.Name)); } paramUpdate.AndWhere(tableAttribute.PrimaryField, data.GetValue(tableAttribute.PrimaryField)); return(paramUpdate); }