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

文章目录

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

创建公共方法

按单下推数据

csharp 复制代码
         /// <summary>
        /// 获取单据转换数据包
        /// </summary>
        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;
        }

按明细行下推数据

csharp 复制代码
         /// <summary>
        /// 获取单据转换数据包 按明细行下推
        /// </summary>
        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;
        }

调用下推操作

csharp 复制代码
 /// <summary>
        /// 下推操作处理
        /// </summary>
        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;
            }
        }

        /// <summary>
        /// 判断操作结果是否成功,如果不成功,则直接抛错中断进程
        /// </summary>
        /// <param name="opResult"></param>
        /// <returns></returns>
        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;
        }

调用公共方法

列表插件完整代码

csharp 复制代码
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;
            }
        }
    }
}
相关推荐
管家婆客服中心14 分钟前
提成制是什么?如何高效管理提成制?
经验分享·管家婆软件
Komorebi.py2 小时前
【Linux】-学习笔记05
linux·笔记·学习
亦枫Leonlew2 小时前
微积分复习笔记 Calculus Volume 1 - 6.5 Physical Applications
笔记·数学·微积分
zy张起灵6 小时前
48v72v-100v转12v 10A大功率转换电源方案CSM3100SK
经验分享·嵌入式硬件·硬件工程
冰帝海岸7 小时前
01-spring security认证笔记
java·笔记·spring
小二·8 小时前
java基础面试题笔记(基础篇)
java·笔记·python
向宇it8 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
九鼎科技-Leo9 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
Heaphaestus,RC10 小时前
【Unity3D】获取 GameObject 的完整层级结构
unity·c#
baivfhpwxf202310 小时前
C# 5000 转16进制 字节(激光器串口通讯生成指定格式命令)
开发语言·c#