开放一些常见功能的工具类代码

public static class CommonUtil
    {
        /// <summary>
        /// 显示表单
        /// </summary>
        /// <param name="view"></param>
        /// <param name="panelKey"></param>
        /// <returns></returns>
        public static void ShowForm(this IDynamicFormView view, string formId, string panelKey = null, string pageId = null, Action<FormResult> callback = null, Action<DynamicFormShowParameter> showParaCallback = null)
        {
            DynamicFormShowParameter showPara = new DynamicFormShowParameter();
            showPara.PageId = string.IsNullOrWhiteSpace(pageId) ? Guid.NewGuid().ToString() : pageId;
            showPara.ParentPageId = view.PageId;
            if (string.IsNullOrWhiteSpace(panelKey))
            {
                showPara.OpenStyle.ShowType = ShowType.Default;
            }
            else
            {
                showPara.OpenStyle.ShowType = ShowType.InContainer;
                showPara.OpenStyle.TagetKey = panelKey;
            }
            showPara.FormId = formId;
            showPara.OpenStyle.CacheId = pageId;
            if (showParaCallback != null)
            {
                showParaCallback(showPara);
            }

            view.ShowForm(showPara, callback);
        }

        /// <summary>
        /// 显示列表
        /// </summary>
        /// <param name="view"></param>
        /// <param name="formId"></param>
        /// <param name="listType"></param>
        /// <param name="bMultiSel"></param>
        /// <param name="callback"></param>
        public static void ShowList(this IDynamicFormView view, string formId, BOSEnums.Enu_ListType listType, bool bMultiSel = true, string filter = "", Action<ListShowParameter> showPara = null, Action<FormResult> callback = null)
        {
            ListShowParameter listShowPara = new ListShowParameter();
            listShowPara.FormId = formId;
            listShowPara.PageId = Guid.NewGuid().ToString();
            listShowPara.ParentPageId = view.PageId;
            listShowPara.MultiSelect = bMultiSel;
            listShowPara.ListType = (int)listType;
            if (listType == BOSEnums.Enu_ListType.SelBill)
            {
                listShowPara.IsLookUp = true;
            }
            listShowPara.ListFilterParameter.Filter = listShowPara.ListFilterParameter.Filter.JoinFilterString(filter);
            listShowPara.IsShowUsed = true;
            listShowPara.IsShowApproved = false;
            if (showPara != null) showPara(listShowPara);

            view.ShowForm(listShowPara, callback);
        }

        /// <summary>
        /// 设置某个实体整体可用性
        /// </summary>
        /// <param name="view"></param>
        /// <param name="entityKey"></param>
        /// <param name="bEnabled"></param>
        public static void SetEntityEnabled(this IDynamicFormView view, string entityKey, bool bEnabled)
        {
            EntityAppearance entityAp = view.LayoutInfo.GetEntityAppearance(entityKey);
            if (entityAp == null) return;
            foreach (var ap in entityAp.Layoutinfo.Controls)
            {
                view.StyleManager.SetEnabled(ap, null, bEnabled);
            }
        }

        /// <summary>
        /// 设置行可用性
        /// </summary>
        /// <param name="view"></param>
        /// <param name="entityKey"></param>
        /// <param name="row"></param>
        /// <param name="bEnabled"></param>
        /// <param name="exceptFieldkeys"></param>
        public static void SetEntityRowEnabled(this IDynamicFormView view, string entityKey, int row, bool bEnabled, IEnumerable<string> exceptFieldkeys = null)
        {
            Entity entity = view.BillBusinessInfo.GetEntity(entityKey);
            DynamicObject rowObj = view.Model.GetEntityDataObject(entity, row);

            SetEntityRowEnabled(view, entityKey, rowObj, bEnabled, exceptFieldkeys);
        }

        /// <summary>
        /// 设置行可用性
        /// </summary>
        /// <param name="view"></param>
        /// <param name="entityKey"></param>
        /// <param name="rowObject"></param>
        /// <param name="bEnabled"></param>
        /// <param name="exceptFieldkeys"></param>
        public static void SetEntityRowEnabled(this IDynamicFormView view, string entityKey, DynamicObject rowObject, bool bEnabled, IEnumerable<string> exceptFieldkeys = null)
        {
            if (exceptFieldkeys == null) exceptFieldkeys = new string[] { };

            foreach (Field field in (from o in view.BillBusinessInfo.GetEntryEntity(entityKey).Fields
                                     where !exceptFieldkeys.Contains(o.Key)
                                     select o).ToList<Field>())
            {
                view.StyleManager.SetEnabled(field, rowObject, null, bEnabled);
            }
        }

        /// <summary>
        /// 设置工具条整体可用状态
        /// </summary>
        /// <param name="view"></param>
        /// <param name="bEnabled">可用性</param>
        /// <param name="barOwnerKey">工具条拥有者标识,单据主工具条不用传值,表格工具条请传表格标识,其它独立工具条请传工具条标识</param>
        public static void SetBarEnabled(this IDynamicFormView view, bool bEnabled, string barOwnerKey = "")
        {
            if (string.IsNullOrWhiteSpace(barOwnerKey))
            {
                FormAppearance formAppearance = view.LayoutInfo.GetFormAppearance();
                if (((formAppearance.Menu != null) && !string.IsNullOrWhiteSpace(formAppearance.Menu.Id)) && (formAppearance.ShowMenu == 1))
                {
                    foreach (var item in formAppearance.Menu.GetAllBarItems())
                    {
                        view.SetBarItemEnabled(item.Key, bEnabled, barOwnerKey);
                    }
                }
            }
            else
            {
                EntryEntityAppearance appearance3 = view.LayoutInfo.GetEntryEntityAppearance(barOwnerKey);
                if ((appearance3 != null) && (appearance3.Menu != null))
                {
                    foreach (var item in appearance3.Menu.GetAllBarItems())
                    {
                        view.SetBarItemEnabled(item.Key, bEnabled, barOwnerKey);
                    }
                }

                ToolBarCtrlAppearance appearance4 = view.LayoutInfo.GetToolbarCtrlAppearances().FirstOrDefault(o => o.Key == barOwnerKey);
                if ((appearance4 != null) && (appearance4.Menu != null))
                {
                    foreach (var item in appearance4.Menu.GetAllBarItems())
                    {
                        view.SetBarItemEnabled(item.Key, bEnabled, barOwnerKey);
                    }
                }
            }

        }

        /// <summary>
        /// 设置按钮可用状态
        /// </summary>
        /// <param name="view"></param>
        /// <param name="barItemKey">按钮标识</param>
        /// <param name="bEnabled">可用性</param>
        /// <param name="barOwnerKey">工具条拥有者标识,单据主工具条不用传值,表格工具条请传表格标识,其它独立工具条请传工具条标识</param>
        public static void SetBarItemEnabled(this IDynamicFormView view, string barItemKey, bool bEnabled, string barOwnerKey = "")
        {
            Appearance ap = null;
            if (!string.IsNullOrWhiteSpace(barOwnerKey))
                ap = view.LayoutInfo.GetAppearance(barOwnerKey);

            BarItemControl barItem = null;
            if (ap == null)
            {
                barItem = view.GetMainBarItem(barItemKey);

                if (barItem != null)
                {
                    barItem.Enabled = bEnabled;
                }
            }

            foreach (var entityAp in view.LayoutInfo.GetEntityAppearances())
            {
                if (entityAp is HeadEntityAppearance || entityAp is SubHeadEntityAppearance) continue;

                if (barOwnerKey.IsNullOrEmptyOrWhiteSpace() || entityAp.Key.EqualsIgnoreCase(barOwnerKey))
                {
                    barItem = view.GetBarItem(entityAp.Key, barItemKey);

                    if (barItem != null)
                    {
                        barItem.Enabled = bEnabled;
                    }
                }
            }
            
        }

        /// <summary>
        /// 设置按钮可见状态
        /// </summary>
        /// <param name="view"></param>
        /// <param name="barItemKey">按钮标识</param>
        /// <param name="bVisible">可见性</param>
        /// <param name="barOwnerKey">工具条拥有者标识,单据主工具条不用传值,表格工具条请传表格标识,其它独立工具条请传工具条标识</param>
        public static void SetBarItemVisible(this IDynamicFormView view, string barItemKey, bool bVisible, string barOwnerKey = "")
        {
            Appearance ap = null;
            if (!string.IsNullOrWhiteSpace(barOwnerKey))
                ap = view.LayoutInfo.GetAppearance(barOwnerKey);

            BarItemControl barItem = null;
            if (ap == null)
            {
                barItem = view.GetMainBarItem(barItemKey);
            }
            else
            {
                barItem = view.GetBarItem(ap.Key, barItemKey);
            }
            if (barItem != null)
            {
                barItem.Visible = bVisible;
            }
        }

        /// <summary>
        /// 更新可视元素宽度
        /// </summary>
        /// <param name="formState"></param>
        /// <param name="key"></param>
        /// <param name="width"></param>
        public static void UpdateColumnWidth(this IDynamicFormView view, ControlAppearance gridAp, string colKey, int width)
        {
            IDynamicFormState formState = view.GetService<IDynamicFormState>();
            //SetFieldPropValue(formState, ctlAp.Key, "width", width, -1);
            SetColumnPropValue(formState, gridAp, colKey, "width", width);
        }

        public static void UpdateColumnHeader(this IDynamicFormView view, ControlAppearance gridAp, string colKey, string header)
        {
            IDynamicFormState formState = view.GetService<IDynamicFormState>();
            SetColumnPropValue(formState, gridAp, colKey, "header", header);
        }

        private static void SetFieldPropValue(IDynamicFormState formState, string key, string propName, object value, int rowIndex)
        {
            JSONObject obj2 = formState.GetControlProperty(key, -1, propName) as JSONObject;
            if (obj2 == null)
            {
                obj2 = new JSONObject();
            }
            obj2[rowIndex.ToString()] = value;
            formState.SetControlProperty(key, rowIndex, propName, obj2);
        }

        private static void SetColumnPropValue(IDynamicFormState formState, ControlAppearance ctlAp, string colKey, string propName, object value)
        {
            JSONObject obj2 = new JSONObject();
            obj2["key"] = colKey;
            obj2[propName] = value;
            formState.InvokeControlMethod(ctlAp, "UpdateFieldStates", new object[] { obj2 });
        }

        /// <summary>
        /// 移动表格分录
        /// </summary>
        /// <param name="view"></param>
        /// <param name="entityKey"></param>
        /// <param name="iSrcRowIndex"></param>
        /// <param name="iDstRowIndex"></param>
        /// <param name="callback"></param>
        public static void MoveEntryRow(this IDynamicFormView view, string entityKey, int iSrcRowIndex, int iDstRowIndex,Action<int,int> callback=null)
        {
            EntryEntity entryEntity = view.BillBusinessInfo.GetEntryEntity(entityKey);
            DynamicObjectCollection dataEntities = view.Model.GetEntityDataObject(entryEntity);
            if (iSrcRowIndex < 0 || iSrcRowIndex >= dataEntities.Count) return;
            if (iDstRowIndex < 0 || iDstRowIndex >= dataEntities.Count) return;
            var srcRow = dataEntities[iSrcRowIndex];
            var dstRow = dataEntities[iDstRowIndex];            
            if (iSrcRowIndex > iDstRowIndex)
            {
                dataEntities.RemoveAt(iSrcRowIndex);
                dataEntities.Insert(iDstRowIndex, srcRow);
            }
            else
            {
                dataEntities.RemoveAt(iDstRowIndex);
                dataEntities.Insert(iSrcRowIndex, dstRow);
            }

            EntryGrid grid = view.GetControl<EntryGrid>(entityKey);
            grid.ExchangeRowIndex(iSrcRowIndex, iDstRowIndex);
            grid.SetFocusRowIndex(iDstRowIndex);

            if (callback != null)
            {
                callback(iSrcRowIndex, iDstRowIndex);
            }
        }

        #region 实现块粘贴的填充功能
        /// <summary>
        /// 处理Excel块粘贴功能
        /// </summary>
        /// <param name="view"></param>
        /// <param name="e"></param>
        /// <param name="bAllowAutoNewRows">允许自动新增行</param>
        /// <param name="bCanPaste">是否允许填充某字段</param>
        public static void PasteBlockData(this IDynamicFormView view, EntityBlockPastingEventArgs e, bool bAllowAutoNewRows = false, Func<FieldAppearance, int, bool> bCanPaste = null)
        {
            if (e.BlockValue.IsNullOrEmptyOrWhiteSpace()) return;
            FieldAppearance startFieldAp = view.LayoutInfo.GetFieldAppearance(e.StartKey);
            if (startFieldAp == null || (startFieldAp.Field.Entity is EntryEntity) == false) return;
            EntryEntity entryEntity = (EntryEntity)startFieldAp.Field.Entity;
            int iTotalRows = view.Model.GetEntryRowCount(entryEntity.Key);

            var copyOperation = view.BillBusinessInfo.GetForm().FormOperations
                        .FirstOrDefault(o => o.OperationId == 31 && string.Equals(o.Parmeter.OperationObjectKey, entryEntity.Key, StringComparison.InvariantCultureIgnoreCase));
            bool isCopyLinkEntry = false;
            //如果表格未配置复制行操作,则不允许自动新增行
            if (copyOperation == null)
            {
                bAllowAutoNewRows = false;
            }
            else
            {
                isCopyLinkEntry = GetIsCopyLinkEntryParam(copyOperation.Parmeter);
            }

            string[] strBlockDataRows = e.BlockValue.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            int iRow = e.StartRow;
            foreach (var rowData in strBlockDataRows)
            {
                if (iRow >= iTotalRows)
                {
                    if (bAllowAutoNewRows)
                    {
                        view.Model.CopyEntryRow(entryEntity.Key, iRow - 1, iRow, isCopyLinkEntry);
                    }
                    else
                    {
                        break;
                    }
                }
                string[] strItemValues = rowData.Split(new char[] { '\t' });

                FieldAppearance fieldAp = startFieldAp;
                foreach (var value in strItemValues)
                {
                    if (fieldAp == null) continue;
                    object objValue = value;

                    if (typeof(ValueType).IsAssignableFrom(fieldAp.Field.GetPropertyType()))
                    {
                        if (value.IsNullOrEmptyOrWhiteSpace())
                        {
                            objValue = 0;
                        }
                        else
                        {
                            ValueTypeConverter converter = new ValueTypeConverter();
                            if (value != null && converter.CanConvertTo(value.GetType()))
                            {
                                objValue = converter.ConvertTo(value, fieldAp.Field.GetPropertyType());
                            }
                        }
                    }
                    if (bCanPaste == null || bCanPaste(fieldAp, iRow))
                    {
                        (view as IDynamicFormViewService).UpdateValue(fieldAp.Key, iRow, objValue);
                    }
                    fieldAp = GetNextEditFieldAp(view, fieldAp, iRow);
                }

                iRow++;
            }
        }

        private static FieldAppearance GetNextEditFieldAp(IDynamicFormView view, FieldAppearance fieldAp, int iRow)
        {
            FieldAppearance nextFieldAp = null;
            if (fieldAp != null)
            {
                EntryEntityAppearance entryEntityAp = view.LayoutInfo.GetEntryEntityAppearance(fieldAp.EntityKey);
                if (entryEntityAp != null)
                {
                    DynamicObject rowData = view.Model.GetEntityDataObject(entryEntityAp.Entity, iRow);
                    int iStartFindPos = entryEntityAp.Layoutinfo.Appearances.IndexOf(fieldAp);
                    if (iStartFindPos >= 0)
                    {
                        for (int i = iStartFindPos + 1; i < entryEntityAp.Layoutinfo.Appearances.Count; i++)
                        {
                            nextFieldAp = entryEntityAp.Layoutinfo.Appearances[i] as FieldAppearance;
                            if (nextFieldAp == null) continue;
                            //跳过不可见或不可编辑的字段
                            if (nextFieldAp.IsLocked(view.OpenParameter.Status) == true
                                || nextFieldAp.IsVisible(view.OpenParameter.Status) == false) continue;

                            //单元格锁定也不填充
                            if (rowData != null && view.StyleManager.GetEnabled(fieldAp, rowData) == false) continue;

                            break;
                        }
                    }
                }
            }

            return nextFieldAp;
        }

        private static bool GetIsCopyLinkEntryParam(OperationParameter operationParameter)
        {
            bool flag = false;
            string expressValue = operationParameter.ExpressValue;
            if (!string.IsNullOrEmpty(expressValue))
            {
                string[] strArray = expressValue.Split(new char[] { ':' });
                if (strArray.Length == 2)
                {
                    flag = Convert.ToInt32(strArray[1]) == 1;
                }
            }
            return flag;
        } 
        #endregion
        
    }
上述代码是一个独立的类,可以放到任何工程里用,是金蝶二开中一组常用的扩展方法,设置行,单元格,菜单等元素的可见性,可用性。

转载于:https://www.cnblogs.com/fyq891014/p/4188780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值