/// <summary> /// 借贷转账 /// </summary> /// <param name="fromUid"></param> /// <param name="toUid"></param> /// <param name="amount"></param> /// <param name="remark"></param> /// <returns></returns> public void Transfer(int fromUid, int toUid, double amount, string remark, ref IDictionary <string, DbParameter[]> dictionary) { // 1.封装参数 PayParam payParam = new PayParam() { Amount = amount, Currency = 0, FromUid = fromUid, ToUid = toUid, Remark = remark == null ? "转账" : remark }; // 2.检查甲方钱包 var cause = string.Empty; if (!CheckFinance(fromUid, amount, ref cause)) { throw new MsgException(cause); } // 3.生成流水账 Pays payAccounts = RechargeUtil.GetPayAccounts(fromUid, toUid, payParam); paysRepository.InsertAccounts(payAccounts, ref dictionary); // 4.生成往来账 var currentAccounts = GetCurrentAccounts(payParam); accountsRepository.BatchInsertAccounts(currentAccounts, ref dictionary); // 5.扣减双方钱包余额 var users = GetUsers(fromUid, toUid); if (fromUid == toUid) { throw new MsgException("您不能购买自己发布的商品"); } if (users == null || users.Count < 2) { throw new MsgException("加载用户信息超时"); } var owner = users.First(item => item.Uid == fromUid); var customer = users.First(item => item.Uid == toUid); walletRepository.OutAccounts(owner.Uid, amount, owner.Version, ref dictionary); walletRepository.PutAccounts(customer.Uid, amount, customer.Version, ref dictionary); }
/// <summary> /// 人工充值转账 /// </summary> /// <param name="uid"></param> /// <param name="amount"></param> /// <returns></returns> public bool Recharge(int uid, double amount) { // 1.封装参数 IDictionary <string, DbParameter[]> dictionary = new Dictionary <string, DbParameter[]>(); PayParam payParam = RechargeUtil.GetParam(uid, amount); // 2.检查甲方钱包 var cause = string.Empty; if (!CheckFinance(Constants.HotAccountID, amount, ref cause)) { throw new MsgException(cause); } // 3.生成流水账 Pays payAccounts = RechargeUtil.GetPayAccounts(Constants.HotAccountID, uid, payParam); paysRepository.InsertAccounts(payAccounts, ref dictionary); // 4.扣减双方钱包余额 var users = GetUsers(Constants.HotAccountID, uid); if (users == null || users.Count < 2) { throw new MsgException("加载用户信息超时"); } var owner = users[0]; var customer = users[1]; walletRepository.OutAccounts(owner.Uid, amount, owner.Version, ref dictionary); walletRepository.PutAccounts(customer.Uid, amount, customer.Version, ref dictionary); // 5.生成往来账 var currentAccounts = GetCurrentAccounts(payParam); accountsRepository.BatchInsertAccounts(currentAccounts, ref dictionary); return(paysRepository.CommitTransactionLock(dictionary)); }
/// <summary> /// 直接充值 /// </summary> /// <param name="uid"></param> /// <param name="amount"></param> /// <returns></returns> public bool DirectRecharge(int uid, double amount) { // 1.生成参数 IDictionary <string, DbParameter[]> dictionary = new Dictionary <string, DbParameter[]>(); PayParam payParam = RechargeUtil.GetDirectParam(uid, amount); // 2.生成流水账 Pays payAccounts = RechargeUtil.GetPayAccounts(Constants.HotAccountID, uid, payParam); paysRepository.InsertAccounts(payAccounts, ref dictionary); // 3.生成往来账 var list = new List <Accounts>() { new Accounts() { AccountsId = IdWorkTool.Instance().GetId(), PayId = payParam.SystemRecordId, TradeAccountId = payParam.ToUid, TradeAccountName = payParam.ToUsername, AccountsType = 1, Amount = payParam.Amount, Remark = payParam.Remark, Currency = payParam.Currency, AddTime = DateTime.Now } }; accountsRepository.BatchInsertAccounts(list, ref dictionary); // 4.目标账户进账 var user = userRepository.SelectFinanceDetail(uid); walletRepository.PutAccounts(user.Uid, amount, user.Version, ref dictionary); return(walletRepository.CommitTransactionLock(dictionary)); }