2023-12-26 10:44:19 +08:00
|
|
|
|
using Kingdee.BOS.App.Data;
|
2023-12-20 22:26:30 +08:00
|
|
|
|
using Kingdee.BOS.Contracts;
|
|
|
|
|
|
using Kingdee.BOS.Core.DynamicForm.Operation;
|
|
|
|
|
|
using Kingdee.BOS.Core.List;
|
|
|
|
|
|
using Kingdee.BOS.Core.Metadata.ConvertElement.ServiceArgs;
|
|
|
|
|
|
using Kingdee.BOS.Core;
|
|
|
|
|
|
using Kingdee.BOS.Orm.DataEntity;
|
|
|
|
|
|
using Kingdee.BOS.Orm;
|
|
|
|
|
|
using Kingdee.BOS.Util;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using Kingdee.BOS;
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using Kingdee.BOS.Core.List.PlugIn;
|
|
|
|
|
|
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
|
|
|
|
|
|
using Kingdee.BOS.Core.Metadata;
|
|
|
|
|
|
using Kingdee.BOS.ServiceHelper;
|
|
|
|
|
|
using Kingdee.BOS.Core.Metadata.FieldElement;
|
|
|
|
|
|
using Kingdee.BOS.App;
|
|
|
|
|
|
using Kingdee.BOS.Core.DynamicForm;
|
2023-12-26 10:44:19 +08:00
|
|
|
|
using Kingdee.BOS.Log;
|
|
|
|
|
|
using Kingdee.BOS.Orm.Exceptions;
|
|
|
|
|
|
using System.Data.SqlClient;
|
2023-12-20 22:26:30 +08:00
|
|
|
|
|
|
|
|
|
|
namespace CY.SAL_OUTSTOCK
|
|
|
|
|
|
{
|
|
|
|
|
|
[Description("测试自动下推功能"), HotUpdate]
|
|
|
|
|
|
public class TestAutoPushPlugIn : AbstractListPlugIn
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void BarItemClick(BarItemClickEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.BarItemClick(e);
|
|
|
|
|
|
|
|
|
|
|
|
if (e.BarItemKey.Equals("tbTestAutoP"))
|
|
|
|
|
|
{
|
2023-12-26 10:44:19 +08:00
|
|
|
|
Run(this.Context, new Schedule
|
|
|
|
|
|
{
|
|
|
|
|
|
Name =new LocaleValue("tbTestAutoP销售出库单自定下推Test")
|
|
|
|
|
|
});
|
2023-12-20 22:26:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-26 10:44:19 +08:00
|
|
|
|
public void Run(Context ctx, Schedule schedule)
|
2023-12-20 22:26:30 +08:00
|
|
|
|
{
|
2023-12-26 10:44:19 +08:00
|
|
|
|
Logger.Info(schedule.Name, $"销售出库单自动下推应付单start...");
|
|
|
|
|
|
var succeedNum = 0;
|
|
|
|
|
|
var failNum = 0;
|
2023-12-20 22:26:30 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-12-26 10:44:19 +08:00
|
|
|
|
IConvertService service = ServiceHelper.GetService<IConvertService>();
|
2023-12-20 22:26:30 +08:00
|
|
|
|
|
2023-12-26 10:44:19 +08:00
|
|
|
|
//源单据标识
|
2023-12-20 22:26:30 +08:00
|
|
|
|
string sourceFormId = "SAL_OUTSTOCK";
|
2023-12-26 10:44:19 +08:00
|
|
|
|
//目标单据标识
|
2023-12-20 22:26:30 +08:00
|
|
|
|
string targetFormId = "AR_RECEIVABLE";
|
2023-12-26 10:44:19 +08:00
|
|
|
|
//目标单据类型id-标准应收单
|
2023-12-20 22:26:30 +08:00
|
|
|
|
string targetBillTypeId = "180ecd4afd5d44b5be78a6efe4a7e041";
|
2023-12-26 10:44:19 +08:00
|
|
|
|
//转换规则
|
2023-12-20 22:26:30 +08:00
|
|
|
|
string convertRuleId = "AR_OutStockToReceivableMap"; //销售出库单下推应付单
|
|
|
|
|
|
var ruleMeta = service.GetConvertRule(ctx, convertRuleId);
|
|
|
|
|
|
|
|
|
|
|
|
var rule = ruleMeta.Rule;
|
|
|
|
|
|
var dbList = GetDBData(ctx);
|
|
|
|
|
|
if (dbList != null && dbList.Any())
|
|
|
|
|
|
{
|
2023-12-26 10:44:19 +08:00
|
|
|
|
var groupbyList = dbList.GroupBy(x => x["FBILLNO"].ToString());
|
|
|
|
|
|
Logger.Info(schedule.Name, $"找到{groupbyList.Count()}条数据");
|
|
|
|
|
|
|
2023-12-20 22:26:30 +08:00
|
|
|
|
foreach (var list in groupbyList)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<ListSelectedRow> selectedRows = list.Select(x => new ListSelectedRow(x["FID"].ToString(), x["FENTRYID"].ToString(), 0, sourceFormId)).ToList();
|
2023-12-26 10:44:19 +08:00
|
|
|
|
|
2023-12-20 22:26:30 +08:00
|
|
|
|
PushArgs pushArgs = new PushArgs(rule, selectedRows.ToArray());//下推入口参数
|
|
|
|
|
|
pushArgs.TargetBillTypeId = targetBillTypeId;
|
2023-12-26 10:44:19 +08:00
|
|
|
|
|
2023-12-20 22:26:30 +08:00
|
|
|
|
OperateOption option = OperateOption.Create();//选项参数
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-12-26 10:44:19 +08:00
|
|
|
|
//下推
|
2023-12-20 22:26:30 +08:00
|
|
|
|
ConvertOperationResult convertResult = service.Push(ctx, pushArgs, option);
|
|
|
|
|
|
|
2023-12-26 10:44:19 +08:00
|
|
|
|
//下推数据
|
2023-12-20 22:26:30 +08:00
|
|
|
|
DynamicObject[] destObjs = convertResult.TargetDataEntities.Select(r => r.DataEntity).ToArray();
|
|
|
|
|
|
|
2023-12-26 10:44:19 +08:00
|
|
|
|
FormMetadata destFormMetadata = ServiceHelper.GetService<IMetaDataService>().Load(ctx, targetFormId) as FormMetadata;
|
2023-12-20 22:26:30 +08:00
|
|
|
|
|
2023-12-26 10:44:19 +08:00
|
|
|
|
//保存
|
|
|
|
|
|
IOperationResult saveResult = ServiceHelper.GetService<ISaveService>().Save(ctx, destFormMetadata.BusinessInfo, destObjs, OperateOption.Create());
|
2023-12-20 22:26:30 +08:00
|
|
|
|
|
2023-12-26 10:44:19 +08:00
|
|
|
|
string msg = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
if (saveResult.ValidationErrors != null && saveResult.ValidationErrors.Count > 0)
|
2023-12-20 22:26:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
var errorInfo = string.Join(";", saveResult.ValidationErrors.Select(x => x.Message));
|
|
|
|
|
|
|
|
|
|
|
|
throw new KDBusinessException("", "未知原因导致自动保存失败原因:" + errorInfo);
|
|
|
|
|
|
}
|
2023-12-26 10:44:19 +08:00
|
|
|
|
|
|
|
|
|
|
object[] pkArray = (from p in destObjs select p[0]).ToArray();
|
|
|
|
|
|
//设置提交参数
|
|
|
|
|
|
IOperationResult submitResult = ServiceHelper.GetService<ISubmitService>().Submit(ctx, destFormMetadata.BusinessInfo, pkArray, "Submit", OperateOption.Create());
|
|
|
|
|
|
|
|
|
|
|
|
// 判断提交结果,如果失败,则内部会抛出错误,回滚代码
|
|
|
|
|
|
if (submitResult.IsSuccess == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new KDBusinessException("", "未知原因导致自动提交失败!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//设置审核参数
|
|
|
|
|
|
IOperationResult auditResult = ServiceHelper.GetService<IAuditService>().Audit(ctx, destFormMetadata.BusinessInfo, pkArray, OperateOption.Create());
|
|
|
|
|
|
// 判断提交结果,如果失败,则内部会抛出错误,回滚代码
|
|
|
|
|
|
if (auditResult.IsSuccess == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new KDBusinessException("", "未知原因导致自动审核失败!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
succeedNum++;
|
|
|
|
|
|
Logger.Info(schedule.Name, $"销售出库单:{list.Key},下推应付单:{saveResult.OperateResult[0].Number}成功!");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (OrmException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
failNum++;
|
|
|
|
|
|
Logger.Error($"服务器插件:{schedule.Name}错误(Orm)", $"销售出库单:{list.Key}", ex);
|
2023-12-20 22:26:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (KDBusinessException ex)
|
|
|
|
|
|
{
|
2023-12-26 10:44:19 +08:00
|
|
|
|
failNum++;
|
|
|
|
|
|
Logger.Error($"服务器插件:{schedule.Name}错误(KDB)", $"销售出库单:{list.Key}", ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (SqlException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
failNum++;
|
|
|
|
|
|
Logger.Error($"服务器插件:{schedule.Name}错误(Sql)", $"销售出库单:{list.Key}", ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
failNum++;
|
|
|
|
|
|
Logger.Error($"服务器插件:{schedule.Name}", $"销售出库单:{list.Key}", ex);
|
2023-12-20 22:26:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2023-12-26 10:44:19 +08:00
|
|
|
|
Logger.Error($"服务器插件:{schedule.Name}", ex.Message, ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.Info(schedule.Name, $"成功{succeedNum}条,失败{failNum}条");
|
|
|
|
|
|
Logger.Info(schedule.Name, $"销售出库单自动下推应付单end...");
|
2023-12-20 22:26:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-26 10:44:19 +08:00
|
|
|
|
|
2023-12-20 22:26:30 +08:00
|
|
|
|
private DynamicObjectCollection GetDBData(Context ctx)
|
|
|
|
|
|
{
|
|
|
|
|
|
var unSql = $@"
|
|
|
|
|
|
select * from V_NOT_RECEIVABLE_OUTSTOCK
|
|
|
|
|
|
";
|
|
|
|
|
|
var dbList = DBUtils.ExecuteDynamicObject(ctx, $"/*dialect*/{unSql}");
|
|
|
|
|
|
|
|
|
|
|
|
return dbList;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|