操作结果说明:
返回结果包含验证信息,验证成功后的操作结果,交互结果等信息。
非批量操作,先检查IsSuccess,如果为true,则直接显示验证成功后的操作结果;为false,则先检查是否是交互结果,不是,则处理验证信息即可。
批量操作,默认情况是存在部分成功和失败的,检查IsSuccess,如果为true,则直接显示验证成功后的操作结果;为false,则先检查是否是交互结果,不是,则需要同时处理验证信息和验证成功后的操作结果。(一个数据包要么验证失败,要么操作成功,不可能两者同时存在)
结果都继承IOperationResult接口,IOperationResult又继承IInteractionResult
交互结果又分为自定义交互,平台交互,警告信息交互。
验证结果又分为警告,错误,致命三个等级,一般警告级别的验证结果需要变成交互来处理,其他两种直接显示
操作结果,有这个结果代表是验证成功后在操作插件事务中得到的结果,结果直接显示
参考代码:
public class BatchOperationResultHandler
{
private var _dicOperationResults = new Dictionary<int, IOperationResult>();
private void BatchSave()
{
for (int i = 0; i < rowCount; i++)//循环逐行保存
{
var obj = this._ResultObjs[i];//数据包几乎
var result = BusinessDataServiceHelper.Save(this.Context, businessInfo, obj, option);
_dicOperationResults[i] = result;
}
}
//结果存在交互信息和警告信息,需要进行交互处理。
var isInteraction = _dicOperationResults.Any(t => !t.Value.IsSuccess && t.Value.InteractionContext != null);
var isWarn = _dicOperationResults.Any(x => !x.Value.IsSuccess && x.Value.ValidationErrors.Count > 0 && x.Value.ValidationErrors.Any(e => e.Level == ErrorLevel.Warning));
if (isInteraction || isWarn)
{
InteractionHandle(option);
}
else //否则,直接显示结果
{
ShowResultMessage();
}
}
/// <summary>
/// 交互信息和警告信息结果处理
/// </summary>
/// <param name="option"></param>
private void InteractionHandle(OperateOption option)
{
var gDicResult = this.GroupInteractionResult(); //对交互结果按类型进行分组
int index = gDicResult.Count();
foreach (var gResults in gDicResult)
{
index--;
InteractionUtil.DoInteraction(this.View, gResults.Key, option, (toResult, fromResult, toOption) =>
{
if (fromResult.InteractionContext != null) //交互结果返回的信息
{
var isRedo = toResult is InteractionFormResult && (toResult as InteractionFormResult).IsOK; //结果为是,继续保存。
AfterInteractionHandler(gResults, toOption, true, isRedo, index, option);
}
else //警告结果返回的信息
{
if (toResult == null || toResult.ReturnData == null) //平台没有处理,自己处理(目前平台是只有一条警告信息不处理)
{
var opResults = fromResult as IOperationResult;
var warnMsg = string.Join(",", opResults.ValidationErrors.Where(x => x.Level == ErrorLevel.Warning).Select(x => x.Message));
this.View.ShowMessage(warnMsg, MessageBoxOptions.YesNo, new Action<MessageBoxResult>((msgResult) =>
{
var isRedo = msgResult == MessageBoxResult.Yes;
toOption.SetIgnoreWarning(true);
AfterInteractionHandler(gResults, toOption, false, isRedo, index, option, createdCount);
}));
}
else
{
var isRedo = toResult.ReturnData is K3DisplayerModel && (toResult.ReturnData as K3DisplayerModel).IsOK;
AfterInteractionHandler(gResults, toOption, false, isRedo, index, option, createdCount);
}
}
});
}
}
/// <summary>
/// 交互信息和警告信息结果进行分组
/// </summary>
/// <returns></returns>
private Dictionary<IOperationResult, Dictionary<int, IOperationResult>> GroupInteractionResult()
{
var interResults = _dicOperationResults.Where(t => !t.Value.IsSuccess && t.Value.InteractionContext != null).ToArray();
var interGroupResults = interResults.GroupBy(x => x.Value.InteractionContext.InteractionFormId);
var returnResult = new Dictionary<IOperationResult, Dictionary<int, IOperationResult>>();
foreach (var gResults in interGroupResults)
{
var gCurrResult = new Dictionary<int, IOperationResult>();
var lstObjs = new List<DynamicObject>();
foreach (var result in gResults)
{
var obj = this._ResultObjs[result.Key];
lstObjs.Add(obj);
gCurrResult.Add(result.Key, result.Value);
}
var option = OperateOption.Create();
if (lstObjs.Count == 0)
{
returnResult.Add(gResults.First().Value, gCurrResult);
}
else
{
var batchResult = BusinessDataServiceHelper.Save(this.Context, this._targetFormMetadata.BusinessInfo, lstObjs.ToArray(), option);
returnResult.Add(batchResult, gCurrResult);
}
}
var dicWarnResult = this._dicOperationResults.Where(x => !x.Value.IsSuccess && x.Value.ValidationErrors.Count > 0 &&
x.Value.ValidationErrors.Any(e => e.Level == ErrorLevel.Warning)).ToList();
var warnCount = dicWarnResult.Count();
if (warnCount > 0)
{
var firstResult = dicWarnResult.First().Value;
var gCurrResult = new Dictionary<int, IOperationResult>();
gCurrResult.Add(dicWarnResult.First().Key, firstResult);
for (int i = 1; i < warnCount; i++)
{
firstResult.MergeResult(dicWarnResult[i].Value);
gCurrResult.Add(dicWarnResult[i].Key, dicWarnResult[i].Value);
}
returnResult.Add(firstResult, gCurrResult);
}
return returnResult;
}
/// <summary>
/// 交互后处理
/// </summary>
/// <param name="gResults"></param>
/// <param name="sOption"></param>
/// <param name="isIntera"></param>
/// <param name="isRedo"></param>
/// <param name="index"></param>
/// <param name="option"></param>
private void AfterInteractionHandler(KeyValuePair<IOperationResult, Dictionary<int, IOperationResult>> gResults,
OperateOption sOption, bool isIntera, bool isRedo, int index, OperateOption option)
{
foreach (var dic in gResults.Value)
{
if (isRedo)
{
var pureOption = sOption.Copy();
if (isIntera)
{
pureOption.SetIgnoreInteractionFlag(true);
pureOption.SetNotShowConfirmInfo(true);
}
var obj = this._ResultObjs[dic.Key];
var result = SaveDataObject(obj, this._targetFormMetadata.BusinessInfo, pureOption);
_dicOperationResults[dic.Key] = result;
}
else
{
dic.Value.OperateResult.Add(new OperateResult() { SuccessStatus = false, Message = Kingdee.BOS.Resource.ResManager.LoadKDString("取消生成", "002013030003802", Kingdee.BOS.Resource.SubSystemType.BOS) });
dic.Value.InteractionContext = null; //
dic.Value.ValidationErrors.RemoveAll(x => x.Level == ErrorLevel.Warning);
}
}
if (index == 0)
{
var isInteraction = _dicOperationResults.Any(t => !t.Value.IsSuccess && t.Value.InteractionContext != null);
var isWarn = this._dicOperationResults.Any(x => !x.Value.IsSuccess && x.Value.ValidationErrors.Count > 0 &&
x.Value.ValidationErrors.Any(e => e.Level == ErrorLevel.Warning));
if (isInteraction || isWarn) //还存在交互警告结果
{
InteractionHandle(option, createdCount);
}
else
{
ShowResultMessage(createdCount);
}
}
}
/// <summary>
/// 显示保存结果
/// </summary>
/// <returns></returns>
private void ShowResultMessage()
{
int successCount = 0, failCount = 0, draftCount = 0; //本次成功生成数量,生成失败数量,暂存成功数量
var batchResult = new OperateResultCollection();
foreach (var dicresult in _dicOperationResults)
{
int i = dicresult.Key;
var result = dicresult.Value;
var obj = this._ResultObjs[i];
var name = string.Format(Kingdee.BOS.Resource.ResManager.LoadKDString("序号{0}:", "002013030003811", Kingdee.BOS.Resource.SubSystemType.BOS), i + 1);
if (result.IsSuccess) //保存成功
{
this.RefreshEntityRow(obj, i);
batchResult.Add(new OperateResult() { Name = name, SuccessStatus = result.IsSuccess, Message = Kingdee.BOS.Resource.ResManager.LoadKDString("生成成功", "002013000004282", Kingdee.BOS.Resource.SubSystemType.BOS) });
successCount++;
}
else //保存失败
{
this.Model.SetValue("FIsSaved", Kingdee.BOS.Resource.ResManager.LoadKDString("生成失败", "002013030003808", Kingdee.BOS.Resource.SubSystemType.BOS), i);
if (result.OperateResult.Count > 0)
{
foreach (var opResult in result.OperateResult)
{
opResult.Name = name;
batchResult.Add(opResult);
}
}
else
{
foreach (var error in result.ValidationErrors)
{
batchResult.Add(new OperateResult() { Name = name, SuccessStatus = result.IsSuccess, Message = error.Message });
}
}
failCount++;
}
}
if (failCount > 0)
{
this.View.ShowOperateResult(batchResult, FormIdConst.BOS_ListBatchTips);
}
}
}
推荐阅读