二开案例.单据插件.获取字段的锁定性策略集合
【应用场景】
字段锁定采用的表决器模式。
表决器工作原理:
锁定一个字段,只需要一个表决项就能锁定。
解锁一个字段,则需要所有表决项都解锁。
表决项万万千,五花八门,如果不知道是谁锁定了字段,大概率就无法解锁这个字段了。
常见的锁定字段的方式有:
<1>字段的锁定性
<2>单据头状态字段锁定
<3>单据体状态字段锁定
<4>实体服务规则锁定
<5>插件锁定
【案例演示】
销售出库单,已审核单据,解锁明细信息单据体的是否赠品字段。
【实现步骤】
<1>编写单据插件,代码如下。
using Kingdee.BOS.Core.Bill.PlugIn; using Kingdee.BOS.Core.DynamicForm.PlugIn.Args; using Kingdee.BOS.Core.Metadata.ControlElement; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.Util; using Newtonsoft.Json; using System; using System.Collections; using System.ComponentModel; using System.Reflection; using System.Text; namespace Jac.XkDemo.BOS.Business.PlugIn { /// <summary> /// 【单据插件】获取字段的锁定性策略集合 /// </summary> [Description("【单据插件】获取字段的锁定性策略集合"), HotUpdate] public class GetFieldEnabledStoreBillPlugIn : AbstractBillPlugIn { private const string key = "FIsFree"; public override void BarItemClick(BarItemClickEventArgs e) { base.BarItemClick(e); #region 获取字段的锁定性策略集合 if (e.BarItemKey.EqualsIgnoreCase("test")) { var msg = new StringBuilder(); msg.Append("FieldKey:" + key + "\r\n"); GetControlAppearanceStyles(msg, key); GetControlInitAppearanceStyles(msg, key); GetControlCellStyles(msg, key); GetInitEnabled(msg, key); this.View.ShowMessage(msg.ToString()); } #endregion #region 解锁字段 else if (e.BarItemKey.EqualsIgnoreCase("test2")) { // 解锁实体服务规则产生的锁定控制 var field = this.View.BillBusinessInfo.GetField(key); var rowIndex = this.Model.GetEntryCurrentRowIndex(field.EntityKey); var row = this.Model.GetEntityDataObject(field.Entity, rowIndex); var entityServiceRuleUd = "cc57e38d-e60f-44f1-a6c1-b9bddab8e1e9"; // 实体服务规则的内码要去元数据XML中获取 //this.View.StyleManager.SetEnabled(key, entityServiceRuleUd, true);// 解锁单据头字段 this.View.StyleManager.SetEnabled(key, row, entityServiceRuleUd, true); // 解锁单据体字段 // 解锁单据状态产生的锁定控制 this.View.StyleManager.SetEnabled(key, "BillStatusByHead", true);// 解锁单据头状态字段产生的锁定控制 //this.View.StyleManager.SetEnabled(key, "BillStatusByEntry", true);// 解锁单据体状态字段产生的锁定控制 // 解锁其它未知的锁定控制 this.View.StyleManager.SetEnabled(key, "", true); //this.View.UpdateView(key);// 刷新单据头字段 this.View.UpdateView(key, rowIndex); // 刷新单据体字段 this.View.ShowMessage("字段已解锁!"); #endregion } } /// <summary> /// 获取字段的锁定性策略集合 /// </summary> /// <param name="msg"></param> /// <param name="key"></param> /// <returns></returns> private void GetControlAppearanceStyles(StringBuilder msg, string key) { msg.Append("_controlAppearanceStyles============================================================\r\n"); var bosStyleManager = this.View.StyleManager; var _enabledStore = RefUtils.GetFieldValue(bosStyleManager, BindingFlags.Instance | BindingFlags.NonPublic, "_enabledStore"); if (_enabledStore == null) { msg.Append("_enabledStore=null\r\n"); return; } var _controlAppearanceStyles = RefUtils.GetFieldValue(_enabledStore, _enabledStore.GetType().BaseType.BaseType.BaseType, BindingFlags.Instance | BindingFlags.NonPublic, "_controlAppearanceStyles"); if (_controlAppearanceStyles == null) { msg.Append("_controlAppearanceStyles=null\r\n"); return; } var hasKey = RefUtils.GetMethodInvokeValue(_controlAppearanceStyles, BindingFlags.Instance | BindingFlags.Public, "ContainsKey", new object[] { key }); if (hasKey == null || !Convert.ToBoolean(hasKey)) { msg.Append("_controlAppearanceStyles not contains key\r\n"); return; } var enabledValue = RefUtils.GetMethodInvokeValue(_controlAppearanceStyles, BindingFlags.Instance | BindingFlags.Public, "get_Item", new object[] { key }); if (enabledValue == null) { msg.Append("enabledValue=null\r\n"); return; } var _policyList = RefUtils.GetFieldValue(enabledValue, enabledValue.GetType().BaseType, BindingFlags.Instance | BindingFlags.NonPublic, "_policyList"); if (_policyList == null) { msg.Append("_policyList=null\r\n"); return; } msg.Append("_policyList:\r\n" + JsonConvert.SerializeObject(_policyList)); msg.Append("\r\n"); } /// <summary> /// 获取字段的初始锁定性策略集合 /// </summary> /// <param name="msg"></param> /// <param name="key"></param> /// <returns></returns> private void GetControlInitAppearanceStyles(StringBuilder msg, string key) { msg.Append("_controlInitAppearanceStyles============================================================\r\n"); var bosStyleManager = this.View.StyleManager; var _enabledStore = RefUtils.GetFieldValue(bosStyleManager, BindingFlags.Instance | BindingFlags.NonPublic, "_enabledStore"); if (_enabledStore == null) { msg.Append("_enabledStore=null\r\n"); return; } var _controlInitAppearanceStyles = RefUtils.GetFieldValue(_enabledStore, _enabledStore.GetType().BaseType.BaseType.BaseType, BindingFlags.Instance | BindingFlags.NonPublic, "_controlInitAppearanceStyles"); if (_controlInitAppearanceStyles == null) { msg.Append("_controlInitAppearanceStyles=null\r\n"); return; } var hasKey = RefUtils.GetMethodInvokeValue(_controlInitAppearanceStyles, BindingFlags.Instance | BindingFlags.Public, "ContainsKey", new object[] { key }); if (hasKey == null || !Convert.ToBoolean(hasKey)) { msg.Append("_controlInitAppearanceStyles not contains key\r\n"); return; } var enabledValue = RefUtils.GetMethodInvokeValue(_controlInitAppearanceStyles, BindingFlags.Instance | BindingFlags.Public, "get_Item", new object[] { key }); if (enabledValue == null) { msg.Append("enabledValue=null\r\n"); return; } msg.Append("enabledValue=" + enabledValue + "\r\n"); msg.Append("\r\n"); } // <summary> /// 获取单据体字段的单元格锁定性策略集合 /// </summary> /// <param name="msg"></param> /// <param name="key"></param> /// <returns></returns> private void GetControlCellStyles(StringBuilder msg, string key) { msg.Append("_enabledStore._cellStyles============================================================\r\n"); var bosStyleManager = this.View.StyleManager; var _enabledStore = RefUtils.GetFieldValue(bosStyleManager, BindingFlags.Instance | BindingFlags.NonPublic, "_enabledStore"); if (_enabledStore == null) { msg.Append("_enabledStore=null\r\n"); return; } var _cellStyles = RefUtils.GetFieldValue(_enabledStore, _enabledStore.GetType().BaseType.BaseType, BindingFlags.Instance | BindingFlags.NonPublic, "_cellStyles"); if (_cellStyles == null) { msg.Append("_cellStyles=null\r\n"); return; } // Dictionary<string, Dictionary<DynamicObject, StyleObject<T>>> var hasKey = RefUtils.GetMethodInvokeValue(_cellStyles, BindingFlags.Instance | BindingFlags.Public, "ContainsKey", new object[] { key }); if (hasKey == null || !Convert.ToBoolean(hasKey)) { msg.Append("_cellStyles not contains key\r\n"); return; } var enabledValue = RefUtils.GetMethodInvokeValue(_cellStyles, BindingFlags.Instance | BindingFlags.Public, "get_Item", new object[] { key }) as IDictionary; if (enabledValue == null) { msg.Append("enabledValue=null\r\n"); return; } var enumerator = enabledValue.GetEnumerator(); for (var i = 0; i < enabledValue.Count; ++i) { enumerator.MoveNext(); var dicKey = enumerator.Key as DynamicObject; if (dicKey == null) { continue; } msg.Append("key=" + dicKey.DynamicObjectType.ExtendName + ";EntryId=" + dicKey[0]); var dicValue = enumerator.Value; var _policyList = RefUtils.GetFieldValue(dicValue, dicValue.GetType().BaseType, BindingFlags.Instance | BindingFlags.NonPublic, "_policyList"); if (_policyList == null) { msg.Append(";_policyList=null\r\n"); } else { msg.Append(";_policyList=\r\n" + JsonConvert.SerializeObject(_policyList)); } } msg.Append("\r\n"); } private void GetInitEnabled(StringBuilder msg, string key) { msg.Append("GetInitEnabled============================================================\r\n"); var ctl = this.View.LayoutInfo.GetAppearance(key) as ControlAppearance; msg.Append("OpenParameter.Status:" + this.View.OpenParameter.Status + "\r\n"); msg.Append("FieldAppearance.Locked:" + ctl.Locked + "\r\n"); msg.Append("FieldAppearance.LockedExt:" + ctl.LockedExt + "\r\n"); //bool bEnabled = !IsControlLocked(ctl, this.OpenParameter.Status); var enabled = !Convert.ToBoolean(RefUtils.GetMethodInvokeValue(this.View, BindingFlags.Instance | BindingFlags.NonPublic, "IsControlLocked", new object[] { ctl, this.View.OpenParameter.Status })); msg.Append("ControlEnabled(控件级可用):" + enabled + "\r\n"); //bEnabled = this.IsFieldAllow(bEnabled, ctl, this._fieldListAllowEdit); var _fieldListAllowEdit = RefUtils.GetFieldValue(this.View, this.View.GetType().BaseType.BaseType, BindingFlags.Instance | BindingFlags.NonPublic, "_fieldListAllowEdit"); enabled = Convert.ToBoolean(RefUtils.GetMethodInvokeValue(this.View, this.View.GetType().BaseType.BaseType, BindingFlags.Instance | BindingFlags.NonPublic, "IsFieldAllow", new object[] { enabled, ctl, _fieldListAllowEdit })); msg.Append("IsFieldAllow(权限级可用):" + enabled + "\r\n"); } #region 反射工具类 /// <summary> /// 反射工具类 /// </summary> public static class RefUtils { /// <summary> /// 获取字段值 /// </summary> /// <param name="instance"></param> /// <param name="bindingAttr"></param> /// <param name="fieldName"></param> /// <returns></returns> public static object GetFieldValue(object instance, BindingFlags bindingAttr, string fieldName) { return GetFieldValue(instance, instance.GetType(), bindingAttr, fieldName); } /// <summary> /// 获取字段值 /// </summary> /// <param name="instance"></param> /// <param name="instranceType"></param> /// <param name="bindingAttr"></param> /// <param name="fieldName"></param> /// <returns></returns> public static object GetFieldValue(object instance, Type instranceType, BindingFlags bindingAttr, string fieldName) { var field = instranceType.GetField(fieldName, bindingAttr); if (field == null) { return null; } return field.GetValue(instance); } /// <summary> /// 调用方法,获取方法返回值 /// </summary> /// <param name="instance"></param> /// <param name="bindingAttr"></param> /// <param name="methodName"></param> /// <param name="methodArgs"></param> /// <returns></returns> public static object GetMethodInvokeValue(object instance, BindingFlags bindingAttr, string methodName, object[] methodArgs) { return GetMethodInvokeValue(instance, instance.GetType(), bindingAttr, methodName, methodArgs); } /// <summary> /// 调用方法,获取方法返回值 /// </summary> /// <param name="instance"></param> /// <param name="instranceType"></param> /// <param name="bindingAttr"></param> /// <param name="methodName"></param> /// <param name="methodArgs"></param> /// <returns></returns> public static object GetMethodInvokeValue(object instance, Type instranceType, BindingFlags bindingAttr, string methodName, object[] methodArgs) { var method = instranceType.GetMethod(methodName, bindingAttr); return method.Invoke(instance, methodArgs); } } #endregion } }
<2>拷贝插件组件到应用站点的WebSite\Bin目录下,重启IIS。
<3>BOSIDE扩展销售出库单,菜单集合新增两个菜单项,注册表单插件,保存元数据,开发完毕。
【功能验证】
<1>登录业务站点,打开销售出库单已审核单据的编辑界面,如下图所示,先查看下是否赠品字段的锁定情况。
从上图的提示信息可知:
<1>单据头上的单据状态字段的已审核状态,锁定了是否赠品字段。
<2>内码为cc57e38d-e60f-44f1-a6c1-b9bddab8e1e9的实体服务规则,锁定了是否赠品字段。
只有这两个锁定,都解锁掉,是否赠品字段才能真正解锁。
---------------------------------------------------------------------------------------------------------
【金蝶云星空BOS二次开发案例演示】https://vip.kingdee.com/article/94751030918525696
二开案例.单据插件.获取字段的锁定性策略集合
【应用场景】字段锁定采用的表决器模式。表决器工作原理:锁定一个字段,只需要一个表决项就能锁定。解锁一个字段,则需要所有表决项都解锁...
点击下载文档
本文2024-09-23 04:01:17发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-162599.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章