TriggerSurvey.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using Azure.Storage.Blobs.Models;
  4. using Azure.Storage.Sas;
  5. using Microsoft.Azure.Documents;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Net.Http;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK;
  15. using TEAMModelOS.SDK.Models;
  16. using TEAMModelOS.SDK.Models.Cosmos;
  17. using TEAMModelOS.SDK.Models.Cosmos.Common;
  18. using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
  19. using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
  20. using TEAMModelOS.SDK.Models.Service;
  21. using HTEXLib.COMM.Helpers;
  22. using Microsoft.Extensions.Configuration;
  23. using System.Linq;
  24. using TEAMModelOS.SDK.Models.Service.BI;
  25. using Newtonsoft.Json;
  26. namespace TEAMModelOS.FunctionV4
  27. {
  28. public class TriggerSurvey
  29. {
  30. public static async Task Trigger(CoreAPIHttpService _coreAPIHttpService,AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  31. CosmosClient client, JsonElement input, TriggerData tdata, AzureRedisFactory _azureRedis, IConfiguration _configuration)
  32. {
  33. try
  34. {
  35. if ((tdata.status != null && tdata.status.Value == 404) )
  36. {
  37. await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(tdata.id, new PartitionKey(tdata.code));
  38. ActivityList data = input.ToObject<ActivityList>();
  39. //await IESActivityService.DeleteActivity(_coreAPIHttpService, client, _dingDing, data);
  40. _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Record:{tdata.id}");
  41. _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Submit:{tdata.id}");
  42. var table_cancel = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  43. List<ChangeRecord> records = await table_cancel.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id } });
  44. foreach (var record in records)
  45. {
  46. try
  47. {
  48. await table_cancel.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
  49. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
  50. }
  51. catch (Exception)
  52. {
  53. continue;
  54. }
  55. }
  56. await BIStats.SetTypeAddStats(client, _dingDing, tdata.school, "Survey", -1, careDate: tdata.startTime);//BI统计增/减量
  57. return;
  58. }
  59. var adid = tdata.id;
  60. var adcode = "";
  61. string blobcntr = null;
  62. if (tdata.scope.Equals("school"))
  63. {
  64. adcode = $"Activity-{tdata.school}";
  65. blobcntr = tdata.school;
  66. }
  67. else
  68. {
  69. adcode = $"Activity-{tdata.creatorId}";
  70. blobcntr = tdata.creatorId;
  71. }
  72. Survey survey = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Survey>(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  73. if (survey != null)
  74. {
  75. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  76. string PartitionKey = string.Format("{0}{1}{2}", survey.code, "-", survey.progress);
  77. List<ChangeRecord> changeRecords = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } });
  78. switch (survey.progress)
  79. {
  80. case "pending":
  81. var messageSurvey = new ServiceBusMessage(new { tdata.id, progress = "going", tdata.code }.ToJsonString());
  82. messageSurvey.ApplicationProperties.Add("name", "Survey");
  83. if (changeRecords.Count > 0)
  84. {
  85. try
  86. {
  87. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  88. }
  89. catch (Exception)
  90. {
  91. }
  92. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  93. changeRecords[0].sequenceNumber = start;
  94. await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  95. }
  96. else
  97. {
  98. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  99. ChangeRecord changeRecord = new()
  100. {
  101. RowKey = tdata.id,
  102. PartitionKey = PartitionKey,
  103. sequenceNumber = start,
  104. msgId = messageSurvey.MessageId
  105. };
  106. await table.Save<ChangeRecord>(changeRecord);
  107. }
  108. break;
  109. case "going":
  110. List<(string pId, List<string> gid)> ps = new List<(string pId, List<string> gid)>();
  111. if (survey.groupLists.Count > 0)
  112. {
  113. var group = survey.groupLists;
  114. foreach (var gp in group)
  115. {
  116. foreach (KeyValuePair<string, List<string>> pp in gp)
  117. {
  118. ps.Add((pp.Key, pp.Value));
  119. }
  120. }
  121. }
  122. List<string> classes = ExamService.getClasses(survey.classes, survey.stuLists);
  123. (List<RMember> tmdIds, List<RGroupList> classLists) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, classes, survey.school);
  124. var addStudentsCls = tmdIds.FindAll(x => x.type == 2);
  125. var addTmdidsCls = tmdIds.FindAll(x => x.type == 1);
  126. #if DEBUG
  127. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}问卷调查{tdata.id}写入学生表作为活动列表!", GroupNames.醍摩豆服務運維群組);
  128. #endif
  129. List<StuActivity> stuActivities = new List<StuActivity>();
  130. List<StuActivity> tmdActivities = new List<StuActivity>();
  131. List<StuActivity> tchActivities = new List<StuActivity>();
  132. /*
  133. List<string> sub = new();
  134. if (survey.tchLists.Count == 0) {
  135. if (survey.targets.Count > 0)
  136. {
  137. foreach (var course in survey.targets)
  138. {
  139. var info = course.ToObject<List<string>>();
  140. if (info.Count > 1)
  141. {
  142. sub.Add(info[0]);
  143. }
  144. }
  145. }
  146. }
  147. if (addTmdidsCls.IsNotEmpty())
  148. {
  149. addTmdidsCls.ForEach(x =>
  150. {
  151. HashSet<string> classIds = new HashSet<string>();
  152. classLists.ForEach(z => {
  153. z.members.ForEach(y => {
  154. if (y.id.Equals(x.id)&& y.type==1)
  155. {
  156. classIds.Add(z.id);
  157. }
  158. });
  159. });
  160. tmdActivities.Add(new StuActivity
  161. {
  162. pk = "Activity",
  163. id = survey.id,
  164. code = $"Activity-{x.id}",
  165. type = "Survey",
  166. name = survey.name,
  167. startTime = survey.startTime,
  168. endTime = survey.endTime,
  169. scode = survey.code,
  170. scope = survey.scope,
  171. school = survey.school,
  172. creatorId = survey.creatorId,
  173. subjects = sub,
  174. blob = survey.blob,
  175. owner = survey.owner,
  176. isSub = survey.isSub,
  177. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  178. taskStatus = -1,
  179. classIds = classIds.ToList()
  180. });
  181. });
  182. }
  183. if (addStudentsCls.IsNotEmpty())
  184. {
  185. addStudentsCls.ForEach(x =>
  186. {
  187. HashSet<string> classIds = new HashSet<string>();
  188. classLists.ForEach(z => {
  189. z.members.ForEach(y => {
  190. if (y.id.Equals(x.id)&& y.code.Equals(survey.school) && y.type == 2)
  191. {
  192. classIds.Add(z.id);
  193. }
  194. });
  195. });
  196. stuActivities.Add(new StuActivity
  197. {
  198. pk = "Activity",
  199. id = survey.id,
  200. code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
  201. type = "Survey",
  202. name = survey.name,
  203. startTime = survey.startTime,
  204. endTime = survey.endTime,
  205. scode = survey.code,
  206. scope = survey.scope,
  207. school = survey.school,
  208. creatorId = survey.creatorId,
  209. subjects = sub,
  210. blob = survey.blob,
  211. owner = survey.owner,
  212. isSub = survey.isSub,
  213. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  214. taskStatus = -1,
  215. classIds = classIds.ToList()
  216. });
  217. });
  218. }
  219. */
  220. (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, survey.tchLists, survey.school, ps);
  221. (string standard, List<string> tmdids, string school, List<string> update, int statistics) list = (null, null, null, new List<string> { StatisticsService.TeacherSurvey }, 0);
  222. if (tchList.IsNotEmpty())
  223. {
  224. list.tmdids = tchList.Select(x => x.id).ToList();
  225. School school = null;
  226. if (!string.IsNullOrEmpty(survey.school))
  227. {
  228. school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(survey.school, new Azure.Cosmos.PartitionKey("Base"));
  229. list.school = school.id;
  230. list.standard = school.standard;
  231. }
  232. tchList.ForEach(x =>
  233. {
  234. HashSet<string> classIds = new HashSet<string>();
  235. classInfos.ForEach(z =>
  236. {
  237. z.members.ForEach(y =>
  238. {
  239. if (y.id.Equals(x.id) && y.type == 1)
  240. {
  241. classIds.Add(z.id);
  242. }
  243. });
  244. });
  245. tchActivities.Add(new StuActivity
  246. {
  247. pk = "Activity",
  248. id = survey.id,
  249. code = $"Activity-{x.id}",
  250. type = "Survey",
  251. name = survey.name,
  252. startTime = survey.startTime,
  253. endTime = survey.endTime,
  254. scode = survey.code,
  255. scope = survey.scope,
  256. school = survey.school,
  257. creatorId = survey.creatorId,
  258. subjects = new List<string> { "" },
  259. blob = survey.blob,
  260. owner = survey.owner,
  261. isSub = survey.isSub,
  262. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  263. taskStatus = -1,
  264. classIds = classIds.ToList()
  265. });
  266. });
  267. }
  268. await IESActivityService.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, tchActivities);
  269. await StatisticsService.SendServiceBus(list, _configuration, _serviceBus, client);
  270. //向学生或醍摩豆账号发起通知
  271. #region
  272. //Notice notice = new Notice()
  273. //{
  274. // creation = survey.startTime,
  275. // expire = survey.endTime,
  276. // creatorId = survey.creatorId,
  277. // stuids = students,
  278. // tmdids = tmdids,
  279. // type = "notice",//问卷参加参加通知
  280. // priority = "normal",
  281. // //data = new { }.ToJsonString()
  282. // msgId = survey.id,
  283. // school = survey.school,
  284. // scope = survey.scope,
  285. // body = new Body { sid = survey.id, scode = survey.code, spk = survey.pk, biztype = "survey-join" }
  286. //};
  287. //var messageBlob = new ServiceBusMessage(notice.ToJsonString());
  288. //messageBlob.ApplicationProperties.Add("name", "Notice");
  289. //await _serviceBus.GetServiceBusClient().SendMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageBlob);
  290. #endregion
  291. #if DEBUG
  292. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}问卷调查{tdata.id}写入完成!", GroupNames.醍摩豆服務運維群組);
  293. #endif
  294. var messageSurveyEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
  295. messageSurveyEnd.ApplicationProperties.Add("name", "Survey");
  296. string pkey = string.Format("{0}{1}{2}", survey.code, "-", "pending");
  297. await table.DeleteSingle<ChangeRecord>(pkey, tdata.id);
  298. if (changeRecords.Count > 0)
  299. {
  300. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  301. try
  302. {
  303. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
  304. }
  305. catch (Exception)
  306. {
  307. }
  308. changeRecords[0].sequenceNumber = end;
  309. await table.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  310. }
  311. else
  312. {
  313. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  314. ChangeRecord changeRecord = new ChangeRecord
  315. {
  316. RowKey = tdata.id,
  317. PartitionKey = PartitionKey,
  318. sequenceNumber = end,
  319. msgId = messageSurveyEnd.MessageId
  320. };
  321. await table.Save<ChangeRecord>(changeRecord);
  322. }
  323. #if DEBUG
  324. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}问卷调查{tdata.id}将于:{tdata.endTime}完成并结算!", GroupNames.醍摩豆服務運維群組);
  325. #endif
  326. break;
  327. case "finish":
  328. #if DEBUG
  329. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}问卷调查{tdata.id}开始结算{tdata.endTime}!", GroupNames.醍摩豆服務運維群組);
  330. #endif
  331. var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Survey:Record:{survey.id}");
  332. var submits = await _azureRedis.GetRedisClient(8).SetMembersAsync($"Survey:Submit:{survey.id}");
  333. List<dynamic> recs = new List<dynamic>();
  334. foreach (var rcd in records)
  335. {
  336. var value = rcd.Value.ToString().ToObject<JsonElement>();
  337. recs.Add(new { index = rcd.Name.ToString(), ans = value });
  338. }
  339. List<dynamic> userids = new List<dynamic>();
  340. foreach (var submit in submits)
  341. {
  342. var value = submit.ToString();
  343. userids.Add(value);
  344. }
  345. List<QuestionRecord> questionRecords = new List<QuestionRecord>();
  346. //结算每道题的答题情况
  347. var ContainerClient = _azureStorage.GetBlobContainerClient(blobcntr);
  348. List<Task<string>> tasks = new List<Task<string>>();
  349. //获取
  350. List<SurveyRecord> surveyRecords = new List<SurveyRecord>();
  351. try
  352. {
  353. List<string> items = await ContainerClient.List($"survey/{survey.id}/urecord");
  354. foreach (string item in items)
  355. {
  356. var Download = await _azureStorage.GetBlobContainerClient(blobcntr).GetBlobClient(item).DownloadAsync();
  357. var json = await JsonDocument.ParseAsync(Download.Value.Content);
  358. var Record = json.RootElement.ToObject<SurveyRecord>();
  359. surveyRecords.Add(Record);
  360. }
  361. #if DEBUG
  362. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}问卷调查问题结算数据{surveyRecords.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  363. #endif
  364. for (int index = 0; index < survey.answers.Count; index++)
  365. {
  366. string url = $"{survey.id}/qrecord/{index}.json";
  367. QuestionRecord question = new QuestionRecord() { index = index };
  368. foreach (SurveyRecord record in surveyRecords)
  369. {
  370. if (record.ans.Count == survey.answers.Count)
  371. {
  372. foreach (var an in record.ans[index])
  373. {
  374. //
  375. if (question.opt.ContainsKey(an))
  376. {
  377. if (question.opt[an] != null)
  378. {
  379. question.opt[an].Add(record.userid);
  380. }
  381. else
  382. {
  383. question.opt[an] = new HashSet<string>() { record.userid };
  384. }
  385. }
  386. else
  387. {
  388. if (survey.answers[index].Contains(an))
  389. {
  390. //如果是客观题code
  391. question.opt.Add(an, new HashSet<string> { record.userid });
  392. }
  393. else
  394. {
  395. //如果不是客观code
  396. question.other[record.userid] = an;
  397. }
  398. }
  399. }
  400. }
  401. }
  402. questionRecords.Add(question);
  403. tasks.Add(_azureStorage.GetBlobContainerClient(blobcntr).UploadFileByContainer(question.ToJsonString(), "survey", url));
  404. }
  405. await Task.WhenAll(tasks);
  406. }
  407. catch (Exception ex)
  408. {
  409. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}问卷调查问题结算异常{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  410. }
  411. List<AnswerRecord> answerRecords = new List<AnswerRecord>();
  412. if (questionRecords.IsNotEmpty())
  413. {
  414. foreach (var questionRecord in questionRecords)
  415. {
  416. AnswerRecord answerRecord = new AnswerRecord();
  417. answerRecord.index = questionRecord.index.ToString();
  418. foreach (var opt in questionRecord.opt)
  419. {
  420. if (!answerRecord.ans.ContainsKey(opt.Key))
  421. {
  422. answerRecord.ans[opt.Key] = 0;
  423. }
  424. answerRecord.ans[opt.Key] += opt.Value.Count;
  425. }
  426. answerRecords.Add(answerRecord);
  427. }
  428. //string resultJson = JsonConvert.SerializeObject(answerRecords, Formatting.Indented);
  429. }
  430. var cods = new { records = answerRecords, userids, question = questionRecords, urecord = surveyRecords };
  431. //问卷整体情况
  432. await _azureStorage.GetBlobContainerClient(blobcntr).UploadFileByContainer(cods.ToJsonString(), "survey", $"{survey.id}/record.json");
  433. List<(string pId, List<string> gid)> gls = new List<(string pId, List<string> gid)>();
  434. if (survey.groupLists.Count > 0)
  435. {
  436. var group = survey.groupLists;
  437. foreach (var gp in group)
  438. {
  439. foreach (KeyValuePair<string, List<string>> pp in gp)
  440. {
  441. gls.Add((pp.Key, pp.Value));
  442. }
  443. }
  444. }
  445. //处理问卷调查活动结束统计账户信息
  446. List<FMember> idsList = await GroupListService.GetFinishMemberInfo(_coreAPIHttpService, client, _dingDing, survey.school, survey.classes, survey.stuLists, survey.tchLists,gls);
  447. survey.staffIds = idsList;
  448. if (string.IsNullOrEmpty(survey.recordUrl))
  449. {
  450. survey.recordUrl = $"/survey/{survey.id}/record.json";
  451. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<Survey>(survey, survey.id, new Azure.Cosmos.PartitionKey(survey.code));
  452. }
  453. else
  454. {
  455. _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Record:{survey.id}");
  456. _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Submit:{survey.id}");
  457. break;
  458. }
  459. string pk = string.Format("{0}{1}{2}", survey.code, "-", "going");
  460. await table.DeleteSingle<ChangeRecord>(pk, tdata.id);
  461. //更新结束状态
  462. //data.progress = "finish";
  463. //if (survey.scope .Equals("school"))
  464. //{
  465. // await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
  466. //}
  467. //else if (survey.scope .Equals("private"))
  468. //{
  469. // await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
  470. //}
  471. break;
  472. }
  473. }
  474. }
  475. catch (CosmosException e)
  476. {
  477. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}", GroupNames.醍摩豆服務運維群組);
  478. }
  479. catch (Exception ex)
  480. {
  481. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}问卷调查{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  482. }
  483. }
  484. }
  485. /**
  486. * {survey.id}/qrecord/{index}.json
  487. {
  488. "opt": {
  489. "A": [
  490. "userid1",
  491. "userid2",
  492. "userid3"
  493. ],
  494. "B": [
  495. "userid1",
  496. "userid2",
  497. "userid3"
  498. ]
  499. },
  500. "other": {
  501. "userid1": "建议XXXX1",
  502. "userid2": "建议XXXX2"
  503. }
  504. }
  505. **/
  506. }