using AutoMapper;
using Model.ModelDto.SysDto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
namespace WMS.BLL.SysServer
{
///
/// 角色服务实践
///
public class RolesServer : IRolesServer
{
///
/// 依赖注入
///
private readonly IMapper _mapper;
private static readonly SqlSugarScope Db = DataContext.Db;
private readonly UserManager _userManager;
///
/// 构造函数
///
/// 角色
/// automapper
public RolesServer(IMapper mapper, UserManager userManager)
{
_mapper = mapper; //automapper
_userManager = userManager;
}
///
/// 获取角色数据列表
///
/// 角色号
/// 角色名称
///
public async Task> GetRoleList(string RoleNo, string RoleName)
{
var modUser = await Db.Queryable().FirstAsync(s => s.Id == _userManager.UserId);
return await Db.Queryable()
.LeftJoin((a, b) => a.CreateUser == b.Id)
.LeftJoin((a, b, c) => a.UpdateUser == c.Id)
//.WhereIF(modUser.UserName.ToUpper() != "ADMIN", a => a.CreateUser == _userManager.UserId)
.WhereIF(!string.IsNullOrEmpty(RoleNo), a => a.RoleNo.Contains(RoleNo))
.WhereIF(!string.IsNullOrEmpty(RoleName), a => a.RoleName.Contains(RoleName))
.Where(a => a.IsDel == "0")
.Select((a, b, c) => new RoleDto()
{
CreateUserName = b.RealName,
UpdateUserName = c.RealName
}, true)
.ToListAsync();
}
///
/// 根据编号获取角色信息列表
///
/// 角色编号
///
public async Task GetRoleByNo(string RoleNo)
{
return await Db.Queryable().CountAsync(s => s.RoleNo == RoleNo && s.IsDel == "0");
}
///
/// 新增角色数据信息
///
/// 角色dto
///
/// 捕获异常
public async Task InsertRoleInfo(RoleDto roledto)
{
//模型映射
SysRoles roles = _mapper.Map(roledto);
//判断角色号是否唯一
int count = await GetRoleByNo(roledto.RoleNo);
if (count > 0)
{
throw Oops.Bah("角色号必须唯一");
}
//新增角色信息
roles.CreateTime = DateTime.Now;
roles.CreateUser = _userManager.UserId;
var i = await Db.Insertable(roles).ExecuteCommandAsync();
if (i <= 0)
throw Oops.Bah("新增角色数据信息失败");
return i;
}
///
/// 删除角色信息(逻辑删除)
///
/// 角色实体模型
///
public async Task DeleteRoleInfo(SysRoles roles)
{
return await Db.Updateable()
.Where(s => s.Id == roles.Id)
.SetColumns(s => s.IsDel == "1")
.SetColumns(s => s.UpdateTime == DateTime.Now)
.SetColumns(s => s.UpdateUser == _userManager.UserId)
.ExecuteCommandAsync();
}
///
/// 编辑角色信息
///
/// 角色实体模型
///
public async Task UpdateRoleInfo(SysRoles role)
{
int count = await Db.Queryable().CountAsync(s => s.RoleNo == role.RoleNo && s.Id != role.Id && s.IsDel == "0");
if (count > 0)
throw Oops.Bah("角色号必须唯一");
role.UpdateTime = DateTime.Now;
role.UpdateUser = _userManager.UserId;
int i = await Db.Updateable(role)
.UpdateColumns(s => new { s.RoleNo, s.RoleName, s.Demo, s.UpdateUser, s.UpdateTime })
.ExecuteCommandAsync();
if (i <= 0)
throw Oops.Bah("修改角色数据信息失败");
return i;
}
///
/// 根据id获取角色信息
///
/// 角色id
///
public async Task GetRoleInfoById(int roleids)
{
return await Db.Queryable().FirstAsync(s => s.Id == roleids && s.IsDel == "0");
}
}
}