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;
}
///
/// 获取组织列表
///
///
///
[HttpPost("list")]
[Authorize(Policy =Constant.Role_RootAdmin)]
public BaseJosnRPCResponse List(PaginationJosnRPCRequest> request)
{
string role = GetLoginUser(JwtClaimTypes.Role);
Dictionary extend = new Dictionary();
JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
List organizations = new List();
if (role.Contains(Constant.Role_Root))
{
Expression> 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> 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> 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> mlinq = null;
mlinq = m => m.unionid == unionid;
List members= memberService.GetList(mlinq);
if (members.IsNotEmpty()) {
Expression> 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> linq = null;
linq = o => o.orgCode;
List 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();
}
///
/// 更新组织
///
///
///
[HttpPost("update")]
[Authorize(Policy = Constant.Role_RootAdmin)]
public BaseJosnRPCResponse Update(JosnRPCRequest 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> mlinq = null;
mlinq = m => m.orgCode == request.@params.code && m.unionid == unionid;
List 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();
}
///
/// 添加组织
///
///
///
[HttpPost("add")]
[Authorize(Policy = Constant.Role_Root)]
public BaseJosnRPCResponse Add(JosnRPCRequest request)
{
JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
Expression> linq = null;
linq = m => m.cellphone == request.@params.adminCellphone;
List 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> mlinq = null;
mlinq = m => m.unionid == lecturer.unionid;
List 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> olinq = null;
olinq = o => o.code;
Expression> whereExpression = null;
whereExpression = w => w.type == 2;
List 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; }
///
/// 时长 ,大于0 按天计算
///
[Required(ErrorMessage = "授权时限必须填写")]
[Range(0, 3650, ErrorMessage = "请输入-1~3650的整数")]
public int expires { get; set; }
}
}