public ActionResult Create(FoodCardRecharge foodcardrecharge)
        {
            if (ModelState.IsValid)
            {
                var foodCard = db.FoodCards.Find(foodcardrecharge.FoodCard_id);
                foodCard.CurrentBalance += foodcardrecharge.Amount;
                db.Entry(foodCard).State = EntityState.Modified;

                var rechargeFor = db.UserAccounts.Where(p =>
                    p.FoodCard_id == foodcardrecharge.FoodCard_id &&
                    p.User_id == foodcardrecharge.OperateFor);

                if (rechargeFor.Count() == 0)
                {
                    var useraccount = new UserAccount() {
                        User_id = foodcardrecharge.OperateFor,
                        FoodCard_id = foodcardrecharge.FoodCard_id,
                        Balance = foodcardrecharge.Amount
                    };

                    db.UserAccounts.Add(useraccount);
                }
                else if (rechargeFor.Count() == 1)
                {
                    var userAccount = rechargeFor.First();
                    userAccount.Balance += foodcardrecharge.Amount;
                    db.Entry(userAccount).State = EntityState.Modified;
                }
                else
                {
                    throw new Exception(string.Format("Invalid user account, user id: {0}, foodcard id: {1}.",
                                                      foodcardrecharge.OperateFor,
                                                      foodcardrecharge.FoodCard_id));
                }

                db.FoodCardRecharges.Add(foodcardrecharge);
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            ViewBag.OperateFor = new SelectList(db.Users, "User_id", "UserName", foodcardrecharge.OperateFor);
            ViewBag.Operator = new SelectList(db.Users, "User_id", "UserName", foodcardrecharge.Operator);
            ViewBag.FoodCard_id = new SelectList(db.FoodCards, "FoodCard_id", "FoodCardName", foodcardrecharge.FoodCard_id);
            return View(foodcardrecharge);
        }
        public ActionResult Edit(FoodCardRecharge foodcardrecharge)
        {
            if (ModelState.IsValid)
            {
                var originalRecharge = db.FoodCardRecharges.Find(foodcardrecharge.FoodCardRecharge_id);
                var originFoodcard = db.FoodCards.Find(originalRecharge.FoodCard_id);
                var rechargeFor = db.Users.Find(foodcardrecharge.OperateFor);

                var originUserAccount = db.UserAccounts.Where(p =>
                    p.FoodCard_id == originalRecharge.FoodCard_id &&
                    p.User_id == originalRecharge.OperateFor);

                var userAccount = db.UserAccounts.Where(p =>
                    p.FoodCard_id == foodcardrecharge.FoodCard_id &&
                    p.User_id == foodcardrecharge.OperateFor);

                if (originUserAccount.Count() == 0)
                {
                    var useraccount = new UserAccount()
                    {
                        User_id = originalRecharge.OperateFor,
                        FoodCard_id = originalRecharge.FoodCard_id,
                        Balance = -originalRecharge.Amount
                    };

                    db.UserAccounts.Add(useraccount);
                }

                if (userAccount.Count() == 0)
                {
                    var useraccount = new UserAccount()
                    {
                        User_id = foodcardrecharge.OperateFor,
                        FoodCard_id = foodcardrecharge.FoodCard_id,
                        Balance = -foodcardrecharge.Amount
                    };

                    db.UserAccounts.Add(useraccount);
                }

                var currentUserAccount = originUserAccount.First();
                var targetUserAccount = userAccount.First();

                // switch to another card
                if (originalRecharge.FoodCard_id != foodcardrecharge.FoodCard_id)
                {
                    originFoodcard.CurrentBalance -= originalRecharge.Amount;

                    var newFoodcard = db.FoodCards.Find(foodcardrecharge.FoodCard_id);
                    newFoodcard.CurrentBalance += foodcardrecharge.Amount;
                    currentUserAccount.Balance -= originalRecharge.Amount;
                    targetUserAccount.Balance += foodcardrecharge.Amount;

                    db.Entry(currentUserAccount).State = EntityState.Modified;
                    db.Entry(targetUserAccount).State = EntityState.Modified;
                    db.Entry(originFoodcard).State = EntityState.Modified;
                    db.Entry(newFoodcard).State = EntityState.Modified;
                }
                else
                {
                    // same card, different amount
                    if (originalRecharge.Amount != foodcardrecharge.Amount)
                    {
                        originFoodcard.CurrentBalance -= originalRecharge.Amount;
                        originFoodcard.CurrentBalance += foodcardrecharge.Amount;
                        db.Entry(originFoodcard).State = EntityState.Modified;

                        currentUserAccount.Balance -= originalRecharge.Amount;
                        currentUserAccount.Balance += foodcardrecharge.Amount;
                        db.Entry(rechargeFor).State = EntityState.Modified;
                    }
                }

                originalRecharge.Amount = foodcardrecharge.Amount;
                originalRecharge.OperateFor = foodcardrecharge.OperateFor;
                originalRecharge.Operator = foodcardrecharge.Operator;
                originalRecharge.FoodCard_id = foodcardrecharge.FoodCard_id;

                db.Entry(originalRecharge).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.OperateFor = new SelectList(db.Users, "User_id", "UserName", foodcardrecharge.OperateFor);
            ViewBag.Operator = new SelectList(db.Users, "User_id", "UserName", foodcardrecharge.Operator);
            ViewBag.FoodCard_id = new SelectList(db.FoodCards, "FoodCard_id", "FoodCardName", foodcardrecharge.FoodCard_id);

            return View(foodcardrecharge);
        }