using JiepeiWMS.Common.Config;
|
using JiepeiWMS.Common.Helper;
|
using JiepeiWMS.IRepository.Base;
|
using JiepeiWMS.IRepository.UnitOfWork;
|
using JiepeiWMS.IServices;
|
using JiepeiWMS.Model.Models;
|
using JiepeiWMS.Services.BASE;
|
using System;
|
using System.Linq;
|
using System.Threading.Tasks;
|
|
namespace JiepeiWMS.FrameWork.Services
|
{
|
/// <summary>
|
/// sysUserInfoServices
|
/// </summary>
|
public class SysUserInfoServices : BaseServices<sysUserInfo>, ISysUserInfoServices
|
{
|
|
IBaseRepository<sysUserInfo> _dal;
|
IUserRoleServices _userRoleServices;
|
IBaseRepository<Role> _roleRepository;
|
IDepartmentServices _departmentServices;
|
IRoleModulePermissionServices _roleModulePermissionServices;
|
IUnitOfWork _DB;
|
public SysUserInfoServices(IBaseRepository<sysUserInfo> dal, IUserRoleServices userRoleServices, IBaseRepository<Role> roleRepository, IDepartmentServices DepartmentServices, IRoleModulePermissionServices RoleModulePermissionServices, IUnitOfWork DB)
|
{
|
this._dal = dal;
|
this._userRoleServices = userRoleServices;
|
this._roleRepository = roleRepository;
|
this._departmentServices = DepartmentServices;
|
this._roleModulePermissionServices = RoleModulePermissionServices;
|
this._DB = DB;
|
base.BaseDal = dal;
|
}
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="loginName"></param>
|
/// <param name="loginPwd"></param>
|
/// <returns></returns>
|
public async Task<sysUserInfo> SaveUserInfo(string loginName, string loginPwd)
|
{
|
sysUserInfo sysUserInfo = new sysUserInfo(loginName, loginPwd);
|
sysUserInfo model = new sysUserInfo();
|
var userList = await base.Query(a => a.uLoginName == sysUserInfo.uLoginName && a.uLoginPWD == sysUserInfo.uLoginPWD);
|
if (userList.Count > 0)
|
{
|
model = userList.FirstOrDefault();
|
}
|
else
|
{
|
var id = await base.Add(sysUserInfo);
|
model = await base.QueryById(id);
|
}
|
|
return model;
|
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="loginName"></param>
|
/// <param name="loginPwd"></param>
|
/// <returns></returns>
|
public async Task<string> GetUserRoleNameStr(string loginName, string loginPwd)
|
{
|
string roleName = "";
|
var user = (await base.Query(a => a.uLoginName == loginName && a.uLoginPWD == loginPwd)).FirstOrDefault();
|
//判断如果查询的用户名为空就用工号登录
|
if (user == null)
|
{
|
user = (await base.Query(a => a.JobNo == loginName && a.uLoginPWD == loginPwd)).FirstOrDefault();
|
}
|
var roleList = await _roleRepository.Query(a => a.IsDeleted == false);
|
if (user != null)
|
{
|
var userRoles = await _userRoleServices.Query(ur => ur.UserId == user.uID);
|
if (userRoles.Count > 0)
|
{
|
var arr = userRoles.Select(ur => ur.RoleId.ObjToString()).ToList();
|
var roles = roleList.Where(d => arr.Contains(d.Id.ObjToString()));
|
|
roleName = string.Join(',', roles.Select(r => r.Name).ToArray());
|
}
|
}
|
return roleName;
|
}
|
|
/// <summary>
|
/// 用户是否在指定角色中 add by wyb 2020/9/3
|
/// </summary>
|
/// <param name="UserId"></param>
|
/// <param name="RoleName"></param>
|
/// <returns></returns>
|
public async Task<bool> IsUserIdIntoRoleName(int UserId, params string[] RoleNames)
|
{
|
var sql = @"
|
SELECT * FROM sysUserInfo where uID in (
|
SELECT top 1 [UserId] Id FROM UserRole where RoleId in (
|
SELECT [RoleId] FROM Role where Name in('" + string.Join("','", RoleNames.Select(t => t.Replace("'", "''"))) + @"')
|
) and UserId=" + UserId + @"
|
)";
|
var userlist = await this.BaseDal.QuerySql(sql);
|
var user = userlist.FirstOrDefault();
|
if (user == null) { return false; }
|
return true;
|
}
|
|
/// <summary>
|
/// OA用户回调
|
/// </summary>
|
/// <returns></returns>
|
public async Task<Tuple<string, int>> BindingUser(string realName, string jobNo, int sex, int orgId)
|
{
|
var id = 0;
|
var error = "";
|
var now = DateTime.Now;
|
|
try
|
{
|
_DB.BeginTran();
|
var model = await base.GetModel(x => x.uRealName == realName && x.JobNo == jobNo);
|
if (model == null)
|
{
|
var departmentModel = await _departmentServices.GetModel(x => x.Name.Contains("生产"));
|
if (departmentModel != null)
|
{
|
//添加用户
|
var UserInfo = new sysUserInfo
|
{
|
uLoginName = PinYinHelper.ConvertToAllSpell(realName),
|
uLoginPWD = MD5Helper.MD5Encrypt32("123456"),
|
uRealName = realName,
|
uStatus = 0,
|
uCreateTime = now,
|
uErrorCount = 0,
|
sex = sex,
|
age = 0,
|
birth = now,
|
tdIsDelete = false,
|
JobNo = jobNo,
|
DepartmentId = departmentModel.Id,//默认生产部门
|
SysOrgId = orgId
|
//Mobile = mobile
|
};
|
var userResult = await base.Add(UserInfo);
|
if (userResult > 0)
|
{
|
var roleModel = await _roleRepository.GetModel(x => x.Name == RoleConfig.ProductionAgentStaff);
|
if (roleModel != null)
|
{
|
//添加用户角色关系
|
var UserRole = new UserRole
|
{
|
IsDeleted = false,
|
UserId = userResult,
|
RoleId = roleModel.Id,
|
CreateId = userResult,
|
CreateTime = now
|
};
|
var userRoleResult = await _userRoleServices.Add(UserRole);
|
if (userRoleResult > 0)
|
{
|
//角色模块权限关系表里无数据才添加有主体的权限
|
var roleModulePermissionModel = await _roleModulePermissionServices.GetModel(x => x.RoleId == roleModel.Id && x.SysOrgId == orgId);
|
if (roleModulePermissionModel == null)
|
{
|
//添加角色模块权限关系
|
var roleModulePermissionList = await _roleModulePermissionServices.Query(x => x.RoleId == roleModel.Id);
|
var roleModulePermissionList2 = roleModulePermissionList.Where(x => x.SysOrgId == roleModulePermissionList.Min(x => x.SysOrgId)).ToList();
|
if (roleModulePermissionList2 != null && roleModulePermissionList2.Any())
|
{
|
foreach (var item in roleModulePermissionList2)
|
{
|
item.CreateTime = now;
|
item.ModifyTime = now;
|
item.SysOrgId = orgId;
|
}
|
var roleModulePermissionResult = await _roleModulePermissionServices.Add(roleModulePermissionList2);
|
if (roleModulePermissionResult <= 0)
|
{
|
_DB.RollbackTran();
|
error = "添加角色模块权限关系失败";
|
}
|
}
|
}
|
_DB.CommitTran();
|
id = userResult;
|
return new Tuple<string, int>(error, id);
|
}
|
else
|
{
|
_DB.RollbackTran();
|
error = "添加用户角色关系失败";
|
}
|
}
|
else
|
{
|
_DB.RollbackTran();
|
error = "无相关角色";
|
}
|
}
|
else
|
{
|
_DB.RollbackTran();
|
error = "添加用户失败";
|
}
|
}
|
}
|
id = model.uID;
|
return new Tuple<string, int>(error, id);
|
}
|
catch (Exception ex)
|
{
|
_DB.RollbackTran();
|
return new Tuple<string, int>(ex.ObjToString(), id);
|
}
|
}
|
|
/// <summary>
|
/// 初始化菜单
|
/// </summary>
|
/// <returns></returns>
|
public async Task<Tuple<string, int>> InitializePermission(string realName, int orgId, string roleName)
|
{
|
var id = 0;
|
var error = "";
|
var now = DateTime.Now;
|
|
try
|
{
|
_DB.BeginTran();
|
var model = await base.GetModel(x => x.uLoginName == realName);
|
if (model != null)
|
{
|
var roleModel = await _roleRepository.GetModel(x => x.Name.Contains(roleName));
|
if (roleModel != null)
|
{
|
var userRoleModel = await _userRoleServices.GetModel(x => x.UserId == model.uID);
|
if (userRoleModel == null)
|
{
|
//添加用户角色关系
|
var UserRole = new UserRole
|
{
|
IsDeleted = false,
|
UserId = model.uID,
|
RoleId = roleModel.Id,
|
CreateId = model.uID,
|
CreateTime = now
|
};
|
var userRoleResult = await _userRoleServices.Add(UserRole);
|
if (userRoleResult > 0)
|
{
|
//角色模块权限关系表里无数据才添加有主体的权限
|
var roleModulePermissionModel = await _roleModulePermissionServices.GetModel(x => x.RoleId == roleModel.Id && x.SysOrgId == orgId);
|
if (roleModulePermissionModel == null)
|
{
|
//添加角色模块权限关系
|
var roleModulePermissionList = await _roleModulePermissionServices.Query(x => x.RoleId == roleModel.Id);
|
var roleModulePermissionList2 = roleModulePermissionList.Where(x => x.SysOrgId == roleModulePermissionList.Min(x => x.SysOrgId)).ToList();
|
if (roleModulePermissionList2 != null && roleModulePermissionList2.Any())
|
{
|
foreach (var item in roleModulePermissionList2)
|
{
|
item.CreateTime = now;
|
item.ModifyTime = now;
|
item.SysOrgId = orgId;
|
}
|
var roleModulePermissionResult = await _roleModulePermissionServices.Add(roleModulePermissionList2);
|
if (roleModulePermissionResult <= 0)
|
{
|
_DB.RollbackTran();
|
error = "添加角色模块权限关系失败";
|
}
|
else
|
{
|
_DB.CommitTran();
|
}
|
}
|
}
|
}
|
else
|
{
|
_DB.RollbackTran();
|
error = "添加用户角色关系失败";
|
}
|
}
|
}
|
else
|
{
|
_DB.RollbackTran();
|
error = "无相关角色";
|
}
|
id = model.uID;
|
}
|
else
|
{
|
_DB.RollbackTran();
|
error = "无相关用户";
|
}
|
|
return new Tuple<string, int>(error, id);
|
}
|
catch (Exception ex)
|
{
|
_DB.RollbackTran();
|
return new Tuple<string, int>(ex.ObjToString(), id);
|
}
|
}
|
}
|
}
|
|
//----------sysUserInfo结束----------
|