CrazyIter_Bin 2 anni fa
parent
commit
e58535958f

+ 52 - 0
TEAMModelOS.SDK/Models/Cosmos/School/Elegant.cs

@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace TEAMModelOS.SDK.Models
+{
+    public class Elegant : CosmosEntity
+    {
+        public string title { get; set; }
+        public string content { get; set; }
+        public string school { get; set; }
+        /// <summary>
+        /// 发布状态
+        /// </summary>
+        [Required(ErrorMessage = "scope 必须设置")]
+        public string scope { get; set; }
+
+        [Required(ErrorMessage = "creatorId 必须设置")]
+        public string creatorId { get; set; }
+        /// <summary>
+        /// 发布状态
+        /// </summary>
+        [Required(ErrorMessage = "publish 必须设置")]
+        public int publish { get; set; } = 0;
+        /// <summary>
+        /// 开始时间
+        /// </summary>
+        public long startTime { get; set; }
+        /// <summary>
+        /// 创建时间
+        /// </summary>
+        public long createTime { get; set; }
+        /// <summary>
+        /// 结束时间
+        /// </summary>
+        public long endTime { get; set; }
+        /// <summary>
+        /// 结束时间
+        /// </summary>
+        public bool autoDelete { get; set; } = false;
+
+        /// <summary>
+        /// school, course,class
+        /// </summary>
+        public string type { get; set; }
+        public List<string> classes { get; set; } = new List<string>();
+        public List<Attachment> attachments { get; set; } = new List<Attachment>();
+    }
+}

+ 312 - 0
TEAMModelOS/Controllers/School/ElegantController.cs

