金蝶云星空创建自动下推并保存公共服务
创建公共方法
按单下推数据
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)
{
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 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;
}
调用公共方法
列表插件完整代码
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);
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;
}
}
}
}