/// <summary> /// 添加, 这里可以人工干预,提高程序的性能 /// </summary> /// <param name="entity">实体</param> /// <param name="identity">自增量方式,表主键是否采用自增的策略</param> /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param> /// <returns>主键</returns> public string Add(BaseUploadLogEntity entity, bool identity = true, bool returnId = true) { Identity = identity; ReturnId = returnId; entity.Id = AddEntity(entity).ToInt(); return(entity.Id.ToString()); }
/// <summary> /// 设置实体 /// </summary> /// <param name="sqlBuilder">Sql语句生成器</param> /// <param name="entity">实体</param> private void SetEntity(SqlBuilder sqlBuilder, BaseUploadLogEntity entity) { SetEntityExtend(sqlBuilder, entity); sqlBuilder.SetValue(BaseUploadLogEntity.FieldSystemCode, entity.SystemCode); sqlBuilder.SetValue(BaseUploadLogEntity.FieldFileName, entity.FileName); sqlBuilder.SetValue(BaseUploadLogEntity.FieldFileExtension, entity.FileExtension); sqlBuilder.SetValue(BaseUploadLogEntity.FieldFilePath, entity.FilePath); sqlBuilder.SetValue(BaseUploadLogEntity.FieldFileSize, entity.FileSize); sqlBuilder.SetValue(BaseUploadLogEntity.FieldRemark, entity.Remark); sqlBuilder.SetValue(BaseUploadLogEntity.FieldSortCode, entity.SortCode); sqlBuilder.SetValue(BaseUploadLogEntity.FieldDeleted, entity.Deleted); sqlBuilder.SetValue(BaseUploadLogEntity.FieldEnabled, entity.Enabled); }
/// <summary> /// 添加或更新(主键是否为0) /// </summary> /// <param name="entity">实体</param> /// <param name="identity">自增量方式,表主键是否采用自增的策略</param> /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param> /// <returns>主键</returns> public string AddOrUpdate(BaseUploadLogEntity entity, bool identity = true, bool returnId = true) { Identity = identity; ReturnId = returnId; if (entity.Id == 0) { entity.Id = AddEntity(entity).ToInt(); return(entity.Id.ToString()); } else { return(UpdateEntity(entity) > 0 ? entity.Id.ToString() : string.Empty); } }
/// <summary> /// 更新实体 /// </summary> /// <param name="entity">实体</param> public int UpdateEntity(BaseUploadLogEntity entity) { var sqlBuilder = new SqlBuilder(DbHelper); sqlBuilder.BeginUpdate(CurrentTableName); SetEntity(sqlBuilder, entity); if (UserInfo != null) { sqlBuilder.SetValue(BaseUploadLogEntity.FieldUpdateUserId, UserInfo.UserId); sqlBuilder.SetValue(BaseUploadLogEntity.FieldUpdateUserName, UserInfo.UserName); sqlBuilder.SetValue(BaseUploadLogEntity.FieldUpdateBy, UserInfo.RealName); } sqlBuilder.SetDbNow(BaseUploadLogEntity.FieldUpdateTime); sqlBuilder.SetValue(BaseUploadLogEntity.FieldUpdateIp, Utils.GetIp()); sqlBuilder.SetWhere(PrimaryKey, entity.Id); //return sqlBuilder.EndUpdate(); var result = sqlBuilder.EndUpdate(); if (result > 0) { RemoveCache(entity.Id); } return(result); }
// 这个是声明扩展方法 partial void SetEntityExtend(SqlBuilder sqlBuilder, BaseUploadLogEntity entity);
/// <summary> /// 添加实体 /// </summary> /// <param name="entity">实体</param> public string AddEntity(BaseUploadLogEntity entity) { var key = string.Empty; if (entity.SortCode == 0) { var managerSequence = new BaseSequenceManager(DbHelper, Identity); if (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2) { key = managerSequence.Increment($"SC_{CurrentTableName}_SEQ"); } else { key = managerSequence.Increment(CurrentTableName); } entity.SortCode = key.ToInt(); } var sqlBuilder = new SqlBuilder(DbHelper, Identity, ReturnId); sqlBuilder.BeginInsert(CurrentTableName, PrimaryKey); if (!Identity) { // 这里已经是指定了主键了,所以不需要返回主键了 sqlBuilder.ReturnId = false; sqlBuilder.SetValue(PrimaryKey, entity.Id); } else { if (!ReturnId && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2)) { if (DbHelper.CurrentDbType == CurrentDbType.Oracle) { sqlBuilder.SetFormula(PrimaryKey, $"{CurrentTableName}_SEQ.NEXTVAL"); } if (DbHelper.CurrentDbType == CurrentDbType.Db2) { sqlBuilder.SetFormula(PrimaryKey, $"NEXT VALUE FOR {CurrentTableName}_SEQ"); } } else { if (Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2)) { var managerSequence = new BaseSequenceManager(DbHelper); entity.Id = managerSequence.Increment($"{CurrentTableName}_SEQ").ToInt(); sqlBuilder.SetValue(PrimaryKey, entity.Id); } } } SetEntity(sqlBuilder, entity); if (UserInfo != null) { sqlBuilder.SetValue(BaseUploadLogEntity.FieldUserCompanyId, UserInfo.CompanyId); sqlBuilder.SetValue(BaseUploadLogEntity.FieldUserSubCompanyId, UserInfo.SubCompanyId); sqlBuilder.SetValue(BaseUploadLogEntity.FieldCreateUserId, UserInfo.UserId); sqlBuilder.SetValue(BaseUploadLogEntity.FieldCreateUserName, UserInfo.UserName); sqlBuilder.SetValue(BaseUploadLogEntity.FieldCreateBy, UserInfo.RealName); } else { sqlBuilder.SetValue(BaseUploadLogEntity.FieldCreateBy, entity.CreateBy); sqlBuilder.SetValue(BaseUploadLogEntity.FieldCreateUserName, entity.CreateUserName); } sqlBuilder.SetDbNow(BaseUploadLogEntity.FieldCreateTime); sqlBuilder.SetValue(BaseUploadLogEntity.FieldCreateIp, Utils.GetIp()); if (UserInfo != null) { sqlBuilder.SetValue(BaseUploadLogEntity.FieldUpdateUserId, UserInfo.UserId); sqlBuilder.SetValue(BaseUploadLogEntity.FieldUpdateUserName, UserInfo.UserName); sqlBuilder.SetValue(BaseUploadLogEntity.FieldUpdateBy, UserInfo.RealName); } sqlBuilder.SetDbNow(BaseUploadLogEntity.FieldUpdateTime); sqlBuilder.SetValue(BaseUploadLogEntity.FieldUpdateIp, Utils.GetIp()); if (Identity && (DbHelper.CurrentDbType == CurrentDbType.SqlServer || DbHelper.CurrentDbType == CurrentDbType.Access)) { key = sqlBuilder.EndInsert().ToString(); } else { sqlBuilder.EndInsert(); } if (Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.Db2)) { key = entity.Id.ToString(); } if (!string.IsNullOrWhiteSpace(key)) { RemoveCache(); } return(key); }
/// <summary> /// 更新 /// </summary> /// <param name="entity">实体</param> public int Update(BaseUploadLogEntity entity) { return(UpdateEntity(entity)); }