如何在物料审核后发送消息给采购员?原创
16人赞赏了该文章
1,329次浏览
编辑于2021年09月15日 17:19:22
此示例是审核后来源是plm的物料发送普通消息给采购员,如下图:
代码示例如下:
using Kingdee.BOS.Contracts; using Kingdee.BOS.Core.DynamicForm.PlugIn; using Kingdee.BOS.Core.Metadata; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using Kingdee.BOS.App; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.Core.Msg; using Kingdee.BOS.Util; using Kingdee.K3.Core.MFG.EntityHelper; using Kingdee.BOS.Workflow.App.Core.Repositories; using Kingdee.BOS.WorkflowMember; using Kingdee.BOS.Msg; using Kingdee.BOS; using Kingdee.BOS.App.Data; using System.Data; namespace ITIBB.App.ServicePlugIn { [Description("物料审核发送消息给采购员")] public class ExtSendMessage : AbstractOperationServicePlugIn { public override void OnPreparePropertys(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.PreparePropertysEventArgs e) { base.OnPreparePropertys(e); e.FieldKeys.Add("FMaterialSRC"); e.FieldKeys.Add("FPurchaserId"); } public override void AfterExecuteOperationTransaction(Kingdee.BOS.Core.DynamicForm.PlugIn.Args.AfterExecuteOperationTransaction e) { base.AfterExecuteOperationTransaction(e); if (e.DataEntitys.IsEmpty()) return; //获取采购页签上采购员内码 List<long> purchaserIdLst = new List<long>(); foreach (var mtrlData in e.DataEntitys) { string mtrlSrc = mtrlData.GetDynamicValue<string>("MaterialSRC"); //物料来源是ERP创建的不发送消息 if (mtrlSrc.EqualsIgnoreCase("B")) continue; //采购页签数据 DynamicObjectCollection mtrlPurchase = mtrlData.GetDynamicValue<DynamicObjectCollection>("MaterialPurchase"); long purchaserId = mtrlPurchase.First().GetDynamicValue<long>("PurchaserId_Id"); if (purchaserId != 0) { purchaserIdLst.Add(purchaserId); } } if (purchaserIdLst.IsEmpty()) return; //根据采购员内码获取对应用户内码 ,采购员内码为Key 用户内码为Value Dictionary<long, long> dicPurIdByUserId = GetPurIdOrUserId(this.Context, purchaserIdLst); if (dicPurIdByUserId.IsEmpty()) return; foreach (var mtrlData in e.DataEntitys) { string mtrlSrc = mtrlData.GetDynamicValue<string>("MaterialSRC"); //物料来源是ERP创建的不发送消息 if (mtrlSrc.EqualsIgnoreCase("B")) continue; //物料内码 long mtrlId = mtrlData.GetDynamicValue<long>("Id"); string mtrlNumber = mtrlData.GetDynamicValue<string>("Number"); //采购页签数据 DynamicObjectCollection mtrlPurchase = mtrlData.GetDynamicValue<DynamicObjectCollection>("MaterialPurchase"); long purchaserId = mtrlPurchase.First().GetDynamicValue<long>("PurchaserId_Id"); long purId = 0; Message msg = new DynamicObject(Message.MessageDynamicObjectType); List<string> receiverIds = new List<string>(); if (purchaserId != 0 && dicPurIdByUserId.TryGetValue(purchaserId, out purId)) { receiverIds.Add(Convert.ToString(purId)); } if (receiverIds.IsEmpty()) continue; // 消息主题、内容 msg.Title = "PLM物料已同步Cloud"; msg.Content = string.Format("需要处理的物料编码是:{0}", mtrlNumber); //判断内容是否超长 if (msg.Title.Length > 100) { msg.Title = msg.Title.Substring(0, 97) + "..."; } if (msg.Content.Length > 1000) { msg.Content = msg.Content.Substring(0, 997) + "..."; } // 发送日期、发送人 IDBService dbService = ServiceHelper.GetService<IDBService>(); ITimeService timeSrv = ServiceHelper.GetService<ITimeService>(); DateTime timeNow = timeSrv.GetSystemDateTime(this.Context); msg.CreateTime = timeNow; msg.MessageId = dbService.GetSequenceString(1)[0]; msg.SenderId = 16394; // 固化为Administrator msg.ObjectTypeId = "BD_MATERIAL"; msg.MsgType = MsgType.CommonMessage;//消息类型 msg.KeyValue = mtrlId.ToString();//关联物料内码 // 消息接收人 List<UserMember> users = new List<UserMember>(); foreach (var receiverId in receiverIds) { UserMember user = new UserMember() { Id = receiverId.ToString(), }; users.Add(user); } // 发送... MessageRepository msgRepository = new MessageRepository(); msgRepository.SendMessage(this.Context, msg, users); } } /// <summary> ///查找采购员与用户之间的关联关系,用采购员内码与用户内码构建字典 /// </summary> /// <param name="ctx"></param> /// <param name="purchaserIdLst"></param> /// <returns></returns> private Dictionary<long, long> GetPurIdOrUserId(Context ctx, List<long> purchaserIdLst) { Dictionary<long, long> dicPurIdByUserId = new Dictionary<long, long>(); string tempTb = StringUtils.GetSqlWithCardinality(purchaserIdLst.Count(), "@PurId", 1); StringBuilder strSQL = new StringBuilder(); strSQL.AppendLine(" SELECT DISTINCT T3.FID ,T1.FUSERID "); strSQL.AppendLine(" FROM T_SEC_USER T1 "); strSQL.AppendLine(" INNER JOIN V_BD_CONTACTOBJECT T2 ON T1.FLinkObject=T2.FID AND T1.FLinkObject!=0 "); strSQL.AppendLine(" INNER JOIN V_BD_BUYER T3 ON T3.FNUMBER=T2.FNUMBER "); strSQL.AppendFormat(" INNER JOIN {0} Temp ON Temp.FID =T3.FID ", tempTb); strSQL.AppendLine(" WHERE T1.FTYPE='1' "); var sqlParams = new[] { new SqlParam("@PurId",KDDbType.udt_inttable,purchaserIdLst) }; using (IDataReader dr = DBUtils.ExecuteReader(this.Context, strSQL.ToString(), sqlParams.ToList())) { while (dr.Read()) { long entryId = Convert.ToInt64(dr[0]); long pickQty = Convert.ToInt64(dr[1]); if (!dicPurIdByUserId.ContainsKey(entryId)) { dicPurIdByUserId.Add(entryId, pickQty); } } } return dicPurIdByUserId; } } }
推荐阅读