private async Task <decimal> AddOneMemberPoint(SaleToCustomerDetail saleToCustomerDetail, MemberPointRule pointRule, string customerID, string customerLevel, LevelRelation relation) { if (string.IsNullOrWhiteSpace(customerID) == false && string.IsNullOrWhiteSpace(customerLevel) == false) { MemberPoint menberPoint = new MemberPoint(saleToCustomerDetail); menberPoint.Owner = this.AppDbContext.FindOrAttachToLocal(customerID); menberPoint.UseableDate = pointRule.CalcAvailableDate(menberPoint.DealDate); menberPoint.Quantity = pointRule.Calc(customerLevel, relation, saleToCustomerDetail.Price - saleToCustomerDetail.CashCoupon); menberPoint.ID = IDGenerator.GetMemberPointIDGenerator(this.AppDbContext).GetNext(); await CalcCurrentTotal(menberPoint); this.AppDbContext.MemberPoint.Add(menberPoint); return(menberPoint.Quantity); } return(0); }
private async Task <SellMemberPointViewModel> AddMemberPoint(SaleToCustomerDetail saleToCustomerDetail) { MemberPointRule pointRule = await this.GetPointRule(); // Left JOIN /* * var customers = await (from m1 in _applicationDbContext.Members * join m2 in _applicationDbContext.Members * on m1.ReferenceMemberID equals m2.MemberID * select new { * MemberID = m1.MemberID, * MemberName = m1.Name, * MemberLevel = m1.Level, * * Up1ID = m2.MemberID, * Up1Name = m2.Name, * Up1Level = m2.Level, * Up1ReferenceMemberID = m2.ReferenceMemberID * } into M12 * join m3 in _applicationDbContext.Members * on M12.Up1ReferenceMemberID equals m3.MemberID into M23 * from m5 in M23.DefaultIfEmpty() * where M12.MemberID.Equals(saleToCustomerDetail.Sale.Customer.MemberID, * StringComparison.InvariantCultureIgnoreCase) * select new SellMemberPointViewModel * { * MemberID = M12.MemberID, * MemberName = M12.MemberName, * MemberLevel = M12.MemberLevel, * * Up1ID = M12.Up1ID, * Up1Name = M12.Up1Name, * Up1Level = M12.Up1Level, * * Up2ID = m5.MemberID, * Up2Name = m5.Name, * Up2Level = m5.Level, * }).FirstOrDefaultAsync(); * */ var customers = GetCustomersFromSqlCommand(_applicationDbContext.Database.AsRelational(), saleToCustomerDetail.Sale.Customer.MemberID); if (customers == null) { return(new SellMemberPointViewModel { MemberID = "", MemberName = "", MemberLevel = "", Up1ID = "", Up1Name = "", Up1Level = "", Up2ID = "", Up2Name = "", Up2Level = "" }); } if (string.IsNullOrWhiteSpace(customers.MemberID) == false) { MemberPoint menberPoint = new MemberPoint(saleToCustomerDetail); menberPoint.Owner = new Member { MemberID = customers.MemberID }; menberPoint.UseableDate = pointRule.CalcAvailableDate(menberPoint.DealDate); menberPoint.Quantity = pointRule.Calc(customers.MemberLevel, LevelRelation.Self, saleToCustomerDetail.Price); menberPoint.ID = IDGenerator.GetMemberPointIDGenerator(this._applicationDbContext).GetNext(); var pointInfo = await GetMemberPointInfo(menberPoint.Owner.MemberID); if (pointInfo != null) { menberPoint.CurrentTotalQuantity = pointInfo.PointTotal; } this._applicationDbContext.MemberPoint.Add(menberPoint); customers.PointCount = menberPoint.Quantity; } if (string.IsNullOrWhiteSpace(customers.Up1ID) == false) { MemberPoint menberPoint = new MemberPoint(saleToCustomerDetail); menberPoint.Owner = new Member { MemberID = customers.Up1ID }; menberPoint.UseableDate = pointRule.CalcAvailableDate(menberPoint.DealDate); menberPoint.Quantity = pointRule.Calc(customers.Up1Level, LevelRelation.Son, saleToCustomerDetail.Price); menberPoint.ID = IDGenerator.GetMemberPointIDGenerator(this._applicationDbContext).GetNext(); var pointInfo = await GetMemberPointInfo(menberPoint.Owner.MemberID); if (pointInfo != null) { menberPoint.CurrentTotalQuantity = pointInfo.PointTotal; } this._applicationDbContext.MemberPoint.Add(menberPoint); customers.Up1PointCount = menberPoint.Quantity; } if (string.IsNullOrWhiteSpace(customers.Up2ID) == false) { MemberPoint menberPoint = new MemberPoint(saleToCustomerDetail); menberPoint.Owner = new Member { MemberID = customers.Up2ID }; menberPoint.UseableDate = pointRule.CalcAvailableDate(menberPoint.DealDate); menberPoint.Quantity = pointRule.Calc(customers.Up2Level, LevelRelation.Grandson, saleToCustomerDetail.Price); menberPoint.ID = IDGenerator.GetMemberPointIDGenerator(this._applicationDbContext).GetNext(); var pointInfo = await GetMemberPointInfo(menberPoint.Owner.MemberID); if (pointInfo != null) { menberPoint.CurrentTotalQuantity = pointInfo.PointTotal; } this._applicationDbContext.MemberPoint.Add(menberPoint); customers.Up2PointCount = menberPoint.Quantity; } return(customers); }