Exemplo n.º 1
0
        /// <summary>
        /// 保存转账信息
        /// </summary>
        /// <param name="info">转账信息</param>
        /// <returns>主键ID</returns>
        public async Task <ResultInfo <int> > SaveTransferAsync(TransferInfo info)
        {
            //判断转出账户是否存在
            var modelFromAmountAccount = await BasicDAL.Inst.GetAmountAccountAsync(this.LoginInfo.FamilyID, info.FromAmountAccountID);

            if (modelFromAmountAccount == null)
            {
                return(new ResultInfo <int>(false, this.Res.Bas.AmountAccountUnexist, -1));
            }

            //判断转入账户是否存在
            var modelToAmountAccount = await BasicDAL.Inst.GetAmountAccountAsync(this.LoginInfo.FamilyID, info.ToAmountAccountID);

            if (modelToAmountAccount == null)
            {
                return(new ResultInfo <int>(false, this.Res.Bas.AmountAccountUnexist, -1));
            }

            //转成model
            var model = new AmountAccountTransfer();

            model.ID                  = info.ID;
            model.FamilyID            = this.LoginInfo.FamilyID;
            model.TransferDate        = info.TransferDate;
            model.FromAmountAccountID = info.FromAmountAccountID;
            model.ToAmountAccountID   = info.ToAmountAccountID;
            model.Amount              = info.Amount;
            model.Remark              = info.Remark;

            //设置创建者/更新者字段值
            this.SetCreateUpdateFields(model);

            //保存到数据库
            var ret = await CacheLock.DoByLockFamilyAsync(this.LoginInfo.FamilyID, () => InOutDAL.Inst.SaveTransferAsync(model));

            if (!ret.IsDone)
            {
                return(new ResultInfo <int>(false, Res.Gen.WaiteTimeOut, model.ID));
            }

            return(new ResultInfo <int>(true, Res.Gen.OK, model.ID));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 保存转账信息
        /// </summary>
        /// <param name="model">转账信息</param>
        /// <returns>主键ID</returns>
        public async Task <int> SaveTransferAsync(AmountAccountTransfer model)
        {
            using (IOSysContext db = new IOSysContext())
            {
                using (var trans = db.Database.BeginTransaction())
                {
                    AmountAccountTransfer oldModel = null;

                    //尝试查找原数据
                    if (model.ID > 0)
                    {
                        oldModel = db.AmountAccountTransfers.AsNoTracking().Where(m => m.ID == model.ID && m.FamilyID == model.FamilyID).FirstOrDefault();

                        if (oldModel == null)
                        {
                            model.CreatorID  = model.UpdateID.Value;
                            model.CreateTime = model.UpdateTime.Value;
                        }
                    }

                    //新增
                    if (oldModel == null)
                    {
                        //新增
                        await db.AmountAccountTransfers.AddAsync(model);

                        //修改账户信息
                        await BasicDAL.Inst.UseAmountAccount(model.FamilyID, model.FromAmountAccountID, -model.Amount, db);

                        await BasicDAL.Inst.UseAmountAccount(model.FamilyID, model.ToAmountAccountID, model.Amount, db);
                    }
                    //更新
                    else
                    {
                        //设置更新者信息
                        model.UpdateID   = model.UpdateID;
                        model.UpdateTime = model.UpdateTime;
                        model.CreatorID  = oldModel.CreatorID;
                        model.CreateTime = oldModel.CreateTime;

                        //更新
                        db.Entry(model).State = EntityState.Modified;

                        //修改账户信息
                        await BasicDAL.Inst.ChangeAmountAccount(model.FamilyID, model.FromAmountAccountID, -model.Amount, oldModel.FromAmountAccountID, -oldModel.Amount, db);

                        await BasicDAL.Inst.ChangeAmountAccount(model.FamilyID, model.ToAmountAccountID, model.Amount, oldModel.ToAmountAccountID, oldModel.Amount, db);
                    }

                    //保存修改
                    await db.SaveChangesAsync();

                    //提交事务
                    trans.Commit();
                }
            }

            //更新最小交易流水日期缓存
            this.UpdateMinTurnoverDate(model.FamilyID, model.TransferDate);

            return(model.ID);
        }