OrganizationController.cs 13 KB

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