/// <summary>
/// 申请提现
/// </summary>
/// <param name="userId">用户id</param>
/// <param name="amount">提现金额</param>
/// <returns></returns>
public int ApplyTakeCash(int userId, decimal amount)
{
if(IsExistUser(userId)) return -3; //用户不存在
using (var context = new MediaDBContext())
{
var channelUser = context.ChannelUsers.FirstOrDefault(c => c.UserId == userId);
if (channelUser == null) return -3;//用户不存在
var purse = context.BackPurses.FirstOrDefault(c => c.OwnerId == channelUser.LeaderId);
if (purse == null) return -2; //未绑定银行卡
if (purse.Balance < amount) return -1; //余额不足
using (var trans = context.Database.BeginTransaction())
{
string description = string.Format("用户【{0}】申请提现【{1}】元金额", channelUser.User.Mobile, amount);
try
{
var model = new BackWithdrawCash
{
Amount = amount,
ApplicantId = 0,
PurseId = purse.Id,
Remark = description
};
//添加提现申请记录
context.BackWithdrawCashs.Add(model);
//减少提现额度
purse.Balance = purse.Balance - amount;
//提交
int res = context.SaveChanges();
if (res > 0)
{
//添加日志
AddOperateRecord(userId, channelUser.LeaderId, description+"成功");
//提交事物
trans.Commit();
return 1; //提交成功
}
//添加日志
AddOperateRecord(userId, channelUser.LeaderId, description + "失败");
return 0;//提交失败
}
catch (Exception ex)
{
Logger.Error("【TradingServices[ApplyTakeCash]】", ex);
//添加日志
AddOperateRecord(userId, channelUser.LeaderId, string.Format("【{0}】【{1}】",description,ex.Message));
//事物回滚
trans.Rollback();
return 0; //提交失败
}
}
}
}