Esempio n. 1
0
        /// <summary>
        /// 保存操作完毕,事务结束之前,进行自动提交、审核
        /// </summary>
        /// <param name="e"></param>
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            // 取到需要自动提交、审核的单据内码
            object[] pkArray = (from p in e.DataEntitys
                                select p[0]).ToArray();

            // 设置提交参数
            // using Kingdee.BOS.Orm;
            OperateOption submitOption = OperateOption.Create();

            submitOption.SetIgnoreWarning(this.Option.GetIgnoreWarning());
            submitOption.SetInteractionFlag(this.Option.GetInteractionFlag());
            submitOption.SetIgnoreInteractionFlag(this.Option.GetIgnoreInteractionFlag());

            // 创建提交服务:using Kingdee.BOS.Contracts; using Kingdee.BOS.App;
            ISubmitService   submitService = ServiceHelper.GetService <ISubmitService>();
            IOperationResult submitResult  = submitService.Submit(
                this.Context, this.BusinessInfo,
                pkArray, "Submit", submitOption);

            // 判断提交结果,如果失败,则内部会抛出错误,回滚代码
            if (CheckOpResult(submitResult) == false)
            {
                return;
            }

            // 构建操作可选参数对象
            OperateOption auditOption = OperateOption.Create();

            auditOption.SetIgnoreWarning(this.Option.GetIgnoreWarning());
            auditOption.SetInteractionFlag(this.Option.GetInteractionFlag());
            auditOption.SetIgnoreInteractionFlag(this.Option.GetIgnoreInteractionFlag());

            // 构建单据主键参数
            List <KeyValuePair <object, object> > pkEntityIds = new List <KeyValuePair <object, object> >();

            foreach (var pkValue in pkArray)
            {
                pkEntityIds.Add(new KeyValuePair <object, object>(pkValue, ""));
            }

            List <object> paras = new List <object>();

            paras.Add("1");
            paras.Add("");

            // 调用审核操作
            ISetStatusService setStatusService = ServiceHelper.GetService <ISetStatusService>();

            // 如下调用方式,需显示交互信息
            IOperationResult auditResult = setStatusService.SetBillStatus(this.Context,
                                                                          this.BusinessInfo,
                                                                          pkEntityIds,
                                                                          paras,
                                                                          "Audit",
                                                                          auditOption);

            // 判断审核结果,如果失败,则内部会抛出错误,回滚代码
            if (CheckOpResult(auditResult) == false)
            {
                return;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 自动下推并保存
        /// </summary>
        /// <param name="sourceFormId">源单FormId</param>
        /// <param name="targetFormId">目标单FormId</param>
        /// <param name="sourceBillIds">源单内码</param>
        private IOperationResult DoPush(string sourceFormId, string targetFormId, long targetOrgId, List <long> sourceBillIds)
        {
            IOperationResult result = new OperationResult();

            result.IsSuccess = false;
            // 获取源单与目标单的转换规则
            IConvertService convertService = Kingdee.BOS.App.ServiceHelper.GetService <IConvertService>();
            var             rules          = convertService.GetConvertRules(this.Context, sourceFormId, targetFormId);

            if (rules == null || rules.Count == 0)
            {
                throw new KDBusinessException("", string.Format("未找到{0}到{1}之间,启用的转换规则,无法自动下推!", sourceFormId, targetFormId));
            }
            // 取勾选了默认选项的规则
            var rule = rules.FirstOrDefault(t => t.IsDefault);

            //var rule = rules.
            // 如果无默认规则,则取第一个
            if (rule == null)
            {
                rule = rules[0];
            }
            // 开始构建下推参数:
            // 待下推的源单数据行
            List <ListSelectedRow> srcSelectedRows = new List <ListSelectedRow>();

            foreach (var billId in sourceBillIds)
            {// 把待下推的源单内码,逐个创建ListSelectedRow对象,添加到集合中
                srcSelectedRows.Add(new ListSelectedRow(billId.ToString(), string.Empty, 0, sourceFormId));
                // 特别说明:上述代码,是整单下推;
                // 如果需要指定待下推的单据体行,请参照下句代码,在ListSelectedRow中,指定EntryEntityKey以及EntryId
                //srcSelectedRows.Add(new ListSelectedRow(billId.ToString(), entityId, 0, sourceFormId) { EntryEntityKey = "FEntity" });
            }

            // 指定目标单单据类型:情况比较复杂,没有合适的案例做参照,示例代码暂略,直接留空,会下推到默认的单据类型
            string targetBillTypeId = string.Empty;
            // 指定目标单据主业务组织:情况更加复杂,需要涉及到业务委托关系,缺少合适案例,示例代码暂略
            // 建议在转换规则中,配置好主业务组织字段的映射关系:运行时,由系统根据映射关系,自动从上游单据取主业务组织,避免由插件指定
            //long targetOrgId = 0;
            // 自定义参数字典:把一些自定义参数,传递到转换插件中;转换插件再根据这些参数,进行特定处理
            Dictionary <string, object> custParams = new Dictionary <string, object>();
            // 组装下推参数对象
            PushArgs pushArgs = new PushArgs(rule, srcSelectedRows.ToArray())
            {
                TargetBillTypeId = targetBillTypeId,
                TargetOrgId      = targetOrgId,
                CustomParams     = custParams
            };
            // 调用下推服务,生成下游单据数据包
            ConvertOperationResult operationResult = convertService.Push(this.Context, pushArgs, OperateOption.Create());

            // 开始处理下推结果:
            // 获取下推生成的下游单据数据包
            DynamicObject[] targetBillObjs = (from p in operationResult.TargetDataEntities select p.DataEntity).ToArray();
            if (targetBillObjs.Length == 0)
            {
                // 未下推成功目标单,抛出错误,中断审核
                throw new KDBusinessException("", string.Format("由{0}自动下推{1},没有成功生成数据包,自动下推失败!", sourceFormId, targetFormId));
            }
            // 对下游单据数据包,进行适当的修订,以避免关键字段为空,自动保存失败
            // 示例代码略
            // 读取目标单据元数据
            IMetaDataService metaService = Kingdee.BOS.App.ServiceHelper.GetService <IMetaDataService>();
            var targetBillMeta           = metaService.Load(this.Context, targetFormId) as FormMetadata;
            // 构建保存操作参数:设置操作选项值,忽略交互提示
            OperateOption saveOption = OperateOption.Create();

            // 忽略全部需要交互性质的提示,直接保存;
            saveOption.SetIgnoreWarning(true);                               // 忽略交互提示
            saveOption.SetInteractionFlag(this.Option.GetInteractionFlag()); // 如果有交互,传入用户选择的交互结果
            // using Kingdee.BOS.Core.Interaction;
            saveOption.SetIgnoreInteractionFlag(this.Option.GetIgnoreInteractionFlag());
            // 调用保存服务,自动保存
            ISaveService saveService = Kingdee.BOS.App.ServiceHelper.GetService <ISaveService>();
            var          saveResult  = saveService.Save(this.Context, targetBillMeta.BusinessInfo, targetBillObjs, saveOption, "Save");

            // 判断自动保存结果:只有操作成功,才会继续
            if (saveResult.SuccessDataEnity != null)
            {
                var submitRet = AppServiceContext.SubmitService.Submit(this.Context, targetBillMeta.BusinessInfo, saveResult.SuccessDataEnity.Select(o => o["Id"]).ToArray(), "Submit", saveOption);
                result.MergeResult(submitRet);
                if (submitRet.SuccessDataEnity != null)
                {
                    var auditResult = AppServiceContext.SetStatusService.SetBillStatus(this.Context, targetBillMeta.BusinessInfo,
                                                                                       submitRet.SuccessDataEnity.Select(o => new KeyValuePair <object, object>(o["Id"], 0)).ToList(),
                                                                                       new List <object> {
                        "1", ""
                    },
                                                                                       "Audit", saveOption);

                    result.MergeResult(auditResult);
                }
            }
            if (this.CheckOpResult(saveResult, saveOption))
            {
                return(result);
            }
            return(result);
        }