TriggerVote.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using Microsoft.Azure.Documents;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Models.Cosmos;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
  17. using TEAMModelOS.SDK.Models.Service;
  18. using HTEXLib.COMM.Helpers;
  19. using Microsoft.Extensions.Configuration;
  20. namespace TEAMModelOS.FunctionV4
  21. {
  22. public static class TriggerVote
  23. {
  24. public static async Task Trigger(CoreAPIHttpService _coreAPIHttpService, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  25. CosmosClient client, JsonElement input, TriggerData tdata, AzureRedisFactory _azureRedis, IConfiguration _configuration)
  26. {
  27. try
  28. {
  29. if ((tdata.status != null && tdata.status.Value == 404) )
  30. {
  31. await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(tdata.id, new PartitionKey(tdata.code));
  32. ActivityList data = input.ToObject<ActivityList>();
  33. await ActivityService. DeleteActivity(_coreAPIHttpService, client, _dingDing, data);
  34. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{tdata.id}");
  35. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{tdata.id}");
  36. var table_cancel = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  37. List<ChangeRecord> records = await table_cancel.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id } });
  38. foreach (var record in records)
  39. {
  40. try
  41. {
  42. await table_cancel.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
  43. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
  44. }
  45. catch (Exception)
  46. {
  47. continue;
  48. }
  49. }
  50. return;
  51. }
  52. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  53. var adid = tdata.id;
  54. var adcode = "";
  55. string blobcntr = null;
  56. if (tdata.scope.Equals("school"))
  57. {
  58. adcode = $"Activity-{tdata.school}";
  59. blobcntr = tdata.school;
  60. }
  61. else
  62. {
  63. adcode = $"Activity-{tdata.creatorId}";
  64. blobcntr = tdata.creatorId;
  65. }
  66. // await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.醍摩豆服務運維群組);
  67. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  68. if (vote != null)
  69. {
  70. string PartitionKey = string.Format("{0}{1}{2}", vote.code, "-", vote.progress);
  71. List<ChangeRecord> voteRecords = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } });
  72. switch (vote.progress)
  73. {
  74. case "pending":
  75. var messageVote = new ServiceBusMessage(new { id = tdata.id, progress = "going", code = tdata.code }.ToJsonString());
  76. messageVote.ApplicationProperties.Add("name", "Vote");
  77. if (voteRecords.Count > 0)
  78. {
  79. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  80. try {
  81. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
  82. } catch (Exception) {
  83. }
  84. voteRecords[0].sequenceNumber = start;
  85. await table.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  86. }
  87. else
  88. {
  89. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  90. ChangeRecord changeRecord = new ChangeRecord
  91. {
  92. RowKey = tdata.id,
  93. PartitionKey = PartitionKey,
  94. sequenceNumber = start,
  95. msgId = messageVote.MessageId
  96. };
  97. await table.Save<ChangeRecord>(changeRecord);
  98. }
  99. break;
  100. case "going":
  101. List<(string pId, List<string> gid)> ps = new List<(string pId, List<string> gid)>();
  102. if (vote.groupLists.Count > 0)
  103. {
  104. var group = vote.groupLists;
  105. foreach (var keys in group)
  106. {
  107. foreach (KeyValuePair<string, List<string>> pp in keys)
  108. {
  109. ps.Add((pp.Key, pp.Value));
  110. }
  111. }
  112. }
  113. List<string> classes = ExamService.getClasses(vote.classes, vote.stuLists);
  114. (List<RMember> tmdIds, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, classes, vote.school, ps);
  115. var addStudentsCls = tmdIds.FindAll(x => x.type == 2);
  116. var addTmdidsCls = tmdIds.FindAll(x => x.type == 1);
  117. //await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动" +
  118. // $"{tmdids.ToJsonString()}\n" +
  119. // $"{students.ToJsonString()}\n" +
  120. // $"{classLists.ToJsonString()}\n" +
  121. // $"{classes.ToJsonString()}\n", GroupNames.醍摩豆服務運維群組);
  122. List<string> tmds = new List<string>();
  123. if (addTmdidsCls.IsNotEmpty())
  124. {
  125. tmds.AddRange(addTmdidsCls.Select(x => x.id).ToList());
  126. }
  127. List<StuActivity> stuActivities = new List<StuActivity>();
  128. List<StuActivity> tmdActivities = new List<StuActivity>();
  129. List<StuActivity> tchActivities = new List<StuActivity>();
  130. if (tmds.IsNotEmpty())
  131. {
  132. tmds.ForEach(x =>
  133. {
  134. HashSet<string> classIds = new HashSet<string>();
  135. classLists.ForEach(z => {
  136. z.members.ForEach(y => {
  137. if (y.id.Equals(x) && y.type == 1)
  138. {
  139. classIds.Add(z.id);
  140. }
  141. });
  142. });
  143. tmdActivities.Add(new StuActivity
  144. {
  145. pk = "Activity",
  146. id = vote.id,
  147. code = $"Activity-{x}",
  148. type = "Vote",
  149. name = vote.name,
  150. startTime = vote.startTime,
  151. endTime = vote.endTime,
  152. scode = vote.code,
  153. scope = vote.scope,
  154. school = vote.school,
  155. creatorId = vote.creatorId,
  156. subjects = new List<string> { "" },
  157. blob = null,
  158. owner = vote.owner,
  159. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  160. taskStatus = -1,
  161. classIds = classIds.ToList()
  162. });
  163. });
  164. }
  165. if (addStudentsCls.IsNotEmpty())
  166. {
  167. addStudentsCls.ForEach(x =>
  168. {
  169. HashSet<string> classIds = new HashSet<string>();
  170. classLists.ForEach(z => {
  171. z.members.ForEach(y => {
  172. if (y.id.Equals(x) && y.code.Equals(vote.school) && y.type == 2)
  173. {
  174. classIds.Add(z.id);
  175. }
  176. });
  177. });
  178. stuActivities.Add(new StuActivity
  179. {
  180. pk = "Activity",
  181. id = vote.id,
  182. code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
  183. type = "Vote",
  184. name = vote.name,
  185. startTime = vote.startTime,
  186. endTime = vote.endTime,
  187. scode = vote.code,
  188. scope = vote.scope,
  189. school = vote.school,
  190. creatorId = vote.creatorId,
  191. subjects = new List<string> { "" },
  192. blob = null,
  193. owner = vote.owner,
  194. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  195. taskStatus = -1,
  196. classIds = classIds.ToList()
  197. });
  198. });
  199. }
  200. (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, vote.tchLists, vote.school, ps);
  201. (string standard, List<string> tmdids, string school, List<string> update, int statistics) list = (null, null, null, new List<string> { StatisticsService.TeacherVote }, 0);
  202. if (tchList.IsNotEmpty())
  203. {
  204. list.tmdids = tchList.Select(x => x.id).ToList();
  205. School school = null;
  206. if (!string.IsNullOrEmpty(vote.school))
  207. {
  208. school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(vote.school, new Azure.Cosmos.PartitionKey("Base"));
  209. list.school = school.id;
  210. list.standard = school.standard;
  211. }
  212. tchList.ForEach(x =>
  213. {
  214. HashSet<string> classIds = new HashSet<string>();
  215. classInfos.ForEach(z => {
  216. z.members.ForEach(y => {
  217. if (y.id.Equals(x) && y.type == 1)
  218. {
  219. classIds.Add(z.id);
  220. }
  221. });
  222. });
  223. tchActivities.Add(new StuActivity
  224. {
  225. pk = "Activity",
  226. id = vote.id,
  227. code = $"Activity-{x.id}",
  228. type = "Vote",
  229. name = vote.name,
  230. startTime = vote.startTime,
  231. endTime = vote.endTime,
  232. scode = vote.code,
  233. scope = vote.scope,
  234. school = vote.school,
  235. creatorId = vote.creatorId,
  236. subjects = new List<string> { "" },
  237. blob = null,
  238. owner = vote.owner,
  239. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  240. taskStatus = -1,
  241. classIds = classIds.ToList()
  242. });
  243. });
  244. }
  245. //await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动,:教研组活动:" +
  246. // $"{tchActivities.ToJsonString()}\n", GroupNames.醍摩豆服務運維群組);
  247. await ActivityService.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, tchActivities);
  248. await StatisticsService.SendServiceBus(list, _configuration, _serviceBus, client);
  249. //向学生或醍摩豆账号发起通知
  250. #region
  251. //Notice notice = new Notice()
  252. //{
  253. // creation = vote.startTime,
  254. // expire = vote.endTime,
  255. // creatorId = vote.creatorId,
  256. // stuids = students,
  257. // tmdids = tmdids,
  258. // type = "notice",//问卷参加参加通知
  259. // priority = "normal",
  260. // msgId=vote.id,
  261. // school = vote.school,
  262. // scope = vote.scope,
  263. // //data = new { }.ToJsonString()
  264. // body = new Body { sid = vote.id, scode = vote.code, spk = vote.pk, biztype = "vote-join" }
  265. //};
  266. //var messageBlob = new ServiceBusMessage(notice.ToJsonString());
  267. //messageBlob.ApplicationProperties.Add("name", "Notice");
  268. //await _serviceBus.GetServiceBusClient().SendMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:NoticeTask"), messageBlob);
  269. #endregion
  270. var messageVoteEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
  271. messageVoteEnd.ApplicationProperties.Add("name", "Vote");
  272. if (voteRecords.Count > 0)
  273. {
  274. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  275. try
  276. {
  277. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
  278. }
  279. catch (Exception)
  280. {
  281. }
  282. voteRecords[0].sequenceNumber = end;
  283. await table.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  284. }
  285. else
  286. {
  287. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  288. ChangeRecord changeRecord = new()
  289. {
  290. RowKey = tdata.id,
  291. PartitionKey = PartitionKey,
  292. sequenceNumber = end,
  293. msgId = messageVoteEnd.MessageId
  294. };
  295. await table.Save<ChangeRecord>(changeRecord);
  296. }
  297. #if DEBUG
  298. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动{tdata.id}将于:{tdata.endTime}完成并结算!", GroupNames.醍摩豆服務運維群組);
  299. #endif
  300. break;
  301. case "finish":
  302. #if DEBUG
  303. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动{tdata.id}开始结算{tdata.endTime}!", GroupNames.醍摩豆服務運維群組);
  304. #endif
  305. //获取投票活动的所有投票记录
  306. var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}");
  307. //获取投票活动的选项及投票数
  308. var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
  309. List<dynamic> countcds = new List<dynamic>();
  310. if (counts != null && counts.Length > 0)
  311. {
  312. foreach (var count in counts)
  313. {
  314. countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score });
  315. }
  316. }
  317. List<Task<string>> tasks = new();
  318. List<VoteRecord> recordsBlob = new();
  319. foreach (var rcd in records)
  320. {
  321. var value = rcd.Value.ToString().ToObject<VoteRecord>();
  322. recordsBlob.Add(value);
  323. }
  324. //分组每个人的
  325. var gp = recordsBlob.GroupBy(x => x.userid).Select(x => new { key = x.Key, list = x.ToList() });
  326. foreach (var g in gp)
  327. {
  328. tasks.Add(_azureStorage.UploadFileByContainer(blobcntr, g.list.ToJsonString(), "vote", $"{vote.id}/urecord/{g.key}.json"));
  329. }
  330. //处理活动方的记录,
  331. string url = $"/vote/{vote.id}/record.json";
  332. tasks.Add(_azureStorage.UploadFileByContainer(blobcntr, new { options = countcds, records = recordsBlob }.ToJsonString(), "vote", $"{vote.id}/record.json"));
  333. //处理投票者的记录
  334. if (string.IsNullOrEmpty(vote.recordUrl))
  335. {
  336. vote.recordUrl = url;
  337. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
  338. }
  339. else
  340. {
  341. //异动,且已经有结算记录则不必再继续。
  342. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}");
  343. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}");
  344. break;
  345. }
  346. await Task.WhenAll(tasks);
  347. break;
  348. }
  349. }
  350. }
  351. catch (CosmosException e)
  352. {
  353. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.StackTrace}{e.Message}\n{e.Status}", GroupNames.醍摩豆服務運維群組);
  354. }
  355. catch (Exception ex)
  356. {
  357. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动异常{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  358. }
  359. }
  360. }
  361. }