using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Log;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace YDL.K3.PlugIns.Service
{
[HotUpdate]
[System.ComponentModel.Description("办单下推,审核时处理获取相关数据行判断")]
public class F_YDL_BDFX : AbstractOperationServicePlugIn
{
// 单据关联配置
private const string EntityKey = "F_SCIZ_Entity_Link";
// 订单明细
private const string OrderEntityKey = "SCIZ_Cust_Entry100018";
private FormMetadata meta;
private QueryBuilderParemeter queryParam;
private DynamicObjectCollection FSaleOrder;
private object SBillId;
public override void OnPreparePropertys(PreparePropertysEventArgs e)
{
base.OnPreparePropertys(e);
e.FieldKeys.Add("F_YDL_XMLCC");
e.FieldKeys.Add("F_SCIZ_SourceBillNo");
}
public override void BeginOperationTransaction(BeginOperationTransactionArgs e)
{
base.BeginOperationTransaction(e);
// 统计耗时时间
var BeginTime = DateTime.Now;
// 获取办单变更单的单据体
var BdBgdDjt = e.DataEntitys[0]["SCIZ_Cust_Entry100018"] as DynamicObjectCollection;
if (BdBgdDjt.Count <= 0) return;
var BDLk = BdBgdDjt[0]["F_SCIZ_Entity_Link"] as DynamicObjectCollection;
foreach (var item in BdBgdDjt)
{
var Link = item["F_SCIZ_Entity_Link"] as DynamicObjectCollection;
if (Link.Count() > 0)
{
BDLk = Link;
break;
}
}
if (BDLk.Count() <= 0)
{
Logger.Info("错误提示:", "未找到对应的关联关系表!");
return;
}
//if (BDLk.Count==0)
//{
// BDLk = BdBgdDjt[1]["F_SCIZ_Entity_Link"] as DynamicObjectCollection;
//}
var SourceBillNo = e.DataEntitys[0]["F_SCIZ_SourceBillNo"];
var FFlowId = BDLk[0]["FlowId"];
var FFlowLineId = BDLk[0]["FlowLineId"];
var FRuleId = BDLk[0]["RuleId"];
var FSTableId = BDLk[0]["STableId"];
var FSTableName = BDLk[0]["STableName"];
if (BDLk.Count <= 0) return;
// 办单的FID
var BDFid = BDLk.Select(t => t["SBillId"]).Distinct().ToArray();
// 变更单编码
var BGDFBillNo = e.DataEntitys[0]["BillNo"];
// 创建一个对象数组
Object[] myArray = new Object[1];
myArray[0] = e.DataEntitys[0]["Id"];
// 创建业务对象元数据信息
var note = new StringBuilder();
// 获取上游单据元数据
// ka4364411bcae45a693aef6e8ef0b78e2 办单标识
meta = MetaDataServiceHelper.Load(this.Context, "ka4364411bcae45a693aef6e8ef0b78e2") as FormMetadata;
var sqlAdd = string.Format(@"SELECT
A2.*
FROM
SCIZ_t_Cust100029 A1
INNER JOIN
SCIZ_t_Cust_Entry100038 A2 ON A1.FID = A2.FID
LEFT JOIN
SCIZ_t_Cust_Entry100038_LK A3 ON A2.FEntryID = A3.FEntryID
WHERE
A3.FEntryID IS NULL
AND
A1.FBILLNO = @BGDFBillNo");
var sqlDelete = string.Format(@"SELECT
A2.*
FROM
SCIZ_t_Cust100003 A1
INNER JOIN
SCIZ_t_Cust_Entry100018 A2 ON A1.FID = A2.FID
LEFT JOIN
SCIZ_t_Cust_Entry100038_LK A3 ON A2.FEntryID = A3.FSId
WHERE
A3.FEntryID IS NULL AND
A1.FBILLNO = @SourceBillNo");
var a2 = DBUtils.ExecuteDynamicObject(Context, sqlAdd, null, null, System.Data.CommandType.Text, new SqlParam[] {
new SqlParam("@BGDFBillNo", KDDbType.String,Convert.ToString(BGDFBillNo))});
var a1 = DBUtils.ExecuteDynamicObject(Context, sqlDelete, null, null, System.Data.CommandType.Text, new SqlParam[] {
new SqlParam("@SourceBillNo", KDDbType.String,Convert.ToString(SourceBillNo))});
if (a1.Count <= 0 && a2.Count <= 0)
{
note.Append("未找到需要关联的数据 ");
goto Lable1;
}
note.AppendFormat("需新增关联数据【{0}】 需取消关联数据【{1}】 ", a1.Count, a2.Count);
var ProcessDatas = BusinessDataServiceHelper.Load(Context, BDFid, meta.BusinessInfo.GetDynamicObjectType());
note.AppendFormat("共成功加载需处理数据包【{0}】个 ", ProcessDatas.Length);
// 获取单据体
var ds = ProcessDatas[0]["SCIZ_Cust_Entry100018"] as DynamicObjectCollection;
for (int i = 0; i < ds.Count; i++)
{
var ad = ds[i];
if (a1.Any(a => a["FEntryID"].ToString().Equals(ad["Id"].ToString())))
{
ds.Remove(ad);
i--;
}
}
var s = DBServiceHelper.GetSequenceInt64(Context, "SCIZ_t_Cust_Entry100018", a2.Count).ToArray();
var s2 = DBServiceHelper.GetSequenceInt64(Context, "SCIZ_t_Cust_Entry100038_LK", a2.Count).ToArray();
for (int i = 0; i < a2.Count; i++)
{
var dynamic = new DynamicObject(meta.BusinessInfo.GetEntity("F_SCIZ_Entity").DynamicObjectType);
dynamic["Id"] = s[i];
var b2 = a2[i];
foreach (var prop in b2.DynamicObjectType.Properties)
{
var propName = prop.Name;
var propValue = prop.GetValue(b2);
if (propName == "FID" || propName == "FEntryID" || propName == "F_SCIZ_INTEGER") continue;
if (propValue.IsNullOrEmptyOrWhiteSpace()) continue;
//if (propName == "SCIZ_t_Cust_Entry100038_LK") continue;
if (propName == "Qst")
{
//dynamic["Seq"] = propValue;
continue;
}
dynamic[propName] = propValue;
}
ds.Add(dynamic);
string insertSql = @"INSERT INTO SCIZ_t_Cust_Entry100038_LK (FEntryID, FLinkId, FFlowId, FFlowLineId, FRuleId, FSTableId, FSTableName, FSBillId, FSId)
VALUES (@FEntryID, @FLinkId, @FFlowId, @FFlowLineId, @FRuleId, @FSTableId, @FSTableName, @FSBillId, @FSId)";
List<SqlParam> parameters = new List<SqlParam>
{
new SqlParam("@FEntryID", KDDbType.Int64, Convert.ToInt64(a2[i]["FEntryID"])),
new SqlParam("@FLinkId", KDDbType.Int64, s2[i]),
new SqlParam("@FFlowId", KDDbType.String, FFlowId),
new SqlParam("@FFlowLineId", KDDbType.Int64, FFlowLineId),
new SqlParam("@FRuleId", KDDbType.String, FRuleId),
new SqlParam("@FSTableId", KDDbType.Int64, FSTableId),
new SqlParam("@FSTableName", KDDbType.String, FSTableName),
new SqlParam("@FSBillId", KDDbType.Int64, BDFid[0]),
new SqlParam("@FSId", KDDbType.Int64, s[i])
};
DBUtils.Execute(Context, insertSql, parameters);
}
IOperationResult returnCount;
try
{
returnCount = BusinessDataServiceHelper.Save(Context, meta.BusinessInfo, ProcessDatas, OperateOption.Create(), "Save");
note.AppendFormat("成功保存【{0}】个数据包 ", returnCount.SuccessDataEnity.Count());
if (returnCount.SuccessDataEnity.Count() > 0)
{
note.AppendFormat("保存失败【{0}】个数据包 ", ProcessDatas.Length - returnCount.SuccessDataEnity.Count());
note.AppendFormat("错误原因清单【{0}】 ", returnCount.ValidationErrors.Select(t => t.Message).JoinEx(",", true));
}
goto Lable1;
}
catch (Exception ex)
{
for (int i = 0; i < a2.Count; i++)
{
string sql = string.Format(@"DELETE SCIZ_t_Cust_Entry100038_LK WHERE FEntryID = @FEntryID AND FLinkId=@FLinkId");
List<SqlParam> parameters = new List<SqlParam>
{
new SqlParam("@FEntryID", KDDbType.Int64, Convert.ToInt64(a2[i]["FEntryID"])),
new SqlParam("@FLinkId", KDDbType.Int64, s2[i]),
};
DBUtils.Execute(Context, sql, parameters);
}
note.AppendFormat("保存数据包失败,错误原因【{0}】", ex.Message);
goto Lable1;
}
Lable1:
var EndTime = DateTime.Now;
note.AppendFormat("共耗时【{0}】秒 \r\n", EndTime.GetTimestamp() - BeginTime.GetTimestamp());
}
}
}
推荐阅读