public bool SynFieldFromDb(int formId) { CPForm form = this._CPFormRep.Get(formId); if (form == null) { return(false); } List <CPFormChildTable> tableCol = this.GetFormChildTable(formId); List <CPFormField> fieldCol = this.GetFormFieldCol(formId); List <CPFormField> add = new List <Form.CPFormField>(); List <CPFormField> edit = new List <Form.CPFormField>(); List <CPFormField> del = new List <Form.CPFormField>(); List <CPDbField> tmpAllDbField = new List <CPDbField>(); #region 先获取主表的字段 List <CPDbField> mainTableFieldCol = CPAppContext.GetTableField(form.DbIns, form.MainTableName); if (mainTableFieldCol.Count > 0) { tmpAllDbField.AddRange(mainTableFieldCol); } mainTableFieldCol.ForEach(t => { List <CPFormField> q = fieldCol.Where(c => c.TableName.Equals(t.TableName, StringComparison.CurrentCultureIgnoreCase) && c.FieldName.Equals(t.FieldName, StringComparison.CurrentCultureIgnoreCase) && c.IsChildTable == false).ToList(); if (q.Count > 0) { //修改 // q[0].IsAllowNull = t.IsAllowNull; q[0].FieldValueType = t.ValueType; q[0].FieldValueLength = t.ValueLength; edit.Add(q[0]); } else { //添加 CPFormField newQ = new CPFormField(); newQ.FormId = form.Id; newQ.IsChildTable = false; newQ.TableName = t.TableName; newQ.FieldName = t.FieldName; newQ.FieldTitle = t.FieldName; newQ.ControlType = CPFormEnum.ControlTypeEnum.TextBox; newQ.IsAllowNull = t.IsAllowNull; newQ.FieldValueType = t.ValueType; newQ.FieldValueLength = t.ValueLength; newQ.OrganIsCanMultiSel = true; newQ.FieldStatus = CPFormEnum.FieldStatusEnum.Edit; newQ.FieldTitleShowWidth = 20; newQ.ShowWidth = "98%"; newQ.MultiRows = 5; newQ.ShowHeight = 100; add.Add(newQ); } }); #endregion #region 再找子表的主段 tableCol.ForEach(childTable => { #region 先找出子表本身 List <CPFormField> qT = fieldCol.Where(c => c.TableName.Equals(childTable.TableName, StringComparison.CurrentCultureIgnoreCase) && c.FieldName.Equals(childTable.TableName, StringComparison.CurrentCultureIgnoreCase) && c.IsChildTable == true).ToList(); if (qT.Count <= 0) { //添加 CPFormField newQ = new CPFormField(); newQ.FormId = form.Id; newQ.IsChildTable = true; newQ.TableName = childTable.TableName; newQ.FieldName = childTable.TableName; newQ.FieldTitle = childTable.TableName + "子表"; newQ.ControlType = CPFormEnum.ControlTypeEnum.ChildTableExtend; newQ.OrganIsCanMultiSel = true; add.Add(newQ); } #endregion List <CPDbField> cTableFieldCol = CPAppContext.GetTableField(form.DbIns, childTable.TableName); if (cTableFieldCol.Count > 0) { tmpAllDbField.AddRange(cTableFieldCol); } cTableFieldCol.ForEach(t => { List <CPFormField> q = fieldCol.Where(c => c.TableName.Equals(t.TableName, StringComparison.CurrentCultureIgnoreCase) && c.FieldName.Equals(t.FieldName, StringComparison.CurrentCultureIgnoreCase) && c.IsChildTable == false).ToList(); if (q.Count > 0) { //修改 // q[0].IsAllowNull = t.IsAllowNull; q[0].FieldValueType = t.ValueType; q[0].FieldValueLength = t.ValueLength; edit.Add(q[0]); } else { //添加 CPFormField newQ = new CPFormField(); newQ.FormId = form.Id; newQ.IsChildTable = false; newQ.TableName = t.TableName; newQ.FieldName = t.FieldName; newQ.FieldTitle = t.FieldName; newQ.ControlType = CPFormEnum.ControlTypeEnum.TextBox; newQ.IsAllowNull = t.IsAllowNull; newQ.FieldValueType = t.ValueType; newQ.FieldValueLength = t.ValueLength; newQ.OrganIsCanMultiSel = true; newQ.FieldStatus = CPFormEnum.FieldStatusEnum.Edit; newQ.FieldTitleShowWidth = 20; newQ.ShowWidth = "98%"; newQ.MultiRows = 5; newQ.ShowHeight = 100; add.Add(newQ); } }); }); #endregion #region 再找出要删除的 fieldCol.ForEach(t => { if (t.IsChildTable == false) { List <CPDbField> qQ = tmpAllDbField.Where(c => c.TableName.Equals(t.TableName, StringComparison.CurrentCultureIgnoreCase) && c.FieldName.Equals(t.FieldName, StringComparison.CurrentCultureIgnoreCase)).ToList(); if (qQ.Count <= 0) { del.Add(t); } } else { List <CPFormChildTable> qQ = tableCol.Where(c => c.TableName.Equals(t.TableName, StringComparison.CurrentCultureIgnoreCase)).ToList(); if (qQ.Count <= 0) { del.Add(t); } } }); #endregion //由于ef core批量写入数据时,采用了批处理方法,但这种方式会导致写入顺序错,暂时没有找到什么方法,所以改成一条条写。 if (add.Count > 0) { this._CPFormFieldRep.AddOneByOne(add); } if (edit.Count > 0) { this._CPFormFieldRep.Update(edit); } if (del.Count > 0) { this._CPFormFieldRep.Delete(del); } #region 时再修改视图排版里的字段信息 List <CPFormView> viewCol = this.GetFormView(form.Id, true); if (add.Count > 0) { #region 增加的 viewCol.ForEach(t => { if (t.ViewType == CPFormEnum.ViewTypeEnum.OneColumn || t.ViewType == CPFormEnum.ViewTypeEnum.TwoColumn) { int nOrder = 0; if (t.ViewFieldCol.Count > 0) { nOrder = t.ViewFieldCol.OrderByDescending(c => c.ShowOrder).FirstOrDefault().ShowOrder.Value; nOrder = nOrder + 10; } List <CPFormViewField> col = new List <CPFormViewField>(); add.ForEach(f => { CPFormViewField vT = new CPFormViewField(); vT.FormId = form.Id; vT.ViewId = t.Id; vT.FieldId = f.Id; vT.ShowOrder = nOrder; nOrder = nOrder + 10; vT.ViewBlockIndex = 0; // vT.FieldStatus = CPFormEnum.FieldStatusEnum.Edit; vT.IsSpanAll = false; //vT.FieldTitleShowWidth = 20; //vT.ShowWidth = "98%"; //vT.MultiRows = 5; //vT.ShowHeight = 100; col.Add(vT); }); if (col.Count > 0) { this._CPFormViewFieldRep.Add(col); } } }); #endregion } if (del.Count > 0) { #region 除的 viewCol.ForEach(t => { if (t.ViewType == CPFormEnum.ViewTypeEnum.OneColumn || t.ViewType == CPFormEnum.ViewTypeEnum.TwoColumn) { List <CPFormViewField> col = new List <CPFormViewField>(); t.ViewFieldCol.ForEach(c => { if (del.Where(f => f.Id.Equals(c.FieldId)).ToList().Count > 0) { col.Add(c); } }); if (col.Count > 0) { this._CPFormViewFieldRep.Delete(col); } } }); #endregion } #endregion return(true); }