@@ -0,0 +1,312 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Dynamic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+using Azure;
+using Azure.Cosmos;
+using Azure.Storage.Sas;
+using HTEXLib.COMM.Helpers;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Cryptography.KeyDerivation;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Options;
+using TEAMModelOS.Filter;
+using TEAMModelOS.Models;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Extension;
+using TEAMModelOS.SDK.Models;
+using TEAMModelOS.SDK.Models.Cosmos;
+using TEAMModelOS.SDK.Models.Cosmos.Common;
+namespace TEAMModelOS.Controllers.School
+{
+    [Route("school/elegant")]
+    [ApiController]
+
+    public class ElegantController : ControllerBase
+    {
+        private readonly AzureCosmosFactory _azureCosmos;
+        private readonly AzureStorageFactory _azureStorage;
+        private readonly DingDing _dingDing;
+        private readonly Option _option;
+        public ElegantController(
+           AzureCosmosFactory azureCosmos,
+           AzureStorageFactory azureStorage,
+           DingDing dingDing,
+           IOptionsSnapshot<Option> option
+           )
+        {
+            _azureCosmos = azureCosmos;
+            _azureStorage = azureStorage;
+            _dingDing = dingDing;
+            _option = option?.Value;
+        }
+        /// <summary>
+        /// 新增 或 修改
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("upsert")]
+        [Authorize(Roles = "IES")]
+        [AuthToken(Roles = "teacher,admin")]
+        public async Task<IActionResult> Upsert(Elegant request)
+        {
+            try
+            {
+                var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
+                //新增
+
+                var client = _azureCosmos.GetCosmosClient();
+                request.school = school;
+                request.code = $"Elegant-{school}";
+                request.pk = "Elegant";
+                request.ttl = -1;
+                request.scope = "school";
+                long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
+                if (request.autoDelete)
+                {
+                    int time = (int)(request.endTime - now) / 1000;
+                    if (time > 0)
+                    {
+                        request.ttl = time;
+                    }
+                }
+                request.createTime = now;
+                request.creatorId = userid;
+                //如果设置的时间是小于当前时间则立即发布
+                if (request.startTime <= 0)
+                {
+                    request.startTime = now;
+                }
+                if (request.publish == 1)
+                {
+                    if (request.endTime <= request.startTime)
+                    {
+                        return BadRequest("结束时间必须大于开始时间");
+                    }
+                }
+                if (string.IsNullOrEmpty(request.id))
+                {
+                    request.id = Guid.NewGuid().ToString();
+
+                    request = await client.GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(request, new PartitionKey($"{request.code}"));
+                }
+                else
+                {
+                    var response = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemStreamAsync(request.id, new PartitionKey($"{request.code}"));
+
+                    if (response.Status == 200)
+                    {
+
+                        request = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(request, request.id, new PartitionKey($"{request.code}"));
+                    }
+                    else
+                    {
+                        request = await client.GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(request, new PartitionKey($"{request.code}"));
+                    }
+                }
+                return Ok(new { elegant = request });
+            }
+            catch (Exception e)
+            {
+                await _dingDing.SendBotMsg($"OS,{_option.Location},school/Elegant/upsert()\n{e.Message}\n{e.StackTrace}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
+                return BadRequest(e.StackTrace);
+            }
+        }
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("find")]
+        [Authorize(Roles = "IES")]
+        [AuthToken(Roles = "teacher,admin,student")]
+        public async Task<IActionResult> Find(JsonElement request)
+        {
+            try
+            {
+                var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
+                //必须有学校或者教师编码
+             
+                request.TryGetProperty("admin", out JsonElement admin);
+                string stimestampsql = "";
+                string etimestampsql = "";
+                if (!$"{admin}".Equals("1"))
+                {
+                    var stimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
+                    if (request.TryGetProperty("stime", out JsonElement stime))
+                    {
+                        if (long.TryParse($"{stime}", out long data))
+                        {
+                            stimestamp = data;
+                        }
+                    }
+                    stimestampsql = $" and c.startTime <= {stimestamp} ";
+                    //默认当前时间
+                    var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
+                    if (request.TryGetProperty("etime", out JsonElement etime))
+                    {
+                        if (long.TryParse($"{etime}", out long data))
+                        {
+                            etimestamp = data;
+                        }
+                    }
+                    etimestampsql = $" and c.endTime >= {etimestamp} ";
+
+                }
+                var publishsql = "";
+                if (request.TryGetProperty("publish", out JsonElement publish))
+                {
+
+                    if (!publish.ValueKind.Equals(JsonValueKind.Undefined) && !publish.ValueKind.Equals(JsonValueKind.Null) && publish.ValueKind.Equals(JsonValueKind.Number))
+                    {
+                        publishsql = $" and c.publish={publish} ";
+                    }
+                }
+
+                var creatorIdSql = "";
+                if (request.TryGetProperty("creatorId", out JsonElement creatorId))
+                {
+
+                    if (!publish.ValueKind.Equals(JsonValueKind.Undefined) && !publish.ValueKind.Equals(JsonValueKind.Null) && publish.ValueKind.Equals(JsonValueKind.String))
+                    {
+                        publishsql = $" and c.creatorId='{creatorId}' ";
+                    }
+                }
+
+                var typesql = "";
+                if (request.TryGetProperty("type", out JsonElement type))
+                {
+
+                    if (!type.ValueKind.Equals(JsonValueKind.Undefined) && !type.ValueKind.Equals(JsonValueKind.Null) && type.ValueKind.Equals(JsonValueKind.String))
+                    {
+                        typesql = $" and c.type='{type}' ";
+                    }
+                }
+                string joinSqlClasses = "";
+                string andSqlClasses = "";
+                if (request.TryGetProperty("classes", out JsonElement _classes))
+                {
+
+                    if (_classes.ValueKind is JsonValueKind.Array)
+                    {
+                        List<string> subjects = _classes.ToObject<List<string>>();
+                        if (subjects.IsNotEmpty())
+                        {
+                            joinSqlClasses = " join A2 in c.classes ";
+                            List<string> sqlList = new List<string>();
+                            subjects.ForEach(x => { sqlList.Add($" '{x}' "); });
+                            string sql = string.Join(" , ", sqlList);
+                            andSqlClasses = $" and A2 in ({sql}) ";
+                        }
+                    }
+                }
+                List<Elegant> elegants = new List<Elegant>();
+                var client = _azureCosmos.GetCosmosClient();
+                var query = $"select distinct value(c) from c {joinSqlClasses} where  1=1 {etimestampsql} {stimestampsql} {publishsql}  {typesql} {andSqlClasses}  {creatorIdSql} ";
+
+                await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIterator<Elegant>(queryText: query,
+                     requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Elegant-{school}") }))
+                {
+                    elegants.Add(item);
+                }
+                return Ok(new { elegants });
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"OS,{_option.Location},school/Elegant/find()\n", GroupNames.醍摩豆服務運維群組);
+                return BadRequest(ex.StackTrace);
+            }
+        }
+
+        /// <summary>
+        /// 
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("find-by-student")]
+        [Authorize(Roles = "IES")]
+        [AuthToken(Roles = "teacher,admin,student")]
+        public async Task<IActionResult> FindByStudent(JsonElement request)
+        {
+            try
+            {
+                var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
+                var publishsql = $" and c.publish=1 ";
+                string joinSqlClasses = "";
+                string andSqlClasses = "";
+                if (request.TryGetProperty("classes", out JsonElement _classes))
+                {
+
+                    if (_classes.ValueKind is JsonValueKind.Array)
+                    {
+                        List<string> subjects = _classes.ToObject<List<string>>();
+                        if (subjects.IsNotEmpty())
+                        {
+                            joinSqlClasses = " join A2 in c.classes ";
+                            List<string> sqlList = new List<string>();
+                            subjects.ForEach(x => { sqlList.Add($" '{x}' "); });
+                            string sql = string.Join(" , ", sqlList);
+                            andSqlClasses = $" and A2 in ({sql}) ";
+                        }
+                    }
+                }
+                else
+                {
+                    return BadRequest();
+                }
+                List<Elegant> elegants = new List<Elegant>();
+                var client = _azureCosmos.GetCosmosClient();
+                var query = $"select distinct value(c) from c {joinSqlClasses} where  1=1 {publishsql}    {andSqlClasses} ";
+                if (!string.IsNullOrWhiteSpace(school))
+                {
+                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIterator<Elegant>(queryText: query,
+                       requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Elegant-{school}") }))
+                    {
+                        elegants.Add(item);
+                    }
+                }
+
+                return Ok(new { elegants });
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"OS,{_option.Location},school/Elegant/find()\n", GroupNames.醍摩豆服務運維群組);
+                return BadRequest(ex.StackTrace);
+            }
+        }
+
+        /// <summary>
+        /// 删除
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("delete")]
+        [Authorize(Roles = "IES")]
+        [AuthToken(Roles = "admin",Permissions = "teacher-upd")]
+        public async Task<IActionResult> Delete(JsonElement request)
+        {
+            try
+            {
+                var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
+                if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
+                var client = _azureCosmos.GetCosmosClient();
+                await client.GetContainer(Constant.TEAMModelOS, Constant.School).DeleteItemStreamAsync(id.GetString(), new PartitionKey($"Elegant-{school}"));
+                return Ok(new { status = 200 });
+            }
+            catch (Exception e)
+            {
+                return BadRequest(e.StackTrace);
+            }
+        }
+    }
+}