• EF 事物Transaction简单操作


            /// <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; //提交失败
                        }
                    }
                }
            }
    

      

  • 相关阅读:
    UML描述
    Tomcat优化
    Tomcat源码
    Tomcat架构
    搭建K8s集群[无需科学shangwang]
    minikube安装
    K8S核心组件和架构图
    Docker数据持久化
    Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式
    cookie与session区别?
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/5531105.html
Copyright © 2020-2023  润新知