refactor: 重构服务

This commit is contained in:
zhangpeihang 2022-04-29 11:23:10 +08:00
parent 831bf2a7f6
commit 608ca9c429
3 changed files with 29 additions and 29 deletions

View File

@ -9,13 +9,13 @@ namespace BootStarpAdmin.DataAccess.SqlSugar.Service;
/// </summary>
public class AppService : IApp
{
private SqlSugarClient Client;
private ISqlSugarClient Client { get; }
/// <summary>
///
/// </summary>
/// <param name="client"></param>
public AppService(SqlSugarClient client) => Client = client;
public AppService(ISqlSugarClient client) => Client = client;
/// <summary>
///
@ -40,8 +40,8 @@ public class AppService : IApp
{
Client.Ado.BeginTran();
Client.Ado.ExecuteCommand("delete from RoleApp where RoleID = @roleId", new { roleId = roleId });
Client.Insertable(appIds.Select(g => new RoleApp { AppID = g, RoleID = roleId }).ToList());
Client.CommitTran();
Client.Insertable(appIds.Select(g => new RoleApp { AppID = g, RoleID = roleId }).ToList()).ExecuteCommand();
Client.Ado.CommitTran();
ret = true;
}
catch (Exception)

View File

@ -24,7 +24,7 @@ public class DictService : IDict
{
private const string DictServiceCacheKey = "DictService-GetAll";
private SqlSugarClient Client { get; }
private ISqlSugarClient Client { get; }
private string AppId { get; set; }
@ -33,7 +33,7 @@ public class DictService : IDict
/// </summary>
/// <param name="client"></param>
/// <param name="configuration"></param>
public DictService(SqlSugarClient client, IConfiguration configuration)
public DictService(ISqlSugarClient client, IConfiguration configuration)
{
Client = client;
AppId = configuration.GetValue("AppId", "BA");
@ -72,7 +72,7 @@ public class DictService : IDict
bool ret;
try
{
Client.UseTran();
Client.Ado.BeginTran();
Client.Ado.ExecuteCommand("delete Dicts where Category=@Category and Name=@Name and Define=@Define", new { Category = "应用首页", Name = appId, Define = EnumDictDefine.System });
Client.Ado.ExecuteCommand("delete Dicts where Category=@Category and Code=@Code and Define=@Define", new { Category = "应用程序", Code = appId, Define = EnumDictDefine.System });
Client.Ado.ExecuteCommand("delete Dicts where Category=@Category and Name in (@Names)", new
@ -89,12 +89,12 @@ public class DictService : IDict
"系统通知地址"
}
});
Client.CommitTran();
Client.Ado.CommitTran();
ret = true;
}
catch (Exception)
{
Client.RollbackTran();
Client.Ado.RollbackTran();
throw;
}
return ret;
@ -129,7 +129,7 @@ public class DictService : IDict
///
/// </summary>
/// <returns></returns>
public List<Dict> GetAll() => CacheManager.GetOrAdd(DictServiceCacheKey, entry => Client.Queryable<Dict>().ToList());
public List<Dict> GetAll() => CacheManager.GetOrAdd(DictServiceCacheKey, entry => Client.Queryable<Dict>().AS("Dicts").ToList());
/// <summary>
///
@ -646,7 +646,7 @@ public class DictService : IDict
DeleteClient(client.AppId);
try
{
Client.UseTran();
Client.Ado.BeginTran();
var items = new List<Dict>()
{
new Dict { Category = "应用程序", Name = client.AppName, Code = client.AppId, Define = EnumDictDefine.System },
@ -659,13 +659,13 @@ public class DictService : IDict
new Dict { Category = client.AppId, Name = "系统设置地址", Code = client.SettingsUrl, Define = EnumDictDefine.Customer },
new Dict { Category = client.AppId, Name = "系统通知地址", Code = client.NotificationUrl, Define = EnumDictDefine.Customer }
};
Client.Insertable(items);
Client.CommitTran();
Client.Insertable(items).AS("Dicts").ExecuteCommand();
Client.Ado.CommitTran();
ret = true;
}
catch (DbException)
{
Client.RollbackTran();
Client.Ado.RollbackTran();
throw;
}
}
@ -802,7 +802,7 @@ public class DictService : IDict
private bool SaveDict(Dict dict)
{
var ret = Client.Updateable(dict).Where(s => s.Category == dict.Category && s.Name == dict.Name).UpdateColumns(s => s.Code).ExecuteCommand() > 0;
var ret = Client.Updateable(dict).AS("Dicts").Where(s => s.Category == dict.Category && s.Name == dict.Name).UpdateColumns(s => s.Code).ExecuteCommand() > 0;
if (ret)
{
// 更新缓存

View File

@ -16,13 +16,13 @@ namespace BootStarpAdmin.DataAccess.SqlSugar.Service;
/// </summary>
public class UserService : IUser
{
private SqlSugarClient Client;
private ISqlSugarClient Client;
/// <summary>
///
/// </summary>
/// <param name="client"></param>
public UserService(SqlSugarClient client) => Client = client;
public UserService(ISqlSugarClient client) => Client = client;
/// <summary>
///
@ -215,7 +215,7 @@ public class UserService : IUser
try
{
// 开始事务
Client.UseTran();
Client.Ado.BeginTran();
user = new User()
{
ApprovedBy = "System",
@ -231,12 +231,12 @@ public class UserService : IUser
// 授权 Default 角色
Client.Ado.ExecuteCommand("insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = @userName", new { UserName = userName });
// 结束事务
Client.CommitTran();
Client.Ado.CommitTran();
ret = true;
}
catch (Exception)
{
Client.RollbackTran();
Client.Ado.RollbackTran();
throw;
}
}
@ -267,15 +267,15 @@ public class UserService : IUser
var ret = false;
try
{
Client.BeginTran();
Client.Ado.BeginTran();
Client.Ado.ExecuteCommand("delete from UserGroup where GroupId = @GroupId", new { GroupId = groupId });
Client.Insertable<UserGroup>(userIds.Select(g => new { UserID = g, GroupID = groupId })).ExecuteCommand();
Client.CommitTran();
Client.Ado.CommitTran();
ret = true;
}
catch (Exception)
{
Client.RollbackTran();
Client.Ado.RollbackTran();
throw;
}
if (ret)
@ -297,15 +297,15 @@ public class UserService : IUser
var ret = false;
try
{
Client.UseTran();
Client.Ado.BeginTran();
Client.Ado.ExecuteCommand("delete from UserRole where RoleID = @RoleID", new { RoleID = roleId });
Client.Insertable<UserRole>(userIds.Select(g => new { UserID = g, RoleID = roleId })).ExecuteCommand();
Client.CommitTran();
Client.Ado.CommitTran();
ret = true;
}
catch (Exception)
{
Client.RollbackTran();
Client.Ado.RollbackTran();
throw;
}
if (ret)
@ -334,7 +334,7 @@ public class UserService : IUser
var user = Client.Queryable<User>().First(s => s.UserName == phone);
if (user == null)
{
Client.UseTran();
Client.Ado.BeginTran();
// 插入用户
user = new User()
{
@ -352,7 +352,7 @@ public class UserService : IUser
// Authorization
var roleIds = Client.Ado.SqlQuery<string>("select ID from Roles where RoleName in (@roles)", new { roles = roles });
Client.Insertable<UserRole>(roleIds.Select(g => new { RoleID = g, UserID = user.Id })).ExecuteCommand();
Client.CommitTran();
Client.Ado.CommitTran();
}
else
{
@ -364,7 +364,7 @@ public class UserService : IUser
}
catch (Exception)
{
Client.RollbackTran();
Client.Ado.RollbackTran();
throw;
}
if (ret)