TriggerVote.cs 21 KB

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