/// <summary> /// 保存借还信息 /// </summary> /// <param name="info">借还信息</param> /// <returns>主键ID</returns> public async Task <ResultInfo <int> > SaveBorrowRepayAsync(BorrowRepayInfo info) { //对方名称不能为空 if (string.IsNullOrWhiteSpace(info.Target)) { return(new ResultInfo <int>(false, this.Res.IO.BRTargetEmpty, -1)); } //判断借还类型是否存在 if (Enum.IsDefined(typeof(EnmBorrowRepayType), info.BRType) == false) { return(new ResultInfo <int>(false, this.Res.Bas.BorrowRepayTypeUnexist, -1)); } //判断账户是否存在 var modelAmountAccount = await BasicDAL.Inst.GetAmountAccountAsync(this.LoginInfo.FamilyID, info.AmountAccountID); if (modelAmountAccount == null) { return(new ResultInfo <int>(false, this.Res.Bas.AmountAccountUnexist, -1)); } //转成model var model = new BorrowRepay(); model.ID = info.ID; model.FamilyID = this.LoginInfo.FamilyID; model.BRDate = info.BRDate; model.Target = info.Target; model.BRType = info.BRType; model.AmountAccountID = info.AmountAccountID; model.Amount = info.Amount; model.Remark = info.Remark; //设置创建者/更新者字段值 this.SetCreateUpdateFields(model); //保存到数据库 var ret = await CacheLock.DoByLockFamilyAsync(this.LoginInfo.FamilyID, () => InOutDAL.Inst.SaveBorrowRepayAsync(model)); if (!ret.IsDone) { return(new ResultInfo <int>(false, Res.Gen.WaiteTimeOut, model.ID)); } return(new ResultInfo <int>(true, Res.Gen.OK, model.ID)); }
/// <summary> /// 保存借还信息 /// </summary> /// <param name="model">借还信息</param> /// <returns>主键ID</returns> public async Task <int> SaveBorrowRepayAsync(BorrowRepay model) { using (IOSysContext db = new IOSysContext()) { using (var trans = db.Database.BeginTransaction()) { BorrowRepay oldModel = null; //尝试查找原数据 if (model.ID > 0) { oldModel = db.BorrowRepays.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.BorrowRepays.AddAsync(model); //账户金额变动值 var amount = this.GetAmountDirectionForBorrowRepay(model.BRType, model.Amount); //修改账户信息 await BasicDAL.Inst.UseAmountAccount(model.FamilyID, model.AmountAccountID, 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; //账户金额变动值 var newAmount = this.GetAmountDirectionForBorrowRepay(model.BRType, model.Amount); var oldAmount = this.GetAmountDirectionForBorrowRepay(oldModel.BRType, oldModel.Amount); //修改账户信息 await BasicDAL.Inst.ChangeAmountAccount(model.FamilyID, model.AmountAccountID, newAmount, oldModel.AmountAccountID, oldAmount, db); } //保存修改 await db.SaveChangesAsync(); //提交事务 trans.Commit(); } } //更新最小交易流水日期缓存 this.UpdateMinTurnoverDate(model.FamilyID, model.BRDate); return(model.ID); }