using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

namespace LQKJ.K3.MFG.PRD.App.ServicePlugIn
{
	[Kingdee.BOS.Util.HotUpdate]
	[Description("采购订单记录同步数据服务插件")]
	public class POrderSyncdataService : AbstractOperationServicePlugIn
	{
		public override void EndOperationTransaction(EndOperationTransactionArgs e)
		{
			List<string> list = new List<string>();
			string id = base.BusinessInfo.GetForm().Id;
			string text = base.Context.UserId.ToString();
			string text2 = base.FormOperation.OperationName.ToString();
			string operation = base.FormOperation.Operation;
			DynamicObject[] dataEntitys = e.DataEntitys;
			HashSet<string> hs = new HashSet<string>();

			string ids = "";
			for (int i = 0; i < dataEntitys.Length; i++)
			{
				DynamicObject dynamicObject = dataEntitys[i];
				ids += dynamicObject["Id"].ToString() + ",";
			}
			string SQL = $@"/*dialect*/select * from T_PUR_POORDER t1 left join T_PUR_POCHANGEENTRY t2 on t1.FID=t2.FOFID left join T_PUR_POCHANGE t3 on t3.FID=t2.FID where t2.FOFID  in({ids.Remove(ids.LastIndexOf(','))}) and t3.FDOCUMENTSTATUS!='B'";
			DataTable dt = DBUtils.ExecuteDataSet(this.Context, SQL).Tables[0];
			foreach (DataRow row in dt.Rows)
			{
				hs.Add(row["FOFID"].ToString());
			}
			for (int i = 0; i < dataEntitys.Length; i++)
			{
				DynamicObject dynamicObject = dataEntitys[i];
				string text3 = dynamicObject["Id"].ToString();
				if (hs.Contains(text3)) continue;
				string item = string.Format("insert into LQKJ_t_IMSSync(FID,F_LQKJ_SYNCDATAID,F_LQKJ_SYNCBILL,F_LQKJ_SYNDDATETIME,FCREATORID,FMODIFIERID,FCREATEDATE,FMODIFYDATE,F_LQKJ_SYNCCHECK,F_LQKJ_Operationname,F_LQKJ_operationinnercode)\r\n              values(NEWID(),'{0}','{1}',null,'{2}','0',GETDATE(),GETDATE(),'0','{3}','{4}')", new object[]
				{
					text3,
					id,
					text,
					text2,
					operation
				});
				list.Add(item);
			}
			DBUtils.ExecuteBatch(base.Context, list, list.Count);
			base.EndOperationTransaction(e);
		}
	}
}
