OrganizationController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using HiTeachCE.Helpers;
  2. using HiTeachCE.Models;
  3. using HiTeachCE.Services;
  4. using IdentityModel;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Mvc;
  7. using OpenXmlPowerTools;
  8. using Org.BouncyCastle.Ocsp;
  9. using SqlSugar;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel.DataAnnotations;
  13. using System.Linq;
  14. using System.Linq.Expressions;
  15. using System.Threading.Tasks;
  16. using TEAMModelOS.SDK.Context.Exception;
  17. using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
  18. using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
  19. using TEAMModelOS.SDK.Extension.DataResult.RequestData;
  20. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  21. namespace HiTeachCE.Controllers
  22. {
  23. [Route("api/[controller]")]
  24. [ApiController]
  25. public class OrganizationController : BaseController
  26. {
  27. public OrganizationService organizationService;
  28. public MemberService memberService;
  29. public LecturerService lecturerService;
  30. public ActivationCodeService activationCodeService;
  31. public OrganizationController(OrganizationService organization, MemberService member, LecturerService lecturer, ActivationCodeService activationCode)
  32. {
  33. organizationService = organization;
  34. memberService = member;
  35. lecturerService = lecturer;
  36. activationCodeService = activationCode;
  37. }
  38. /// <summary>
  39. /// 获取组织列表
  40. /// </summary>
  41. /// <param name="request"></param>
  42. /// <returns></returns>
  43. [HttpPost("list")]
  44. [Authorize(Policy =Constant.Role_RootAdmin)]
  45. public BaseJosnRPCResponse List(PaginationJosnRPCRequest<Dictionary<string, string>> request)
  46. {
  47. string role = GetLoginUser(JwtClaimTypes.Role);
  48. Dictionary<string, object> extend = new Dictionary<string, object>();
  49. JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
  50. List<Organization> organizations = new List<Organization>();
  51. if (role.Contains(Constant.Role_Root))
  52. {
  53. Expression<Func<Organization, bool>> linq = null;
  54. linq = f => 1 == 1;
  55. if (request.@params.data.TryGetValue("code", out string code) && !string.IsNullOrEmpty(code))
  56. {
  57. linq = linq.And(m => m.code == code);
  58. }
  59. if (request.@params.data.TryGetValue("name", out string name) && !string.IsNullOrEmpty(name))
  60. {
  61. linq = linq.And(m => m.name.Contains(name));
  62. }
  63. if (request.@params.data.TryGetValue("id", out string id) && !string.IsNullOrEmpty(id))
  64. {
  65. linq = linq.And(m => m.id == id);
  66. }
  67. if (linq != null)
  68. {
  69. Expression<Func<Organization, object>> order = null;
  70. order = o => o.createTime;
  71. organizations = organizationService.GetPageList(linq, request.@params.page, order, OrderByType.Desc);
  72. extend.Add("manager", organizations.Select(x => x.code).ToList());
  73. }
  74. else {
  75. Expression<Func<Organization, object>> order = null;
  76. order = o => o.createTime;
  77. linq = m => 1 == 1;
  78. organizations = organizationService.GetPageList(linq, request.@params.page, order, OrderByType.Desc);
  79. extend.Add("manager", organizations.Select(x => x.code).ToList());
  80. }
  81. }
  82. else {
  83. string unionid = GetLoginUser(JwtClaimTypes.Id);
  84. Expression<Func<Member, bool>> mlinq = null;
  85. mlinq = m => m.unionid == unionid;
  86. List<Member> members= memberService.GetList(mlinq);
  87. if (members.IsNotEmpty()) {
  88. Expression<Func<Organization, object>> olinq = null;
  89. olinq = m => m.code;
  90. organizations = organizationService.GetListIn(olinq,members.Select(x=>x.orgCode).ToArray());
  91. request.@params.page.total = organizations.Count;
  92. request.@params.page.totalPage =1;
  93. request.@params.page.currPage = 1;
  94. request.@params.page.pageSize = organizations.Count;
  95. }
  96. extend.Add("manager", members.Where(m => m.admin == 1).Select(x => x.orgCode).ToList());
  97. }
  98. if (organizations.IsNotEmpty()) {
  99. Expression<Func<ActivationCode, object>> linq = null;
  100. linq = o => o.orgCode;
  101. List<ActivationCode> activationCodes = activationCodeService.GetListIn(linq, organizations.Select(x => x.code).ToArray());
  102. activationCodes.ForEach(x => { x.cdkey = ""; });
  103. extend.Add("Activation", activationCodes);
  104. }
  105. return builder.Data(organizations).Page(request.@params.page).Extend(extend).build();
  106. }
  107. /// <summary>
  108. /// 更新组织
  109. /// </summary>
  110. /// <param name="request"></param>
  111. /// <returns></returns>
  112. [HttpPost("update")]
  113. [Authorize(Policy = Constant.Role_RootAdmin)]
  114. public BaseJosnRPCResponse Update(JosnRPCRequest<Organization> request)
  115. {
  116. string unionid = GetLoginUser(JwtClaimTypes.Id);
  117. string role = GetLoginUser(JwtClaimTypes.Role);
  118. JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
  119. Organization organization = organizationService.GetById(request.@params.id);
  120. bool b = false;
  121. if (role.Contains(Constant.Role_Root))
  122. {
  123. if (organization != null)
  124. {
  125. request.@params.code = organization.code;
  126. request.@params.type = organization.type;
  127. b = organizationService.Update(request.@params);
  128. }
  129. }
  130. else {
  131. Expression<Func<Member, bool>> mlinq = null;
  132. mlinq = m => m.orgCode == request.@params.code && m.unionid == unionid;
  133. List<Member> members = memberService.GetList(mlinq);
  134. if (members.IsNotEmpty() && members[0].admin == 1 && organization != null)
  135. {
  136. request.@params.code = organization.code;
  137. request.@params.type = organization.type;
  138. b = organizationService.Update(request.@params);
  139. }
  140. else {
  141. throw new BizException("登录管理员不能管理该组织机构", 2);
  142. }
  143. }
  144. return builder.Data(b).build();
  145. }
  146. /// <summary>
  147. /// 添加组织
  148. /// </summary>
  149. /// <param name="request"></param>
  150. /// <returns></returns>
  151. [HttpPost("add")]
  152. [Authorize(Policy = Constant.Role_Root)]
  153. public BaseJosnRPCResponse Add(JosnRPCRequest<OrgDto> request)
  154. {
  155. JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
  156. Expression<Func<Lecturer, bool>> linq = null;
  157. linq = m => m.cellphone == request.@params.adminCellphone;
  158. List<Lecturer> lecturers = lecturerService.GetList(linq);
  159. Lecturer lecturer = null;
  160. if (!lecturers.IsNotEmpty())
  161. {
  162. //新增组织机构管理员
  163. Random random = new Random();
  164. string seed = new string(Constant.az09);
  165. string pfx = "";
  166. for (int i = 0; i < 4; i++)
  167. {
  168. string c = seed.ToCharArray()[random.Next(0, seed.Length)] + "";
  169. seed.Replace(c, "");
  170. pfx = pfx + c;
  171. }
  172. lecturer = new Lecturer
  173. {
  174. id = Guid.NewGuid().ToString(),
  175. unionid = Guid.NewGuid().ToString("N"),
  176. username = request.@params.adminCellphone + "手机用户",
  177. password = "",
  178. account = "hitmd-" + request.@params.adminCellphone.Substring(request.@params.adminCellphone.Length - 4, 4) + "#" + pfx,
  179. areaCode = "86",
  180. registerTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),
  181. status = 1,
  182. setaccount = 0,
  183. cellphone = request.@params.adminCellphone,
  184. avatar = "https://cdhabook.teammodel.cn/avatar/usertile" + random.Next(10, 44) + ".png"
  185. };
  186. lecturerService.Insert(lecturer);
  187. }
  188. else
  189. {
  190. lecturer = lecturers[0];
  191. }
  192. Expression<Func<Member, bool>> mlinq = null;
  193. mlinq = m => m.unionid == lecturer.unionid;
  194. List<Member> members = memberService.GetList(mlinq);
  195. if (members.IsNotEmpty())
  196. {
  197. //如果存在个人组织 则判断个人是管理员
  198. if (request.@params.orgType == 2)
  199. {
  200. string[] orgs = members.Where(x => x.admin == 1).Select(x => x.orgCode).ToArray();
  201. Expression<Func<Organization, object>> olinq = null;
  202. olinq = o => o.code;
  203. Expression<Func<Organization, bool>> whereExpression = null;
  204. whereExpression = w => w.type == 2;
  205. List<Organization> organizations = organizationService.GetListWhereIn(whereExpression, olinq, orgs);
  206. if (organizations.IsNotEmpty())
  207. {
  208. throw new BizException("该手机号已经授权个人用户!", 2);
  209. }
  210. }
  211. }
  212. long time = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
  213. Organization organization = new Organization
  214. {
  215. id = Guid.NewGuid().ToString(),
  216. code = Guid.NewGuid().ToString("N"),
  217. name = request.@params.orgName,
  218. type = request.@params.orgType,
  219. status = 1,
  220. createTime = time
  221. };
  222. Random rdid = new Random();
  223. string cdkey = "";
  224. for (int i = 0; i < 25; i++)//从数组随机抽取字符组成新的字符生成机器三
  225. {
  226. cdkey += Constant.az09[rdid.Next(0, Constant.az09.Length)];
  227. if (i != 24 && (i + 1) % 5 == 0)
  228. {
  229. cdkey += "-";
  230. }
  231. }
  232. ActivationCode activationCode = new ActivationCode
  233. {
  234. id = Guid.NewGuid().ToString(),
  235. cdkey = cdkey.ToUpper(),
  236. maximum = request.@params.maximum,
  237. orgCode = organization.code,
  238. createTime = time,
  239. clientId = Guid.NewGuid().ToString("N"),
  240. secret = Guid.NewGuid().ToString("N"),
  241. status = 1
  242. };
  243. if (request.@params.expires > 0)
  244. {
  245. activationCode.expires = time + request.@params.expires * 60 * 60 * 24;
  246. }
  247. else
  248. {
  249. activationCode.expires = time;
  250. }
  251. Member member = new Member
  252. {
  253. id = Guid.NewGuid().ToString(),
  254. orgCode = organization.code,
  255. admin = 1,
  256. // expires = activationCode.expires,
  257. status = 1,
  258. unionid = lecturer.unionid,
  259. createTime=time
  260. };
  261. bool f = organizationService.Insert(organization);
  262. if (f) {
  263. f = memberService.Insert(member);
  264. }
  265. if (f)
  266. {
  267. f = activationCodeService.Insert(activationCode);
  268. }
  269. if (f)
  270. {
  271. var data = new { organization, activationCode, member };
  272. return builder.Data(data).build();
  273. }
  274. else {
  275. throw new BizException("创建失败!", 2);
  276. }
  277. }
  278. }
  279. public class OrgDto
  280. {
  281. [Required(ErrorMessage = "组织名称必须填写")]
  282. public string orgName { get; set; }
  283. [Required(ErrorMessage = "组织类型必须填写")]
  284. [Range(1, 2, ErrorMessage = "请输入1~2的整数")]
  285. public int orgType { get; set; }
  286. [Required(ErrorMessage = "组织管理员手机号必须填写")]
  287. public string adminCellphone { get; set; }
  288. [Required(ErrorMessage = "授权上限必须填写")]
  289. [Range(1, 1000, ErrorMessage = "请输入1~1000的整数")]
  290. public int maximum { get; set; }
  291. /// <summary>
  292. /// 时长 ,大于0 按天计算
  293. /// </summary>
  294. [Required(ErrorMessage = "授权时限必须填写")]
  295. [Range(0, 3650, ErrorMessage = "请输入-1~3650的整数")]
  296. public int expires { get; set; }
  297. }
  298. }