批量数据库条目-计算佣金

本文关键字:计算 数据库 | 更新日期: 2023-09-27 18:10:51

我正在写一个MVC 3项目,我正在使用实体框架。我已经写了一个方法,生成批量数据库条目,这是客户的发票。该系统适用于一所大学,每个学生在"入学"类中都有一个特定的导师。此注册的月费存储在enrollment中。发票方法可以正确地生成发票,但现在我希望为学院的导师生成佣金单的数据库条目。该方法需要找到每个导师(通过enrollment类)注册的所有学生,并累积这个数量。然后将这一总额乘以导师获得的佣金百分比。这就是佣金金额。我不确定如何为此编写代码。这些是相关的模型类:

public class Enrollment
{
    [Key]
    [Display(Name = "Enrollment ID Number")]
    public long EnrollmentIDNumber { get; set; }
    [Display(Name = "Client ID Number")]
    public long ClientNumberID { get; set; }
    [Display(Name = "Tutor ID Number")]
    public long TutorNoID { get; set; }
    [Display(Name = "Course Name")]
    public string CourseName { get; set; }
    [Display(Name = "Lesson Time")]
    public string LessonTime { get; set; }
    [Display(Name = "Lesson Day")]
    public string LessonDay { get; set; }
    [Display(Name = "Lesson Location")]
    public string LessonLocation { get; set; }
    [Display(Name = "Lesson Type")]
    public string LessonType { get; set; }
    [Display(Name = "Lesson Level")]
    public string LessonLevel { get; set; }
    [Display(Name = "Monthly Fee")]
    public long MonthlyFee { get; set; }
    public virtual Client Client { get; set; }
    public virtual Tutor Tutor { get; set; }
}
public class TutorCommission
{
    [Key]
    [Display(Name = "Commission ID")]
    public long CommissionID { get; set; }
    [Display(Name = "Commission Month")]
    public string CommissionMonth {get; set;}
    [Display(Name = "Commission Amount")]
    public long CommissionAmount { get; set; }
    [Display(Name = "Commission Status")]
    public string CommissionStatus { get; set; }
    [Display(Name = "Tutor ID Number")]
    public long TutorNoID { get; set; }
    public virtual Tutor Tutor { get; set; }
    public virtual ICollection<CommissionPayments> CommissionPayments { get; set; }
}
public class TutorCommissionPercentage
{
    [Key]
    public int TutorCommissionID { get; set; }
    public long TutorNoID { get; set; }
    [Range(0, 100, ErrorMessage="Percentage must be between 0 and 100")]
    [Display(Name="Commission Percentage")]
    public decimal CommissionPercentage { get; set; }
    public virtual Tutor Tutor { get; set; }
}
生成发票的代码如下:
    public ActionResult CreateBulkInvoices()
    {
        var month = DateTime.Now.ToString("MMMM");
        var enrolments = db.Enrollments.ToList();
        var newInvoices = from enrolment in enrolments
                          select new Invoices()
                          {
                              InvoiceAmount = enrolment.MonthlyFee,
                              InvoiceMonth = month,  // string constant
                              InvoiceStatus = "Unpaid",
                              ClientNumberID = enrolment.ClientNumberID
                          };
        foreach (var newInvoice in newInvoices)
        {
            db.Invoice.Add(newInvoice);
            db.SaveChanges();
        }
        return RedirectToAction("Index");
    }
public class Tutor
{
    [Key]
    [Display(Name = "Tutor ID Number")]
    public long TutorNoID { get; set; }
    [Required]
    [StringLength(50, ErrorMessage="First name must be less than 50 characters")]
    [Display(Name = "First Name")]
    public string TutorFirstName { get; set; }
    [StringLength(50, ErrorMessage = "Last name must be less than 50 characters")]
    [Display(Name = "Last Name")]
    public string TutorLastName { get; set; }
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    [Display(Name = "Birth Date")]
    public DateTime? TutorBirthDate { get; set; }
    [Display(Name = "Cellphone Number")]
    public string TutorCellphoneNumber { get; set; }
    [Display(Name = "Home Number")]
    public string TutorHomeNumber { get; set; }
    [RegularExpression("^[a-z0-9_''+-]+(''.[a-z0-9_''+-]+)*@[a-z0-9-]+(''.[a-z0-9-]+)*''.([a-z]{2,4})$", ErrorMessage = "Not a valid email address")]
    [Display(Name = "Email Address")]
    public string TutorEmailAddress { get; set; }
    [Display(Name = "Street Address")]
    public string TutorStreetAddress { get; set; }
    [Display(Name = "Suburb")]
    public string TutorSuburb { get; set; }
    [Display(Name = "City")]
    public string TutorCity { get; set; }
    [Display(Name = "Postal Code")]
    public string TutorPostalCode { get; set; }
    [Display(Name="Full Name")]
    public string FullName
    {
        get
        {
            return TutorFirstName + " " + TutorLastName;
        }
    }
    [Display(Name="Commission Percentage")]
    [Required]
    public double TutorCommissionPercentage { get; set; }
    public virtual ICollection<Enrollment> Enrollments { get; set; }
    public virtual ICollection<TutorCommission> TutorCommissions { get; set; }
}

这些批量数据库输入方法的目的是使经理可以单击一个链接并为数据库中的所有客户和导师生成发票和佣金单。

谢谢艾米

批量数据库条目-计算佣金

要在一个语句中完成所有操作,您需要加入注册和佣金,然后按导师分组。

第一个例子使用的查询语法就像你使用的:

        IEnumerable<TutorCommission> tutorsCommissions =
            from enrollment in enrollments // Take the enrollments
            join tutorsCommissionPercentage in tutorCommissionPercentages // Join with the tutor's commission percentages.
                on enrollment.TutorNoID equals tutorsCommissionPercentage.TutorNoID
            group enrollment by new { enrollment.TutorNoID, tutorsCommissionPercentage.CommissionPercentage } into enrollmentsAndCommissionByTutor // group enrollments and commission by the tutor
            select new TutorCommission
            {
                TutorNoID = enrollmentsAndCommissionByTutor.Key.TutorNoID, // the grouping which is the tutor
                CommissionAmount = (long) enrollmentsAndCommissionByTutor.Sum(e => e.MonthlyFee * enrollmentsAndCommissionByTutor.Key.CommissionPercentage)
            };

第二个例子使用的方法语法对于

操作来说更直观一些
        IEnumerable<TutorCommission> tutorsCommissionsAlt = enrollments // Take the enrollments
            .GroupJoin( // This will group enrollments by the tutor
                tutorCommissionPercentages, // Join with the tutor's commission percentages.
                e => e.TutorNoID, // Use tutorNoID for left Key
                tcp => tcp.TutorNoID, // ... and right key
                (e, tcp) => new TutorCommission // Create entry which is the tutor and his total commission
                {
                    TutorNoID = e.TutorNoID,
                    CommissionAmount = (long) tcp.Sum(c => c.CommissionPercentage * e.MonthlyFee)
                });