/// <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="helper"></param> /// <param name="item"></param> /// <param name="error"></param> /// <returns></returns> public bool SubmitTransaction(DBExtend helper, ITransaction item, out string error) { //if (item.TradeType is System.Enum) //{ // item.TradeType = (int)item.TradeType; //} error = ""; var account = AccountBusiness <TType> .Instance.GetAccountFromCache(item.AccountId); item.TransactionType = account.TransactionType; item.Amount = Math.Abs(item.Amount); if (item.OperateType == OperateType.支出) { item.Amount = 0 - item.Amount; } if (string.IsNullOrEmpty(item.TransactionNo)) { item.TransactionNo = GetSerialNumber(1, item.TradeType, (int)item.OperateType); } int transactionId = 0; try { //检测余额 if (item.OperateType == OperateType.支出 && item.CheckBalance) { string sql1 = "select CurrentBalance-LockedAmount from $IAccountDetail with (nolock) where id=@AccountId"; //sql1 = FormatTable(sql1); helper.AddParam("AccountId", item.AccountId); var balance = helper.AutoExecuteScalar <decimal>(sql1, typeof(IAccountDetail)); //var balance = helper.ExecScalar<decimal>(sql1, typeof(IAccountDetail)); if (balance + item.Amount < 0) { error = "对应帐户余额不足"; return(false); } } transactionId = helper.InsertFromObj(item); string sql = @" update $ITransaction set CurrentBalance=b.CurrentBalance+Amount,LastBalance=b.CurrentBalance from $IAccountDetail b where b.id=@AccountId and $ITransaction.id=@id update $IAccountDetail set CurrentBalance=CurrentBalance+@amount where id=@AccountId"; //helper.Clear(); helper.AddParam("id", item.Id); helper.AddParam("amount", item.Amount); helper.AddParam("AccountId", item.AccountId); //sql = FormatTable(sql); helper.AutoSpUpdate(sql, typeof(ITransaction), typeof(IAccountDetail)); //helper.Execute(sql, typeof(ITransaction), typeof(IAccountDetail)); } catch (Exception ero) { error = ero.Message; CoreHelper.EventLog.Log("SubmitTransaction 发生错误" + ero, true); return(false); } 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> public static bool UnlockAmount(DBExtend helper, int lockedId, out string message) { //helper.Clear(); message = ""; if (lockedId <= 0) { message = "参数值lockedId不能为0"; return(false); } string key = string.Format("UnlockAmount_{0}", lockedId); if (!CoreHelper.ConcurrentControl.Check(key)) { message = "同时提交了多次相同的参数" + key; return(false); } string sql = "update $IAccountDetail set LockedAmount=LockedAmount-b.Amount from $ILockRecord b where $IAccountDetail.id=b.AccountId and b.id=@id\r\n"; sql += "delete from $ILockRecord where id=@id"; //sql = FormatTable(sql); helper.AddParam("id", lockedId); int count; try { count = helper.Execute(sql, typeof(IAccountDetail), typeof(ILockRecord)); if (count == 0) { message = "找不到锁定的记录"; } } catch (Exception ero) { CoreHelper.ConcurrentControl.Remove(key); CoreHelper.EventLog.Log("UnlockAmount 执行出错" + ero, true); message = ero.Message; return(false); } bool ok = count > 0; if (!ok) { CoreHelper.ConcurrentControl.Remove(key); } return(ok); }
/// <summary> /// 解锁金额,没有事务 /// </summary> public bool UnlockAmount(DBExtend helper, int lockedId, out string message) { //helper.Clear(); var lockRecord = helper.QueryItem <ILockRecord>(b => b.Id == lockedId); message = ""; if (lockRecord == null) { message = "找不到锁ID:" + lockedId; return(false); } if (lockRecord.Checked) { message = "该锁已经解过ID:" + lockedId; return(false); } string key = string.Format("UnlockAmount_{0}", lockedId); if (!CoreHelper.ConcurrentControl.Check(key)) { message = "同时提交了多次相同的参数" + key; return(false); } string sql = "update $IAccountDetail set LockedAmount=LockedAmount-b.Amount from $ILockRecord b where $IAccountDetail.id=b.AccountId and b.id=@id "; //sql += "update $ILockRecord set checked=1 where id=@id"; sql += "delete from $ILockRecord where id=@id"; //sql = FormatTable(sql); helper.AddParam("id", lockedId); int count; try { count = helper.Execute(sql, typeof(IAccountDetail), typeof(ILockRecord)); } catch (Exception ero) { CoreHelper.ConcurrentControl.Remove(key); CoreHelper.EventLog.Log("UnlockAmount 执行出错" + ero, true); message = ero.Message; return(false); } CoreHelper.ConcurrentControl.Remove(key); return(true); }