C# 如何使用更新上下文消息获取更新字段
我不知道更新最大值后如何获取最大值 当我创建新的最大金额时,0.01将自动更新下一个最小金额 当我更新最大金额时,最小金额不会更新为新的最小金额,即25000.01C# 如何使用更新上下文消息获取更新字段,c#,plugins,dynamics-crm,C#,Plugins,Dynamics Crm,我不知道更新最大值后如何获取最大值 当我创建新的最大金额时,0.01将自动更新下一个最小金额 当我更新最大金额时,最小金额不会更新为新的最小金额,即25000.01 if (context.MessageName == "Create") { Guid RebateLevelsId = new Guid(); RebateLevelsId = targetEntity.Id; if (targetEntity.Attributes.Contains("new_usa
if (context.MessageName == "Create") {
Guid RebateLevelsId = new Guid();
RebateLevelsId = targetEntity.Id;
if (targetEntity.Attributes.Contains("new_usagerebate")) {
EntityReference usageRebateEnt = (EntityReference) targetEntity.Attributes["new_usagerebate"];
Guid usageRebateId = usageRebateEnt.Id;
EntityCollection rebateLevelEnt = GetEntityCollectionRebateLevel(service, "new_rebatelevels", "new_usagerebate", usageRebateId, new ColumnSet(true));
Entity rebateLevelEntity = new Entity("new_rebatelevels", RebateLevelsId);
attributes A = new attributes();
if (rebateLevelEnt.Entities.Count > 0) {
for (int i = 0; i < rebateLevelEnt.Entities.Count; i++) {
if (i == rebateLevelEnt.Entities.Count - 1) {
A.level = i + 1;
}
if (i == rebateLevelEnt.Entities.Count - 2) {
A.Max_Amount = rebateLevelEnt.Entities[i].GetAttributeValue < decimal > ("new_maxamount");
}
}
rebateLevelEntity["new_level"] = A.level.ToString();
rebateLevelEntity["new_minamount"] = A.Max_Amount + 0.01 m;
service.Update(rebateLevelEntity);
} else {
rebateLevelEntity["new_level"] = 1. ToString();
service.Update(rebateLevelEntity);
}
}
}
if(context.MessageName==“Create”){
Guid RebateLevelsId=新Guid();
RebateLevelsId=targetEntity.Id;
if(targetEntity.Attributes.Contains(“new_UsageRate”)){
EntityReference usagerbate=(EntityReference)targetEntity.Attributes[“new_usagerbate”];
Guid USAGERBATEID=USAGERBATENT.Id;
EntityCollection rebateLevelEnt=GetEntityCollection REBATELEVEL(服务,“新”rebatelevels,“新”USAGERBATE,“USAGERBATEID”,新列集(true));
实体rebateLevelEntity=新实体(“新”REBATELEVELES“,REBATELEVELESID);
属性A=新属性();
如果(rebateLevelEnt.Entities.Count>0){
对于(int i=0;i(“新的最大金额”);
}
}
rebateLevelEntity[“新_级别”]=A.level.ToString();
REBATELEVELEENTITY[“新的最低限额”]=最高限额+0.01米;
服务更新(实体);
}否则{
rebateLevelEntity[“新_级别”]=1.ToString();
服务更新(实体);
}
}
}
我对这个问题进行了一些思考,并编写了一个如何处理的示例
我希望构建一个健壮的解决方案,允许对我们正在创建或更新的解决方案的上面和下面的级别进行调整。本解决方案涵盖的情况包括:
IOrganizationService
调用被注释掉。但是,它使用了EntityCollection
和Entity
类,因此您应该能够非常轻松地插入实时数据
我采用了面向对象的方法。Level
类最初是一个包装器,用于公开new_rebatelevel的
数据,而不必经常使用GetAttributeValue
。它成长为一个强大的类,承担了大部分的负载
目标
表示已创建或更新的级别
主要内容如下:
class Program
{
static void Main(string[] args)
{
var app = new App_CalcMinMax();
app.Run();
if (System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine("\nPress <Enter> to continue...");
Console.ReadLine();
}
}
}
类程序
{
静态void Main(字符串[]参数)
{
var app=新app_CalcMinMax();
app.Run();
if(System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine(“\n按继续…”);
Console.ReadLine();
}
}
}
以及“应用程序”:
使用Microsoft.Xrm.Sdk;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
类应用程序\u CalcMinMax
{
公开募捐
{
var retbates=new usagerbate(toCollection(new List());//从空列表开始
//处理各种级别的创建和更新事件
//并显示每个事件的结果
//.Process()接受级别#、最小值和最大值
回扣。过程(1,0,10000);
回扣。流程(21100.01m,20000);
回扣。过程(321000.01m,30000);
回扣。过程(1,0,9000);
回扣。流程(2,10000,15000);
回扣。流程(31170035000);
回扣。流程(31200024000);
回扣。流程(2,8000,20000);
回扣。过程(1,0,25000);
退税。流程(21400019000);
}
私人班级
{
私营实体e;
私人层面较低;
私人高层;
public int Num=>e.GetAttributeValue(“新级别”);
public decimal Min=>e.GetAttributeValue(“new_minamount”).值;
public decimal Max=>e.GetAttributeValue(“new_maxamount”).Value;
public bool IsValidLower=>lower==null?true:Min>lower.Min;
public bool IsValidUpper=>upper==null?true:MaxIsValidLower&&IsValidUpper;
公共bool AdjustLower=>lower==null?false:Min-0.01m!=lower.Max;
公共bool AdjustUpper=>upper==null?错误:最大值+0.01m!=upper.Min;
公共层面(实体e)
{
这个。e=e;
}
public void ValidateLower(级别更低)
{
this.lower=更低;
}
公共无效验证上限(上一级)
{
this.upper=upper;
}
public void SetMin(十进制值,IOOrganizationService=null)
{
e[“新_minamou
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class App_CalcMinMax
{
public void Run()
{
var rebates = new UsageRebate(toCollection(new List<Entity>())); //start with an empty list
//process various level create and update events
//and display the results of each event
//.Process() takes the level #, the min, and the max
rebates.Process(1, 0, 10000);
rebates.Process(2, 11000.01m, 20000);
rebates.Process(3, 21000.01m, 30000);
rebates.Process(1, 0, 9000);
rebates.Process(2, 10000, 15000);
rebates.Process(3, 17000, 35000);
rebates.Process(3, 12000, 24000);
rebates.Process(2, 8000, 20000);
rebates.Process(1, 0, 25000);
rebates.Process(2, 14000, 19000);
}
private class Level
{
private Entity e;
private Level lower;
private Level upper;
public int Num => e.GetAttributeValue<int>("new_level");
public decimal Min => e.GetAttributeValue<Money>("new_minamount").Value;
public decimal Max => e.GetAttributeValue<Money>("new_maxamount").Value;
public bool IsValidLower => lower == null ? true : Min > lower.Min;
public bool IsValidUpper => upper == null ? true : Max < upper.Max;
public bool IsValid => IsValidLower && IsValidUpper;
public bool AdjustLower => lower == null ? false : Min - 0.01m != lower.Max;
public bool AdjustUpper => upper == null ? false : Max + 0.01m != upper.Min;
public Level(Entity e)
{
this.e = e;
}
public void ValidateLower(Level lower)
{
this.lower = lower;
}
public void ValidateUpper(Level upper)
{
this.upper = upper;
}
public void SetMin(decimal value, IOrganizationService service = null)
{
e["new_minamount"] = new Money(value);
//service.Update(e);
}
public void SetMax(decimal value, IOrganizationService service = null)
{
e["new_maxamount"] = new Money(value);
//service.Update(e)
}
public override string ToString()
{
return $" Level: {Num}\tmin: { Min.ToString("N2"),9}\tmax: { Max.ToString("N2"),9}";
}
}
private class UsageRebate
{
private IOrganizationService service;
private EntityCollection ec;
private IEnumerable<int> levelNums => Levels.Select(l => l.Num);
public IEnumerable<Entity> Entities => ec.Entities;
public IEnumerable<Level> Levels => Entities.Select(e => new Level(e));
public UsageRebate(EntityCollection ec, IOrganizationService service = null)
{
this.ec = ec;
this.service = service;
}
public void Process(int num, decimal min, decimal max)
{
var target = toEntity(num, min, max);
var level = new Level(target);
Level prior = null;
Level next = null;
Console.WriteLine($"Target: {level.ToString()}");
if (tryGetLevel(level.Num-1,out prior))
{
level.ValidateLower(prior);
}
if (tryGetLevel(level.Num + 1, out next))
{
level.ValidateUpper(next);
}
if (level.IsValid)
{
if (exists(level.Num))
{
update(target);
}
else
{
add(target);
}
if (level.AdjustLower)
{
prior.SetMax(level.Min - 0.01m);
}
if (level.AdjustUpper)
{
next.SetMin(level.Max + 0.01m);
}
}
else
{
string message = "Exception: ";
if (!level.IsValidLower)
{
message += $"Level {level.Num} Min is less than Level {prior.Num} Min\n";
}
if (!level.IsValidUpper)
{
message += $"Level {level.Num} Max exceeds Level {next.Num} Max";
}
Console.WriteLine(message);
//throw new Exception(message);
}
Console.WriteLine($"Results:\n{ToString()}");
}
private bool tryGetLevel(int num, out Level level)
{
var ex = exists(num);
level = ex ? get(num) : null;
return ex;
}
private void add(Entity entity)
{
ec.Entities.Add(entity);
//service.Create(entity);
}
private void update(Entity entity)
{
var e = get(entity);
e["new_minamount"] = entity["new_minamount"];
e["new_maxamount"] = entity["new_maxamount"];
//service.Update(entity);
}
private bool exists(int num)
{
return levelNums.Contains(num);
}
private Level get(int num)
{
return Levels.Where(l => l.Num == num).Single();
}
private Entity get(Entity entity)
{
return Entities.Where(e => new Level(e).Num == new Level(entity).Num).Single();
}
private Entity toEntity(int level, decimal min, decimal max)
{
return new Entity
{
LogicalName = "new_rebatelevel",
Id = Guid.NewGuid(),
Attributes =
{
new KeyValuePair<string, object>("new_level", level),
new KeyValuePair<string, object>("new_minamount", new Money(min)),
new KeyValuePair<string, object>("new_maxamount", new Money(max))
}
};
}
public override string ToString()
{
var sb = new StringBuilder();
Levels.ToList().ForEach(l => sb.AppendLine(l.ToString()));
return sb.ToString();
}
}
private EntityCollection toCollection(List<Entity> list)
{
var ec = new EntityCollection();
ec.EntityName = "new_rebatelevel";
ec.Entities.AddRange(list);
return ec;
}
}