二开案例.执行计划.批量提交单据
【应用场景】
在执行计划中批量提交单据。
【案例演示】
新建执行计划,定时将一周内的未提交的采购订单进行提交,每10分钟执行一次。
【实现步骤】
<1>编写执行计划,代码如下。
using Kingdee.BOS;
using Kingdee.BOS.App.Core;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Log;
using Newtonsoft.Json;
using System;
using System.Linq;
namespace Jac.XkDemo.BOS.App.PlugIn
{
/// <summary>
/// 【执行计划】批量提交单据
/// </summary>
public class BatchSubmitBillScheduleService : IScheduleService
{
/// <summary>
/// 执行计划入口函数
/// </summary>
/// <param name="ctx"></param>
/// <param name="schedule"></param>
public void Run(Context ctx, Schedule schedule)
{
try
{
Logger.Info("JAC-SubmitBill", "批量提交单据开始。");
Logger.Info("JAC-SubmitBill", "获取操作用户(提交人)信息。");
var userName = "demo";
var sql = string.Format("SELECT FUSERID FROM T_SEC_USER WHERE FNAME='{0}'", userName);
var userId = DBUtils.ExecuteScalar(ctx, sql, 0);
if (userId == 0)
{
Logger.Error("JAC-SubmitBill", "用户不存在:" + userName, null);
return;
}
Logger.Info("JAC-SubmitBill", "上下文切换操作用户(提交人)。");
ctx.UserId = userId;
ctx.UserName = userName;
Logger.Info("JAC-SubmitBill", "批量抓取一周内的待提交的采购订单。");
sql = "/*dialect*/SELECT FID,FBillNo FROM dbo.T_PUR_POORDER WHERE FCREATEDATE>=CONVERT(VARCHAR(10),GETDATE()-7,120) AND FDOCUMENTSTATUS IN ('A','D')";
var objs = DBUtils.ExecuteDynamicObject(ctx, sql);
if (objs == null || objs.Count == 0)
{
Logger.Info("JAC-SubmitBill", "批量抓取一周内的待提交的采购订单的数量为0,任务终止。");
return;
}
Logger.Info("JAC-SubmitBill", "待提交的采购订单:" + string.Join(",", objs.Select(o => string.Format("{0}({1})", o[1], o[0]))));
var businessInfo = ((FormMetadata)new MetaDataService().Load(ctx, "PUR_PurchaseOrder")).BusinessInfo;
var pkids = objs.Select(o => o[0]).ToArray();
var result = new SubmitService().Submit(ctx, businessInfo, pkids, "Submit", null);
if (result.IsSuccess)
{
var log = "提交成功的单据:" + string.Join(",", result.SuccessDataEnity.Select(o => string.Format("{0}({1})", o["BillNo"], o[0])));
if (result.OperateResult != null && result.OperateResult.Count > 0)
{
log += "\r\n操作结果:" + string.Join(",", result.OperateResult.Select(o => o.Message));
}
Logger.Info("JAC-SubmitBill", "采购订单批量提交成功:\r\n" + log);
}
else
{
var successPkIds = result.SuccessDataEnity == null ? new long[] { } : result.SuccessDataEnity.Select(o => Convert.ToInt64(o[0])).ToArray();
var successBills = result.SuccessDataEnity == null ? new string[] { } : result.SuccessDataEnity.Select(o => string.Format("{0}({1})", o["BillNo"], o[0])).ToArray();
var failureBills = objs.Where(o => !successPkIds.Contains(Convert.ToInt64(o[0]))).Select(o => string.Format("{0}({1})", o[1], o[0])).ToArray();
var log = "提交成功的单据:" + string.Join(",", successBills);
log += "\r\n提交失败的单据:" + string.Join(",", failureBills);
log += "\r\n失败原因:" + JsonConvert.SerializeObject(result.OperateResult.GetFailResult()) + "\r\n" + JsonConvert.SerializeObject(result.ValidationErrors);
if (result.OperateResult != null && result.OperateResult.Count > 0)
{
log += "\r\n操作结果:" + string.Join(",", result.OperateResult.Select(o => o.Message));
}
Logger.Info("JAC-SubmitBill", "采购订单批量提交失败:\r\n" + log);
}
}
catch (Exception ex)
{
Logger.Error("JAC-SubmitBill", "批量提交单据任务执行失败。", ex);
}
finally
{
Logger.Info("JAC-SubmitBill", "批量提交单据结束。");
}
}
}
}
<2>拷贝插件组件到应用站点的WebSite\Bin目录下,重启IIS。
<3>登录业务站点,打开执行计划列表,新增执行计划,如下图,开发完毕。
【功能验证】
<1>登录业务站点,打开执行计划列表,找到执行计划【批量提交单据】,点击测试。
打开采购订单列表,可以看到批量提交的效果。
前往应用服务器,可拿到执行日志。
---------------------------------------------------------------------------------------------------------
【金蝶云星空BOS二次开发案例演示】
二开案例.执行计划.批量提交单据
本文2024-09-23 04:18:41发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-164492.html