
【逻辑】系统默认逻辑:整单转换不会排序
在没有强制排序时,最终是什么顺序最终依赖数据库的返回
【场景】列表整单下推默认按照列表选中行顺序整单排序
【案例】采购申请单下推采购订单
(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);
if (SelectByBillId) return false;
return true;
}
}
}
```
```python
#引入clr运行库
import clr
#添加对cloud插件开发的常用组件的引用
clr.AddReference("mscorlib")
clr.AddReference("Kingdee.BOS")
clr.AddReference("Kingdee.BOS.ServiceHelper")
clr.AddReference("Kingdee.BOS.Core")
#导入cloud基础库中的常用实体对象(分命名空间导入,不会递归导入)
from System import *
from System.Collections.Generic import *
from Kingdee.BOS.Util import *
from Kingdee.BOS.ServiceHelper import FormMetaDataCache
def OnGetSourceData(e):
if e.SourceData == None or e.SourceData.Count <=1:
return;
optionDic = this.Option.GetVariables();
srcEntryKey = optionDic["SourceEntryKey"] if optionDic.ContainsKey("SourceEntryKey") else None;
if srcEntryKey == None or srcEntryKey=="":
return;
selectRows = optionDic["SelectedRows"] if optionDic.ContainsKey("SelectedRows") else None;
if selectRows == None or len(selectRows) <=1:
return;
selectByBillId = optionDic["SelectByBillId"] if optionDic.ContainsKey("SelectByBillId") else False;
if not selectByBillId:
return;
pkFieldName = e.SourceBusinessInfo.GetForm().PkFieldName;
entity = e.SourceBusinessInfo.GetEntity(srcEntryKey);
if entity == None:
return;
entityFieldName = entity.Key + "_" + entity.EntryPkFieldName;
if entity.SeqFieldKey == None or entity.SeqFieldKey == "":
entityFieldName = entity.Key + "_" + entity.SeqFieldKey;
pkIdx = {};
for idx in range(len(selectRows)):
pk = selectRows[idx].PrimaryKeyValue;
if pkIdx.has_key(pk):
continue;
pkIdx[pk] = str(idx).zfill(6);
e.SourceData.Sort[str](lambda x: getKey(x, pkIdx, pkFieldName, entityFieldName));
##raise Exception('error'+ str(1).zfill(6));
def getKey(rowObj, pkIdx, pkFieldName, entityFieldName):
key = "";
pk = str(rowObj[pkFieldName]);
if pkIdx.has_key(pk):
key +=pkIdx[pk];
else:
key +="999999";
subKey = str(rowObj[entityFieldName]).zfill(6);
key +="_" + subKey;
return key;
```
【效果】按照选中行单据顺序,单据内按照序号排序
