コード例 #1
0
 public void AddAmount(MoneyEarned money)
 {
     using (var ctx = new MaaserContext(_connString))
     {
         money.UserId = _user.Id;
         ctx.MoneyEarned.Add(money);
         ctx.SaveChanges();
     }
 }
コード例 #2
0
        private List <int> AddGiveToMoney(MaaserGiven maaserGiven)
        {
            List <int> moneyIds = new List <int>();

            using (var ctx = new MaaserContext(_connString))
            {
                IEnumerable <MoneyEarned> money = ctx.MoneyEarned.Where(z => z.UserId == _user.Id);
                MoneyEarned m      = money.FirstOrDefault(mo => mo.PaidUp == false);
                decimal     amount = maaserGiven.Amount * 10;
                if (m == null)
                {
                    return(null);
                }

                while (m.PaidUp == false && amount != 0 && m.AmountLeft != 0)
                {
                    m.AmountLeft = m.AmountLeft - amount;
                    moneyIds.Add(m.Id);
                    if (m.AmountLeft < 0)
                    {
                        amount       = 0 - m.AmountLeft;
                        m.AmountLeft = 0;
                        m.PaidUp     = true;
                        ctx.MoneyEarned.Attach(m);
                        ctx.Entry(m).State = EntityState.Modified;
                        m = money.FirstOrDefault(mo => mo.PaidUp == false);
                        if (m == null)
                        {
                            break;
                        }
                    }
                    else if (m.AmountLeft == 0)
                    {
                        m.PaidUp = true;
                        ctx.MoneyEarned.Attach(m);
                        ctx.Entry(m).State = EntityState.Modified;
                    }
                    else
                    {
                        ctx.SaveChanges();
                        break;
                    }
                    ctx.SaveChanges();
                }

                return(moneyIds);
            }
        }