gpt4 book ai didi

c# - Nopcommerce MessageTemplate,无法将方法添加为 token

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:00 24 4
gpt4 key购买 nike

我正在 nopCommerce 中制作一个 MessageTemplate,我想添加一个 token 来实现另一个类的方法。

在“ProductService”类中,我有一个名为“DailyBestsellersReport”的方法,我想将其添加为“MessageTokenProvider”中我的 MessageTemplate 的标记。但是,当我在 MessageTokenProvider 中添加“ProductService”作为引用时,它告诉我方法“DailyBestsellerReport”在当前上下文中无效,这让我认为某处存在语法错误。这是我想添加为 token 的方法:

产品服务类:

public IList<BestsellersReportLine> DailyBestSellersReport(
int recordsToReturn = 5, int orderBy = 1, int groupBy = 1)
{
var yesterDay = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
var earliest = new DateTime(yesterDay.Year, yesterDay.Month, yesterDay.Day, 0, 0, 0);
var latest = earliest.Add(new TimeSpan(1, 0, 0, 0, -1));
var currentDay = DateTime.Now;
var dayBefore = DateTime.Now.AddDays(-1);


var query1 = from opv in _opvRepository.Table
where earliest <= currentDay && latest >= dayBefore
join o in _orderRepository.Table on opv.OrderId equals o.Id
join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
join p in _productRepository.Table on pv.ProductId equals p.Id
select opv;


var query2 = groupBy == 1 ?
//group by product variants
from opv in query1
group opv by opv.ProductVariantId into g
select new
{
EntityId = g.Key,
TotalAmount = g.Sum(x => x.PriceExclTax),
TotalQuantity = g.Sum(x => x.Quantity),
}
:
//group by products
from opv in query1
group opv by opv.ProductVariant.ProductId into g
select new
{
EntityId = g.Key,
TotalAmount = g.Sum(x => x.PriceExclTax),
TotalQuantity = g.Sum(x => x.Quantity),
}
;

switch (orderBy)
{
case 1:
{
query2 = query2.OrderByDescending(x => x.TotalQuantity);
}
break;
case 2:
{
query2 = query2.OrderByDescending(x => x.TotalAmount);
}
break;
default:
throw new ArgumentException("Wrong orderBy parameter", "orderBy");
}

if (recordsToReturn != 0 && recordsToReturn != int.MaxValue)
query2 = query2.Take(recordsToReturn);

var result = query2.ToList().Select(x =>
{
var reportLine = new BestsellersReportLine()
{
EntityId = x.EntityId,
TotalAmount = x.TotalAmount,
TotalQuantity = x.TotalQuantity
};
return reportLine;
}).ToList();

return result;
}

我想在这里添加“DailyBestsellersReport”:

MessageTokenProvider 类

public void AddReportTokens(IList<Token> tokens, BestsellersReportLine DailyBestSellersReport, ProductService productService, int languageId)
{
tokens.Add(new Token("BestsellersReportLine.EntityId", DailyBestSellersReport.EntityId.ToString()));
tokens.Add(new Token("BestsellersReportLine.TotaAmount", DailyBestSellersReport.TotalAmount.ToString()));
tokens.Add(new Token("BestsellersReportLine.TotalQuantity", DailyBestSellersReport.TotalQuantity.ToString()));

tokens.Add(new Token("ProductService.DailyBestSellersReport", productService.DailyBestSellersReport.ToString)());
}

当我添加时:

tokens.Add(new Token("ProductService.DailyBestSellersReport", productService.DailyBestSellersReport.ToString)());

它告诉我:

Error 10 'Nop.Services.Catalog.ProductService.DailyBestSellersReport(int, int, int)' is a 'method', which is not valid in the given context

我还添加了来自“BestsellersReportLine”类的标记,它工作正常,但是这些是属性而不是像这样的方法:

public partial class BestsellersReportLine
{
public int EntityId { get; set; }

public decimal TotalAmount { get; set; }

public int TotalQuantity { get; set; }
}

有什么想法吗?

谢谢

最佳答案

错误本身会告诉解决方案,当方法接受参数时,如何在不向其传递任何参数的情况下调用方法。

您需要编写根据 productService.DailyBestSellersReport

的结果返回字符串的方法
public string ReturnTable()
{
var report = productService.DailyBestSellersReport(param1,param2,param2)

StringBuilder sb = new StringBuilder();

foreach (var r in report)
{
// I believe you are trying to build HTML table so you can append any string here to sb
}
return sb.ToString();
}

然后使用

tokens.Add(new Token("ProductService.DailyBestSellersReport",ReturnTable());

sb.AppendLine("<table border=\"0\" style=\"width:100%;\">");//sb is stringbuilder's object

sb.AppendLine(string.Format("<tr style=\"background-color:{0};text-align:center;font-size:12px; \">", _templatesSettings.Color1));
sb.AppendLine(string.Format("<th>Sr. No.</th>"));
sb.AppendLine(string.Format("<th>Item1</th>"));
sb.AppendLine(string.Format("<th>Item2</th>"));
sb.AppendLine("</tr>");

// Header is closed
// Next is data which will be created using foreach on data from `IList<BestsellersReportLine>`
foreach (var item in result)
{
sb.AppendLine(string.Format("<tr style=\"background-color: {0};text-align: center;\">", _templatesSettings.Color2));
// you can place all your data in below tds, you can create any number of tds.
sb.AppendLine(string.Format("<td style=\"padding: 0.6em 0.4em;text-align: right;\">{0}</td>", item.Prop1));
sb.AppendLine("<td style=\"padding: 0.6em 0.4em;text-align: left;\">" + item.Prop2);
sb.AppendLine("</td>");
sb.AppendLine("</tr>");
}
sb.AppendLine("</table>"); // don't forget to close table

return sb.ToString();

关于c# - Nopcommerce MessageTemplate,无法将方法添加为 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21426768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com