You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

484 lines
18 KiB
C#

using DS.Module.Core;
11 months ago
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
10 months ago
using DS.Module.Core.Helpers;
10 months ago
using DS.Module.SqlSugar;
11 months ago
using DS.Module.UserModule;
10 months ago
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Sys.Dtos;
using DS.WMS.Core.Sys.Entity;
using DS.WMS.Core.Sys.Interface;
11 months ago
using Mapster;
11 months ago
using Masuit.Tools.Systems;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
10 months ago
using SqlSugar.IOC;
10 months ago
using SysTenantLink = DS.Module.SqlSugar.SysTenantLink;
namespace DS.WMS.Core.Sys.Method;
11 months ago
public class TenantApplyService : ITenantApplyService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
11 months ago
private readonly IUser user;
10 months ago
private readonly ISaasDbService saasService;
10 months ago
/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public TenantApplyService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
10 months ago
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
11 months ago
user = _serviceProvider.GetRequiredService<IUser>();
}
11 months ago
11 months ago
#region 企业用户审批
/// <summary>
11 months ago
/// 企业注册用户列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<TenantApplyListRes>> GetListByPage(PageRequest request)
{
// var query = _commonService.GetDataRuleFilter<SysRole>();
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable<SysTenantApply>().ClearFilter<ITenantId>()
.Select<TenantApplyListRes>()
10 months ago
.Where(x => x.AuditStatus != AuditStatusEnum.NoAudit)
11 months ago
.Where(whereList).ToQueryPage(request.PageCondition);
return data;
}
10 months ago
11 months ago
/// <summary>
/// 获取注册企业审批信息
/// </summary>
/// <returns></returns>
public DataResult<TenantApplyInfoRes> GetTenantAuditInfo(string id)
10 months ago
{
11 months ago
var data = db.Queryable<SysTenantApply>()
.Where(a => a.Id == long.Parse(id))
.Select<TenantApplyInfoRes>()
.First();
return DataResult<TenantApplyInfoRes>.Success(data);
}
10 months ago
11 months ago
/// <summary>
10 months ago
/// 企业用户审批 - 驳回
11 months ago
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
10 months ago
public DataResult RejectTenantApply(CommonAuditInput req)
11 months ago
{
var userId = user.UserId;
10 months ago
11 months ago
var apply = db.Queryable<SysTenantApply>().Where(x => x.Id == req.Id).First();
if (apply.IsNull())
{
return DataResult.Failed("企业用户信息不存在!");
}
if (apply.AuditStatus == AuditStatusEnum.Approve)
{
return DataResult.Failed("已审批通过!");
}
if (req.Status == 1)
10 months ago
{
return DataResult.Failed("非法状态!");
}
else
{
apply.AuditStatus = (AuditStatusEnum)(int)req.Status;
10 months ago
apply.AuditNote = req.AuditNote;
10 months ago
db.Updateable(req).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("驳回成功!");
}
}
10 months ago
10 months ago
/// <summary>
/// 企业用户审批 - 通过
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult> TenantApplyAudit(TenantApplyAuditReq req)
{
10 months ago
var dbScope = (SqlSugarScope)db;
var masterDb = saasService.GetMasterDb();
var apply = await masterDb.Queryable<SysTenantApply>().Where(x => x.Id == req.AuditInfo.Id).FirstAsync();
10 months ago
if (apply.IsNull())
{
10 months ago
return await Task.FromResult(DataResult.Failed("企业用户信息不存在!"));
10 months ago
}
if (apply.AuditStatus == AuditStatusEnum.Approve)
{
10 months ago
return await Task.FromResult(DataResult.Failed("已审批通过!"));
10 months ago
}
10 months ago
if (req.PermissionIds.Count == 0)
10 months ago
{
10 months ago
return await Task.FromResult(DataResult.Failed("请勾选权限模块!"));
10 months ago
}
if (req.AuditInfo.Status == 1)
11 months ago
{
try
10 months ago
{
11 months ago
//开启事务
10 months ago
await dbScope.Ado.BeginTranAsync();
10 months ago
10 months ago
#region 租户相关
10 months ago
10 months ago
//初始化新租户
10 months ago
var entity = apply.Adapt<SysTenant>();
10 months ago
entity.PinYinCode = PinYinUtil.GetFristLetter(entity.Name);
10 months ago
var secret = MD5Helper.Encrypt(DateTime.Now.Ticks.ToString(), false, true);
entity.AppSecret = secret;
10 months ago
10 months ago
var tenant = await masterDb.Insertable(entity).ExecuteReturnEntityAsync();
10 months ago
//初始化租户数据库链接
10 months ago
var link = req.DbInfo.Adapt<SysTenantLink>();
link.TenantId = tenant.Id;
10 months ago
await masterDb.Insertable(link).ExecuteReturnEntityAsync();
10 months ago
//初始化租户权限模块
10 months ago
var permissions = await masterDb.Queryable<SysPermission>().Where(x => req.PermissionIds.Contains(x.Id)).ToListAsync();
10 months ago
10 months ago
var tenantPerList = new List<SysPermissionTenant>();
10 months ago
foreach (var item in permissions)
{
var tenantPer = item.Adapt<SysPermissionTenant>();
tenantPer.Id = 0;
10 months ago
tenantPer.TenantId = tenant.Id;
10 months ago
tenantPer.PermissionId = item.Id;
10 months ago
10 months ago
tenantPerList.Add(tenantPer);
}
10 months ago
10 months ago
await masterDb.Insertable(tenantPerList).ExecuteCommandAsync();
11 months ago
10 months ago
//初始化租户合同
var contract = new SysTenantContract()
{
ContractName = "新签约租户",
BeginDate = apply.ContractBeginDate,
EndDate = apply.ContractEndDate,
TenantId = tenant.Id,
AuditStatus = AuditStatusEnum.Approve,
};
10 months ago
await masterDb.Insertable(contract).ExecuteCommandAsync();
10 months ago
#region 加密授权信息
//租户用户授权数
var userAuth = new SysTenantUserAuth()
{
TenantId = tenant.Id,
10 months ago
AuthNum = EncrypteHelper.EncryptData(req.AuthUserNum.ToString(), secret)
10 months ago
};
10 months ago
await masterDb.Insertable(userAuth).ExecuteCommandAsync();
10 months ago
//租户模块授权数
#endregion
var perAuthList = new List<SysTenantPermissionAuth>();
foreach (var item in req.PermissionAuthList)
{
var perAuth = new SysTenantPermissionAuth()
{
TenantId = tenant.Id,
PermissionId = item.PermissionId,
10 months ago
AuthNum = EncrypteHelper.EncryptData(item.AuthNum.ToString(), secret)
10 months ago
};
perAuthList.Add(perAuth);
}
10 months ago
await masterDb.Insertable(perAuthList).ExecuteCommandAsync();
10 months ago
#endregion
#region 租户用户相关数据
10 months ago
var adminUser =await masterDb.Queryable<SysUser>().Where(x=>x.UserCode == apply.AdminUserCode && x.UserType == UserTypeEnum.ApplyUser.ToEnumInt()).FirstAsync();
10 months ago
//删除租户申请
10 months ago
var delUserRoles = await masterDb.Queryable<SysRoleUser>().Where(x => x.UserId == adminUser.Id).ToListAsync();
await masterDb.Deleteable(delUserRoles).ExecuteCommandAsync();
10 months ago
//初始化租户角色
var roleEntity = new SysRole()
{
RoleCode = "tenantAdmin",
RoleName = "租户管理员",
TenantId = tenant.Id
};
10 months ago
var role = await masterDb.Insertable(roleEntity).ExecuteReturnEntityAsync();
10 months ago
//初始化租户用户角色关系
var roleUserEntity = new SysRoleUser()
{
UserId = adminUser.Id,
RoleId = role.Id,
// TenantId = tenant.Id
};
10 months ago
await masterDb.Insertable(roleUserEntity).ExecuteCommandAsync();
10 months ago
10 months ago
//写入租户管理员权限
var rolePerList = new List<SysRolePermission>();
foreach (var item in permissions)
{
var rolePer = item.Adapt<SysRolePermission>();
rolePer.RoleId = role.Id;
rolePer.PermissionId = item.Id;
rolePer.IsPermission = 1;
rolePerList.Add(rolePer);
}
10 months ago
await masterDb.Insertable(rolePerList).ExecuteCommandAsync();
10 months ago
10 months ago
//初始化租户机构信息
var orgEntity = new SysOrg()
{
OrgName = apply.Name,
ParentId = 0,
Leader = apply.Chief,
Tel = apply.Tel,
OrderNo = 1,
IsDepartment = false,
10 months ago
TenantId = tenant.Id,
10 months ago
Status = StatusEnum.Enable
};
10 months ago
var org = await masterDb.Insertable(orgEntity).ExecuteReturnEntityAsync();
10 months ago
//初始化租户用户机构
var orgUserEntity = new SysOrgUser()
{
UserId = adminUser.Id,
OrgId = org.Id,
};
10 months ago
await masterDb.Insertable(orgUserEntity).ExecuteCommandAsync();
//更新租户管理员信息 机构Id
adminUser.UserType = UserTypeEnum.Admin.ToEnumInt();
adminUser.TenantId = tenant.Id;
adminUser.DefaultOrgId = org.Id;
await masterDb.Updateable(adminUser).ExecuteCommandAsync();
10 months ago
#endregion
#region 创建租户数据库及基础数初始化
10 months ago
if (!dbScope.IsAnyConnection(tenant.Id))
{
dbScope.AddConnection(new ConnectionConfig() {
ConfigId = tenant.Id,
ConnectionString = link.Connection,
DbType = link.DbType,
IsAutoCloseConnection = true });
}
var tenantDb = saasService.GetBizDbScopeById(tenant.Id);
10 months ago
StaticConfig.CodeFirst_MySqlCollate="utf8mb4_0900_ai_ci";//较高版本支持
10 months ago
//初始化数库
tenantDb.DbMaintenance.CreateDatabase();
tenantDb.CodeFirst.InitTables(typeof(CodeCountry));
#endregion
10 months ago
//更新租户申请
apply.AuditStatus = (AuditStatusEnum)(int)req.AuditInfo.Status;
apply.AuditTime = DateTime.Now;
apply.AuditNote = req.AuditInfo.AuditNote;
await masterDb.Updateable(apply).IgnoreColumns(ignoreAllNullColumns: true).EnableDiffLogEvent().ExecuteCommandAsync();
10 months ago
10 months ago
await dbScope.Ado.CommitTranAsync();
return await Task.FromResult(DataResult.Successed("审批成功!"));
11 months ago
}
catch (Exception ex)
{
10 months ago
await dbScope.Ado.RollbackTranAsync();
return await Task.FromResult(DataResult.Failed("审批失败!" + ",请联系管理员!"+ex.ToString()));
10 months ago
}
11 months ago
}
else
{
10 months ago
return await Task.FromResult(DataResult.Failed("非法状态!"));
11 months ago
}
}
10 months ago
11 months ago
#endregion
10 months ago
11 months ago
#region 企业用户维护信息
10 months ago
/// <summary>
/// 获取注册企业申请信息
/// </summary>
/// <returns></returns>
11 months ago
public DataResult<TenantApplyInfoRes> GetTenantApplyInfo()
{
11 months ago
var userInfo = db.Queryable<SysUser>().First(x => x.Id == long.Parse(user.UserId));
var data = db.Queryable<SysTenantApply>()
11 months ago
.Where(a => a.AdminUserCode == userInfo.UserCode)
.Select<TenantApplyInfoRes>()
.First();
return DataResult<TenantApplyInfoRes>.Success(data);
}
11 months ago
/// <summary>
/// 维护企业用户信息
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public DataResult EditTenantApplyInfo(TenantApplyReq req)
{
var existApply = db.Queryable<SysTenantApply>().First(x => x.Id == req.Id);
if (existApply.AuditStatus == AuditStatusEnum.Approve || existApply.AuditStatus == AuditStatusEnum.Auditing)
{
return DataResult.Failed("审核通过或待审批状态不能提交!");
}
try
{
//开启事务
db.Ado.BeginTran();
var apply = req.Adapt<SysTenantApply>();
db.Updateable(apply).IgnoreColumns(x =>
new
{
x.AdminUserPassword, x.AuditStatus, x.AuditNote
}).ExecuteCommand();
//TO DO 处理附件
// var gid = model.GID; //写入
// #region 处理文件
//
// #region 处理法人身份证
// //先删除 法人身份证
// var existCards = db.Queryable<INFO_FILES>().Where(x => x.PID == gid && x.FILETYPE == "法人身份证").Select(n=>n.IMGPATH).ToArray();
// var cardFiles = model.cardfiles.Select(x => x.FilePath).ToArray();
// IEnumerable<string> delCards = existCards.AsQueryable().Except(cardFiles);
// if (delCards.Count()>0)
// {
// foreach (var item in delCards)
// {
// var existInfo = db.Queryable<INFO_FILES>()
// .Where(x => x.PID == gid && x.IMGPATH == item).First();
// db.Deleteable(existInfo).ExecuteCommand();
// }
// }
// //2.添加新的法人身份证
// IEnumerable<string> addCards = cardFiles.AsQueryable().Except(existCards);
// if (addCards.Count()>0)
// {
// foreach (var item in addCards)
// {
// var infoFile = new INFO_FILES
// {
// GID = Guid.NewGuid(),
// PID = gid,
// FILENAME = model.cardfiles.Where(x=>x.FilePath ==item).Select(x=>x.FileName).First(),
// IMGPATH = item,
// FILETYPE = "法人身份证",
// CORPID = gid.ToString(),
// CreateDate = DateTime.Now,
// ModifyDate = DateTime.Now,
// UPLOADEMPLY = ""
// };
// db.Insertable(infoFile).ExecuteCommand();
// }
// }
// #endregion
// #region 处理营业执照
// //先删除 营业执照
// var existLicenses = db.Queryable<INFO_FILES>().Where(x => x.PID == gid && x.FILETYPE == "营业执照").Select(n=>n.IMGPATH).ToArray();
// var licenseFiles = model.licensefiles.Select(x => x.FilePath).ToArray();
// IEnumerable<string> delLicenses = existLicenses.AsQueryable().Except(licenseFiles);
// if (delLicenses.Count()>0)
// {
// foreach (var item in delLicenses)
// {
// var existInfo = db.Queryable<INFO_FILES>()
// .Where(x => x.PID == gid && x.IMGPATH == item).First();
// db.Deleteable(existInfo).ExecuteCommand();
// }
// }
// //2.添加新的营业执照
// IEnumerable<string> addLicenses = licenseFiles.AsQueryable().Except(existLicenses);
// if (addLicenses.Count()>0)
// {
// foreach (var item in addLicenses)
// {
// var infoFile = new INFO_FILES
// {
// GID = Guid.NewGuid(),
// PID = gid,
// FILENAME = model.licensefiles.Where(x=>x.FilePath ==item).Select(x=>x.FileName).First(),
// IMGPATH = item,
// FILETYPE = "营业执照",
// CORPID = gid.ToString(),
// CreateDate = DateTime.Now,
// ModifyDate = DateTime.Now,
// UPLOADEMPLY = ""
// };
// db.Insertable(infoFile).ExecuteCommand();
// }
// }
// #endregion
// #endregion
db.Ado.CommitTran();
return DataResult.Successed("更新成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("更新失败!" + ex);
}
}
/// <summary>
/// 提交审核
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataResult SubmitTenantApply(string id)
{
var existApply = db.Queryable<SysTenantApply>().First(x => x.Id == long.Parse(id));
if (existApply == null)
{
return DataResult.Failed("企业信息不存在,请先保存再提交!");
}
if (existApply.AuditStatus == AuditStatusEnum.Approve || existApply.AuditStatus == AuditStatusEnum.Auditing)
{
return DataResult.Failed("审核通过或待审批状态不能提交!");
}
// var licensefiles = db.Queryable<INFO_FILES>().Where(x => x.PID == gid && x.FILETYPE =="营业执照").ToList();
// if (licensefiles.Count() == 0)
// {
// return DataResult.Failed("营业执照认证资料未上传不能提交!");
// }
// var cardfiles = db.Queryable<INFO_FILES>().Where(x => x.PID == gid && x.FILETYPE =="法人身份证").ToList();
// if (cardfiles.Count() == 0)
// {
// return DataResult.Failed("营业执照认证资料未上传不能提交!");
// }
//更新状态为待审批
existApply.AuditStatus = AuditStatusEnum.Auditing;
db.Updateable(existApply).ExecuteCommand();
return DataResult.Successed("提交成功");
}
11 months ago
#endregion
}