TriggerVote.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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.GetMemberByListids(_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. List<string> sub = new();
  131. if (vote.targets.Count > 0)
  132. {
  133. foreach (var course in vote.targets)
  134. {
  135. var info = course.ToObject<List<string>>();
  136. if (info.Count > 1)
  137. {
  138. sub.Add(info[0]);
  139. }
  140. }
  141. }
  142. if (tmds.IsNotEmpty())
  143. {
  144. tmds.ForEach(x =>
  145. {
  146. HashSet<string> classIds = new HashSet<string>();
  147. classLists.ForEach(z => {
  148. z.members.ForEach(y => {
  149. if (y.id.Equals(x) && y.type == 1)
  150. {
  151. classIds.Add(z.id);
  152. }
  153. });
  154. });
  155. tmdActivities.Add(new StuActivity
  156. {
  157. pk = "Activity",
  158. id = vote.id,
  159. code = $"Activity-{x}",
  160. type = "Vote",
  161. name = vote.name,
  162. startTime = vote.startTime,
  163. endTime = vote.endTime,
  164. scode = vote.code,
  165. scope = vote.scope,
  166. school = vote.school,
  167. creatorId = vote.creatorId,
  168. subjects = sub,
  169. blob = null,
  170. owner = vote.owner,
  171. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  172. taskStatus = -1,
  173. classIds = classIds.ToList()
  174. });
  175. });
  176. }
  177. if (addStudentsCls.IsNotEmpty())
  178. {
  179. addStudentsCls.ForEach(x =>
  180. {
  181. HashSet<string> classIds = new HashSet<string>();
  182. classLists.ForEach(z => {
  183. z.members.ForEach(y => {
  184. if (y.id.Equals(x.id) && y.code.Equals(vote.school) && y.type == 2)
  185. {
  186. classIds.Add(z.id);
  187. }
  188. });
  189. });
  190. stuActivities.Add(new StuActivity
  191. {
  192. pk = "Activity",
  193. id = vote.id,
  194. code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
  195. type = "Vote",
  196. name = vote.name,
  197. startTime = vote.startTime,
  198. endTime = vote.endTime,
  199. scode = vote.code,
  200. scope = vote.scope,
  201. school = vote.school,
  202. creatorId = vote.creatorId,
  203. subjects = sub,
  204. blob = null,
  205. owner = vote.owner,
  206. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  207. taskStatus = -1,
  208. classIds = classIds.ToList()
  209. });
  210. });
  211. }
  212. (List<RMember> tchList, List<RGroupList> classInfos) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, vote.tchLists, vote.school, ps);
  213. (string standard, List<string> tmdids, string school, List<string> update, int statistics) list = (null, null, null, new List<string> { StatisticsService.TeacherVote }, 0);
  214. if (tchList.IsNotEmpty())
  215. {
  216. list.tmdids = tchList.Select(x => x.id).ToList();
  217. School school = null;
  218. if (!string.IsNullOrEmpty(vote.school))
  219. {
  220. school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(vote.school, new Azure.Cosmos.PartitionKey("Base"));
  221. list.school = school.id;
  222. list.standard = school.standard;
  223. }
  224. tchList.ForEach(x =>
  225. {
  226. HashSet<string> classIds = new HashSet<string>();
  227. classInfos.ForEach(z => {
  228. z.members.ForEach(y => {
  229. if (y.id.Equals(x.id) && y.type == 1)
  230. {
  231. classIds.Add(z.id);
  232. }
  233. });
  234. });
  235. tchActivities.Add(new StuActivity
  236. {
  237. pk = "Activity",
  238. id = vote.id,
  239. code = $"Activity-{x.id}",
  240. type = "Vote",
  241. name = vote.name,
  242. startTime = vote.startTime,
  243. endTime = vote.endTime,
  244. scode = vote.code,
  245. scope = vote.scope,
  246. school = vote.school,
  247. creatorId = vote.creatorId,
  248. subjects = new List<string> { "" },
  249. blob = null,
  250. owner = vote.owner,
  251. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  252. taskStatus = -1,
  253. classIds = classIds.ToList()
  254. });
  255. });
  256. }
  257. //await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动,:教研组活动:" +
  258. // $"{tchActivities.ToJsonString()}\n", GroupNames.醍摩豆服務運維群組);
  259. await ActivityService.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, tchActivities);
  260. await StatisticsService.SendServiceBus(list, _configuration, _serviceBus, client);
  261. //向学生或醍摩豆账号发起通知
  262. #region
  263. //Notice notice = new Notice()
  264. //{
  265. // creation = vote.startTime,
  266. // expire = vote.endTime,
  267. // creatorId = vote.creatorId,
  268. // stuids = students,
  269. // tmdids = tmdids,
  270. // type = "notice",//问卷参加参加通知
  271. // priority = "normal",
  272. // msgId=vote.id,
  273. // school = vote.school,
  274. // scope = vote.scope,
  275. // //data = new { }.ToJsonString()
  276. // body = new Body { sid = vote.id, scode = vote.code, spk = vote.pk, biztype = "vote-join" }
  277. //};
  278. //var messageBlob = new ServiceBusMessage(notice.ToJsonString());
  279. //messageBlob.ApplicationProperties.Add("name", "Notice");
  280. //await _serviceBus.GetServiceBusClient().SendMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:NoticeTask"), messageBlob);
  281. #endregion
  282. var messageVoteEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
  283. messageVoteEnd.ApplicationProperties.Add("name", "Vote");
  284. if (voteRecords.Count > 0)
  285. {
  286. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  287. try
  288. {
  289. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
  290. }
  291. catch (Exception)
  292. {
  293. }
  294. voteRecords[0].sequenceNumber = end;
  295. await table.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  296. }
  297. else
  298. {
  299. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  300. ChangeRecord changeRecord = new()
  301. {
  302. RowKey = tdata.id,
  303. PartitionKey = PartitionKey,
  304. sequenceNumber = end,
  305. msgId = messageVoteEnd.MessageId
  306. };
  307. await table.Save<ChangeRecord>(changeRecord);
  308. }
  309. #if DEBUG
  310. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动{tdata.id}将于:{tdata.endTime}完成并结算!", GroupNames.醍摩豆服務運維群組);
  311. #endif
  312. break;
  313. case "finish":
  314. #if DEBUG
  315. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动{tdata.id}开始结算{tdata.endTime}!", GroupNames.醍摩豆服務運維群組);
  316. #endif
  317. //获取投票活动的所有投票记录
  318. var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}");
  319. //获取投票活动的选项及投票数
  320. var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
  321. List<dynamic> countcds = new List<dynamic>();
  322. if (counts != null && counts.Length > 0)
  323. {
  324. foreach (var count in counts)
  325. {
  326. countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score });
  327. }
  328. }
  329. List<Task<string>> tasks = new();
  330. List<VoteRecord> recordsBlob = new();
  331. foreach (var rcd in records)
  332. {
  333. var value = rcd.Value.ToString().ToObject<VoteRecord>();
  334. recordsBlob.Add(value);
  335. }
  336. //分组每个人的
  337. var gp = recordsBlob.GroupBy(x => x.userid).Select(x => new { key = x.Key, list = x.ToList() });
  338. foreach (var g in gp)
  339. {
  340. tasks.Add(_azureStorage.GetBlobContainerClient(blobcntr).UploadFileByContainer(g.list.ToJsonString(), "vote", $"{vote.id}/urecord/{g.key}.json"));
  341. }
  342. //处理活动方的记录,
  343. string url = $"/vote/{vote.id}/record.json";
  344. tasks.Add(_azureStorage.GetBlobContainerClient(blobcntr).UploadFileByContainer(new { options = countcds, records = recordsBlob }.ToJsonString(), "vote", $"{vote.id}/record.json"));
  345. //处理投票者的记录
  346. //处理投票活动结束统计账户信息
  347. List<FMember> idsList = await GroupListService.GetFinishMemberInfo(_coreAPIHttpService, client, _dingDing, vote.school, vote.classes, vote.stuLists, vote.tchLists);
  348. vote.staffIds = idsList;
  349. if (string.IsNullOrEmpty(vote.recordUrl))
  350. {
  351. vote.recordUrl = url;
  352. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
  353. }
  354. else
  355. {
  356. //异动,且已经有结算记录则不必再继续。
  357. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}");
  358. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}");
  359. break;
  360. }
  361. await Task.WhenAll(tasks);
  362. break;
  363. }
  364. }
  365. }
  366. catch (CosmosException e)
  367. {
  368. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}", GroupNames.醍摩豆服務運維群組);
  369. }
  370. catch (Exception ex)
  371. {
  372. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}投票活动异常{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  373. }
  374. }
  375. }
  376. }