示例#1
0
        public bool Draw(string memberId, WalletType walletType, decimal money, string remark, out string error, string tag = null, string toMemberId = null, bool drawFrozen = false)
        {
            error = string.Empty;
            if (money == 0)
            {
                return(true);
            }

            using (var dbContext = new WalletDbContext())
            {
                var wallet =
                    dbContext.Set <Models.Wallet>()
                    .FirstOrDefault(w => w.WalletType == walletType && w.MemberId == memberId);

                if (drawFrozen)
                {
                    if (wallet == null || wallet.Frozen < money)
                    {
                        error = "冻结余额不足";
                        return(false);
                    }

                    wallet.Frozen -= money;
                }
                else
                {
                    if (wallet == null || wallet.Available < money)
                    {
                        error = "余额不足";
                        return(false);
                    }

                    wallet.Available -= money;
                }
                dbContext.Set <Models.Wallet>().Attach(wallet);
                dbContext.Entry(wallet).State = EntityState.Modified;

                WalletBill walletBill = new WalletBill();
                walletBill.Id           = KeyGenerator.GetGuidKey();
                walletBill.MemberId     = memberId;
                walletBill.BillType     = BillType.TakeOut;
                walletBill.WalletType   = walletType;
                walletBill.Money        = money;
                walletBill.Remark       = remark;
                walletBill.BillTag      = tag;
                walletBill.CreateTime   = DateTime.Now;
                walletBill.FromMemberId = toMemberId;
                dbContext.WalletBills.Add(walletBill);
                dbContext.SaveChanges();
                return(true);
            }
        }
示例#2
0
        public bool Deposit(string memberId, WalletType walletType, decimal money, string remark, out string error, string tag = null, string fromMemberId = null)
        {
            error = string.Empty;
            if (money == 0)
            {
                return(true);
            }
            using (var dbContext = new WalletDbContext())
            {
                var wallet =
                    dbContext.Set <Models.Wallet>()
                    .FirstOrDefault(w => w.WalletType == walletType && w.MemberId.Equals(memberId, StringComparison.OrdinalIgnoreCase));
                if (wallet == null)
                {
                    wallet            = new Models.Wallet();
                    wallet.Id         = KeyGenerator.GetGuidKey();
                    wallet.MemberId   = memberId;
                    wallet.WalletType = walletType;
                    wallet.Frozen     = 0;
                    wallet.Available  = money;
                    dbContext.Wallets.Add(wallet);
                }
                else
                {
                    wallet.Available += money;
                    dbContext.Set <Models.Wallet>().Attach(wallet);
                    dbContext.Entry(wallet).State = EntityState.Modified;
                }

                WalletBill walletBill = new WalletBill();
                walletBill.Id           = KeyGenerator.GetGuidKey();
                walletBill.MemberId     = memberId;
                walletBill.BillType     = BillType.TakeIn;
                walletBill.WalletType   = walletType;
                walletBill.Money        = money;
                walletBill.Remark       = remark;
                walletBill.BillTag      = tag;
                walletBill.CreateTime   = DateTime.Now;
                walletBill.FromMemberId = fromMemberId;
                dbContext.WalletBills.Add(walletBill);
                dbContext.SaveChanges();
                return(true);
            }
        }