金蝶云星空创建自动下推并保存公共服务

文章目录

  • 金蝶云星空创建自动下推并保存公共服务
    • 创建公共方法
      • 按单下推数据
      • 按明细行下推数据
      • 调用下推操作
    • 调用公共方法

金蝶云星空创建自动下推并保存公共服务

创建公共方法

按单下推数据

         /// 
        /// 获取单据转换数据包
        /// 
        public DynamicObject[] GetBillObjDoPush(Context ctx, string sourceFormId, string targetFormId, List<object> sourceBillIds, string convertId = null, string billTypeId = null)
        {
            IConvertService convertService = Kingdee.BOS.App.ServiceHelper.GetService<IConvertService>();
            var rules = convertService.GetConvertRules(ctx, sourceFormId, targetFormId);
            if (rules == null || rules.Count == 0)
            {
                throw new KDBusinessException("", string.Format("未找到{0}到{1}之间,启用的转换规则,无法自动下推!", sourceFormId, targetFormId));
            }
            ConvertRuleElement rule = null;
            if (convertId.IsNullOrEmptyOrWhiteSpace())
            {
                rule = rules.FirstOrDefault(t => t.IsDefault);
            }
            else
            {
                rule = rules.FirstOrDefault(s => s.Id.Equals(convertId));
            }
            if (rule == null)
            {
                rule = rules[0];
            }
            List<ListSelectedRow> srcSelectedRows = new List<ListSelectedRow>();
            foreach (var billId in sourceBillIds)
            {
                srcSelectedRows.Add(new ListSelectedRow(billId.ToString(), string.Empty, 0, sourceFormId));
            }
            string targetBillTypeId = billTypeId == null ? string.Empty : billTypeId;
            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(ctx, 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));
            }
            return targetBillObjs;
        }

按明细行下推数据

         /// 
        /// 获取单据转换数据包 按明细行下推
        /// 
        public DynamicObject[] GetBillObjDoPushByEntryId(Context ctx, string sourceFormId, string targetFormId, IEnumerable<ListSelectedRow> srcSelectedRows, string convertId = null, string billTypeId = null)
        {
            IConvertService convertService = Kingdee.BOS.App.ServiceHelper.GetService<IConvertService>();
            var rules = convertService.GetConvertRules(ctx, sourceFormId, targetFormId);
            if (rules == null || rules.Count == 0)
            {
                throw new KDBusinessException("", string.Format("未找到{0}到{1}之间,启用的转换规则,无法自动下推!", sourceFormId, targetFormId));
            }
            ConvertRuleElement rule = null;
            if (convertId.IsNullOrEmptyOrWhiteSpace())
            {
                rule = rules.FirstOrDefault(t => t.IsDefault);
            }
            else
            {
                rule = rules.FirstOrDefault(s => s.Id.Equals(convertId));
            }
            if (rule == null)
            {
                rule = rules[0];
            }

            string targetBillTypeId = billTypeId == null ? string.Empty : billTypeId;
            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(ctx, 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));
            }
            return targetBillObjs;
        }

