/// <summary> /// 解锁锁定金额并确认流水 /// </summary> /// <param name="item"></param> /// <param name="lockId"></param> /// <param name="error"></param> /// <returns></returns> public bool SubmitTransactionAndUnlock(ITransaction item, int lockId, out string error) { error = ""; if (lockId <= 0) { throw new Exception("lockId值不符合"); } DBExtend helper = dbHelper; helper.BeginTran(); bool a = UnlockAmount(helper, lockId, out error); if (!a) { helper.RollbackTran(); return(false); } bool b = SubmitTransaction(helper, item, out error); if (!b) { helper.RollbackTran(); return(false); } helper.CommitTran(); return(true); }
/// <summary> /// 锁定一定金额 /// </summary> public bool LockAmount(ILockRecord record, out int id, out string message) { message = ""; if (record.Amount <= 0) { id = 0; message = "amount格式不正确"; return(false); } string key = string.Format("LockAmount_{0}_{1}_{2}_{3}", record.AccountId, 0, record.Remark, 0); if (!CoreHelper.ConcurrentControl.Check(key, 3)) { throw new Exception("同时提交了多次相同的参数" + key); } DBExtend helper = dbHelper; string sql = "update $IAccountDetail set LockedAmount=LockedAmount+@LockedAmount where id=@AccountId and CurrentBalance-(abs(LockedAmount)+@LockedAmount)>=0"; //sql = FormatTable(sql); helper.AddParam("LockedAmount", Math.Abs(record.Amount)); helper.AddParam("AccountId", record.AccountId); helper.BeginTran(); try { int n = helper.Execute(sql, typeof(IAccountDetail)); if (n == 0) { message = "余额不足"; id = 0; helper.RollbackTran(); return(false); } //helper.Clear(); id = helper.InsertFromObj(record); helper.CommitTran(); } catch (Exception ero) { message = "提交事物时发生错误:" + ero.Message; helper.RollbackTran(); CoreHelper.ConcurrentControl.Remove(key); CoreHelper.EventLog.Log("LockAmount 执行出错" + ero, true); throw ero; } bool ok = id > 0; if (!ok) { CoreHelper.ConcurrentControl.Remove(key); } return(ok); }
/// <summary> /// 确认流水,并更改库存 /// </summary> /// <param name="batchNo"></param> /// <param name="operateType"></param> public bool ConfirmSubmit <TStock>(string batchNo, StockOperateType operateType) where TStock : Style, new() { DBExtend helper = dbHelper; helper.BeginTran(); string op = operateType == StockOperateType.出 ? "-" : "+"; string sql = "update $Style set Num=$Style.num" + op + "b.num from $IStockRecord b where $Style.id=b.styleId and b.Handled=0 and b.batchNo=@batchNo"; sql += @" update $IStockRecord set Handled=1,OperateType=@OperateType,UpdateTime=getdate(),$IStockRecord.num=0" + op + "$IStockRecord.num where batchNo=@batchNo"; //sql = AutoFormat(sql, typeof(TStock), typeof(TRecord)); helper.AddParam("batchNo", batchNo); helper.AddParam("OperateType", (int)operateType); try { helper.Execute(sql, typeof(TStock), typeof(TModel)); } catch (Exception ero) { helper.RollbackTran(); return(false); } helper.CommitTran(); return(true); }
/// <summary> /// 解锁 /// </summary> /// <param name="id"></param> /// <returns></returns> public bool UnlockAmount(int id, out string message) { DBExtend helper = dbHelper; helper.BeginTran(); bool a = UnlockAmount(helper, id, out message); if (!a) { helper.RollbackTran(); return(false); } helper.CommitTran(); return(a); }
/// <summary> /// 提交流水 /// </summary> /// <param name="error"></param> /// <param name="items">多个流水,请根据实际情况处理</param> /// <returns></returns> public bool SubmitTransaction(out string error, params ITransaction[] items) { error = ""; if (items.Length == 0) { error = "流水为空 items"; return(false); } DBExtend helper = dbHelper; foreach (var item in items) { if (item.Amount == 0) { error = "金额不能为0"; return(false); } if (string.IsNullOrEmpty(item.Remark)) { error = "备注必须填写"; return(false); } if (string.IsNullOrEmpty(item.OutOrderId)) { error = "外部订单号必须填写"; //return false; } //if (CheckTransactionSubmited(helper, item)) //{ // error = "该流水已经提交过" + item.ToString(); // return false; //} } helper.BeginTran(); foreach (var item in items) { bool a = SubmitTransaction(helper, item, out error); if (!a) { helper.RollbackTran(); return(false); } } helper.CommitTran(); return(true); }