|
@@ -36,7 +36,7 @@ namespace TEAMModelOS.Controllers
|
|
|
/// </summary>
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
-
|
|
|
+
|
|
|
[Route("common/survey")]
|
|
|
[ApiController]
|
|
|
public class SurveyController : ControllerBase
|
|
@@ -50,10 +50,10 @@ namespace TEAMModelOS.Controllers
|
|
|
private readonly AzureStorageFactory _azureStorage;
|
|
|
private readonly IHttpClientFactory _clientFactory;
|
|
|
public IConfiguration _configuration { get; set; }
|
|
|
- public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
|
|
|
- AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, IHttpClientFactory clientFactory,IConfiguration configuration)
|
|
|
+ public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
|
|
|
+ AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, IHttpClientFactory clientFactory, IConfiguration configuration)
|
|
|
{
|
|
|
- _snowflakeId= snowflakeId;
|
|
|
+ _snowflakeId = snowflakeId;
|
|
|
_serviceBus = serviceBus;
|
|
|
_azureCosmos = azureCosmos;
|
|
|
_dingDing = dingDing;
|
|
@@ -74,13 +74,15 @@ namespace TEAMModelOS.Controllers
|
|
|
[HttpPost("upsert")]
|
|
|
[Authorize(Roles = "IES")]
|
|
|
[AuthToken(Roles = "teacher,admin", Permissions = "schoolAc-upd")]
|
|
|
- public async Task<IActionResult> Upsert(Survey request) {
|
|
|
- try {
|
|
|
+ public async Task<IActionResult> Upsert(Survey request)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
var client = _azureCosmos.GetCosmosClient();
|
|
|
request.code = request.pk + "-" + request.code;
|
|
|
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
request.createTime = now;
|
|
|
- request.ttl = -1;
|
|
|
+ request.ttl = -1;
|
|
|
//如果设置的时间是小于当前时间则立即发布
|
|
|
if (request.startTime <= 0)
|
|
|
{
|
|
@@ -147,10 +149,10 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
using var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
var info = json.ToObject<Survey>();
|
|
|
- /* if (info.progress.Equals("going"))
|
|
|
- {
|
|
|
- return Ok(new { v = "活动正在进行中" });
|
|
|
- }*/
|
|
|
+ /* if (info.progress.Equals("going"))
|
|
|
+ {
|
|
|
+ return Ok(new { v = "活动正在进行中" });
|
|
|
+ }*/
|
|
|
if (string.IsNullOrEmpty(request.progress))
|
|
|
{
|
|
|
if (request.startTime > now)
|
|
@@ -166,7 +168,8 @@ namespace TEAMModelOS.Controllers
|
|
|
request.recordUrl = $"/survey/{request.id}/record.json";
|
|
|
request = await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(request, info.id, new PartitionKey($"{info.code}"));
|
|
|
}
|
|
|
- else {
|
|
|
+ else
|
|
|
+ {
|
|
|
if (string.IsNullOrEmpty(request.progress))
|
|
|
{
|
|
|
if (request.startTime > now)
|
|
@@ -184,12 +187,14 @@ namespace TEAMModelOS.Controllers
|
|
|
request = await client.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(request, new PartitionKey($"{request.code}"));
|
|
|
}
|
|
|
}
|
|
|
- return Ok(new { survey = request});
|
|
|
- } catch (Exception ex) {
|
|
|
+ return Ok(new { survey = request });
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
await _dingDing.SendBotMsg($"OS,{_option.Location},common/survey/save()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
|
|
|
return BadRequest(ex.StackTrace);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 查询问卷调查,用于列表,编辑,查看
|
|
@@ -210,15 +215,16 @@ namespace TEAMModelOS.Controllers
|
|
|
[AuthToken(Roles = "teacher,admin,student", Permissions = "schoolAc-read,schoolAc-upd")]
|
|
|
public async Task<IActionResult> Find(JsonElement request)
|
|
|
{
|
|
|
- try {
|
|
|
+ try
|
|
|
+ {
|
|
|
var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
|
|
|
//必须有学校或者教师编码
|
|
|
if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
//开始时间,
|
|
|
- var stimestamp ="";
|
|
|
+ var stimestamp = "";
|
|
|
if (request.TryGetProperty("stime", out JsonElement stime))
|
|
|
{
|
|
|
- if (long.TryParse($"{stime}",out long data))
|
|
|
+ if (long.TryParse($"{stime}", out long data))
|
|
|
{
|
|
|
stimestamp = $" and c.createTime >= {data} ";
|
|
|
|
|
@@ -226,10 +232,10 @@ namespace TEAMModelOS.Controllers
|
|
|
};
|
|
|
};
|
|
|
//默认当前时间
|
|
|
- var etimestamp ="";
|
|
|
+ var etimestamp = "";
|
|
|
if (request.TryGetProperty("etime", out JsonElement etime))
|
|
|
{
|
|
|
- if (long.TryParse($"{etime}",out long data))
|
|
|
+ if (long.TryParse($"{etime}", out long data))
|
|
|
{
|
|
|
etimestamp = $" and c.createTime <= {data} ";
|
|
|
};
|
|
@@ -248,7 +254,7 @@ namespace TEAMModelOS.Controllers
|
|
|
int? topcout = null;
|
|
|
if (request.TryGetProperty("count", out JsonElement jcount))
|
|
|
{
|
|
|
- if (int.TryParse($"{jcount}",out int data))
|
|
|
+ if (int.TryParse($"{jcount}", out int data))
|
|
|
{
|
|
|
topcout = data;
|
|
|
}
|
|
@@ -267,16 +273,18 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
token = continuation.GetString();
|
|
|
}
|
|
|
- };
|
|
|
+ };
|
|
|
List<Survey> surveys = new List<Survey>();
|
|
|
var client = _azureCosmos.GetCosmosClient();
|
|
|
- var query = $"select c.owner, c.id,c.name,c.code,c.startTime,c.endTime,c.progress,c.classes,c.scope,c.school,c.targetType from c where (c.status<>404 or IS_DEFINED(c.status) = false ) and c.ttl=-1 {stimestamp} {etimestamp} {progresssql } ";
|
|
|
- if (string.IsNullOrEmpty(school)) {
|
|
|
+ var query = $"select c.owner, c.id,c.name,c.code,c.startTime,c.endTime,c.progress,c.classes,c.scope,c.school,c.targetType from c where (c.status<>404 or IS_DEFINED(c.status) = false ) and c.ttl=-1 {stimestamp} {etimestamp} {progresssql} ";
|
|
|
+ if (string.IsNullOrEmpty(school))
|
|
|
+ {
|
|
|
query = $"{query} and c.scope='private' ";
|
|
|
}
|
|
|
query = $"{query} order by c.createTime desc ";
|
|
|
await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: query, continuationToken: token,
|
|
|
- requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Survey-{code}") })) {
|
|
|
+ requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Survey-{code}") }))
|
|
|
+ {
|
|
|
using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
{
|
|
@@ -329,11 +337,13 @@ namespace TEAMModelOS.Controllers
|
|
|
// }
|
|
|
//}
|
|
|
return Ok(new { surveys, token });
|
|
|
- } catch (Exception ex) {
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
await _dingDing.SendBotMsg($"OS,{_option.Location},common/survey/find()\n", GroupNames.醍摩豆服務運維群組);
|
|
|
return BadRequest(ex.StackTrace);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
///<summary>
|
|
|
/// 查询问卷调查,用于创建者列表,编辑,查看,作答人员查看
|
|
@@ -375,7 +385,8 @@ namespace TEAMModelOS.Controllers
|
|
|
|
|
|
return Ok(new { survey, status = 404 });
|
|
|
}
|
|
|
- catch (Exception ex) {
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
await _dingDing.SendBotMsg($"OS,{_option.Location},common/survey/find-id\n{ex.Message}\n{ex.StackTrace}\n{id}\n{code} ", GroupNames.醍摩豆服務運維群組);
|
|
|
return Ok(new { survey, status = 404 });
|
|
|
}
|
|
@@ -409,7 +420,7 @@ namespace TEAMModelOS.Controllers
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (survey.scope .Equals("school") && survey.school.Equals(school))
|
|
|
+ if (survey.scope.Equals("school") && survey.school.Equals(school))
|
|
|
{
|
|
|
flag = true;
|
|
|
}
|
|
@@ -418,11 +429,11 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
//使用ttl删除,并处理相关事务逻辑
|
|
|
survey.status = 404;
|
|
|
- survey = await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(survey,survey.id, new PartitionKey($"{survey.code}"));
|
|
|
+ survey = await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(survey, survey.id, new PartitionKey($"{survey.code}"));
|
|
|
_azureRedis.GetRedisClient(8).KeyDelete($"Survey:Record:{survey.id}");
|
|
|
_azureRedis.GetRedisClient(8).KeyDelete($"Survey:Submit:{survey.id}");
|
|
|
//删除blob 相关资料
|
|
|
- await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{code}".Replace("Survey-", ""), new List<string> { Path.Combine("survey" , survey.id)+"/" });
|
|
|
+ await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{code}".Replace("Survey-", ""), new List<string> { Path.Combine("survey", survey.id) + "/" });
|
|
|
//通知删除信息
|
|
|
var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "delete", root = $"survey/{survey.id}", name = $"{code}" }.ToJsonString());
|
|
|
messageBlob.ApplicationProperties.Add("name", "BlobRoot");
|
|
@@ -472,9 +483,10 @@ namespace TEAMModelOS.Controllers
|
|
|
standard = _standard != null && string.IsNullOrEmpty($"{userScope}") ? _standard.ToString() : null;
|
|
|
}
|
|
|
var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
|
|
|
- if (request.TryGetProperty("userid", out JsonElement _userid)) {
|
|
|
- (int msgid ,int taskStatus) = await ActivityStudentService.Answer(_dingDing,_option, request, _azureCosmos, _azureRedis, $"{userid}",school, _azureStorage, standard, _serviceBus, _configuration);
|
|
|
- return Ok(new { msgid , taskStatus });
|
|
|
+ if (request.TryGetProperty("userid", out JsonElement _userid))
|
|
|
+ {
|
|
|
+ (int msgid, int taskStatus) = await ActivityStudentService.Answer(_dingDing, _option, request, _azureCosmos, _azureRedis, $"{userid}", school, _azureStorage, standard, _serviceBus, _configuration);
|
|
|
+ return Ok(new { msgid, taskStatus });
|
|
|
}
|
|
|
else { return Ok(new { msgid = 0 }); }
|
|
|
}
|
|
@@ -510,14 +522,16 @@ namespace TEAMModelOS.Controllers
|
|
|
return BadRequest();
|
|
|
}
|
|
|
List<string> userids = new List<string>();
|
|
|
- var values= await _azureRedis.GetRedisClient(8).SetMembersAsync($"Survey:Submit:{id}");
|
|
|
- if (values != default && values.Length>0) {
|
|
|
- foreach(var val in values) {
|
|
|
+ var values = await _azureRedis.GetRedisClient(8).SetMembersAsync($"Survey:Submit:{id}");
|
|
|
+ if (values != default && values.Length > 0)
|
|
|
+ {
|
|
|
+ foreach (var val in values)
|
|
|
+ {
|
|
|
userids.Add(val);
|
|
|
}
|
|
|
}
|
|
|
- return Ok(new { userids = userids });
|
|
|
-
|
|
|
+ return Ok(new { userids = userids });
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -539,7 +553,7 @@ namespace TEAMModelOS.Controllers
|
|
|
[AuthToken(Roles = "teacher,admin,student", Permissions = "schoolAc-upd,schoolAc-read")]
|
|
|
public async Task<IActionResult> Answered(JsonElement request)
|
|
|
{
|
|
|
- var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
|
|
|
+ var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
|
|
|
if (!request.TryGetProperty("id", out JsonElement id))
|
|
|
{
|
|
|
return BadRequest();
|
|
@@ -550,8 +564,8 @@ namespace TEAMModelOS.Controllers
|
|
|
return BadRequest();
|
|
|
}
|
|
|
List<string> userids = new List<string>();
|
|
|
- var answered = await _azureRedis.GetRedisClient(8).SetContainsAsync($"Survey:Submit:{id}",userid);
|
|
|
-
|
|
|
+ var answered = await _azureRedis.GetRedisClient(8).SetContainsAsync($"Survey:Submit:{id}", userid);
|
|
|
+
|
|
|
return Ok(new { answered = answered });
|
|
|
|
|
|
}
|
|
@@ -590,12 +604,89 @@ namespace TEAMModelOS.Controllers
|
|
|
foreach (var rcd in records)
|
|
|
{
|
|
|
var value = rcd.Value.ToString().ToObject<JsonElement>();
|
|
|
- res.Add(new { index = rcd.Name.ToString(), ans = value }) ;
|
|
|
+ res.Add(new { index = rcd.Name.ToString(), ans = value });
|
|
|
}
|
|
|
- return Ok(new {records = res});
|
|
|
+ return Ok(new { records = res });
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /*[ProducesDefaultResponseType]
|
|
|
+ [HttpPost("draft-record")]
|
|
|
+ [Authorize(Roles = "IES")]
|
|
|
+ [AuthToken(Roles = "teacher,admin,student")]
|
|
|
+ public async Task<IActionResult> DraftRecord(JsonElement request)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //活动ID
|
|
|
+ if (!request.TryGetProperty("activityId", out JsonElement activityId))
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ //学生ID
|
|
|
+ if (!request.TryGetProperty("stuId", out JsonElement stuId))
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ if (!request.TryGetProperty("survey", out JsonElement survey))
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ //获取问卷记录
|
|
|
+ DraftSurvey draft = new()
|
|
|
+ {
|
|
|
+ id = activityId.GetString(),
|
|
|
+ ttl = -1,
|
|
|
+ code = $"DraftSurvey-{stuId}",
|
|
|
+ stuId = stuId.GetString(),
|
|
|
+ JsonElement = survey
|
|
|
+ };
|
|
|
+ await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(draft, new PartitionKey($"{draft.code}"));
|
|
|
+ return Ok(new { records = draft });
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ return Ok(new { code = 500 });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("find-draft")]
|
|
|
+ [Authorize(Roles = "IES")]
|
|
|
+ [AuthToken(Roles = "teacher,admin,student")]
|
|
|
+ public async Task<IActionResult> findDraft(JsonElement request)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //活动ID
|
|
|
+ if (!request.TryGetProperty("id", out JsonElement id))
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ if (!request.TryGetProperty("stuId", out JsonElement stuId))
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ var response = await client.GetContainer("TEAMModelOS", "Student").ReadItemStreamAsync(id.GetString(), new PartitionKey($"DraftSurvey-{stuId}"));
|
|
|
+ DraftSurvey draft = new();
|
|
|
+ if (response.Status == 200)
|
|
|
+ {
|
|
|
+ using var json = await JsonDocument.ParseAsync(response.ContentStream);
|
|
|
+ draft = json.ToObject<DraftSurvey>();
|
|
|
+ }
|
|
|
+ return Ok(new { records = draft });
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ return Ok(new { code = 500 });
|
|
|
+ }
|
|
|
+
|
|
|
+ }*/
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|