调用下推操作

 /// 
        /// 下推操作处理
        /// 
        public IOperationResult DoPushOper(Context ctx, DynamicObject[] targetBillObjs, string targetFormId, OperateOption option, IOperationResult billOperationResult, bool isSubmit = false, bool isAudit = false)
        {
            IMetaDataService metaService = Kingdee.BOS.App.ServiceHelper.GetService<IMetaDataService>();
            var targetBillMeta = metaService.Load(ctx, targetFormId) as FormMetadata;
            OperateOption saveOption = OperateOption.Create();
            saveOption.SetIgnoreWarning(true);
            saveOption.SetInteractionFlag(option.GetInteractionFlag());
            saveOption.SetIgnoreInteractionFlag(option.GetIgnoreInteractionFlag());
            try
            {
                var saveResult = Kingdee.K3.MFG.App.AppServiceContext.SaveService.Save(ctx, targetBillMeta.BusinessInfo, targetBillObjs, saveOption, "Save");
                this.CheckOpResult(saveResult, billOperationResult, saveOption);
                if (isSubmit && saveResult.IsSuccess)
                {
                    saveResult = Kingdee.K3.MFG.App.AppServiceContext.SubmitService.Submit(ctx, targetBillMeta.BusinessInfo, targetBillObjs.Select(s => s["Id"]).ToArray(), "Submit", saveOption);
                    this.CheckOpResult(saveResult, billOperationResult, saveOption);
                }
                if (isAudit && saveResult.IsSuccess)
                {
                    List<KeyValuePair<object, object>> keyValuePairs = new List<KeyValuePair<object, object>>();
                    targetBillObjs.ToList().ForEach(item => keyValuePairs.Add(new KeyValuePair<object, object>(item.GetPrimaryKeyValue(), item)));
                    List<object> auditObjs = new List<object>();
                    auditObjs.Add("1");
                    auditObjs.Add("");
                    saveResult = Kingdee.K3.MFG.App.AppServiceContext.SetStatusService.SetBillStatus(ctx, targetBillMeta.BusinessInfo, keyValuePairs, auditObjs, "Audit", saveOption);
                    if (this.CheckOpResult(saveResult, billOperationResult, saveOption)) return saveResult;
                }
                return saveResult;
            }
            catch (Exception ex)
            {
                //string aa = ex.Message;
                throw new KDBusinessException("", ex.Message);
                return null;
            }
        }

        /// 
        /// 判断操作结果是否成功,如果不成功,则直接抛错中断进程
        /// 
        /// 
        /// 
        private bool CheckOpResult(IOperationResult opResult, IOperationResult operationResult, OperateOption opOption = null, bool isShowError = false)
        {
            bool isSuccess = false;
            if (opResult.IsSuccess)
            {
                // 操作成功
                isSuccess = true;
            }
            else
            {
                if (opResult.InteractionContext != null
                    && opResult.InteractionContext.Option.GetInteractionFlag().Count > 0)
                {// 有交互性提示

                    // 传出交互提示完整信息对象
                    operationResult.InteractionContext = opResult.InteractionContext;
                    // 传出本次交互的标识,
                    // 用户在确认继续后,会重新进入操作;
                    // 将以此标识取本交互是否已经确认过,避免重复交互
                    operationResult.Sponsor = opResult.Sponsor;
                    if (opOption != null)
                    {
                        if (isShowError)
                            throw new KDException("", opResult.InteractionContext.SimpleMessage);
                        else
                        {

                            //throw new KDInteractionException(opOption, opResult.Sponsor);

                            throw new KDException("", ((AbstractInteractionResult)opResult).InteractionContext.SimpleMessage);
                        }

                    }
                    else
                    {
                        // 抛出错误,终止本次操作
                        throw new KDBusinessException("", "本次操作需要用户确认是否继续,暂时中断");
                    }
                }
                else
                {
                    // 操作失败,拼接失败原因,然后抛出中断
                    opResult.MergeValidateErrors();
                    if (opResult.OperateResult == null)
                    {// 未知原因导致提交失败
                        throw new KDBusinessException("", "未知原因导致自动提交、审核失败!");
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("自动提交、审核失败,失败原因:");
                        foreach (var operateResult in opResult.OperateResult)
                        {
                            sb.AppendLine(operateResult.Message);
                        }
                        throw new KDBusinessException("", sb.ToString());
                    }
                }
            }
            return isSuccess;
        }

调用公共方法

金蝶云星空创建自动下推并保存公共服务_第1张图片
列表插件完整代码

using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Util;
using mm.K3.App.Service.Common;
using mm.K3.Core.Const;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mm.K3.SCM.Business.PlugIn.SC
{
    [Description("售后单-列表界面"), HotUpdate]
    public class AfterOrderList : AbstractListPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            switch (e.BarItemKey.ToUpperInvariant())
            {
                case "XXXX_TBPUSHREPLACE":
                    #region 下推对照 XXXX_tbPushReplace
                    #region v3.0
                    Context ctx = this.View.Context;
                    string sourceFormId = FormIdConst.AS_AfterOrder;//售后单
                    string targetFormId = FormIdConst.AS_Replace;//对照表
                    string convertRuleKey = "AS_AfterOrderToReplace";//转换规则
                    // 获取在列表上当前选择需下推的行
                    ListSelectedRow[] selectedRows = ((IListView)this.View).SelectedRowsInfo.ToArray();
                    var action = new ActionCommon();
                    try
                    {
                        var targetBills = action.GetBillObjDoPushByEntryId(ctx, sourceFormId, targetFormId, selectedRows, convertRuleKey);
                        var result=action.DoPushOper(ctx, targetBills, targetFormId, null, null, false, false);//不提交 不审核
                        //var result = action.DoPushOper(ctx, targetBills, targetFormId, null, null, true, true); //自动提交 ,审核
                        if (result.IsSuccess)
                        {
                            string _msg = string.Empty;
                            if (result.OperateResult.Count() > 0)
                            {
                                _msg += string.Join(",", result.OperateResult.Select(s => s.Message));
                            }
                            if (!_msg.IsNullOrEmptyOrWhiteSpace())
                            {
                                this.View.ShowMessage(_msg);
                            }

                        }
                        else
                        {
                            string _msg = string.Empty;
                            if (result.ValidationErrors.Count > 0)
                            {
                                _msg += string.Join(",", result.ValidationErrors.Select(s => s.Message));
                            }
                            if (!_msg.IsNullOrEmptyOrWhiteSpace())
                            {
                                this.View.ShowErrMessage("", _msg, MessageBoxType.Error);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        this.View.ShowErrMessage("", "下推对照时存在异常:" + ex.Message, MessageBoxType.Error);
                        e.Cancel = true;
                        return;

                    }

                    #endregion

                    #endregion
                    break;
            }
        }
    }
}

你可能感兴趣的:(金蝶云星空,工作笔记,C#,经验分享,笔记,c#)