public int Edit(JObject data, RequestWrapper formWrapper, List <RequestWrapper> tabsWrapper) { const string DATA_TYPE_DELETED = "deleted"; const string DATA_TYPE_UPDATED = "updated"; const string DATA_TYPE_INSERTED = "inserted"; const string DATA_TYPE_FORM = "form"; const string DATA_TYPE_TABS = "tabs"; const string DATA_TYPE_ARRAY = "Array"; Dictionary <string, OptType> actionTypes = new Dictionary <string, OptType> { { DATA_TYPE_DELETED, OptType.Del }, { DATA_TYPE_UPDATED, OptType.Mod }, { DATA_TYPE_INSERTED, OptType.Add } }; var handles = new Dictionary <string, Func <RequestWrapper, int> > { { DATA_TYPE_DELETED, x => BuilderParse(x.ToParamDelete()).Execute() }, { DATA_TYPE_UPDATED, x => BuilderParse(x.ToParamUpdate()).Execute() }, { DATA_TYPE_INSERTED, x => BuilderParse(x.ToParamInsert()).Execute() } }; var rowsAffected = 0; Logger("编辑记录", () => { var formData = data[DATA_TYPE_FORM]; var tabsData = data[DATA_TYPE_TABS]; var editArgs = new EditEventArgs() { db = db, data = data, formWrapper = formWrapper, tabsWrapper = tabsWrapper }; //开启事务 db.UseTransaction(true); //更新全部数据之前事件 var rtnBefore = this.OnBeforeEdit(editArgs); if (!rtnBefore) { return; } //更新主表 if (formData != null && formWrapper != null) { formWrapper.SetValue(formData); var changedFieldCount = formWrapper.ToParamUpdate().GetData().Columns.Count; //如果有字段被修改则更新 if (changedFieldCount > 0) { //到数据库中取得旧值 var formOld = BuilderParse(formWrapper.ToParamQuery()).QuerySingleDynamic(); var strAction = (formOld == null) ? DATA_TYPE_INSERTED : DATA_TYPE_UPDATED; //事件参数 editArgs.dataNew = formData; editArgs.dataOld = formOld ?? new JObject(); editArgs.dataAction = actionTypes[strAction]; editArgs.dataWrapper = formWrapper; //主表编辑前事件 rtnBefore = this.OnBeforeEditForm(editArgs); if (!rtnBefore) { return; } //主表数据处理 formWrapper.SetValue(editArgs.dataNew); editArgs.executeValue = handles[strAction](formWrapper); rowsAffected += editArgs.executeValue; //把未修改的数据更新到form新值上,避免在后面的事件中使用formNew值时为null出现错误 if (editArgs.dataAction == OptType.Mod) { EachHelper.EachObjectProperty(editArgs.dataOld as object, (i, name, value) => { if (editArgs.dataNew[name] == null) { editArgs.dataNew[name] = JToken.FromObject(value ?? string.Empty); } }); } //主表编辑结束事件 this.OnAfterEditForm(editArgs); } } //更新tabs if (tabsData != null && tabsWrapper != null) { var current = 0; var wrapperCount = tabsWrapper.Count; foreach (JToken tab in tabsData.Children()) { //如果Tab数据长度大于wrapper长度,则结束 if (current > wrapperCount - 1) { break; } //如果数据为null或未配置相应的wrapper则跳过处理 var wrapper = tabsWrapper[current]; if (tab == null || wrapper == null) { continue; } //判断是form类型还是grid类型 bool IsGrid = (tab[DATA_TYPE_DELETED] != null && tab[DATA_TYPE_DELETED].Type.ToString() == DATA_TYPE_ARRAY) || (tab[DATA_TYPE_UPDATED] != null && tab[DATA_TYPE_UPDATED].Type.ToString() == DATA_TYPE_ARRAY) || (tab[DATA_TYPE_INSERTED] != null && tab[DATA_TYPE_INSERTED].Type.ToString() == DATA_TYPE_ARRAY); //事件参数 editArgs.dataWrapper = wrapper; editArgs.tabIndex = current++; editArgs.tabType = IsGrid ? TabType.Grid : TabType.Form; editArgs.tabData = tab; if (IsGrid) { foreach (JProperty item in tab.Children()) { //只处理deleted updated inserted三个节点 if (!handles.ContainsKey(item.Name)) { continue; } //循环每一行数据 foreach (var row in item.Value.Children()) { //到数据库中取得旧值 wrapper.SetValue(row); var rowOld = BuilderParse(wrapper.ToParamQuery()).QuerySingleDynamic(); //事件参数 editArgs.dataNew = row; editArgs.dataOld = rowOld ?? new JObject(); editArgs.dataAction = actionTypes[item.Name]; //行编辑前事件 rtnBefore = this.OnBeforeEditRow(editArgs); if (!rtnBefore) { return; } //数据处理 wrapper.SetValue(editArgs.dataNew); editArgs.executeValue = handles[item.Name](wrapper); rowsAffected += editArgs.executeValue; //行编辑后事件 this.OnAfterEditRow(editArgs); } } } else { wrapper.SetValue(tab); var changedFieldCount = wrapper.ToParamUpdate().GetData().Columns.Count; //如果有字段被修改则更新 if (changedFieldCount > 0)//更新主表 { //到数据库中取得旧值 var formOld = BuilderParse(wrapper.ToParamQuery()).QuerySingleDynamic(); var strAction = (formOld == null) ? DATA_TYPE_INSERTED : DATA_TYPE_UPDATED; //事件参数 editArgs.dataNew = tab; editArgs.dataOld = formOld ?? new JObject(); editArgs.dataAction = actionTypes[strAction]; editArgs.dataWrapper = wrapper; //Form编辑前事件 rtnBefore = this.OnBeforeEditRow(editArgs); if (!rtnBefore) { return; } //Form数据处理 wrapper.SetValue(editArgs.dataNew); editArgs.executeValue = handles[strAction](wrapper); rowsAffected += editArgs.executeValue; //把未修改的数据更新到form新值上 if (editArgs.dataAction == OptType.Mod) { EachHelper.EachObjectProperty(editArgs.dataOld as object, (i, name, value) => { if (editArgs.dataNew[name] == null) { editArgs.dataNew[name] = JToken.FromObject(value ?? string.Empty); } }); } //Form编辑结束事件 this.OnAfterEditRow(editArgs); } } } } //更新全部数据之后事件 editArgs.executeValue = rowsAffected; this.OnAfterEdit(editArgs); //提交事务 if (rowsAffected > 0) { db.Commit(); } }, e => db.Rollback()); return(rowsAffected); }
protected virtual void OnAfterEdit(EditEventArgs arg) { }
protected virtual bool OnBeforeEditRow(EditEventArgs arg) { return(true); }
public int Edit(RequestWrapper formWrapper, RequestWrapper listWrapper, JObject data) { #region 定义变量 var types = new Dictionary <string, OptType> { { "deleted", OptType.Del }, { "updated", OptType.Mod }, { "inserted", OptType.Add } }; var handles = new Dictionary <string, Func <RequestWrapper, int> > { { "deleted", x => BuilderParse(x.ToParamDelete()).Execute() }, { "updated", x => BuilderParse(x.ToParamUpdate()).Execute() }, { "inserted", x => BuilderParse(x.ToParamInsert()).Execute() } }; #endregion var rowsAffected = 0; Logger("编辑记录", () => { db.UseTransaction(true); //开启事务 var editArgs = new EditEventArgs() { db = db, form = data["form"], list = data["list"] }; if (data["form"] != null && formWrapper != null) { editArgs.wrapper = formWrapper; editArgs.formOld = this.GetDynamic(formWrapper.SetRequestData(editArgs.form).ToParamQuery()) ?? new JObject(); var rtnBefore = this.OnBeforEdit(editArgs); if (!rtnBefore) { return; } var pUpdate = formWrapper.SetRequestData(editArgs.form).ToParamUpdate(); if (pUpdate.GetData().Columns.Count > 0)//更新主表 { rtnBefore = this.OnBeforEditMaster(editArgs); if (rtnBefore) { pUpdate = formWrapper.SetRequestData(editArgs.form).ToParamUpdate(); //在before事件中更改了form中的值,刷新更新时的值 rowsAffected = BuilderParse(pUpdate).Execute(); if (rowsAffected == 0) { rowsAffected = BuilderParse(formWrapper.ToParamInsert()).Execute(); } editArgs.executeValue = rowsAffected; this.OnAfterEditMaster(editArgs); } } if (editArgs.formOld != null && editArgs.formOld.GetType() != typeof(JObject)) { EachHelper.EachObjectProperty(editArgs.formOld as object, (i, name, value) => { if (editArgs.form[name] == null) { editArgs.form[name] = JToken.FromObject(value ?? string.Empty); } }); } } if (data["list"] != null && listWrapper != null) { editArgs.wrapper = listWrapper; foreach (JProperty item in data["list"].Children()) { if (!handles.ContainsKey(item.Name)) { continue; } foreach (var row in item.Value.Children()) { editArgs.row = row; editArgs.type = types[item.Name]; editArgs.rowOld = this.GetDynamic(listWrapper.SetRequestData(editArgs.row).ToParamQuery()) ?? new JObject(); var rtnBefore = this.OnBeforEditDetail(editArgs); if (!rtnBefore) { continue; } editArgs.executeValue = handles[item.Name](listWrapper.SetRequestData(row)); rowsAffected += editArgs.executeValue; this.OnAfterEditDetail(editArgs); } } } editArgs.executeValue = rowsAffected; editArgs.wrapper = formWrapper; this.OnAfterEdit(editArgs); if (rowsAffected > 0) { db.Commit(); this.OnAfterSave(editArgs); Msg.Set(MsgType.Success, APP.MSG_SAVE_SUCCESS); } }, e => db.Rollback()); return(rowsAffected); }
protected virtual bool OnBeforEditDetail(EditEventArgs arg) { return(true); }
protected virtual bool OnBeforEditMaster(EditEventArgs arg) { return(true); }