二开案例.执行计划模拟点击单据菜单原创
金蝶云社区-齐111
齐111
13人赞赏了该文章 206次浏览 未经作者许可,禁止转载编辑于2024年10月10日 09:28:48

image.png

执行效果:

image.png


代码:

using Kingdee.BOS;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.ListFilter;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Log;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.BOS.Web.Core;
using Kingdee.BOS.Web.DynamicForm;
using Kingdee.BOS.Web.List;
using System;
using System.Collections.Generic;

namespace Jac.XkDemo.BOS.WebApi
{
    public class 执行计划模拟点击菜单_执行计划 : IScheduleService
    {
        public void Run(Context ctx, Schedule schedule)
        {
            var formId = "UNW_test20241010";

            //模拟点击单据菜单
            var view = (IDynamicFormViewService)CreateView(ctx, formId);
            view.MainBarItemClick("BtnTest");

            //模拟点击列表菜单
            var schemeId = "67072920014191";// 通过SQL查询:SELECT * FROM T_BAS_FILTERSCHEME
            var listView = (IListViewService)CreateListView(ctx, formId, schemeId);
            listView.MainBarItemClick("BtnTest");
        }

        #region 创建单据视图
        /// <summary>
        /// 创建单据视图  
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="metaData"></param>
        /// <returns></returns>
        private IDynamicFormView CreateView(Context ctx, string formId)
        {
            FormMetadata metadata = FormMetaDataCache.GetCachedFormMetaData(ctx, formId);
            var OpenParameter = CreateOpenParameter(ctx, metadata);
            var Provider = metadata.BusinessInfo.GetForm().GetFormServiceProvider(true);
            string importViewClass = "Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web";
            Type type = Type.GetType(importViewClass);
            IDynamicFormView view = (IDynamicFormView)Activator.CreateInstance(type);
            ((IDynamicFormViewService)view).Initialize(OpenParameter, Provider);
            return view;
        }


        /// <summary>
        /// 创建输入参数
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="metaData"></param>
        /// <returns></returns>
        private BillOpenParameter CreateOpenParameter(Context ctx, FormMetadata metaData)
        {
            Form form = metaData.BusinessInfo.GetForm();
            BillOpenParameter openPara = new BillOpenParameter(form.Id, metaData.GetLayoutInfo().Id);

            openPara = new BillOpenParameter(form.Id, string.Empty);
            openPara.Context = ctx;
            openPara.ServiceName = form.FormServiceName;
            openPara.PageId = Guid.NewGuid().ToString();

            // 单据
            openPara.FormMetaData = metaData;
            openPara.LayoutId = metaData.GetLayoutInfo().Id;

            // 操作相关参数
            openPara.Status = OperationStatus.ADDNEW;
            openPara.PkValue = null;
            openPara.CreateFrom = CreateFrom.Default;
            openPara.ParentId = 0;
            openPara.GroupId = "";
            openPara.DefaultBillTypeId = null;
            openPara.DefaultBusinessFlowId = null;
            // 修改主业务组织无须用户确认
            openPara.SetCustomParameter("ShowConfirmDialogWhenChangeOrg", false);

            // 插件
            List<AbstractDynamicFormPlugIn> plugins = form.CreateFormPlugIns();
            openPara.SetCustomParameter(FormConst.PlugIns, plugins);

            return openPara;
        }
        #endregion



