【问题标题】:Nopcommerce MessageTemplate, can't add method as tokenNopcommerce MessageTemplate,无法将方法添加为令牌
【发布时间】:2014-02-21 00:02:05
【问题描述】:

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

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

ProductService 类:

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)());

它告诉我:

错误 10 'Nop.Services.Catalog.ProductService.DailyBestSellersReport(int, int, int)' 是一个“方法”,在给定的上下文中无效

我还添加了来自“BestsellersReportLine”类的标记,效果很好,但是这些是属性而不是方法,例如:

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

    public decimal TotalAmount { get; set; }

    public int TotalQuantity { get; set; }
}

有什么想法吗?

谢谢

【问题讨论】:

    标签: c# asp.net-mvc email nopcommerce


    【解决方案1】:

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

    您需要编写根据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();
    

    【讨论】:

    • 我明白了,谢谢。在“DailyBestSellersReport()”方法之后在 ProductServices 中写“Returnable()”是否合适,还是应该在其他地方写?我怀疑“参数”将是 DailyBestSellersReport 的属性:“int RecordsToReturn,int OrderBy,int GroupBy,因为它抱怨属性是“int,int,int”。谢谢
    • 如果您打算将该功能仅限于该类,您只能在该类中使用它。是的,参数就是你所说的
    • 这实际上是有道理的 =) 你知道我在哪里可以找到 html 表的示例代码。我需要做一个,但我不知道怎么做,我只是查看了 MessageTemplate 和 MessageTokenProvider 中的现有代码。
    • 你检查过 MessageToken 提供程序吗?我认为有一些方法可以解决这个问题。如果你找不到我可以从我身边给你类似的代码之王。告诉我
    • stringbuilder 应该保存格式化的字符串,这些字符串是表格行、表格数据、图像等
    猜你喜欢
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2014-07-17
    • 2019-07-13
    • 2019-12-22
    • 2021-09-15
    相关资源
    最近更新 更多