二开插件-执行计划同步第三方库动态创建单据原创
金蝶云社区-墨白64
墨白64
33人赞赏了该文章 827次浏览 未经作者许可,禁止转载编辑于2023年09月15日 17:29:51
using Kingdee.BOS;
using Kingdee.BOS.Core;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Log;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.BusinessEntity.Organizations;





namespace BL.K3.Plugln.APP.Core
{
    public class AutoSyncFlowScheduleService : IScheduleService
    {

        // 数据库连接字符串
        private string connectionString = "Data Source='数据库服务器';Initial Catalog='数据库名';User ID=sa;Password=123456";        
        /// <summary>
        /// 执行计划入口函数
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="schedule"></param>
        public void Run(Context ctx, Schedule schedule)
        {
            try
            {
                //系统管理员的当前登录组织是固定的,内码为1.
                long orgId = 340390;
                //得到组织并给执行计划上下文赋值
                Organization curOrg = OrganizationServiceHelper.ReadOrgInfoByOrgId(ctx, orgId);
                List<long> functions = Array.ConvertAll(curOrg.OrgFunctions.Split(','), (a) =>
                { return Convert.ToInt64(a); }).ToList();
                ctx.CurrentOrganizationInfo = new OrganizationInfo() //给当前上下文增加组织
                {
                    ID = curOrg.Id,
                    Name = curOrg.Name,
                    FunctionIds = functions,
                    AcctOrgType = curOrg.AcctOrgType
                };
                string query = "/*dialect*/select Code from MI_Flow where Code <> 'ME' and shop <> '' ";
                DataTable dataTable = ExecuteSqlQuery(query);
                if (dataTable.Rows.Count > 0)
                {
                    String[] code = dataTable.AsEnumerable().Select(row => row.Field<string>("Code").Trim()).ToArray();//AMB
                    string sql = "select FNUMBER from T_ENG_PROCESS";
                    var ret = DBServiceHelper.ExecuteDynamicObject(ctx, sql);
                    String[] number = ret.Select(row => row["FNUMBER"].ToString()).ToArray(); //金蝶
                    String[] differ = code.Except(number).ToArray();//差集
                    String difCode = "'"+String.Join("','",differ)+"'";
                    string sql2 = string.Format("/*dialect*/select Code,Name,PNLPCS,Shop from MI_Flow where Code IN ({0}) ", difCode);
                    DataTable dataTable2 = ExecuteSqlQuery(sql2);
                    if (dataTable.Rows.Count > 0)
                    {
                        foreach (DataRow row in dataTable2.Rows)
                        {
                            String[] obj = new String[4];
                            obj[0] = Convert.ToString(row.ItemArray[0]).Trim();//编码
                            obj[1] = Convert.ToString(row.ItemArray[1]).Trim();//名称
                            obj[2] = Convert.ToString(row.ItemArray[2]).Trim();//过数
                            obj[3] = Convert.ToString(row.ItemArray[3]).Trim();//工作中心
                            ImportBill(ctx, obj);
                        }
                    }     
                }
            }
            catch (Exception ex)
            {
                // 处理异常
                Logger.Info("ENG", string.Format("自动同步阿米巴工序失败!errorMessage:{0}", ex.Message));
            }
        }
        //执行sql返回DataTable
        public DataTable ExecuteSqlQuery(string sqlQuery)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand command = new SqlCommand(sqlQuery, connection))
                {
                    connection.Open();
                    using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                    {
                        DataTable dataTable = new DataTable();
                        adapter.Fill(dataTable);
                        return dataTable;
                    }                    
                }
            }
        }
        public void ImportBill(Context ctx,String[] obj)
        {
            // 构建一个IBillView实例,通过此实例,可以方便的填写物料各属性
            IBillView billView = this.CreateBillView(ctx, "ENG_Process");
            // 新建一个空白物料
            ((IBillViewService)billView).LoadData();
            // 触发插件的OnLoad事件:
            // 组织控制基类插件,在OnLoad事件中,对主业务组织改变是否提示选项进行初始化。
            // 如果不触发OnLoad事件,会导致主业务组织赋值不成功
            DynamicFormViewPlugInProxy eventProxy = billView.GetService<DynamicFormViewPlugInProxy>();
            eventProxy.FireOnLoad();
            // 填写物料各属性
            this.FillBillPropertys(billView,obj);
            // 保存物料
            OperateOption saveOption = OperateOption.Create();
            this.SaveBill(billView, saveOption,ctx);
        }
        public void FillBillPropertys(IBillView billView,String[] obj)
        {
            // 把billView转换为IDynamicFormViewService接口:
            // 调用IDynamicFormViewService.UpdateValue: 会执行字段的值更新事件
            // 调用 dynamicFormView.SetItemValueByNumber :不会执行值更新事件,需要继续调用:
            IDynamicFormViewService dynamicFormView = billView as IDynamicFormViewService;
            //给编码赋值
            dynamicFormView.UpdateValue("FNumber", 0, obj[0]);
            //名称赋值
            dynamicFormView.UpdateValue("FName", 0, obj[1]);
            //是否过数
            if (obj[2].ToString().Equals("PNL"))
            {
                dynamicFormView.UpdateValue("F_BL_isPass", 0, true);
            }           
            dynamicFormView.SetItemValueByNumber("FWorkCenterID",obj[3] , 0);            
        }        
        public void SaveBill(IBillView billView, OperateOption saveOption,Context ctx)
        {
            try
            {
                object[] pkArray = null;
                Form form = billView.BillBusinessInfo.GetForm();
                if (form.FormIdDynamicProperty != null)
                {
                    form.FormIdDynamicProperty.SetValue(billView.Model.DataObject, form.Id);
                }           
                // 调用保存操作
                IOperationResult saveResult = BusinessDataServiceHelper.Save(
                ctx,
                billView.BillBusinessInfo,
                billView.Model.DataObject,
                saveOption,
                "Save");
                if (saveResult.IsSuccess)
                {
                    pkArray = (from p in (saveResult as OperationResult).SuccessDataEnity
                                            select p[0]).ToArray();
                }
                if (pkArray !=null)
                {   
                    IOperationResult submitResult = BusinessDataServiceHelper.Submit(
                        ctx,
                        billView.BillBusinessInfo, 
                        pkArray.ToArray(), 
                        "Submit",
                        saveOption
                        );
                    if (submitResult.IsSuccess)
                    {
                        pkArray = (from p in (submitResult as OperationResult).SuccessDataEnity
                                   select p[0]).ToArray();
                    }
                    if (pkArray != null)
                    {
                        IOperationResult AuditResult = BusinessDataServiceHelper.Audit(
                            ctx,
                            billView.BillBusinessInfo,
                            pkArray,
                            null
                            );
                    }
                }
                
            }
            catch (Exception ex)
            {
                // 处理异常
                Logger.Info("ENG", string.Format("作业单据保存失败!errorMessage:{0}", ex.Message));
            }
        }
        //创建一个单据视图,后续将利用此视图的各种方法,设置物料字段值
        public IBillView CreateBillView(Context ctx,String formId)
        {
            //读取作业的元数据
            FormMetadata meta = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
            Form form = meta.BusinessInfo.GetForm();
            // 创建用于引入数据的单据view
            Type type = Type.GetType("Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web");
            var billView = (IDynamicFormViewService)Activator.CreateInstance(type);
            // 开始初始化billView:
            // 创建视图加载参数对象,指定各种参数,如FormId, 视图(LayoutId)等
            BillOpenParameter openParam = CreateOpenParameter(meta,ctx);
            // 动态领域模型服务提供类,通过此类,构建MVC实例
            var provider = form.GetFormServiceProvider();
            billView.Initialize(openParam, provider);
            return billView as IBillView;
        }
        //创建视图加载参数对象,指定各种初始化视图时,需要指定的属性
        public BillOpenParameter CreateOpenParameter(FormMetadata meta,Context ctx)
        {
            Form form = meta.BusinessInfo.GetForm();
            // 指定FormId, LayoutId
            BillOpenParameter openParam = new BillOpenParameter(form.Id, meta.GetLayoutInfo().Id);
            openParam.Context = ctx;
            // 本单据模型使用的MVC框架
            openParam.ServiceName = form.FormServiceName;
            // 随机产生一个不重复的PageId,作为视图的标识
            openParam.PageId = Guid.NewGuid().ToString();
            // 元数据
            openParam.FormMetaData = meta;
            // 界面状态:新增 (修改、查看)
            openParam.Status = OperationStatus.ADDNEW;
            // 单据主键:本案例演示新建物料,不需要设置主键
            openParam.PkValue = null;
            // 界面创建目的:普通无特殊目的 (为工作流、为下推、为复制等)
            openParam.CreateFrom = CreateFrom.Default;
            // 基础资料分组维度:基础资料允许添加多个分组字段,每个分组字段会有一个分组维度
            // 具体分组维度Id,请参阅 form.FormGroups 属性
            openParam.GroupId = "";
            // 基础资料分组:如果需要为新建的基础资料指定所在分组,请设置此属性
            openParam.ParentId = 0;
            // 单据类型
            openParam.DefaultBillTypeId = "";
            // 业务流程
            openParam.DefaultBusinessFlowId = "";
            // 主业务组织改变时,不用弹出提示界面
            openParam.SetCustomParameter("ShowConfirmDialogWhenChangeOrg", false);
            // 插件
            var plugs = form.CreateFormPlugIns();
            openParam.SetCustomParameter(FormConst.PlugIns, plugs);
            PreOpenFormEventArgs args = new PreOpenFormEventArgs(ctx, openParam);
            foreach (var plug in plugs)
            {
                // 触发插件PreOpenForm事件,供插件确认是否允许打开界面
                plug.PreOpenForm(args);
            } 
            if (args.Cancel == true)
            {
                // 插件不允许打开界面
                // 本案例不理会插件的诉求,继续....
            }
            // 返回
            return openParam;           
        }
        private void ModifyBill(IBillView billView, string pkValue)
        {
            billView.OpenParameter.Status = OperationStatus.EDIT;
            billView.OpenParameter.CreateFrom = CreateFrom.Default;
            billView.OpenParameter.PkValue = pkValue;
            billView.OpenParameter.DefaultBillTypeId = string.Empty;
            ((IDynamicFormViewService)billView).LoadData();
        }
    }
}


①执行计划插件开发参考:执行计划插件开发指南 二开案例.执行计划.获取并保存账表数据


②插件中生成单据直接构造单据的数据包生成就行。

可参考:知识分享 - 如何使用纯插件创建物料并保存


更多插件生成单据的开发参考:金蝶云星空 如何使用插件生成单据



如果遇到执行计划不能自动执行   

https://vip.kingdee.com/questions/7429/answers/10923?productLineId=1&isKnowledge=2


同时若 检测到 对应的 Job服务未开启    查阅官方文档  查阅官方解决资料

        

K/3 Cloud Job Process

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe" /i "D:\Program Files (x86)\Kingdee\K3Cloud\WebSite\bin\Kingdee.BOS.ScheduleService.exe"


    

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