TriggerVote.cs 25 KB

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