
【逻辑】系统默认逻辑:整单转换不会排序
在没有强制排序时,最终是什么顺序最终依赖数据库的返回
【场景】列表整单下推默认按照列表选中行顺序整单排序
【案例】采购申请单下推采购订单
(0)数据准备

默认下推效果,设置了按照物料编码升序

(1)插件排序,整单转换时按照选中行单据顺序 + 分录序号排序

```csharp
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn;
using Kingdee.BOS.Core.Metadata.ConvertElement.PlugIn.Args;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.Linq;
namespace DynamicFormPlugIn.BillConvert
{
[Kingdee.BOS.Util.HotUpdate]
public class ConvertServicePlugIn_SortSourceBill : AbstractConvertPlugIn
{
/*
* 当单据转换是整单转换时,按照列表选中行 + 实体序号排序
*/
public override void OnGetSourceData(GetSourceDataEventArgs e)
{
if (e.SourceData == null || e.SourceData.Count <= 1)
return;
string srcEntryKey;
Option.TryGetVariableValue("SourceEntryKey", out srcEntryKey);
if (string.IsNullOrEmpty(srcEntryKey))
return;
ListSelectedRow[] selectRows;
Option.TryGetVariableValue("SelectedRows", out selectRows);
if (selectRows == null || selectRows.Length <= 1)
return;
if (IsGetByEntry(srcEntryKey, selectRows))
{
//分录下推
return;
}
//整单下推
//按照单据编号和序号排序
string pkFieldName = e.SourceBusinessInfo.GetForm().PkFieldName;
var entity = e.SourceBusinessInfo.GetEntity(srcEntryKey) as Kingdee.BOS.Core.Metadata.EntityElement.Entity;
if (entity == null)
return;
string entityFieldName = string.Concat(entity.Key, "_", entity.EntryPkFieldName);
if (string.IsNullOrWhiteSpace(entity.SeqFieldKey))
{
entityFieldName = string.Concat(entity.Key, "_", entity.SeqFieldKey);
}
//取选中行的主键和序号
Dictionary<string, string> pkIdx = new Dictionary<string, string>();
for (int i = 0; i < selectRows.Length; ++i)
{
string pk = selectRows[i].PrimaryKeyValue;
if (pkIdx.ContainsKey(pk))
continue;
pkIdx[pk] = i.ToString("000000");
}
e.SourceData.Sort(x => { return string.Format("{0}_{1}",
pkIdx.ContainsKey(x[pkFieldName].ToString()) ? pkIdx[x[pkFieldName].ToString()] : "999999",
ObjectUtils.Object2Int(x[entityFieldName]).ToString("000000")); });
}
private bool IsGetByEntry(string srcEntryKey, ListSelectedRow[] selectRows)
{
var firstRow = selectRows.First();
if (string.IsNullOrEmpty(firstRow.EntryPrimaryKeyValue))
return false;
if (!srcEntryKey.Equals(firstRow.EntryEntityKey, StringComparison.OrdinalIgnoreCase))
return false;
bool SelectByBillId = false;
Option.TryGetVariableValue("SelectByBillId", out SelectByBillId);