123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- using HiTeachCE.Helpers;
- using HiTeachCE.Models;
- using HiTeachCE.Services;
- using IdentityModel;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using OpenXmlPowerTools;
- using Org.BouncyCastle.Ocsp;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Context.Exception;
- using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
- using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
- using TEAMModelOS.SDK.Extension.DataResult.RequestData;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- namespace HiTeachCE.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class OrganizationController : BaseController
- {
- public OrganizationService organizationService;
- public MemberService memberService;
- public LecturerService lecturerService;
- public ActivationCodeService activationCodeService;
- public OrganizationController(OrganizationService organization, MemberService member, LecturerService lecturer, ActivationCodeService activationCode)
- {
- organizationService = organization;
- memberService = member;
- lecturerService = lecturer;
- activationCodeService = activationCode;
- }
- /// <summary>
- /// 获取组织列表
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("list")]
- [Authorize(Policy =Constant.Role_RootAdmin)]
- public BaseJosnRPCResponse List(PaginationJosnRPCRequest<Dictionary<string, string>> request)
- {
- string role = GetLoginUser(JwtClaimTypes.Role);
- Dictionary<string, object> extend = new Dictionary<string, object>();
- JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
- List<Organization> organizations = new List<Organization>();
- if (role.Contains(Constant.Role_Root))
- {
- Expression<Func<Organization, bool>> linq = null;
- linq = f => 1 == 1;
- if (request.@params.data.TryGetValue("code", out string code) && !string.IsNullOrEmpty(code))
- {
- linq = linq.And(m => m.code == code);
- }
- if (request.@params.data.TryGetValue("name", out string name) && !string.IsNullOrEmpty(name))
- {
- linq = linq.And(m => m.name.Contains(name));
- }
- if (request.@params.data.TryGetValue("id", out string id) && !string.IsNullOrEmpty(id))
- {
- linq = linq.And(m => m.id == id);
- }
- if (linq != null)
- {
- Expression<Func<Organization, object>> order = null;
- order = o => o.createTime;
- organizations = organizationService.GetPageList(linq, request.@params.page, order, OrderByType.Desc);
- extend.Add("manager", organizations.Select(x => x.code).ToList());
- }
- else {
- Expression<Func<Organization, object>> order = null;
- order = o => o.createTime;
- linq = m => 1 == 1;
- organizations = organizationService.GetPageList(linq, request.@params.page, order, OrderByType.Desc);
- extend.Add("manager", organizations.Select(x => x.code).ToList());
- }
- }
- else {
- string unionid = GetLoginUser(JwtClaimTypes.Id);
- Expression<Func<Member, bool>> mlinq = null;
- mlinq = m => m.unionid == unionid;
- List<Member> members= memberService.GetList(mlinq);
- if (members.IsNotEmpty()) {
- Expression<Func<Organization, object>> olinq = null;
- olinq = m => m.code;
- organizations = organizationService.GetListIn(olinq,members.Select(x=>x.orgCode).ToArray());
- request.@params.page.total = organizations.Count;
- request.@params.page.totalPage =1;
- request.@params.page.currPage = 1;
- request.@params.page.pageSize = organizations.Count;
- }
- extend.Add("manager", members.Where(m => m.admin == 1).Select(x => x.orgCode).ToList());
- }
- if (organizations.IsNotEmpty()) {
- Expression<Func<ActivationCode, object>> linq = null;
- linq = o => o.orgCode;
- List<ActivationCode> activationCodes = activationCodeService.GetListIn(linq, organizations.Select(x => x.code).ToArray());
- activationCodes.ForEach(x => { x.cdkey = ""; });
- extend.Add("Activation", activationCodes);
- }
- return builder.Data(organizations).Page(request.@params.page).Extend(extend).build();
- }
- /// <summary>
- /// 更新组织
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("update")]
- [Authorize(Policy = Constant.Role_RootAdmin)]
- public BaseJosnRPCResponse Update(JosnRPCRequest<Organization> request)
- {
- string unionid = GetLoginUser(JwtClaimTypes.Id);
- string role = GetLoginUser(JwtClaimTypes.Role);
- JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
- Organization organization = organizationService.GetById(request.@params.id);
- bool b = false;
- if (role.Contains(Constant.Role_Root))
- {
- if (organization != null)
- {
- request.@params.code = organization.code;
- request.@params.type = organization.type;
- b = organizationService.Update(request.@params);
- }
- }
- else {
- Expression<Func<Member, bool>> mlinq = null;
- mlinq = m => m.orgCode == request.@params.code && m.unionid == unionid;
- List<Member> members = memberService.GetList(mlinq);
- if (members.IsNotEmpty() && members[0].admin == 1 && organization != null)
- {
- request.@params.code = organization.code;
- request.@params.type = organization.type;
- b = organizationService.Update(request.@params);
- }
- else {
- throw new BizException("登录管理员不能管理该组织机构", 2);
- }
- }
- return builder.Data(b).build();
- }
- /// <summary>
- /// 添加组织
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("add")]
- [Authorize(Policy = Constant.Role_Root)]
- public BaseJosnRPCResponse Add(JosnRPCRequest<OrgDto> request)
- {
- JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
- Expression<Func<Lecturer, bool>> linq = null;
- linq = m => m.cellphone == request.@params.adminCellphone;
- List<Lecturer> lecturers = lecturerService.GetList(linq);
- Lecturer lecturer = null;
- if (!lecturers.IsNotEmpty())
- {
- //新增组织机构管理员
- Random random = new Random();
- string seed = new string(Constant.az09);
- string pfx = "";
- for (int i = 0; i < 4; i++)
- {
- string c = seed.ToCharArray()[random.Next(0, seed.Length)] + "";
- seed.Replace(c, "");
- pfx = pfx + c;
- }
- lecturer = new Lecturer
- {
- id = Guid.NewGuid().ToString(),
- unionid = Guid.NewGuid().ToString("N"),
- username = request.@params.adminCellphone + "手机用户",
- password = "",
- account = "hitmd-" + request.@params.adminCellphone.Substring(request.@params.adminCellphone.Length - 4, 4) + "#" + pfx,
- areaCode = "86",
- registerTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),
- status = 1,
- setaccount = 0,
- cellphone = request.@params.adminCellphone,
- avatar = "https://cdhabook.teammodel.cn/avatar/usertile" + random.Next(10, 44) + ".png"
- };
- lecturerService.Insert(lecturer);
- }
- else
- {
- lecturer = lecturers[0];
- }
- Expression<Func<Member, bool>> mlinq = null;
- mlinq = m => m.unionid == lecturer.unionid;
- List<Member> members = memberService.GetList(mlinq);
- if (members.IsNotEmpty())
- {
- //如果存在个人组织 则判断个人是管理员
- if (request.@params.orgType == 2)
- {
- string[] orgs = members.Where(x => x.admin == 1).Select(x => x.orgCode).ToArray();
- Expression<Func<Organization, object>> olinq = null;
- olinq = o => o.code;
- Expression<Func<Organization, bool>> whereExpression = null;
- whereExpression = w => w.type == 2;
- List<Organization> organizations = organizationService.GetListWhereIn(whereExpression, olinq, orgs);
- if (organizations.IsNotEmpty())
- {
- throw new BizException("该手机号已经授权个人用户!", 2);
- }
- }
- }
- long time = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
- Organization organization = new Organization
- {
- id = Guid.NewGuid().ToString(),
- code = Guid.NewGuid().ToString("N"),
- name = request.@params.orgName,
- type = request.@params.orgType,
- status = 1,
- createTime = time
- };
- Random rdid = new Random();
- string cdkey = "";
- for (int i = 0; i < 25; i++)//从数组随机抽取字符组成新的字符生成机器三
- {
- cdkey += Constant.az09[rdid.Next(0, Constant.az09.Length)];
- if (i != 24 && (i + 1) % 5 == 0)
- {
- cdkey += "-";
- }
- }
- ActivationCode activationCode = new ActivationCode
- {
- id = Guid.NewGuid().ToString(),
- cdkey = cdkey.ToUpper(),
- maximum = request.@params.maximum,
- orgCode = organization.code,
- createTime = time,
- clientId = Guid.NewGuid().ToString("N"),
- secret = Guid.NewGuid().ToString("N"),
- status = 1
- };
- if (request.@params.expires > 0)
- {
- activationCode.expires = time + request.@params.expires * 60 * 60 * 24;
- }
- else
- {
- activationCode.expires = time;
- }
- Member member = new Member
- {
- id = Guid.NewGuid().ToString(),
- orgCode = organization.code,
- admin = 1,
- // expires = activationCode.expires,
- status = 1,
- unionid = lecturer.unionid,
- createTime=time
- };
-
- bool f = organizationService.Insert(organization);
- if (f) {
- f = memberService.Insert(member);
- }
- if (f)
- {
- f = activationCodeService.Insert(activationCode);
- }
- if (f)
- {
- var data = new { organization, activationCode, member };
- return builder.Data(data).build();
- }
- else {
- throw new BizException("创建失败!", 2);
- }
- }
- }
- public class OrgDto
- {
- [Required(ErrorMessage = "组织名称必须填写")]
- public string orgName { get; set; }
- [Required(ErrorMessage = "组织类型必须填写")]
- [Range(1, 2, ErrorMessage = "请输入1~2的整数")]
- public int orgType { get; set; }
- [Required(ErrorMessage = "组织管理员手机号必须填写")]
- public string adminCellphone { get; set; }
- [Required(ErrorMessage = "授权上限必须填写")]
- [Range(1, 1000, ErrorMessage = "请输入1~1000的整数")]
- public int maximum { get; set; }
- /// <summary>
- /// 时长 ,大于0 按天计算
- /// </summary>
- [Required(ErrorMessage = "授权时限必须填写")]
- [Range(0, 3650, ErrorMessage = "请输入-1~3650的整数")]
- public int expires { get; set; }
- }
- }
|