CompanyRepository.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using MathNet.Numerics.Distributions;
  2. using NPOI.POIFS.Crypt.Dsig;
  3. using NPOI.SS.Formula.Functions;
  4. using OASystem.Domain;
  5. using OASystem.Domain.Entities.Groups;
  6. using OASystem.Domain.Entities.System;
  7. using OASystem.Domain.ViewModels.QiYeWeChat;
  8. namespace OASystem.Infrastructure.Repositories.System
  9. {
  10. public class CompanyRepository : BaseRepository<Sys_Company, CompanyView>
  11. {
  12. public CompanyRepository(SqlSugarClient sqlSugar) : base(sqlSugar)
  13. {
  14. }
  15. public async Task<Result> AddCompany(Sys_Company company)
  16. {
  17. Result result = new Result() { Code = -2, Msg = "未知错误" };
  18. try
  19. {
  20. BeginTran();
  21. Sys_Company Company = _sqlSugar.Queryable<Sys_Company>().First(a => a.CompanyName == company.CompanyName && a.IsDel == 0);
  22. if (Company != null)
  23. {
  24. return result = new Result() { Code = -1, Msg = "该公司已存在,请勿重复添加!"};
  25. }
  26. else
  27. {
  28. int CompanyId = 0;
  29. int DepartmentId = 0;
  30. int JobPostId = 0;
  31. int UsersId = 0;
  32. CompanyId = await AddAsyncReturnId(company);
  33. if (CompanyId > 0)
  34. {
  35. #region 添加默认部门
  36. Sys_Department _Department = new Sys_Department();
  37. _Department.CompanyId = CompanyId;
  38. _Department.DepName = "默认部门";
  39. _Department.DepCode = "00";
  40. _Department.ParentDepId = 0;
  41. _Department.CreateUserId = company.CreateUserId;
  42. _Department.Remark = "添加公司时默认生成部门,可修改!";
  43. _Department.IsDel = 0;
  44. DepartmentId = await _sqlSugar.Insertable(_Department).ExecuteReturnIdentityAsync();
  45. #endregion
  46. if (DepartmentId>0)
  47. {
  48. #region 添加默认岗位
  49. Sys_JobPost _JobPost = new Sys_JobPost();
  50. _JobPost.CompanyId = CompanyId;
  51. _JobPost.DepId = DepartmentId;
  52. _JobPost.JobName = "默认岗位";
  53. _JobPost.CreateUserId = company.CreateUserId;
  54. _JobPost.Remark = "添加公司时默认生成岗位,可修改!";
  55. _JobPost.IsDel = 0;
  56. JobPostId = await _sqlSugar.Insertable(_JobPost).ExecuteReturnIdentityAsync();
  57. #endregion
  58. if (JobPostId>0)
  59. {
  60. #region 添加默认管理员
  61. Sys_Users _Users = new Sys_Users();
  62. _Users.CnName = "管理员";
  63. _Users.EnName = "Guan LiYuan";
  64. _Users.Number = CompanyId.ToString() + DepartmentId.ToString() + JobPostId.ToString()+"01";
  65. _Users.CompanyId = CompanyId;
  66. _Users.DepId = DepartmentId;
  67. _Users.JobPostId = JobPostId;
  68. _Users.Password = "123456";
  69. _Users.Sex = 0;
  70. _Users.HrAudit = 1;
  71. _Users.CreateUserId = company.CreateUserId;
  72. _Users.Remark = "添加公司时默认生成管理员,Number可修改!";
  73. _Users.IsDel = 0;
  74. UsersId = await _sqlSugar.Insertable(_Users).ExecuteReturnIdentityAsync();
  75. if (UsersId>0)
  76. {
  77. List<int> fid = new List<int>();//功能权限
  78. List<int> SmId=new List<int>();//页面权限
  79. List<Sys_UserAuthority> adds = new List<Sys_UserAuthority>();
  80. List<Sys_PageFunctionPermission> sys_PageFunctionPermissions = _sqlSugar.Queryable<Sys_PageFunctionPermission>().Where(a=>a.IsDel==0).ToList();
  81. foreach (var item in sys_PageFunctionPermissions)
  82. {
  83. fid.Add(item.Id);
  84. }
  85. List<Sys_SystemMenuPermission> _SystemMenuPermissions = _sqlSugar.Queryable<Sys_SystemMenuPermission>().Where(a => a.IsDel == 0 && (a.Mid==12 || a.Mid==13 || a.Mid==17) &&
  86. a.WebUrl!="Null" && a.WebUrl!="/" && a.IsEnable==1).ToList();
  87. foreach(var item in _SystemMenuPermissions)
  88. {
  89. SmId.Add(item.Id);
  90. }
  91. for (int i = 0;i< SmId.Count; i++)
  92. {
  93. for (int j = 0; j < fid.Count; j++)
  94. {
  95. adds.Add(new Sys_UserAuthority
  96. {
  97. CreateTime = DateTime.Now,
  98. CreateUserId = company.CreateUserId,
  99. FId = fid[j],
  100. UId = UsersId,
  101. SmId = SmId[i],
  102. IsTemp = 0
  103. });
  104. }
  105. }
  106. _sqlSugar.Insertable<Sys_UserAuthority>(adds).ExecuteCommand();
  107. }
  108. else
  109. {
  110. RollbackTran();
  111. result = new Result() { Code = 0, Msg = "默认管理员添加失败!" };
  112. }
  113. #endregion
  114. }
  115. else
  116. {
  117. RollbackTran();
  118. result = new Result() { Code = 0, Msg = "默认岗位添加失败!" };
  119. }
  120. }
  121. else
  122. {
  123. RollbackTran();
  124. result = new Result() { Code = 0, Msg = "默认部门添加失败!" };
  125. }
  126. CommitTran();
  127. var data = new
  128. {
  129. Number = CompanyId.ToString() + DepartmentId.ToString() + JobPostId.ToString() + "01",
  130. Password = "123456",
  131. };
  132. result = new Result() { Code = 0, Msg = "添加成功!可通过Number和密码登录进去管理!初始密码为:123456", };
  133. }
  134. else
  135. {
  136. result = new Result() { Code = -1, Msg = "添加失败!请稍后重试!" };
  137. }
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. return result = new Result() { Code = -2, Msg = "未知错误" };
  143. }
  144. return result;
  145. }
  146. /// <summary>
  147. /// 获取所有公司数据
  148. /// </summary>
  149. /// <param name="_CompanyRepository"></param>
  150. /// <returns></returns>
  151. public Result GetCompanyData()
  152. {
  153. Result result = new Result();
  154. string sql = "select * from Sys_Company where isdel = 0";
  155. var DBdata = GetListBySqlWithNolock(sql);
  156. if (DBdata == null || DBdata.Count == 0)
  157. {
  158. result.Code = -1;
  159. result.Msg = "暂无数据!";
  160. return result;
  161. }
  162. result.Code = 0;
  163. result.Msg = "成功!";
  164. result.Data = DBdata;
  165. return result;
  166. }
  167. /// <summary>
  168. /// 获取公司名称数据
  169. /// </summary>
  170. /// <param name="_CompanyRepository"></param>
  171. /// <returns></returns>
  172. public async Task<Result> GetCompanyNameData()
  173. {
  174. Result result = new Result();
  175. string sql = "Select Id,CompanyName From Sys_Company Where IsDel = 0";
  176. var companyNameData = await _sqlSugar.SqlQueryable<CompanyNameView>(sql).ToListAsync();
  177. if (companyNameData == null || companyNameData.Count == 0)
  178. {
  179. result.Code = -1;
  180. result.Msg = "暂无数据!";
  181. return result;
  182. }
  183. result.Code = 0;
  184. result.Msg = "成功!";
  185. result.Data = companyNameData;
  186. return result;
  187. }
  188. }
  189. }