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

192 lines
7.0 KiB

  1. using BPA.SAAS.Manage.Application.Org.Dtos.Company;
  2. using BPA.SAAS.Manage.Application.Org.Dtos.Role;
  3. using BPA.SAAS.Manage.Application.Org.Dtos.Users;
  4. using BPA.SAAS.Manage.Application.Org.Interface;
  5. using BPA.SAAS.Manage.Comm.Enum;
  6. using BPA.SAAS.Manage.Core.Base;
  7. using BPA.SAAS.Manage.Core.Org;
  8. using Furion.LinqBuilder;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace BPA.SAAS.Manage.Application.Org.Services
  15. {
  16. public class CompanyService: ICompanyService, ITransient
  17. {
  18. private readonly ISqlSugarClient _db;
  19. IUserService _UserService;
  20. IRoleService _RoleService;
  21. public CompanyService(ISqlSugarClient db, IUserService userService, IRoleService roleService)
  22. {
  23. _db = db;
  24. _UserService = userService;
  25. _RoleService = roleService;
  26. }
  27. /// <summary>
  28. /// 查询企业信息
  29. /// </summary>
  30. /// <param name="input"></param>
  31. /// <returns></returns>
  32. public async Task<PageUtil> CompanyPage(CompanyDtoPageInput input)
  33. {
  34. var total = 0;
  35. var data =await _db.Queryable<BPA_Company, BPA_Roles>((a, b) => new JoinQueryInfos(JoinType.Inner, a.Id == b.GroupId))
  36. .Where((a, b) => a.IsDeleted == 0)
  37. .WhereIF(!input.Name.IsNullOrEmpty(), (a, b) => a.Name.Contains(input.Name))
  38. .WhereIF(!input.AdminName.IsNullOrEmpty(), (a, b) => a.AdminName.Contains(input.AdminName))
  39. .WhereIF(!input.Email.IsNullOrEmpty(), (a, b) => a.Email.Contains(input.Email))
  40. .Filter(null, true)
  41. .Select((a, b) => new CompanyDtoInput
  42. {
  43. Id = a.Id.SelectAll(),
  44. SysRoleId = b.Id
  45. })
  46. .OrderBy(a => a.CreateAt, OrderByType.Desc)
  47. .ToPageListAsync(input.Current, input.PageSize, total);
  48. PageUtil util = new PageUtil()
  49. {
  50. Total = total,
  51. Data = data
  52. };
  53. return util;
  54. }
  55. /// <summary>
  56. /// 添加企业信息
  57. /// </summary>
  58. /// <param name="input"></param>
  59. /// <returns></returns>
  60. public async Task<bool> AddCompany(CompanyDtoInput input)
  61. {
  62. var company = input.Adapt<BPA_Company>();
  63. var Companycheck = _db.Queryable<BPA_Company>().Where(x => x.IsDeleted == 0).First(x => x.Email == company.Email);
  64. if (Companycheck != null)
  65. throw Oops.Oh($"账号已存在");
  66. var res =await _db.Insertable(company).ExecuteReturnEntityAsync();
  67. input.Email = input.Email.IsNullOrEmpty() ? input.Phone : input.Email;
  68. //创建企业,管理员用户,管理员角色
  69. bool adduser =await _UserService.AddUser(new UserDtoInput
  70. {
  71. GroupId = res.Id,
  72. Account = input.Email,
  73. AdminType = AdminEnums.Admin,
  74. Name = input.Name,
  75. });
  76. if (adduser)
  77. {
  78. var addrole = _RoleService.Add(new RoleDtoInput
  79. {
  80. Id= Guid.NewGuid().ToString(),
  81. GroupId = res.Id,
  82. Name = input.Name + "管理员",
  83. Remark = "系统默认创建管理员角色不允许修改",
  84. Code = "sys_manager_role",
  85. });
  86. // 查询用户
  87. var UserRes = _db.Queryable<BPA_Users>().Filter(null, true).Where(a => a.GroupId == res.Id && a.Account == input.Email).ToList();
  88. ////查询角色
  89. var RoleRes = _db.Queryable<BPA_Roles>().Filter(null, true).
  90. Where(a => a.GroupId == res.Id && a.Code == "sys_manager_role").ToList();
  91. if (UserRes.Count() > 0 && RoleRes.Count() > 0)
  92. {
  93. BPA_UserRole userRole = new BPA_UserRole();
  94. userRole.Id= Guid.NewGuid().ToString();
  95. userRole.SysUserId = UserRes.First().Id;
  96. userRole.SysRoleId = RoleRes.First().Id;
  97. _db.Insertable(userRole).ExecuteReturnEntity();
  98. }
  99. }
  100. else
  101. {
  102. _db.Deleteable(res).ExecuteCommand();
  103. return false;
  104. }
  105. return true;
  106. }
  107. /// <summary>
  108. /// 修改企业信息
  109. /// </summary>
  110. /// <param name="input"></param>
  111. /// <returns></returns>
  112. public async Task<bool> UpdateCompany(CompanyDtoInput input)
  113. {
  114. var company = input.Adapt<BPA_Company>();
  115. var check = _db.Queryable<BPA_Company>().Where(x => x.IsDeleted == 0).First(t => t.Id == company.Id);
  116. if (check == null)
  117. return false;
  118. if (check.Email != input.Email)
  119. return false;
  120. return await _db.Updateable(company).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandHasChangeAsync();
  121. }
  122. /// <summary>
  123. /// 删除企业信息
  124. /// </summary>
  125. /// <param name="inputList"></param>
  126. /// <returns></returns>
  127. public async Task<bool> BatchDelCompany(List<string> inputList)
  128. {
  129. try
  130. {
  131. _db.Ado.BeginTran();
  132. // 查询数据库中是否存在未删除的活动信息
  133. var resEntitites = _db.Queryable<BPA_Company>().In(inputList).ToList();
  134. resEntitites.ForEach(x =>
  135. {
  136. x.IsDeleted = 1;
  137. });
  138. var res = await _db.Updateable(resEntitites).ExecuteCommandAsync();
  139. _db.Ado.CommitTran();
  140. return true;
  141. }
  142. catch (Exception)
  143. {
  144. _db.Ado.RollbackTran();
  145. return false;
  146. }
  147. }
  148. /// <summary>
  149. /// 启用
  150. /// </summary>
  151. /// <param name="Id"></param>
  152. /// <returns></returns>
  153. public async Task<bool> Enable(string Id)
  154. {
  155. var resEntitites = _db.Queryable<BPA_Company>().In(Id).ToList();
  156. resEntitites.ForEach(x =>
  157. {
  158. x.Status = CommonStatus.ENABLE;
  159. });
  160. var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
  161. return true;
  162. }
  163. /// <summary>
  164. /// 禁用
  165. /// </summary>
  166. /// <param name="Id"></param>
  167. /// <returns></returns>
  168. public async Task<bool> Disable(string Id)
  169. {
  170. var resEntitites = _db.Queryable<BPA_Company>().In(Id).ToList();
  171. resEntitites.ForEach(x =>
  172. {
  173. x.Status = CommonStatus.DISABLE;
  174. });
  175. var res =await _db.Updateable(resEntitites).ExecuteCommandAsync();
  176. return true;
  177. }
  178. }
  179. }