using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TEAMModelOS.Models;
using TEAMModelOS.SDK;
using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
using TEAMModelOS.SDK.DI;
using System.Text.Json;
using TEAMModelOS.SDK.Helper.Common.StringHelper;
using TEAMModelOS.Models.CommonInfo;
using Microsoft.AspNetCore.Http;
using TEAMModelOS.SDK.Extension;
using Azure.Cosmos;
using Microsoft.AspNetCore.Authentication;
using System.Text;
using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
namespace TEAMModelOS.Controllers
{
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
//[Authorize(Roles = "IES5")]
[Route("common/item")]
//[Route("api/[controller]")]
[ApiController]
public class ItemInfoController : ControllerBase
{
private readonly SnowflakeId _snowflakeId;
private readonly AzureCosmosFactory _azureCosmos;
public ItemInfoController(AzureCosmosFactory azureCosmos, SnowflakeId snowflakeId)
{
_azureCosmos = azureCosmos;
_snowflakeId = snowflakeId;
}
///
/// 批量保存题目
///
///
///
[ProducesDefaultResponseType]
[HttpPost("upsert-all")]
public async Task UpsertAll(List request)
{
// ResponseBuilder builder = ResponseBuilder.custom();
request.ForEach(x => {
if (string.IsNullOrEmpty(x.id)) {
x.id = _snowflakeId.NextId()+"";
x.code = typeof(ItemInfo).Name + "-" + x.code;
};
x.createTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
});
return Ok(await _azureCosmos.SaveOrUpdateAll(request));
}
[ProducesDefaultResponseType]
[HttpPost("upsert")]
public async Task Upsert(JsonElement request)
{
ResponseBuilder builder = ResponseBuilder.custom();
/* if (string.IsNullOrEmpty(request.id))
{
request.id = _snowflakeId.NextId() + "";
request.code = typeof(ItemInfo).Name + "-" + request.code;
};
request.createTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();
return Ok(await _azureCosmos.SaveOrUpdate(request));*/
var client = _azureCosmos.GetCosmosClient();
if (!request.TryGetProperty("itemInfo", out JsonElement item)) return BadRequest();
if (!request.TryGetProperty("option", out JsonElement option)) return BadRequest();
ItemInfo itemInfo;
itemInfo = item.ToObject();
itemInfo.ttl = -1;
itemInfo.pk = "Item";
if (option.ToString().Equals("insert"))
{
itemInfo.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
//new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
itemInfo.code = "Item-" + itemInfo.code;
var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
if (response.Status == 200)
{
return Ok();
}
else {
if (itemInfo.scope.Equals("private"))
{
itemInfo = await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
}
else
{
itemInfo = await client.GetContainer("TEAMModelOS", "School").CreateItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
}
}
}
else
{
itemInfo.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (itemInfo.scope.Equals("private"))
{
if (!itemInfo.code.Contains("Item"))
{
itemInfo.code = "Item-" + itemInfo.code;
// itemInfo = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
}
itemInfo = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
}
else
{
if (!itemInfo.code.Contains("Item"))
{
itemInfo.code = "Item-" + itemInfo.code;
//itemInfo = await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
}
itemInfo = await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
}
}
return Ok(new { itemInfo });
}
///
//获取题目摘要信息
///
///
///
[ProducesDefaultResponseType]
//[AuthToken(Roles = "teacher")]
[HttpPost("find-summary")]
public async Task FindSummary(JsonElement requert)
{
var client = _azureCosmos.GetCosmosClient();
StringBuilder sql = new StringBuilder();
sql.Append("select c.id, c.question,c.useCount,c.level,c.field,c.points,c.type,c.option,c.createTime from c ");
if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
/* if (!requert.TryGetProperty("@CURRPAGE", out JsonElement page)) return BadRequest();
if (!requert.TryGetProperty("@PAGESIZE", out JsonElement size)) return BadRequest();*/
if (!requert.TryGetProperty("@DESC", out JsonElement desc)) return BadRequest();
if (!requert.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
List