        #region 创建列表视图
        /// 创建单据视图
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="formId"></param>
        /// <param name="schemeId"></param>
        /// <returns></returns>
        private static ListView CreateListView(Context ctx, string formId, string schemeId = null)
        {
            // 元数据
            var formMetaData = FormMetaDataCache.GetCachedFormMetaData(ctx, formId);
            // ShowParameter
            var showParam = new ListShowParameter();
            showParam.FormId = formId;
            showParam.PageId = Guid.NewGuid().ToString();
            var paraObj = showParam.CreateWebParams();
            // OpenParameter
            var dynamicFormMetaDataService = new DynamicFormMetaDataService();
            var sessionManager = new SessionManager();
            dynamicFormMetaDataService.SetContext(sessionManager, ctx);
            dynamicFormMetaDataService.FormMetaData = formMetaData;
            dynamicFormMetaDataService.GetDynamicFormConfig(paraObj);
            var openParam = sessionManager.Get<DynamicFormOpenParameter>(showParam.PageId, FormConst.OPEN_PARAM);
            openParam.SetCustomParameter("SessionManager", sessionManager);
            // 创建视图
            var listView = new ListPrintWebService(schemeId, null, null);
            // 初始化视图
            var provider = GetListServiceProvider(openParam);
            listView.Initialize(openParam, provider);
            listView.LoadData();
            return listView;
        }
        /// <summary>
        /// 获取报表服务提供者
        /// </summary>
        /// <returns></returns>
        private static IResourceServiceProvider GetListServiceProvider(DynamicFormOpenParameter param)
        {
            var provider = new FormServiceProvider();
            provider.Add(typeof(IDynamicFormView), CreateListView(param));
            provider.Add(typeof(DynamicFormViewPlugInProxy), new ListViewPlugInProxy());
            provider.Add(typeof(DynamicFormModelPlugInProxy), new ListModelPlugInProxy());
            provider.Add(typeof(IDynamicFormModelService), GetListModel(param));
            provider.Add(typeof(IListFilterModelService), GetListFilterModel());
            var type = TypesContainer.GetOrRegister("Kingdee.BOS.Business.DynamicForm.DefaultValue.DefaultValueCalculator,Kingdee.BOS.Business.DynamicForm");
            provider.Add(typeof(IDefaultValueCalculator), Activator.CreateInstance(type));
            // 注册IDBModelService
            type = TypesContainer.GetOrRegister("Kingdee.BOS.Business.DynamicForm.DBModel.DBModelService,Kingdee.BOS.Business.DynamicForm");
            provider.Add(typeof(IDBModelService), Activator.CreateInstance(type));
            return provider;
        }
        /// <summary>
        /// 获取视图
        /// </summary>
        /// <returns></returns>
        private static IDynamicFormView CreateListView(DynamicFormOpenParameter param)
        {
            var form = param.FormMetaData.BusinessInfo.GetForm();
            if (form.FormGroups != null && form.FormGroups.Count > 0)
            {
                return new TreeListView();
            }
            else
            {
                return new ListView();
            }
        }
        /// <summary>
        /// 获取视图模型
        /// </summary>
        /// <returns></returns>
        private static IDynamicFormModelService GetListModel(DynamicFormOpenParameter param)
        {
            var form = param.FormMetaData.BusinessInfo.GetForm();
            if (form.FormGroups != null && form.FormGroups.Count > 0)
            {
                var type = TypesContainer.GetOrRegister("Kingdee.BOS.Model.List.TreeListModel,Kingdee.BOS.Model");
                return (IDynamicFormModelService)Activator.CreateInstance(type);
            }
            else
            {
                var type = TypesContainer.GetOrRegister("Kingdee.BOS.Model.List.ListModel,Kingdee.BOS.Model");
                return (IDynamicFormModelService)Activator.CreateInstance(type);
            }
        }
        /// <summary>
        /// 创建过滤条件模型
        /// </summary>
        /// <returns></returns>
        private static IListFilterModelService GetListFilterModel()
        {
            var type = TypesContainer.GetOrRegister("Kingdee.BOS.Model.ListFilter.ListFilterModel,Kingdee.BOS.Model");
            return (IListFilterModelService)Activator.CreateInstance(type);
        }
        #endregion
    }


    [Kingdee.BOS.Util.HotUpdate]
    public class 执行计划模拟点击菜单_列表插件 : AbstractListPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            if (e.BarItemKey.EqualsIgnoreCase("BtnTest"))
            {
                Logger.Info("执行计划模拟点击菜单", "单据菜单点击了");
            }
        }
    }


    [Kingdee.BOS.Util.HotUpdate]
    public class 执行计划模拟点击菜单_表单插件 : AbstractDynamicFormPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            if (e.BarItemKey.EqualsIgnoreCase("BtnTest"))
            {
                Logger.Info("执行计划模拟点击菜单", "列表菜单点击了");
            }
        }
    }
}











图标赞 13
13人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!