工作中,需要完成以下的工作场景:
【场景】单据转换不支持多选基础资料下推;
【案例】通过单据转换插件,实现应收单单据头的多选基础资料下推到付款申请单的单据头的多选基础资料
原文链接:https://vip.kingdee.com/article/324304152484608000?productLineId=1
需要将原代码转换为python代码,首先咨询了CSDN的AI,给我的解答是:
可以使用java2python库将Java代码转换为Python代码。以下是使用java2python库的步骤:
安装java2python库:可以使用pip命令进行安装,命令如下:
pip install java2python
将Java代码保存到文件中,例如保存为example.java。使用java2python库将Java代码转换为Python代码,命令如下:
java2python example.java > example.py
这将把Java代码转换为Python代码并将其保存到example.py文件中。可以使用Python解释器运行生成的Python代码,例如:
python example.py
这将运行生成的Python代码并输出结果
实测pip install java2python 这一步报错,一直安装不上,找不到java2python,可能我的源太少了。
后边再测试一下百度AI,看是如何解答的。
以下是测试过程:
我:将以下代码转换为python:
百度AI:很遗憾,您没有提供任何代码片段,因此我无法为您转换任何代码。如果您能提供一些具体的代码,我将非常乐意帮助您进行转换。
我:public override void AfterConvert(AfterConvertEventArgs e)
{
base.AfterConvert(e);
if (e.Result == null)
return;
/*
* <0>仅支持单据头到单据头,其他自行处理
*/
string tgtFieldKey = "F_BHR_MulBase";
string srcFieleKey = "F_BHR_MulBase";
MulBaseDataField tgtBaseDataField = e.TargetBusinessInfo.GetField(tgtFieldKey) as MulBaseDataField;
MulBaseDataField srcBaseDataField = e.SourceBusinessInfo.GetField(srcFieleKey) as MulBaseDataField;
if (tgtBaseDataField == null || srcBaseDataField == null)
return;
if (tgtBaseDataField.Entity.ElementType != Kingdee.BOS.Core.Metadata.ElementMetadata.ElementType.ELEMENTTYPE_BILLHEAD)
return;
if (srcBaseDataField.Entity.ElementType != Kingdee.BOS.Core.Metadata.ElementMetadata.ElementType.ELEMENTTYPE_BILLHEAD)
return;
/*
* <1>收集源单内码批量获取
*/
Entity tgtEntity = tgtBaseDataField.Entity;
var targetEntityObjs = e.Result.FindByEntityKey(tgtEntity.Key);
HashSet<object> srcPkIds = new HashSet<object>();
for (int i = 0, sz = targetEntityObjs.Length; i < sz; ++i)
{
var targetEntityRow = targetEntityObjs[i];
if (targetEntityRow.DataEntity == null)
continue;
//获取当前目标行的源单数据集合
var sourceEntityObjs = targetEntityRow["ConvertSource"] as List<Kingdee.BOS.Orm.DataEntity.DynamicObject>;
if (sourceEntityObjs == null)
continue;
foreach (var sourceObj in sourceEntityObjs)
{
if (sourceObj == null)
continue;
srcPkIds.Add(sourceObj[0]);
}
}
//<2>加载源单多选基础资料,这个地方由于是单据头可以直接取数
List<string> loadFields = new List<string>
百度AI:看起来你的代码是在处理一些特定的数据转换操作,特别是在Kingdee BOS系统中。然而,你的问题并没有完整地给出,因此我无法为你提供完整的Python代码转换。
不过,我可以尝试帮助你理解这段代码的大致逻辑,并给出如何在Python中实现相同逻辑的建议。由于我不清楚你的数据结构和库,以下代码可能需要根据你的具体情况进行修改:
python
def after_convert(self, args):
super().after_convert(args)
if args.result is None:
return
# <0> Only support billhead to billhead, handle others as needed.
tgt_field_key = "F_BHR_MulBase"
src_field_key = "F_BHR_MulBase"
tgt_base_data_field = args.target_business_info.get_field(tgt_field_key) as MulBaseDataField
src_base_data_field = args.source_business_info.get_field(src_field_key) as MulBaseDataField
if tgt_base_data_field is None or src_base_data_field is None:
return
if tgt_base_data_field.entity.element_type != ElementType.ELEMENTTYPE_BILLHEAD:
return
if src_base_data_field.entity.element_type != ElementType.ELEMENTTYPE_BILLHEAD:
return
# <1> Collect source bill IDs in batch.
tgt_entity = tgt_base_data_field.entity
target_entity_objs = args.result.find_by_entity_key(tgt_entity.key)
src_pk_ids = set()
for row in target_entity_objs:
if row.data_entity is None:
continue
source_entity_objs = row["ConvertSource"] as List[DynamicObject]
if source_entity_objs is None:
continue
for source_obj in source_entity_objs:
if source_obj is None:
continue
src_pk_ids.add(source_obj[0])
# <2> Load multiple source bills based on collected IDs.
# You will need to modify this section to fit your specific data loading methods.
load_fields = new List[string] # Here you were starting to define fields to load, but did not finish the code. Please fill in the necessary information here based on your needs.
请注意,这段Python代码可能不会完全按照你原始的C#代码运行,因为我没有你的数据模型和库的具体信息。你可能需要根据你自己的需求对上面的代码进行一些修改。
因为限制了字数,复制不全,所以继续复制下一段代码
我:Kingdee.BOS.Core.Metadata.BusinessInfo subBusinessInfo = e.SourceBusinessInfo.GetSubBusinessInfo(loadFields);
var srcBillObjs = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.Load(this.Context, srcPkIds.ToArray(), subBusinessInfo.GetDynamicObjectType());
var srcBillObjsMap = srcBillObjs.ToDictionary((x) => { return x[0]; });
Dictionary<object, Kingdee.BOS.Orm.DataEntity.DynamicObject> baseDataObjCache = new Dictionary<object, Kingdee.BOS.Orm.DataEntity.DynamicObject>();
百度AI:这段代码是使用Kingdee BOS平台进行业务数据操作的一段示例代码。下面是对这段代码的简单解释:
从数据源(e.SourceBusinessInfo)中获取子业务信息(subBusinessInfo)。
使用BusinessDataServiceHelper.Load方法加
回答不符合,我就让AI停止回答了。
换一种思路提问:
我:等我分多次,发完整的代码之后,你再帮我转成python代码。
后边是分三段发的代码,段落太长,这里就不再复制,重点看AI的回答。
百度AI:以下是转换后的Python代码:
python
# 第一段代码
def after_convert(self, e):
super().after_convert(e)
if e.result is None:
return
tgt_field_key = "F_BHR_MulBase"
src_field_key = "F_BHR_MulBase"
tgt_base_data_field = e.target_business_info.get_field(tgt_field_key)
src_base_data_field = e.source_business_info.get_field(src_field_key)
if tgt_base_data_field is None or src_base_data_field is None:
return
if tgt_base_data_field.element_type != 'ELEMENTTYPE_BILLHEAD':
return
if src_base_data_field.element_type != 'ELEMENTTYPE_BILLHEAD':
return
tgt_entity = tgt_base_data_field.entity
target_entity_objs = e.result.find_by_entity_key(tgt_entity.key)
src_pk_ids = set()
for i in range(len(target_entity_objs)):
target_entity_row = target_entity_objs[i]
if target_entity_row.data_entity is None:
continue
src_entity_objs = target_entity_row['convert_source'] as List[Kingdee.BOS.Orm.DataEntity.DynamicObject]
if src_entity_objs is None:
continue
for src_obj in src_entity_objs:
if src_obj is None:
continue
src_pk_ids.add(src_obj[0])
# 第二段代码
for src_base_data in src_mul_base:
if src_base_data is None:
continue
# 引用属性不一样,重新加载数据
src_base_data_id = (src_base_data[src_base_data_field.key])[0]
if not base_data_obj_cache.get(srcBaseDataId):
baseDataObjCache[srcBaseDataId] = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.Load(self.context, { srcBaseDataId }, tgtBaseDataField.refFormDynamicObjectType)[0]
tgtBaseDataObjRow = Kingdee.BOS.Orm.DataEntity.DynamicObject(tgtBaseDataField.refEntityDynamicObjectType)
tgtBaseDataObjRow[tgtBaseDataField.refIDDynamicProperty] = srcBaseDataId
tgtBaseDataObjRow[tgtBaseDataField.DynamicProperty] = baseDataObjCache[srcBaseDataId]
tgtMulBaseData.add(tgtBaseDataObjRow) # 第三段代码
我:继续
python
# 第三段代码
def load_data_to_target(self, src_data, tgt_data):
if src_data is None or tgt_data is None:
return
for i in range(len(src_data)):
src_row = src_data[i]
if src_row.DataEntity is None:
continue
# 获取多选基础资料的数据
src_bill_objs = src_row["ConvertSource"] as List[Kingdee.BOS.Orm.DataEntity.DynamicObject]
if src_bill_objs is None:
continue
# 获取多选基础资料的数据的键值
src_pk = src_bill_objs[0][0]
# 根据键值获取源单数据在目标单据中的映射对象
src_bill_obj = src_data[src_pk]
# 获取目标单据中的多选基础资料的数据集合
tgt_mul_base = tgt_row.DataEntity[tgtBaseDataField.RefEntityDynamicProperty] as Kingdee.BOS.Orm.DataEntity.DynamicObjectCollection
if tgt_mul_base is None:
continue
# 将源单数据集合添加到目标单据的多选基础资料的数据集合中
tgt_mul_base.Add(src_bill_obj)
我:三段代码汇总成一个完整的代码,不要分段了。
百度AI:
以下是将三段代码汇总成一个完整代码的Python版本:
python
def after_convert(self, e):
super().after_convert(e)
if e.result is None:
return
tgt_field_key = "F_BHR_MulBase"
src_field_key = "F_BHR_MulBase"
tgt_base_data_field = e.target_business_info.get_field(tgt_field_key)
src_base_data_field = e.source_business_info.get_field(src_field_key)
if tgt_base_data_field is None or src_base_data_field is None:
return
if tgt_base_data_field.element_type != 'ELEMENTTYPE_BILLHEAD':
return
if src_base_data_field.element_type != 'ELEMENTTYPE_BILLHEAD':
return
tgt_entity = tgt_base_data_field.entity
target_entity_objs = e.result.find_by_entity_key(tgt_entity.key)
src_pk_ids = set()
for i in range(len(target_entity_objs)):
target_entity_row = target_entity_objs[i]
if target_entity_row.data_entity is None:
continue
src_entity_objs = target_entity_row['convert_source'] as List[Kingdee.BOS.Orm.DataEntity.DynamicObject]
if src_entity_objs is None:
continue
for src_obj in src_entity_objs:
if src_obj is None:
continue
src_pk_ids.add(src_obj[0])
baseDataObjCache = {}
for src_base_data in srcMulBase:
if src_base_data is None:
continue
srcBaseDataId = (src_base_data[srcBaseDataField.key])[0]
if not baseDataObjCache.get(srcBaseDataId):
baseDataObjCache[srcBaseDataId] = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.Load(self.context, { srcBaseDataId }, tgtBaseDataField.refFormDynamicObjectType)[0]
tgtBaseDataObjRow = Kingdee.BOS.Orm.DataEntity.DynamicObject(tgtBaseDataField.refEntityDynamicObjectType)
tgtBaseDataObjRow[tgtBaseDataField.refIDDynamicProperty] = srcBaseDataId
tgtBaseDataObjRow[tgtBaseDataField.DynamicProperty] = baseDataObjCache[srcBaseDataId]
tgtMulBaseData.add(tgtBaseDataObjRow)
for srcBillObj in srcBillObjsMap.values():
if srcBillObj is None:
continue
srcMulBaseData = srcBillObj[srcBaseDataFieldKey] as Kingdee.BOS.Orm.DataEntity.DynamicObjectCollection
if srcMulBaseData is None or len(srcMulBaseData) <= 0:
continue
tgtMulBaseDataColl = tgtEntityRow['ConvertSource'] as Kingdee.BOS.Orm.DataEntity.DynamicObjectCollection
if tgtMulBaseDataColl is None:
continue
for tgtBillObj in tgtMulBaseDataColl:
if tgtBillObj is None:
continue
# Load data to target
我:继续
python
def load_data_to_target(self, src_data, tgt_data):
if src_data is None or tgt_data is None:
return
for i in range(len(src_data)):
src_row = src_data[i]
if src_row.DataEntity is None:
continue
# 获取多选基础资料的数据
src_bill_objs = src_row["ConvertSource"] as List[Kingdee.BOS.Orm.DataEntity.DynamicObject]
if src_bill_objs is None:
continue
# 获取多选基础资料的数据的键值
src_pk = src_bill_objs[0][0]
# 根据键值获取源单数据在目标单据中的映射对象
src_bill_obj = src_data[src_pk]
# 获取目标单据中的多选基础资料的数据集合
tgt_mul_base = tgt_row.DataEntity[tgtBaseDataField.RefEntityDynamicProperty] as Kingdee.BOS.Orm.DataEntity.DynamicObjectCollection
if tgt_mul_base is None:
continue
# 将源单数据集合添加到目标单据的多选基础资料的数据集合中
tgt_mul_base.Add(src_bill_obj)
后边再继续,就一直重复了。
不得不说,现在的AI还是比较智能的,能够分析代码,分析语义,但是还是不能真正解决问题。需要再手工调整。
CSDN的AI更加高大上,直接提供转化工具,一劳永逸。百度AI认真分析和解答。各有千秋。文章来源:https://www.toymoban.com/news/detail-774609.html
以上就是测试的CSDN的AI和百度AI的过程。各位朋友们,还有哪些好用的AI,欢迎留言分享。文章来源地址https://www.toymoban.com/news/detail-774609.html
到了这里,关于人工智能CSDN版AI和百度AI代码转化测试,C#、Java代码转Python的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!