基础服务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.
 
 

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