/// <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> /// <typeparam name="TMain"></typeparam> /// <returns></returns> public static bool SubmitOrder <TMain>(TMain order) where TMain : IOrder, new() { DBExtend helper = dbHelper; int id = helper.InsertFromObj(order); order.Id = id; 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="dic"></param> /// <returns></returns> public static int Add(IDicConfig dic) { if (allCache.Find(b => b.Name == dic.Name && b.DicType == dic.DicType) != null) { return(0); } DBExtend helper = dbHelper; int id = helper.InsertFromObj(dic); dic.Id = id; allCache.Add(dic); return(id); }
/// <summary> /// 指定父级,添加分类 /// 如果父级为空,则为第一级 /// </summary> /// <param name="parentSequenceCode"></param> /// <param name="category"></param> /// <returns></returns> public TModel Add(string parentSequenceCode, TModel category) { DBExtend helper = dbHelper; string newCode = MakeNewCode(parentSequenceCode, category); //helper.Clear(); category.SequenceCode = newCode; category.ParentCode = parentSequenceCode; int id = helper.InsertFromObj(category); category.Id = id; //ClearCache(); return(category); }
/// <summary> /// 检测是否存在,并添加 /// </summary> /// <typeparam name="TItem"></typeparam> /// <param name="p"></param> /// <returns></returns> public static new int Add(IFreight p) { DBExtend helper = dbHelper; IFreight item = helper.QueryItem <IFreight>(b => b.AreaId == p.AreaId && b.DeliverType == p.DeliverType); if (item != null) { return(0); } //helper.Clear(); int id = helper.InsertFromObj(p); return(id); }
/// <summary> /// 添加名称的值 /// </summary> /// <param name="values"></param> /// <param name="propertyId"></param> public void AddPropertyValue(List <string> values, int propertyId) { DBExtend helper = dbHelper; foreach (string s in values) { PropertyValue v = new PropertyValue(); v.PropertyId = propertyId; v.Name = s.Trim(); //helper.Params.Clear(); int id = helper.InsertFromObj(v); v.Id = id; valueCache.Add(id, v); } }
/// <summary> /// 指定父级,添加分类 /// 如果父级为空,则为第一级 /// </summary> /// <param name="parentSequenceCode"></param> /// <param name="category"></param> /// <returns></returns> public static T Add <T>(string parentSequenceCode, T category) where T : ICategory, new() { DBExtend helper = dbHelper; InItCache <T>(); string newCode = MakeNewCode <T>(parentSequenceCode, category); //helper.Clear(); category.SequenceCode = newCode; category.ParentCode = parentSequenceCode; int id = helper.InsertFromObj(category); category.Id = id; typeCache.Remove(typeof(T)); return(category); }
public bool Create(DataBase db, Table table, out string error) { error = ""; table.DataBaseName = db.Name; if (table.IsMainTable) { table.MaxPartDataTotal = db.MaxMainDataTotal; } table.TablePartTotal = 1; var item = QueryItem(b => b.DataBaseName == table.DataBaseName && b.TableName == table.TableName); if (item != null) { error = "有重复的表" + table.TableName; return(false); } Add(table); //生成分表 var part = new TablePart(); part.DataBaseName = table.DataBaseName; part.TableName = table.TableName; if (table.IsMainTable) { part.MainDataStartIndex = db.MainDataStartIndex; part.MainDataEndIndex = db.MainDataEndIndex; } else { part.MainDataStartIndex = db.MainDataStartIndex; part.MainDataEndIndex = db.MainDataStartIndex + table.MaxPartDataTotal - 1; } part.PartName = table.TableName; DBExtend.InsertFromObj(part); return(true); }