SystemService.cs 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442
  1. using Azure.Storage.Blobs.Models;
  2. using DocumentFormat.OpenXml.Bibliography;
  3. using DocumentFormat.OpenXml.Drawing.Charts;
  4. using DocumentFormat.OpenXml.Office2010.Excel;
  5. using DocumentFormat.OpenXml.Wordprocessing;
  6. using HTEXLib;
  7. using HTEXLib.COMM.Helpers;
  8. using HTEXLib.PPTX.Models;
  9. using Microsoft.Azure.Amqp.Framing;
  10. using Microsoft.Extensions.Logging;
  11. using Newtonsoft.Json.Linq;
  12. using NUnit.Framework.Internal.Execution;
  13. using OpenXmlPowerTools;
  14. using StackExchange.Redis;
  15. using System;
  16. using System.Collections.Concurrent;
  17. using System.Collections.Generic;
  18. using System.Diagnostics.PerformanceData;
  19. using System.IdentityModel.Tokens.Jwt;
  20. using System.Linq;
  21. using System.Net.Http;
  22. using System.Net.Http.Json;
  23. using System.Security.Policy;
  24. using System.Text;
  25. using System.Text.Json;
  26. using System.Text.RegularExpressions;
  27. using System.Threading.Tasks;
  28. using TEAMModelOS.SDK.DI;
  29. using TEAMModelOS.SDK.DI.Mail;
  30. using TEAMModelOS.SDK.Extension;
  31. using TEAMModelOS.SDK.Models.Cosmos.Common;
  32. using TEAMModelOS.SDK.Models.Dtos;
  33. using static Azure.Core.HttpHeader;
  34. using static OpenXmlPowerTools.RevisionProcessor;
  35. using static TEAMModelOS.SDK.CoreAPIHttpService;
  36. using static TEAMModelOS.SDK.Models.Service.SystemService;
  37. namespace TEAMModelOS.SDK.Models.Service
  38. {
  39. public static class SystemService
  40. {
  41. #region
  42. static string cn_wb = @"
  43. IES晚间报告:<br>{tmdname}您好,以下是您的今日个人IES教学汇总报告,截至{sendTime}时,您已发布{examCount}次评测任务,{homeworkCount}次作业任务,并使用HiTeach教师端开设了{lessonCount}节课堂教学活动。
  44. <br>&nbsp;&nbsp;&nbsp;以下是报告的具体详细信息。
  45. <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{cn_examTitle}<br>{cn_examList}
  46. <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{cn_lessonTitle}<br>{cn_lessonList}
  47. <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{cn_groupTitle}<br>{cn_groupList}
  48. <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如有布置作业任务,将于次日8点通过早报方式发送。
  49. ";
  50. static string cn_examList = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;【{examName}】已经有{submitCount}位学生提交,仍有{unsubmitCount}位学生未提交。<br>";
  51. static string cn_lessonList = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;【{lessonName}】课例应出席人数{memberCount},实际出席人数{attendCount},出席率{attendRate}。<br>";
  52. static string cn_groupListJoin = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;【{grouplistName}】已有总人数{memberCount},有{joinCount}加入。<br>";
  53. static string cn_groupListLeave = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;【{grouplistName}】已有总人数{memberCount},有{joinCount}离开。<br>";
  54. static string cn_zb = @"
  55. IES早间报告:
  56. {tmdname}您好,以下是您发布过的作业任务汇总报告。
  57. 以下是报告的具体详细信息。
  58. {cn_homeworkTitle}
  59. {cn_homeworkList}
  60. ";
  61. static string cn_homeworkList = "【{homeworkName}】已经有{submitCount}位学生提交,仍有{unsubmitCount}位学生未提交。<br>";
  62. static string cn_homeworkTitle = "作业任务提交详情:";
  63. static string cn_examTitle = "评测任务提交详情:";
  64. static string cn_lessonTitle = "课堂教学出席详情:";
  65. static string cn_groupTitle = "个人课程名单变化详情:";
  66. #endregion
  67. #region
  68. static string tw_wb = @"
  69. IES晚間報告:
  70. {tmdname}您好,以下是您的今日個人IES教學總結報告,截至{sendTime},您已發布{examCount}次測驗任務,{homeworkCount}次作業任務,並使用HiTeach上傳了{lessonCount}節課堂教學活動。
  71. 以下是詳細資訊。
  72. {tw_examTitle}
  73. {tw_examList}
  74. {tw_lessonTitle}
  75. {tw_lessonList}
  76. {tw_groupTitle}
  77. {tw_groupList}
  78. 如有佈署作業任務,將於隔天早上8點透過晨間報告方式發送。
  79. ";
  80. static string tw_examList = "【{examName}】已經有{submitCount}位學生繳交,仍有{unsubmitCount}位學生未繳交。\r\n";
  81. static string tw_lessonList = "【{lessonName}】課堂應出席人數{memberCount},實際出席人數{attendCount},出席率{attendRate}。\r\n";
  82. static string tw_groupListJoin = "【{grouplistName}】已有總人數{memberCount},有{joinCount}加入。\r\n";
  83. static string tw_groupListLeave = "【{grouplistName}】已有總人數{memberCount},有{joinCount}離開。\r\n";
  84. static string tw_zb = @"
  85. IES晨間報告:
  86. {tmdname}您好,以下是您曾經發佈過的作業任務總結報告。
  87. 以下是具體詳細資訊。
  88. {tw_homeworkTitle}
  89. {tw_homeworkList}
  90. ";
  91. static string tw_homeworkList = "【{homeworkName}】已經有{submitCount}位學生繳交,仍有{unsubmitCount}位學生未繳交。\r\n";
  92. static string tw_homeworkTitle = "\r\n作業任務繳交詳情:";
  93. static string tw_examTitle = "\r\n測驗任務完成詳情:";
  94. static string tw_lessonTitle = "\r\n課堂教學出席詳情:";
  95. static string tw_groupTitle = "\r\n個人課程名單變動詳情:";
  96. #endregion
  97. #region
  98. static string en_wb = @"
  99. IES Evening Report:
  100. Hello {tmdname}, here is your personal IES teaching summary report for today. As of {sendTime}, you have published {examCount} test tasks, {homeworkCount} homework tasks, and uploaded {lessonCount} HiTeach lesson activity records.
  101. The following are the specific details.
  102. {en_examTitle}
  103. {en_examList}
  104. {en_lessonTitle}
  105. {en_lessonList}
  106. {en_groupTitle}
  107. {en_groupList}
  108. If there are homework tasks assigned, they will be sent through the morning report at 8 am the next day.
  109. ";
  110. static string en_examList = "[ {examName} ] has {submitCount} students submitted, and there are still {unsubmitCount} students who have not submitted.\r\n";
  111. static string en_lessonList = "[ {lessonName} ] lesson should have {memberCount} attendees, actual attendees {attendCount}, attendance rate {attendRate}.\r\n";
  112. static string en_groupListJoin = "[ {grouplistName} ] has a total of {memberCount}, {joinCount} joined.\r\n";
  113. static string en_groupListLeave = "[ {grouplistName} ] has a total of {memberCount}, {joinCount} left.\r\n";
  114. static string en_zb = @"
  115. IES Morning Report:
  116. Hello {tmdname}, here is the summary report of the homework tasks you have posted.
  117. {en_homeworkTitle}
  118. {en_homeworkList}
  119. ";
  120. static string en_homeworkList = "[ {homeworkName} ] has {submitCount} students submitted, and there are still {unsubmitCount} students who have not submitted.\r\n";
  121. static string en_homeworkTitle = "\r\nHomework Submission Details:";
  122. static string en_examTitle = "\r\nTest task submission details:";
  123. static string en_lessonTitle = "\r\nLesson attendance details:";
  124. static string en_groupTitle = "\r\nPersonal course list change details:";
  125. #endregion
  126. public static async Task<List<CodeValue> > AccumulateDaily(AzureRedisFactory _azureRedis,AzureCosmosFactory _azureCosmos,
  127. CoreAPIHttpService coreAPIHttpService, DingDing dingDing,HttpClient _httpClient, SnowflakeId _snowflakeId,string notifyUrl, MailFactory _mailFactory)
  128. {
  129. DateTimeOffset now = DateTimeOffset.Now;
  130. string day = now.ToString("yyyyMMdd");
  131. string homeworkYesterday = now.AddDays(-1).ToString("yyyyMMdd");
  132. HashSet<string> keys = new HashSet<string>();
  133. //当天评测发布的。
  134. //当天的个人评测提交数
  135. Dictionary<string, double?> exam_submit = new();
  136. SortedSetEntry[] exam_goingScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:teacher:exam-going:{day}");
  137. // Dictionary<string, double?> exam_going = new();
  138. if (exam_goingScores != null)
  139. {
  140. foreach (var score in exam_goingScores)
  141. {
  142. double val = score.Score;
  143. string key = score.Element.ToString();
  144. // exam_going.Add(key, val);
  145. exam_submit.Add(key, val);
  146. keys.Add(key);
  147. }
  148. }
  149. ///需要再次获取 没有任何人提交的作业和评测任务。
  150. SortedSetEntry[] exam_submitScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:private:exam-submit:{day}");
  151. if (exam_submitScores != null)
  152. {
  153. foreach (var score in exam_submitScores)
  154. {
  155. double val = score.Score;
  156. string key = score.Element.ToString();
  157. exam_submit.TryAdd(key, val);
  158. keys.Add(key);
  159. }
  160. }
  161. //今天发布的作业
  162. SortedSetEntry[] homework_goingScores_today = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:teacher:homework-going:{day}");
  163. Dictionary<string, double?> homework_submit_today = new();
  164. //Dictionary<string, double?> homework_going = new();
  165. if (homework_goingScores_today != null)
  166. {
  167. foreach (var score in homework_goingScores_today)
  168. {
  169. double val = score.Score;
  170. string key = score.Element.ToString();
  171. //homework_going.Add(key, val);
  172. homework_submit_today.TryAdd(key, val);
  173. }
  174. }
  175. //昨天发布的作业
  176. SortedSetEntry[] homework_goingScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:teacher:homework-going:{homeworkYesterday}");
  177. Dictionary<string, double?> homework_submit = new();
  178. //Dictionary<string, double?> homework_going = new();
  179. if (homework_goingScores != null)
  180. {
  181. foreach (var score in homework_goingScores)
  182. {
  183. double val = score.Score;
  184. string key = score.Element.ToString();
  185. //homework_going.Add(key, val);
  186. homework_submit.TryAdd(key, val);
  187. keys.Add(key);
  188. }
  189. }
  190. //昨天的个人作业提交数
  191. SortedSetEntry[] homework_submitScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:teacher:homework-submit:{homeworkYesterday}");
  192. if (homework_submitScores != null)
  193. {
  194. foreach (var score in homework_submitScores)
  195. {
  196. double val = score.Score;
  197. string key = score.Element.ToString();
  198. homework_submit.TryAdd(key, val);
  199. keys.Add(key);
  200. }
  201. }
  202. //当天的个人名单加入人数
  203. Dictionary<string, double?> grouplist = new();
  204. SortedSetEntry[] grouplistScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:teacher:grouplist:{day}");
  205. if (grouplistScores != null)
  206. {
  207. foreach (var score in grouplistScores)
  208. {
  209. double val = score.Score;
  210. string key = score.Element.ToString();
  211. grouplist.TryAdd(key, val);
  212. keys.Add(key);
  213. }
  214. }
  215. //当天教师的开课数量Accumulate:Daily:teacher:lesson-create:20240527
  216. Dictionary<string, double?> lessoncreate = new();
  217. SortedSetEntry[] llessoncreateScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:teacher:lesson-create:{day}");
  218. if (llessoncreateScores != null)
  219. {
  220. foreach (var score in llessoncreateScores)
  221. {
  222. double val = score.Score;
  223. string key = score.Element.ToString();
  224. lessoncreate.TryAdd(key, val);
  225. keys.Add(key);
  226. }
  227. }
  228. List<string> ids = new List<string>();
  229. keys.ToList().ForEach(x => {
  230. ids.Add(x.Split("::")[0]);
  231. });
  232. List<CoreUser> coreUsers = new List<CoreUser>();
  233. List<Teacher> teachers= new List<Teacher>();
  234. if (ids.IsNotEmpty())
  235. {
  236. string sql = $"select value c from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))})";
  237. var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).GetList<Teacher>(sql, "Base");
  238. if (result.list.IsNotEmpty())
  239. {
  240. teachers.AddRange(result.list);
  241. }
  242. var content = new StringContent(ids.Distinct().ToJsonString(), Encoding.UTF8, "application/json");
  243. try
  244. {
  245. string json = await coreAPIHttpService.GetUserInfos(content);
  246. if (!string.IsNullOrWhiteSpace(json))
  247. {
  248. coreUsers = json.ToObject<List<CoreUser>>();
  249. }
  250. }
  251. catch (Exception ex)
  252. {
  253. await dingDing.SendBotMsg($"{coreAPIHttpService.options.location}用户转换失败:{coreAPIHttpService.options.coreUrl}", GroupNames.醍摩豆服務運維群組);
  254. }
  255. }
  256. List<CodeValue> notifys = new List<CodeValue>();
  257. foreach (var teacher in teachers)
  258. {
  259. StringBuilder notify = new StringBuilder();
  260. #if DEBUG
  261. int sendTime_pm = now.Hour;
  262. int sendTime_am = now.Hour;
  263. #else
  264. int sendTime_pm = 20;
  265. int sendTime_am =8;
  266. #endif
  267. string lang = teacher.lang;
  268. var tzt = now.GetGMTTime((int)teacher.timezone);
  269. if (string.IsNullOrWhiteSpace(teacher.lang))
  270. {
  271. if (coreAPIHttpService.options.location.Contains("China", StringComparison.OrdinalIgnoreCase))
  272. {
  273. lang= "zh-cn";
  274. }
  275. if (coreAPIHttpService.options.location.Contains("Global",StringComparison.OrdinalIgnoreCase))
  276. {
  277. lang= "en-us";
  278. }
  279. }
  280. int examCount = 0,homeworkCount=0,lessonCount=0;
  281. StringBuilder examSB= new StringBuilder();
  282. foreach (var exam in exam_submit)
  283. {
  284. string[] ks = exam.Key.Split("::");
  285. string tid = ks[0];
  286. string examId = ks[1];
  287. string examName = ks[3];
  288. if (tid.Equals(teacher.id)) {
  289. if (tzt.Hour==sendTime_pm)
  290. {
  291. string sql = $"select c.status from c where c.examId='{examId}' and c.pk='ExamClassResult'";
  292. var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Common).GetList<ExamClassResult>(sql, $"ExamClassResult-{tid}");
  293. if (result.list.IsNotEmpty()) {
  294. var unsubmit = result.list.SelectMany(x => x.status).Where(x => x==1);
  295. var submit = result.list.SelectMany(x => x.status).Where(x => x==0);
  296. examCount++;
  297. switch (lang)
  298. {
  299. case "zh-cn":
  300. examSB.Append(cn_examList.Replace("{examName}", examName).Replace("{submitCount}", $"{submit.Count()}").Replace("{unsubmitCount}", $"{unsubmit.Count()}"));
  301. break;
  302. case "zh-tw":
  303. examSB.Append(tw_examList.Replace("{examName}", examName).Replace("{submitCount}", $"{submit.Count()}").Replace("{unsubmitCount}", $"{unsubmit.Count()}"));
  304. break;
  305. case "en-us":
  306. examSB.Append(en_examList.Replace("{examName}", examName).Replace("{submitCount}", $"{submit.Count()}").Replace("{unsubmitCount}", $"{unsubmit.Count()}"));
  307. break;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. StringBuilder homeworkSB = new StringBuilder();
  314. foreach (var homeworksm in homework_submit_today)
  315. {
  316. string[] ks = homeworksm.Key.Split("::");
  317. string tid = ks[0];
  318. string homeworkId = ks[1];
  319. string homeworkName = ks[3];
  320. if (tid.Equals(teacher.id))
  321. {
  322. if (tzt.Hour==sendTime_pm)
  323. {
  324. homeworkCount++;
  325. }
  326. }
  327. }
  328. foreach (var homeworksm in homework_submit)
  329. {
  330. string[] ks = homeworksm.Key.Split("::");
  331. string tid = ks[0];
  332. string homeworkId = ks[1];
  333. string homeworkName = ks[3];
  334. if (tid.Equals(teacher.id))
  335. {
  336. if (tzt.Hour==sendTime_am)
  337. {
  338. Azure .Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Common).ReadItemStreamAsync(homeworkId, new Azure.Cosmos.PartitionKey($"Homework-{tid}"));
  339. if (response.Status==200) {
  340. Homework homework= JsonDocument.Parse(response.Content).RootElement.ToObject<Homework>();
  341. List<string > classes= new List<string>();
  342. classes.AddRange(homework.stuLists);
  343. classes.AddRange(homework.classes);
  344. List<HomeworkUser> homeworkUsers= await HomeworkService.AnswerRecordAll(_azureCosmos.GetCosmosClient(), coreAPIHttpService, dingDing, homework, tid, "Student", classes, "student");
  345. // homeworkCount++;
  346. switch (lang)
  347. {
  348. case "zh-cn":
  349. homeworkSB.Append(cn_homeworkList.Replace("{homeworkName}", homework.name).Replace("{submitCount}", $"{homeworkUsers.Where(x => x.submit).Count()}").Replace("{unsubmitCount}", $"{homeworkUsers.Where(x => !x.submit).Count()}"));
  350. break;
  351. case "zh-tw":
  352. homeworkSB.Append(tw_homeworkList.Replace("{homeworkName}", homework.name).Replace("{submitCount}", $"{homeworkUsers.Where(x => x.submit).Count()}").Replace("{unsubmitCount}", $"{homeworkUsers.Where(x => !x.submit).Count()}"));
  353. break;
  354. case "en-us":
  355. homeworkSB.Append(en_homeworkList.Replace("{homeworkName}", homework.name).Replace("{submitCount}", $"{homeworkUsers.Where(x => x.submit).Count()}").Replace("{unsubmitCount}", $"{homeworkUsers.Where(x => !x.submit).Count()}"));
  356. break;
  357. }
  358. }
  359. }
  360. }
  361. }
  362. StringBuilder lessonSB = new StringBuilder();
  363. foreach (var lesson in lessoncreate)
  364. {
  365. string[] ks = lesson.Key.Split("::");
  366. string tid = ks[0];
  367. if (tid.Equals(teacher.id))
  368. {
  369. if (tzt.Hour==sendTime_pm)
  370. {
  371. long stime = now.AddHours(-20).ToUnixTimeMilliseconds();
  372. string sql = $"select value c from c where c.tmdid='{teacher.id}' and c.pk='LessonRecord' and c.startTime> {stime} ";
  373. List<LessonRecord>lessons = new List<LessonRecord>();
  374. var schoolResult= await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<LessonRecord>(sql);
  375. var teahcerResult = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).GetList<LessonRecord>(sql, "LessonRecord");
  376. lessons.AddRange(schoolResult.list);
  377. lessons.AddRange(teahcerResult.list);
  378. foreach(var lessonRecord in lessons)
  379. {
  380. lessonCount++;
  381. switch (lang)
  382. {
  383. case "zh-cn":
  384. lessonSB.Append(cn_lessonList.Replace("{lessonName}", lessonRecord.name).Replace("{memberCount}", $"{lessonRecord.clientCount}").Replace("{attendCount}", $"{lessonRecord.attendCount}")).Replace("{attendRate}", $"{lessonRecord.attendRate}");
  385. break;
  386. case "zh-tw":
  387. lessonSB.Append(tw_lessonList.Replace("{lessonName}", lessonRecord.name).Replace("{memberCount}", $"{lessonRecord.clientCount}").Replace("{attendCount}", $"{lessonRecord.attendCount}")).Replace("{attendRate}", $"{lessonRecord.attendRate}");
  388. break;
  389. case "en-us":
  390. lessonSB.Append(en_lessonList.Replace("{lessonName}", lessonRecord.name).Replace("{memberCount}", $"{lessonRecord.clientCount}").Replace("{attendCount}", $"{lessonRecord.attendCount}")).Replace("{attendRate}", $"{lessonRecord.attendRate}");
  391. break;
  392. }
  393. }
  394. }
  395. }
  396. }
  397. StringBuilder grouplistSB = new StringBuilder();
  398. List<string> grouplistIds=new List<string>();
  399. foreach (var group in grouplist)
  400. {
  401. string[] ks = group.Key.Split("::");
  402. grouplistIds.Add(ks[1]);
  403. }
  404. List<GroupListDto> groupListDtos= await GroupListService.GetGroupListByListids(_azureCosmos.GetCosmosClient(), dingDing, grouplistIds, null);
  405. foreach (var group in grouplist)
  406. {
  407. string[] ks = group.Key.Split("::");
  408. string tid = ks[0];
  409. string grouplistId = ks[1];
  410. string grouplistName = ks[3];
  411. var gpdto= groupListDtos.Find(x => x.id.Equals(grouplistId));
  412. if (gpdto!= null)
  413. {
  414. if (tid.Equals(teacher.id))
  415. {
  416. if (tzt.Hour==sendTime_pm)
  417. {
  418. switch (lang)
  419. {
  420. case "zh-cn":
  421. if (group.Value>=0)
  422. {
  423. grouplistSB.Append(cn_groupListJoin.Replace("{grouplistName}", grouplistName).Replace("{memberCount}", $"{gpdto.tcount+gpdto.scount}").Replace("{joinCount}", $"{group.Value}"));
  424. }
  425. else
  426. {
  427. grouplistSB.Append(cn_groupListLeave.Replace("{grouplistName}", grouplistName).Replace("{memberCount}", $"{gpdto.tcount+gpdto.scount}").Replace("{joinCount}", $"{group.Value}"));
  428. }
  429. break;
  430. case "zh-tw":
  431. if (group.Value>=0)
  432. {
  433. grouplistSB.Append(tw_groupListJoin.Replace("{grouplistName}", grouplistName).Replace("{memberCount}", $"{gpdto.tcount+gpdto.scount}").Replace("{joinCount}", $"{group.Value}"));
  434. }
  435. else
  436. {
  437. grouplistSB.Append(tw_groupListLeave.Replace("{grouplistName}", grouplistName).Replace("{memberCount}", $"{gpdto.tcount+gpdto.scount}").Replace("{joinCount}", $"{group.Value}"));
  438. }
  439. break;
  440. case "en-us":
  441. if (group.Value>=0)
  442. {
  443. grouplistSB.Append(en_groupListJoin.Replace("{grouplistName}", grouplistName).Replace("{memberCount}", $"{gpdto.tcount+gpdto.scount}").Replace("{joinCount}", $"{group.Value}"));
  444. }
  445. else
  446. {
  447. grouplistSB.Append(en_groupListLeave.Replace("{grouplistName}", grouplistName).Replace("{memberCount}", $"{gpdto.tcount+gpdto.scount}").Replace("{joinCount}", $"{group.Value}"));
  448. }
  449. break;
  450. }
  451. }
  452. }
  453. }
  454. }
  455. if (tzt.Hour==sendTime_pm && (examCount>0 || lessonCount>0 || homeworkCount>0 || (grouplist.Count>0 && !string.IsNullOrWhiteSpace(grouplistSB.ToString()))))
  456. {
  457. string template=string.Empty;
  458. string title=string.Empty;
  459. switch (lang)
  460. {
  461. case "zh-cn":
  462. title="IES晚间报告";
  463. template=cn_wb.Replace("{tmdname}", teacher.name).Replace("{sendTime}", $"{now.ToString("yyyy-MM-dd")} {sendTime_pm}")
  464. .Replace("{examCount}", $"{examCount}").Replace("{homeworkCount}", $"{homeworkCount}").Replace("{lessonCount}", $"{lessonCount}")
  465. .Replace("{cn_examList}", examSB.ToString()).Replace("{cn_lessonList}", lessonSB.ToString()).Replace("{cn_groupList}", grouplistSB.ToString());
  466. if (examCount>0)
  467. {
  468. template= template.Replace("{cn_examTitle}", cn_examTitle);
  469. }
  470. else {
  471. template= template.Replace("{cn_examTitle}", "");
  472. }
  473. if (homeworkCount>0)
  474. {
  475. template= template.Replace("{cn_homeworkTitle}", cn_homeworkTitle);
  476. }
  477. else
  478. {
  479. template= template.Replace("{cn_homeworkTitle}", "");
  480. }
  481. if (lessonCount>0)
  482. {
  483. template= template.Replace("{cn_lessonTitle}", cn_lessonTitle);
  484. }
  485. else
  486. {
  487. template= template.Replace("{cn_lessonTitle}", "");
  488. }
  489. if (grouplist.Count>0 && !string.IsNullOrWhiteSpace(grouplistSB.ToString()))
  490. {
  491. template=template.Replace("{cn_groupTitle}", cn_groupTitle);
  492. }
  493. else
  494. {
  495. template= template.Replace("{cn_groupTitle}", "");
  496. }
  497. break;
  498. case "zh-tw":
  499. title="IES晚間報告";
  500. template=tw_wb.Replace("{tmdname}", teacher.name).Replace("{sendTime}", $"{now.ToString("yyyy-MM-dd")} {sendTime_pm}")
  501. .Replace("{examCount}", $"{examCount}").Replace("{homeworkCount}", $"{homeworkCount}").Replace("{lessonCount}", $"{lessonCount}")
  502. .Replace("{tw_examList}", examSB.ToString()).Replace("{tw_lessonList}", lessonSB.ToString()).Replace("{tw_groupList}", grouplistSB.ToString());
  503. if (examCount>0)
  504. {
  505. template= template.Replace("{tw_examTitle}", tw_examTitle);
  506. }
  507. else
  508. {
  509. template= template.Replace("{tw_examTitle}", "");
  510. }
  511. if (homeworkCount>0)
  512. {
  513. template= template.Replace("{tw_homeworkTitle}", tw_homeworkTitle);
  514. }
  515. else
  516. {
  517. template= template.Replace("{tw_homeworkTitle}", "");
  518. }
  519. if (lessonCount>0)
  520. {
  521. template= template.Replace("{tw_lessonTitle}", tw_lessonTitle);
  522. }
  523. else
  524. {
  525. template= template.Replace("{tw_lessonTitle}", "");
  526. }
  527. if (grouplist.Count>0)
  528. {
  529. template=template.Replace("{tw_groupTitle}", tw_groupTitle);
  530. }
  531. else
  532. {
  533. template= template.Replace("{tw_groupTitle}", "");
  534. }
  535. break;
  536. case "en-us":
  537. title ="IES Evening Report";
  538. template=en_wb.Replace("{tmdname}", teacher.name).Replace("{sendTime}", $"{now.ToString("yyyy-MM-dd")} {sendTime_pm}")
  539. .Replace("{examCount}", $"{examCount}").Replace("{homeworkCount}", $"{homeworkCount}").Replace("{lessonCount}", $"{lessonCount}")
  540. .Replace("{en_examList}", examSB.ToString()).Replace("{en_lessonList}", lessonSB.ToString()).Replace("{en_groupList}", grouplistSB.ToString());
  541. if (examCount>0)
  542. {
  543. template= template.Replace("{en_examTitle}", en_examTitle);
  544. }
  545. else
  546. {
  547. template= template.Replace("{en_examTitle}", "");
  548. }
  549. if (homeworkCount>0)
  550. {
  551. template= template.Replace("{en_homeworkTitle}", en_homeworkTitle);
  552. }
  553. else
  554. {
  555. template= template.Replace("{en_homeworkTitle}", "");
  556. }
  557. if (lessonCount>0)
  558. {
  559. template= template.Replace("{en_lessonTitle}", en_lessonTitle);
  560. }
  561. else
  562. {
  563. template= template.Replace("{en_lessonTitle}", "");
  564. }
  565. if (grouplist.Count>0)
  566. {
  567. template=template.Replace("{en_groupTitle}", en_groupTitle);
  568. }
  569. else
  570. {
  571. template= template.Replace("{en_groupTitle}", "");
  572. }
  573. break;
  574. }
  575. #if DEBUG
  576. string eventId = $"Evening_Report-{_snowflakeId.NextId()}";
  577. NotifyData notifyData = new NotifyData
  578. {
  579. hubName = "hita5",
  580. sender = "IES",
  581. tags = new List<string>() { $"{teacher.id}_{Constant.NotifyType_IES5_Course}" },
  582. title = title,
  583. eventId =eventId,
  584. eventName =title,
  585. data = "{\"value\":{}}",
  586. body=template,
  587. };
  588. HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync($"{notifyUrl}/service/PushNotify", notifyData);
  589. var coreUser = coreUsers.Find(x => x.id.Equals(teacher.id) && !string.IsNullOrWhiteSpace(x.mail));
  590. if (coreUser!=null)
  591. {
  592. var token = _mailFactory.GetSmtpClient().SendEmail(_azureCosmos, dingDing, eventId, title, template, coreUser.mail, teacher.id, teacher.name);
  593. }
  594. #endif
  595. notify.Append(template);
  596. }
  597. if (tzt.Hour==sendTime_am && !string.IsNullOrWhiteSpace(homeworkSB.ToString()))
  598. {
  599. string template = string.Empty;
  600. string title = string.Empty;
  601. switch (lang)
  602. {
  603. case "zh-cn":
  604. title ="IES早间报告";
  605. template= cn_zb.Replace("{tmdname}", teacher.name).Replace("{sendTime}", $"{now.ToString("yyyy-MM-dd")} {sendTime_am}")
  606. .Replace("{cn_homeworkList}", homeworkSB.ToString());
  607. template=template.Replace("{cn_homeworkTitle}", cn_homeworkTitle);
  608. break;
  609. case "zh-tw":
  610. title ="IES晨間報告";
  611. template= tw_zb.Replace("{tmdname}", teacher.name).Replace("{sendTime}", $"{now.ToString("yyyy-MM-dd")} {sendTime_am}")
  612. .Replace("{tw_homeworkList}", homeworkSB.ToString());
  613. template=template.Replace("{tw_homeworkTitle}", tw_homeworkTitle);
  614. break;
  615. case "en-us":
  616. title ="IES Morning Report";
  617. template=en_zb.Replace("{tmdname}", teacher.name).Replace("{sendTime}", $"{now.ToString("yyyy-MM-dd")} {sendTime_am}")
  618. .Replace("{en_homeworkList}", homeworkSB.ToString());
  619. template=template.Replace("{en_homeworkTitle}", en_homeworkTitle);
  620. break;
  621. }
  622. #if DEBUG
  623. string eventId = $"Morning_Report-{_snowflakeId.NextId()}";
  624. NotifyData notifyData = new NotifyData
  625. {
  626. hubName = "hita5",
  627. sender = "IES",
  628. tags = new List<string>() { $"{teacher.id}_{Constant.NotifyType_IES5_Course}" },
  629. title = title,
  630. eventId = eventId,
  631. eventName =title,
  632. data = "{\"value\":{}}",
  633. body=template,
  634. };
  635. HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync($"{notifyUrl}/service/PushNotify", notifyData);
  636. var coreUser = coreUsers.Find(x => x.id.Equals(teacher.id) && !string.IsNullOrWhiteSpace(x.mail));
  637. if (coreUser!=null)
  638. {
  639. var token = _mailFactory.GetSmtpClient().SendEmail(_azureCosmos, dingDing, eventId, title, template, coreUser.mail, teacher.id, teacher.name);
  640. }
  641. #endif
  642. notify.Append(template);
  643. }
  644. notifys.Add(new CodeValue { code=teacher.id, value= notify.ToString() });
  645. }
  646. return notifys;
  647. }
  648. public static async Task RecordAccumulateData(AzureRedisFactory azureRedis, DingDing dingDing, Accumulate accumulate)
  649. {
  650. if (!string.IsNullOrWhiteSpace(accumulate.key) && !string.IsNullOrWhiteSpace(accumulate.target) &&
  651. !string.IsNullOrWhiteSpace(accumulate.id) && !string.IsNullOrWhiteSpace(accumulate.name) &&
  652. !string.IsNullOrWhiteSpace(accumulate.scope) && !string.IsNullOrWhiteSpace(accumulate.client))
  653. {
  654. await RecordAccumulateData(azureRedis, accumulate.key, accumulate.target, accumulate.id, accumulate.name, accumulate.scope, accumulate.client, accumulate.count);
  655. }
  656. else
  657. {
  658. await dingDing.SendBotMsg($"IES累计数据变更统计参数异常,{accumulate.ToJsonString()}", GroupNames.成都开发測試群組);
  659. }
  660. }
  661. /// <summary>
  662. /// 记录累计数据
  663. /// </summary>
  664. public static async Task RecordAccumulateData(AzureRedisFactory azureRedis, string key, string target, string id, string name, string scope, string client, int count)
  665. {
  666. if (!string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(target) &&
  667. !string.IsNullOrWhiteSpace(id) && !string.IsNullOrWhiteSpace(name) &&
  668. !string.IsNullOrWhiteSpace(scope)&& !string.IsNullOrWhiteSpace(client))
  669. {
  670. //处理UTC时差
  671. var nowTime = DateTimeOffset.UtcNow.GetGMTTime();
  672. int difference = (DayOfWeek.Sunday - nowTime.DayOfWeek + 7) % 7; //1-7的结果0-6
  673. var day = nowTime.ToString("yyyyMMdd");
  674. string redisKey = $"Accumulate:Daily:{scope}:{key}:{day}";
  675. string member = $"{target}::{id}::{client}::{name}";
  676. await azureRedis.GetRedisClient(8).SortedSetIncrementAsync(redisKey, member, count);
  677. await azureRedis.GetRedisClient(8).KeyExpireAsync(redisKey, new TimeSpan((difference+1)*24, 10, 0));
  678. //if (key.Equals("lesson") || key.StartsWith("login_"))
  679. //{
  680. // redisKey = $"Accumulate:Daily:ies:{key}:{day}";
  681. // if (scope.Equals("school"))
  682. // {
  683. // member = $"ies::{id}::{name}";
  684. // }
  685. // else {
  686. // member = $"ies::ies::{name}";
  687. // }
  688. // await azureRedis.GetRedisClient(8).SortedSetIncrementAsync(redisKey, member, 1);
  689. //}
  690. }
  691. }
  692. static double GetUserDuration(List<long> times )
  693. {
  694. if (times.IsNotEmpty())
  695. {
  696. if (times.Count>=2)
  697. {
  698. double totalDuration = 0;
  699. // DateTime lastTime = DateTimeOffset.FromUnixTimeMilliseconds((long)jsonData[0]["time"]).UtcDateTime;
  700. long ltime = times[0];
  701. for (int i = 1; i < times.Count; i++)
  702. {
  703. long ctime = times[i];
  704. //DateTime currentTime = DateTimeOffset.FromUnixTimeMilliseconds((long)jsonData[i]["time"]).UtcDateTime;
  705. long timeDifference = ctime - ltime;
  706. ///如果一小时内连续操作,则按真实时间累计
  707. if (timeDifference < 3600000)
  708. {
  709. totalDuration += timeDifference;
  710. }
  711. else
  712. { ///如果一小时内没有连续操作,则按象征性加10秒,以确保数据有效性。 如果因为一小时没有操作,则表示没有新的接口再次请求,当即就已经退出系统。否则后续仍然会有接口进入,并且带有时间戳
  713. totalDuration += 10000;
  714. }
  715. ltime = ctime;
  716. }
  717. return totalDuration;
  718. }
  719. else { return 10000; }
  720. }
  721. else {
  722. return 0;
  723. }
  724. }
  725. public static List<SchoolStick> CountSchoolStickiness(IEnumerable<IGrouping<string,ApiVisit>> schoolApiVisits, IEnumerable<ApiVisit> apiVisits)
  726. {
  727. List<SchoolStick> schoolStickiness = new List<SchoolStick>();
  728. foreach (var chinaSchoolKey in schoolApiVisits)
  729. {
  730. SchoolStick stickiness = new SchoolStick()
  731. {
  732. id = chinaSchoolKey.Key
  733. };
  734. var teachers = chinaSchoolKey.ToList().Where(x => !string.IsNullOrWhiteSpace(x.userId) && !string.IsNullOrWhiteSpace(x.scope) && x.scope.Equals("teacher")).GroupBy(x => x.userId);
  735. double hitaTime = 0, hiteachTime = 0, ies5TchTime = 0, otherTchTime = 0, ies5StuTime = 0, otherStuTime = 0;
  736. int hitaTch= 0, hiteachTch = 0, ies5Tch = 0, otherTch = 0, ies5Stu = 0, otherStu = 0;
  737. double hitaCount = 0, hiteachCount = 0, ies5TchCount = 0, otherTchCount = 0, ies5StuCount = 0, otherStuCount = 0;
  738. double lessonCount= chinaSchoolKey.ToList().Where(x => x.path.Contains("hiteach/create-lesson")).Count() ;
  739. int teacherCount= teachers.Count() ;
  740. foreach (var user in teachers)
  741. {
  742. TchStick tchStick = new TchStick()
  743. {
  744. id = user.Key,
  745. school= chinaSchoolKey.Key
  746. };
  747. //HiTA
  748. var teacherHitaVisit = apiVisits.Where(x => !string.IsNullOrWhiteSpace(x.userId) && x.userId.Equals(user.Key) && x.client.Equals("hita"));
  749. var teacherHitaVisitCount = teacherHitaVisit.Count();
  750. if (teacherHitaVisitCount>0)
  751. { hitaTch+=1; }
  752. var teacherHitaVisitTime = GetUserDuration(teacherHitaVisit.OrderBy(x => x.time).Select(x => x.time).ToList());
  753. hitaCount+=teacherHitaVisitCount;
  754. hitaTime+=teacherHitaVisitTime;
  755. tchStick.hita.count.value=teacherHitaVisitCount;
  756. tchStick.hita.duration.value=teacherHitaVisitTime;
  757. //Other
  758. var teacherOtherVisit = user.Where(x => !x.client.Equals("hita")&&!x.client.Equals("hiteach")&&!x.client.Equals("ies5"));
  759. var teacherOtherVisitCount= teacherOtherVisit.Count();
  760. var teacherOtherVisitTime= GetUserDuration(teacherOtherVisit.OrderBy(x => x.time).Select(x => x.time).ToList());
  761. if (teacherOtherVisitCount>0)
  762. { otherTch+=1; }
  763. otherTchCount+=teacherOtherVisitCount;
  764. otherTchTime+=teacherOtherVisitTime;
  765. tchStick.otherTch.count.value=teacherOtherVisitCount;
  766. tchStick.otherTch.duration.value=teacherOtherVisitTime;
  767. //HiTeach
  768. //教师访问hiteach 不带学校信息的
  769. var teacherHiteachVisit = apiVisits.Where(x => !string.IsNullOrWhiteSpace(x.userId) && x.userId.Equals(user.Key) && x.client.Equals("hiteach") && string.IsNullOrWhiteSpace(x.school));
  770. //教师访问hiteach 包含学校信息的
  771. var teacherHiteachSchoolVisit = user.Where(x => x.client.Equals("hiteach"));
  772. //教师所有的Hiteach访问
  773. List<ApiVisit> teacherHiteachAllVisit = new List<ApiVisit>();
  774. teacherHiteachAllVisit.AddRange(teacherHiteachVisit);
  775. teacherHiteachAllVisit.AddRange(teacherHiteachSchoolVisit);
  776. var teacherHiteachAllVisitCount = teacherHiteachAllVisit.Count();
  777. if (teacherHiteachAllVisitCount>0)
  778. { hiteachTch+=1; }
  779. var teacherHiteachAllVisitTime = GetUserDuration(teacherHiteachAllVisit.OrderBy(x => x.time).Select(x => x.time).ToList());
  780. hiteachCount+=teacherHiteachAllVisitCount;
  781. hiteachTime+=teacherHiteachAllVisitTime;
  782. tchStick.hiteach.count.value=teacherHiteachAllVisitCount;
  783. tchStick.hiteach.duration.value=teacherHiteachAllVisitTime;
  784. //IES5
  785. var teacherIes5Visit = user.Where(x => x.client.Equals("ies5"));
  786. var teacherIes5VisitCount= teacherIes5Visit.Count();
  787. if (teacherIes5VisitCount>0)
  788. { ies5Tch+=1; }
  789. var teacherIes5VisitTime = GetUserDuration(teacherIes5Visit.OrderBy(x => x.time).Select(x => x.time).ToList());
  790. ies5TchCount+=teacherIes5VisitCount;
  791. ies5TchTime+=teacherIes5VisitTime;
  792. tchStick.ies5Tch.count.value=teacherIes5VisitCount;
  793. tchStick.ies5Tch.duration.value=teacherIes5VisitTime;
  794. double lessonCountTch = chinaSchoolKey.ToList().Where(x => x.path.Contains("hiteach/create-lesson") && x.userId.Equals(user.Key)).Count();
  795. tchStick.lesson.value=lessonCountTch;
  796. tchStick.tmd.count.value=tchStick.hita.count.value+tchStick.hiteach.count.value+tchStick.ies5Tch.count.value+tchStick.otherTch.count.value;
  797. tchStick.tmd.duration.value=tchStick.hita.duration.value+tchStick.hiteach.duration.value+tchStick.ies5Tch.duration.value+tchStick.otherTch.duration.value;
  798. tchStick.hita.userCount=1;
  799. tchStick.hiteach.userCount=1;
  800. tchStick.ies5Tch.userCount=1;
  801. tchStick.otherTch.userCount=1;
  802. tchStick.tmd.userCount=1;
  803. if (tchStick.hita.count.value>0 && tchStick.hita.duration.value>0)
  804. {
  805. tchStick.hita.stick.value= (tchStick.hita.duration.value/1000/tchStick.hita.count.value);
  806. }
  807. if (tchStick.ies5Tch.count.value>0 && tchStick.ies5Tch.duration.value>0)
  808. {
  809. tchStick.ies5Tch.stick.value= (tchStick.ies5Tch.duration.value/1000/tchStick.ies5Tch.count.value);
  810. }
  811. if (tchStick.otherTch.count.value>0 && tchStick.otherTch.duration.value>0)
  812. {
  813. tchStick.otherTch.stick.value= (tchStick.otherTch.duration.value/1000/tchStick.otherTch.count.value);
  814. }
  815. if (tchStick.hiteach.count.value>0 && tchStick.hiteach.duration.value>0)
  816. {
  817. tchStick.hiteach.stick.value= (tchStick.hiteach.duration.value/1000/tchStick.hiteach.count.value)+tchStick.lesson.value;
  818. }
  819. if (tchStick.tmd.count.value>0 && tchStick.tmd.duration.value>0)
  820. {
  821. tchStick.tmd.stick.value= (tchStick.tmd.duration.value/1000/tchStick.tmd.count.value)+ tchStick.lesson.value;
  822. }
  823. stickiness.tchSticks.Add(tchStick);
  824. }
  825. var student = chinaSchoolKey.ToList().Where(x => !string.IsNullOrWhiteSpace(x.userId) && !string.IsNullOrWhiteSpace(x.scope) && (x.scope.Equals("student")||x.scope.Equals("tmduser"))).GroupBy(x => x.userId);
  826. var studentCount = student.Count();
  827. foreach (var user in student)
  828. {
  829. StuStick stuStick = new StuStick
  830. {
  831. id=user.Key,
  832. school=chinaSchoolKey.Key
  833. };
  834. var studentOtherVisit = user.Where(x => !x.client.Equals("ies5"));
  835. var studentOtherVisitCount = studentOtherVisit.Count();
  836. if (studentOtherVisitCount>0)
  837. { otherStu+=1; }
  838. double studentOtherVisitTime = GetUserDuration(studentOtherVisit.OrderBy(x => x.time).Select(x => x.time).ToList()) ;
  839. var studentIes5Visit = user.Where(x => x.client.Equals("ies5"));
  840. var studentIes5VisitCount = studentIes5Visit.Count();
  841. if (studentIes5VisitCount>0)
  842. { ies5Stu+=1; }
  843. double studentIes5VisitTime = GetUserDuration(studentIes5Visit.OrderBy(x => x.time).Select(x => x.time).ToList());
  844. ies5StuCount+=studentIes5VisitCount;
  845. ies5StuTime+=studentIes5VisitTime;
  846. otherStuCount+=studentOtherVisitCount;
  847. otherStuTime+=studentOtherVisitTime;
  848. stuStick.ies5Stu.count.value=studentIes5VisitCount;
  849. stuStick.ies5Stu.duration.value=studentIes5VisitTime;
  850. stuStick.otherStu.count.value=studentOtherVisitCount;
  851. stuStick.otherStu.duration.value=studentOtherVisitTime;
  852. stuStick.tmd.count.value= stuStick.ies5Stu.count.value+stuStick.otherStu.count.value;
  853. stuStick.tmd.duration.value= stuStick.ies5Stu.duration.value+stuStick.otherStu.duration.value;
  854. stuStick.ies5Stu.userCount=1;
  855. stuStick.otherStu.userCount=1;
  856. stuStick.tmd.userCount=1;
  857. if (stuStick.ies5Stu.count.value>0 && stuStick.ies5Stu.duration.value>0)
  858. {
  859. stuStick.ies5Stu.stick.value= (stuStick.ies5Stu.duration.value/1000/stuStick.ies5Stu.count.value);
  860. }
  861. if (stuStick.otherStu.count.value>0 && stuStick.otherStu.duration.value>0)
  862. {
  863. stuStick.otherStu.stick.value=(stuStick.otherStu.duration.value/1000/stuStick.otherStu.count.value);
  864. }
  865. if (stuStick.tmd.count.value>0 && stuStick.tmd.duration.value>0)
  866. {
  867. stuStick.tmd.stick.value=(stuStick.tmd.duration.value/1000/stuStick.tmd.count.value);
  868. }
  869. stickiness.stuSticks.Add(stuStick);
  870. }
  871. stickiness.hita.duration.value=hitaTime;
  872. stickiness.hiteach.duration.value=hiteachTime;
  873. stickiness.otherTch.duration.value=otherTchTime;
  874. stickiness.ies5Tch.duration.value=ies5TchTime;
  875. stickiness.tmd.duration.value=hitaTime+hiteachTime+otherTchTime+ies5TchTime+otherStuTime+ies5StuTime;
  876. stickiness.hita.userCount=hitaTch;
  877. stickiness.hiteach.userCount=hiteachTch;
  878. stickiness.ies5Tch.userCount=ies5Tch;
  879. stickiness.otherTch.userCount=otherTch;
  880. stickiness.ies5Stu.userCount=ies5Stu;
  881. stickiness.otherStu.userCount=otherStu;
  882. stickiness.hita.count.value=hitaCount;
  883. stickiness.hiteach.count.value=hiteachCount;
  884. stickiness.ies5Tch.count.value=ies5TchCount;
  885. stickiness.otherTch.count.value=otherTchCount;
  886. stickiness.ies5Stu.count.value=ies5StuCount;
  887. stickiness.otherStu.count.value=otherStuCount;
  888. stickiness.tmd.count.value=hitaCount+hiteachCount+ies5TchCount+otherTchCount+ies5StuCount+otherStuCount;
  889. stickiness.lesson.value=lessonCount;
  890. stickiness.tmd.userCount=teacherCount+studentCount;
  891. if(stickiness.hita.count.value>0 && stickiness.hita.duration.value>0)
  892. {
  893. stickiness.hita.stick.value=(stickiness.hita.userCount* stickiness.hita.count.value)/(stickiness.hita.duration.value/1000/stickiness.hita.count.value);
  894. }
  895. if (stickiness.ies5Stu.count.value>0 && stickiness.ies5Stu.duration.value>0)
  896. {
  897. stickiness.ies5Stu.stick.value=(stickiness.ies5Stu.userCount* stickiness.ies5Stu.count.value)/(stickiness.ies5Stu.duration.value/1000/stickiness.ies5Stu.count.value);
  898. }
  899. if (stickiness.ies5Tch.count.value>0 && stickiness.ies5Tch.duration.value>0)
  900. {
  901. stickiness.ies5Tch.stick.value=(stickiness.ies5Tch.userCount* stickiness.ies5Tch.count.value)/(stickiness.ies5Tch.duration.value/1000/stickiness.ies5Tch.count.value);
  902. }
  903. if (stickiness.otherStu.count.value>0 && stickiness.otherStu.duration.value>0)
  904. {
  905. stickiness.otherStu.stick.value=(stickiness.otherStu.userCount* stickiness.otherStu.count.value)/(stickiness.otherStu.duration.value/1000/stickiness.otherStu.count.value);
  906. }
  907. if (stickiness.otherTch.count.value>0 && stickiness.otherTch.duration.value>0)
  908. {
  909. stickiness.otherTch.stick.value=(stickiness.otherTch.userCount* stickiness.otherTch.count.value)/(stickiness.otherTch.duration.value/1000/stickiness.otherTch.count.value);
  910. }
  911. if (stickiness.hiteach.count.value>0 && stickiness.hiteach.duration.value>0)
  912. {
  913. stickiness.hiteach.stick.value=(stickiness.hiteach.userCount* stickiness.hiteach.count.value)/(stickiness.hiteach.duration.value/1000/stickiness.hiteach.count.value)+stickiness.lesson.value;
  914. }
  915. if (stickiness.tmd.count.value>0 && stickiness.tmd.duration.value>0)
  916. {
  917. stickiness.tmd.stick.value=(stickiness.tmd.userCount* stickiness.tmd.count.value)/(stickiness.tmd.duration.value/1000/stickiness.tmd.count.value)+ stickiness.lesson.value;
  918. }
  919. stickiness.tchSticks= OrderByTchStick(stickiness.tchSticks);
  920. stickiness.stuSticks= OrderByStuStick(stickiness.stuSticks);
  921. schoolStickiness.Add(stickiness);
  922. }
  923. return OrderBySchoolStick(schoolStickiness);
  924. }
  925. private static List<StuStick> OrderByStuStick(List<StuStick> stuStickiness)
  926. {
  927. // 假设visit已经填充了数据
  928. #region
  929. var rank_tmd_count = stuStickiness.Select(x => x.tmd.count.value).Distinct().OrderByDescending(v => v).ToList();
  930. var rank_ies5Stu_count = stuStickiness.Select(x => x.ies5Stu.count.value).Distinct().OrderByDescending(v => v).ToList();
  931. var rank_otherStu_count = stuStickiness.Select(x => x.otherStu.count.value).Distinct().OrderByDescending(v => v).ToList();
  932. #endregion
  933. #region
  934. var rank_tmd_duration = stuStickiness.Select(x => x.tmd.duration.value).Distinct().OrderByDescending(v => v).ToList();
  935. var rank_ies5Stu_duration = stuStickiness.Select(x => x.ies5Stu.duration.value).Distinct().OrderByDescending(v => v).ToList();
  936. var rank_otherStu_duration = stuStickiness.Select(x => x.otherStu.duration.value).Distinct().OrderByDescending(v => v).ToList();
  937. #endregion
  938. #region
  939. var rank_tmd_stick = stuStickiness.Select(x => x.tmd.stick.value).Distinct().OrderByDescending(v => v).ToList();
  940. var rank_ies5Stu_stick = stuStickiness.Select(x => x.ies5Stu.stick.value).Distinct().OrderByDescending(v => v).ToList();
  941. var rank_otherStu_stick = stuStickiness.Select(x => x.otherStu.stick.value).Distinct().OrderByDescending(v => v).ToList();
  942. #endregion
  943. stuStickiness.ForEach(x =>
  944. {
  945. #region
  946. if (x.tmd.count.value>0)
  947. {
  948. int index_tmd_count = rank_tmd_count.FindIndex(i => i==x.tmd.count.value);
  949. if (index_tmd_count!=-1)
  950. {
  951. x.tmd.count.rank = index_tmd_count + 1;
  952. }
  953. }
  954. if (x.ies5Stu.count.value>0)
  955. {
  956. int index_ies5Stu_count = rank_ies5Stu_count.FindIndex(i => i==x.ies5Stu.count.value);
  957. if (index_ies5Stu_count!=-1)
  958. {
  959. x.ies5Stu.count.rank = index_ies5Stu_count + 1;
  960. }
  961. }
  962. if (x.otherStu.count.value>0)
  963. {
  964. int index_otherStu_count = rank_otherStu_count.FindIndex(i => i==x.otherStu.count.value);
  965. if (index_otherStu_count!=-1)
  966. {
  967. x.otherStu.count.rank = index_otherStu_count + 1;
  968. }
  969. }
  970. #endregion
  971. #region
  972. if (x.tmd.duration.value> 0) {
  973. int index_tmd_duration = rank_tmd_duration.FindIndex(i => i==x.tmd.duration.value);
  974. if (index_tmd_duration!=-1)
  975. {
  976. x.tmd.duration.rank = index_tmd_duration + 1;
  977. }
  978. }
  979. if (x.ies5Stu.duration.value>0) {
  980. int index_ies5Stu_duration = rank_ies5Stu_duration.FindIndex(i => i==x.ies5Stu.duration.value);
  981. if (index_ies5Stu_duration!=-1)
  982. {
  983. x.ies5Stu.duration.rank = index_ies5Stu_duration + 1;
  984. }
  985. }
  986. if (x.otherStu.duration.value>0) {
  987. int index_otherStu_duration = rank_otherStu_duration.FindIndex(i => i==x.otherStu.duration.value);
  988. if (index_otherStu_duration!=-1)
  989. {
  990. x.otherStu.duration.rank = index_otherStu_duration + 1;
  991. }
  992. }
  993. #endregion
  994. #region
  995. if (x.tmd.stick.value>0)
  996. {
  997. int index_tmd_stick = rank_tmd_stick.FindIndex(i => i==x.tmd.stick.value);
  998. if (index_tmd_stick!=-1)
  999. {
  1000. x.tmd.stick.rank = index_tmd_stick + 1;
  1001. }
  1002. }
  1003. if (x.ies5Stu.stick.value>0)
  1004. {
  1005. int index_ies5Stu_stick = rank_ies5Stu_stick.FindIndex(i => i==x.ies5Stu.stick.value);
  1006. if (index_ies5Stu_stick!=-1)
  1007. {
  1008. x.ies5Stu.stick.rank = index_ies5Stu_stick + 1;
  1009. }
  1010. }
  1011. if (x.otherStu.stick.value>0)
  1012. {
  1013. int index_otherStu_stick = rank_otherStu_stick.FindIndex(i => i==x.otherStu.stick.value);
  1014. if (index_otherStu_stick!=-1)
  1015. {
  1016. x.otherStu.stick.rank = index_otherStu_stick + 1;
  1017. }
  1018. }
  1019. #endregion
  1020. });
  1021. return stuStickiness;
  1022. }
  1023. private static List<TchStick> OrderByTchStick(List<TchStick> tchStickiness)
  1024. {
  1025. // 假设visit已经填充了数据
  1026. #region
  1027. var rank_tmd_count = tchStickiness.Select(x => x.tmd.count.value).Distinct().OrderByDescending(v => v).ToList();
  1028. var rank_hita_count = tchStickiness.Select(x => x.hita.count.value).Distinct().OrderByDescending(v => v).ToList();
  1029. var rank_hiteach_count = tchStickiness.Select(x => x.hiteach.count.value).Distinct().OrderByDescending(v => v).ToList();
  1030. var rank_ies5Tch_count = tchStickiness.Select(x => x.ies5Tch.count.value).Distinct().OrderByDescending(v => v).ToList();
  1031. var rank_otherTch_count = tchStickiness.Select(x => x.otherTch.count.value).Distinct().OrderByDescending(v => v).ToList();
  1032. var rank_lesson_count = tchStickiness.Select(x => x.lesson.value).Distinct().OrderByDescending(v => v).ToList();
  1033. #endregion
  1034. #region
  1035. var rank_tmd_duration = tchStickiness.Select(x => x.tmd.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1036. var rank_hita_duration = tchStickiness.Select(x => x.hita.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1037. var rank_hiteach_duration = tchStickiness.Select(x => x.hiteach.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1038. var rank_ies5Tch_duration = tchStickiness.Select(x => x.ies5Tch.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1039. var rank_otherTch_duration = tchStickiness.Select(x => x.otherTch.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1040. #endregion
  1041. #region
  1042. var rank_tmd_stick = tchStickiness.Select(x => x.tmd.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1043. var rank_hita_stick = tchStickiness.Select(x => x.hita.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1044. var rank_hiteach_stick = tchStickiness.Select(x => x.hiteach.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1045. var rank_ies5Tch_stick = tchStickiness.Select(x => x.ies5Tch.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1046. var rank_otherTch_stick = tchStickiness.Select(x => x.otherTch.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1047. #endregion
  1048. tchStickiness.ForEach(x => {
  1049. #region
  1050. int index_tmd_count = rank_tmd_count.FindIndex(i => i==x.tmd.count.value);
  1051. if (index_tmd_count!=-1)
  1052. {
  1053. x.tmd.count.rank = index_tmd_count + 1;
  1054. }
  1055. int index_hita_count = rank_hita_count.FindIndex(i => i==x.hita.count.value);
  1056. if (index_hita_count!=-1)
  1057. {
  1058. x.hita.count.rank = index_hita_count + 1;
  1059. }
  1060. int index_hiteach_count = rank_hiteach_count.FindIndex(i => i==x.hiteach.count.value);
  1061. if (index_hiteach_count!=-1)
  1062. {
  1063. x.hiteach.count.rank = index_hiteach_count + 1;
  1064. }
  1065. int index_ies5Tch_count = rank_ies5Tch_count.FindIndex(i => i==x.ies5Tch.count.value);
  1066. if (index_ies5Tch_count!=-1)
  1067. {
  1068. x.ies5Tch.count.rank = index_ies5Tch_count + 1;
  1069. }
  1070. int index_otherTch_count = rank_otherTch_count.FindIndex(i => i==x.otherTch.count.value);
  1071. if (index_otherTch_count!=-1)
  1072. {
  1073. x.otherTch.count.rank = index_otherTch_count + 1;
  1074. }
  1075. int index_lesson_count = rank_lesson_count.FindIndex(i => i==x.lesson.value);
  1076. if (index_lesson_count!=-1)
  1077. {
  1078. x.lesson.rank = index_lesson_count + 1;
  1079. }
  1080. #endregion
  1081. #region
  1082. int index_tmd_duration = rank_tmd_duration.FindIndex(i => i==x.tmd.duration.value);
  1083. if (index_tmd_duration!=-1)
  1084. {
  1085. x.tmd.duration.rank = index_tmd_duration + 1;
  1086. }
  1087. int index_hita_duration = rank_hita_duration.FindIndex(i => i==x.hita.duration.value);
  1088. if (index_hita_duration!=-1)
  1089. {
  1090. x.hita.duration.rank = index_hita_duration + 1;
  1091. }
  1092. int index_hiteach_duration = rank_hiteach_duration.FindIndex(i => i==x.hiteach.duration.value);
  1093. if (index_hiteach_duration!=-1)
  1094. {
  1095. x.hiteach.duration.rank = index_hiteach_duration + 1;
  1096. }
  1097. int index_ies5Tch_duration = rank_ies5Tch_duration.FindIndex(i => i==x.ies5Tch.duration.value);
  1098. if (index_ies5Tch_duration!=-1)
  1099. {
  1100. x.ies5Tch.duration.rank = index_ies5Tch_duration + 1;
  1101. }
  1102. int index_otherTch_duration = rank_otherTch_duration.FindIndex(i => i==x.otherTch.duration.value);
  1103. if (index_otherTch_duration!=-1)
  1104. {
  1105. x.otherTch.duration.rank = index_otherTch_duration + 1;
  1106. }
  1107. #endregion
  1108. #region
  1109. int index_tmd_stick = rank_tmd_stick.FindIndex(i => i==x.tmd.stick.value);
  1110. if (index_tmd_stick!=-1)
  1111. {
  1112. x.tmd.stick.rank = index_tmd_stick + 1;
  1113. }
  1114. int index_hita_stick = rank_hita_stick.FindIndex(i => i==x.hita.stick.value);
  1115. if (index_hita_stick!=-1)
  1116. {
  1117. x.hita.stick.rank = index_hita_stick + 1;
  1118. }
  1119. int index_hiteach_stick = rank_hiteach_stick.FindIndex(i => i==x.hiteach.stick.value);
  1120. if (index_hiteach_stick!=-1)
  1121. {
  1122. x.hiteach.stick.rank = index_hiteach_stick + 1;
  1123. }
  1124. int index_ies5Tch_stick = rank_ies5Tch_stick.FindIndex(i => i==x.ies5Tch.stick.value);
  1125. if (index_ies5Tch_stick!=-1)
  1126. {
  1127. x.ies5Tch.stick.rank = index_ies5Tch_stick + 1;
  1128. }
  1129. int index_otherTch_stick = rank_otherTch_stick.FindIndex(i => i==x.otherTch.stick.value);
  1130. if (index_otherTch_stick!=-1)
  1131. {
  1132. x.otherTch.stick.rank = index_otherTch_stick + 1;
  1133. }
  1134. #endregion
  1135. });
  1136. return tchStickiness;
  1137. }
  1138. private static List<SchoolStick> OrderBySchoolStick(List<SchoolStick> schoolStickiness)
  1139. {
  1140. // 假设visit已经填充了数据
  1141. #region
  1142. var rank_tmd_count = schoolStickiness.Select(x => x.tmd.count.value).Distinct().OrderByDescending(v => v).ToList();
  1143. var rank_hita_count = schoolStickiness.Select(x => x.hita.count.value).Distinct().OrderByDescending(v => v).ToList();
  1144. var rank_hiteach_count = schoolStickiness.Select(x => x.hiteach.count.value).Distinct().OrderByDescending(v => v).ToList();
  1145. var rank_ies5Tch_count = schoolStickiness.Select(x => x.ies5Tch.count.value).Distinct().OrderByDescending(v => v).ToList();
  1146. var rank_otherTch_count = schoolStickiness.Select(x => x.otherTch.count.value).Distinct().OrderByDescending(v => v).ToList();
  1147. var rank_ies5Stu_count = schoolStickiness.Select(x => x.ies5Stu.count.value).Distinct().OrderByDescending(v => v).ToList();
  1148. var rank_otherStu_count = schoolStickiness.Select(x => x.otherStu.count.value).Distinct().OrderByDescending(v => v).ToList();
  1149. var rank_lesson_count = schoolStickiness.Select(x => x.lesson.value).Distinct().OrderByDescending(v => v).ToList();
  1150. #endregion
  1151. #region
  1152. var rank_tmd_duration = schoolStickiness.Select(x => x.tmd.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1153. var rank_hita_duration = schoolStickiness.Select(x => x.hita.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1154. var rank_hiteach_duration = schoolStickiness.Select(x => x.hiteach.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1155. var rank_ies5Tch_duration = schoolStickiness.Select(x => x.ies5Tch.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1156. var rank_otherTch_duration = schoolStickiness.Select(x => x.otherTch.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1157. var rank_ies5Stu_duration = schoolStickiness.Select(x => x.ies5Stu.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1158. var rank_otherStu_duration = schoolStickiness.Select(x => x.otherStu.duration.value).Distinct().OrderByDescending(v => v).ToList();
  1159. #endregion
  1160. #region
  1161. var rank_tmd_stick = schoolStickiness.Select(x => x.tmd.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1162. var rank_hita_stick = schoolStickiness.Select(x => x.hita.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1163. var rank_hiteach_stick = schoolStickiness.Select(x => x.hiteach.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1164. var rank_ies5Tch_stick = schoolStickiness.Select(x => x.ies5Tch.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1165. var rank_otherTch_stick = schoolStickiness.Select(x => x.otherTch.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1166. var rank_ies5Stu_stick = schoolStickiness.Select(x => x.ies5Stu.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1167. var rank_otherStu_stick = schoolStickiness.Select(x => x.otherStu.stick.value).Distinct().OrderByDescending(v => v).ToList();
  1168. #endregion
  1169. schoolStickiness.ForEach(x => {
  1170. #region
  1171. if (x.tmd.count.value>0)
  1172. {
  1173. int index_tmd_count = rank_tmd_count.FindIndex(i => i==x.tmd.count.value);
  1174. if (index_tmd_count!=-1)
  1175. {
  1176. x.tmd.count.rank = index_tmd_count + 1;
  1177. }
  1178. }
  1179. if (x.hita.count.value>0)
  1180. {
  1181. int index_hita_count = rank_hita_count.FindIndex(i => i==x.hita.count.value);
  1182. if (index_hita_count!=-1)
  1183. {
  1184. x.hita.count.rank = index_hita_count + 1;
  1185. }
  1186. }
  1187. if (x.hiteach.count.value>0)
  1188. {
  1189. int index_hiteach_count = rank_hiteach_count.FindIndex(i => i==x.hiteach.count.value);
  1190. if (index_hiteach_count!=-1)
  1191. {
  1192. x.hiteach.count.rank = index_hiteach_count + 1;
  1193. }
  1194. }
  1195. if (x.ies5Tch.count.value>0)
  1196. {
  1197. int index_ies5Tch_count = rank_ies5Tch_count.FindIndex(i => i==x.ies5Tch.count.value);
  1198. if (index_ies5Tch_count!=-1)
  1199. {
  1200. x.ies5Tch.count.rank = index_ies5Tch_count + 1;
  1201. }
  1202. }
  1203. if (x.otherTch.count.value>0)
  1204. {
  1205. int index_otherTch_count = rank_otherTch_count.FindIndex(i => i==x.otherTch.count.value);
  1206. if (index_otherTch_count!=-1)
  1207. {
  1208. x.otherTch.count.rank = index_otherTch_count + 1;
  1209. }
  1210. }
  1211. if (x.ies5Stu.count.value>0)
  1212. {
  1213. int index_ies5Stu_count = rank_ies5Stu_count.FindIndex(i => i==x.ies5Stu.count.value);
  1214. if (index_ies5Stu_count!=-1)
  1215. {
  1216. x.ies5Stu.count.rank = index_ies5Stu_count + 1;
  1217. }
  1218. }
  1219. if (x.otherStu.count.value>0)
  1220. {
  1221. int index_otherStu_count = rank_otherStu_count.FindIndex(i => i==x.otherStu.count.value);
  1222. if (index_otherStu_count!=-1)
  1223. {
  1224. x.otherStu.count.rank = index_otherStu_count + 1;
  1225. }
  1226. }
  1227. if (x.lesson.value>0)
  1228. {
  1229. int index_lesson_count = rank_lesson_count.FindIndex(i => i==x.lesson.value);
  1230. if (index_lesson_count!=-1)
  1231. {
  1232. x.lesson.rank = index_lesson_count + 1;
  1233. }
  1234. }
  1235. #endregion
  1236. #region
  1237. if (x.tmd.duration.value>0)
  1238. {
  1239. int index_tmd_duration = rank_tmd_duration.FindIndex(i => i==x.tmd.duration.value);
  1240. if (index_tmd_duration!=-1)
  1241. {
  1242. x.tmd.duration.rank = index_tmd_duration + 1;
  1243. }
  1244. }
  1245. if (x.hita.duration.value>0)
  1246. {
  1247. int index_hita_duration = rank_hita_duration.FindIndex(i => i==x.hita.duration.value);
  1248. if (index_hita_duration!=-1)
  1249. {
  1250. x.hita.duration.rank = index_hita_duration + 1;
  1251. }
  1252. }
  1253. if (x.hiteach.duration.value>0)
  1254. {
  1255. int index_hiteach_duration = rank_hiteach_duration.FindIndex(i => i==x.hiteach.duration.value);
  1256. if (index_hiteach_duration!=-1)
  1257. {
  1258. x.hiteach.duration.rank = index_hiteach_duration + 1;
  1259. }
  1260. }
  1261. if (x.ies5Tch.duration.value>0) {
  1262. int index_ies5Tch_duration = rank_ies5Tch_duration.FindIndex(i => i==x.ies5Tch.duration.value);
  1263. if (index_ies5Tch_duration!=-1)
  1264. {
  1265. x.ies5Tch.duration.rank = index_ies5Tch_duration + 1;
  1266. }
  1267. }
  1268. if (x.otherTch.duration.value>0)
  1269. {
  1270. int index_otherTch_duration = rank_otherTch_duration.FindIndex(i => i==x.otherTch.duration.value);
  1271. if (index_otherTch_duration!=-1)
  1272. {
  1273. x.otherTch.duration.rank = index_otherTch_duration + 1;
  1274. }
  1275. }
  1276. if (x.ies5Stu.duration.value>0)
  1277. {
  1278. int index_ies5Stu_duration = rank_ies5Stu_duration.FindIndex(i => i==x.ies5Stu.duration.value);
  1279. if (index_ies5Stu_duration!=-1)
  1280. {
  1281. x.ies5Stu.duration.rank = index_ies5Stu_duration + 1;
  1282. }
  1283. }
  1284. if (x.otherStu.duration.value>0)
  1285. {
  1286. int index_otherStu_duration = rank_otherStu_duration.FindIndex(i => i==x.otherStu.duration.value);
  1287. if (index_otherStu_duration!=-1)
  1288. {
  1289. x.otherStu.duration.rank = index_otherStu_duration + 1;
  1290. }
  1291. }
  1292. #endregion
  1293. #region
  1294. if (x.tmd.stick.value>0)
  1295. {
  1296. int index_tmd_stick = rank_tmd_stick.FindIndex(i => i==x.tmd.stick.value);
  1297. if (index_tmd_stick!=-1)
  1298. {
  1299. x.tmd.stick.rank = index_tmd_stick + 1;
  1300. }
  1301. }
  1302. if (x.hita.stick.value>0)
  1303. {
  1304. int index_hita_stick = rank_hita_stick.FindIndex(i => i==x.hita.stick.value);
  1305. if (index_hita_stick!=-1)
  1306. {
  1307. x.hita.stick.rank = index_hita_stick + 1;
  1308. }
  1309. }
  1310. if (x.hiteach.stick.value>0)
  1311. {
  1312. int index_hiteach_stick = rank_hiteach_stick.FindIndex(i => i==x.hiteach.stick.value);
  1313. if (index_hiteach_stick!=-1)
  1314. {
  1315. x.hiteach.stick.rank = index_hiteach_stick + 1;
  1316. }
  1317. }
  1318. if (x.ies5Tch.stick.value>0)
  1319. {
  1320. int index_ies5Tch_stick = rank_ies5Tch_stick.FindIndex(i => i==x.ies5Tch.stick.value);
  1321. if (index_ies5Tch_stick!=-1)
  1322. {
  1323. x.ies5Tch.stick.rank = index_ies5Tch_stick + 1;
  1324. }
  1325. }
  1326. if (x.otherTch.stick.value>0)
  1327. {
  1328. int index_otherTch_stick = rank_otherTch_stick.FindIndex(i => i==x.otherTch.stick.value);
  1329. if (index_otherTch_stick!=-1)
  1330. {
  1331. x.otherTch.stick.rank = index_otherTch_stick + 1;
  1332. }
  1333. }
  1334. if (x.ies5Stu.stick.value>0)
  1335. {
  1336. int index_ies5Stu_stick = rank_ies5Stu_stick.FindIndex(i => i==x.ies5Stu.stick.value);
  1337. if (index_ies5Stu_stick!=-1)
  1338. {
  1339. x.ies5Stu.stick.rank = index_ies5Stu_stick + 1;
  1340. }
  1341. }
  1342. if (x.otherStu.stick.value>0)
  1343. {
  1344. int index_otherStu_stick = rank_otherStu_stick.FindIndex(i => i==x.otherStu.stick.value);
  1345. if (index_otherStu_stick!=-1)
  1346. {
  1347. x.otherStu.stick.rank = index_otherStu_stick + 1;
  1348. }
  1349. }
  1350. #endregion
  1351. });
  1352. return schoolStickiness;
  1353. }
  1354. /// <summary>
  1355. /// 结算访问日志
  1356. /// </summary>
  1357. public static async Task<(List<SchoolStick> chinaSchoolStickiness, List<SchoolStick> globalSchoolStickiness)> VisitSettle(List<string> times ,AzureStorageFactory _azureStorage,AzureRedisFactory _azureRedis, Region2LongitudeLatitudeTranslator _longitudeLatitudeTranslator, IPSearcher _ipSearcher) {
  1358. List<SchoolStick> chinaSchoolStickiness = new List<SchoolStick>();
  1359. List<SchoolStick> globalSchoolStickiness = new List<SchoolStick>();
  1360. foreach (var timeDate in times)
  1361. {
  1362. List<ApiVisit> apiVisits = new List<ApiVisit>();
  1363. if (DateTimeOffset.TryParse(timeDate, out DateTimeOffset date))
  1364. {
  1365. BlobDownloadResult result = await _azureStorage.GetBlobContainerClient("0-service-log").GetBlobClient($"http-log/{date:yyyy}/{date:MM}/{date:dd}/index.log").DownloadContentAsync();
  1366. var content = result.Content.ToString();
  1367. content= content.Substring(0, content.Length-2);
  1368. if (content.EndsWith("}"))
  1369. {
  1370. content=$"[{content}]";
  1371. }
  1372. else
  1373. {
  1374. content=$"[{content}}}]";
  1375. }
  1376. apiVisits.AddRange(content.ToObject<List<ApiVisit>>());
  1377. var dateNow = DateTimeOffset.Now.GetGMTTime(8);
  1378. if ($"{dateNow:yyyy-MM-dd}".Equals($"{date:yyyy-MM-dd}"))
  1379. { //当前小时一致的
  1380. BlobDownloadResult resultHour = await _azureStorage.GetBlobContainerClient("0-service-log").GetBlobClient($"http-log/{date:yyyy}/{date:MM}/{date:dd}/{date:HH}.log").DownloadContentAsync();
  1381. var contentHour = resultHour.Content.ToString();
  1382. contentHour= contentHour.Substring(0, contentHour.Length-2);
  1383. if (contentHour.EndsWith("}"))
  1384. {
  1385. contentHour=$"[{contentHour}]";
  1386. }
  1387. else
  1388. {
  1389. contentHour=$"[{contentHour}}}]";
  1390. }
  1391. var httpLog = contentHour.ToObject<List<HttpLog>>();
  1392. (ConcurrentBag<ApiVisit> visits, ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)> uuidInfo) = await SystemService.ConvertHttpLog(httpLog, _azureRedis, _ipSearcher, _longitudeLatitudeTranslator, dateNow, false);
  1393. apiVisits.AddRange(visits);
  1394. }
  1395. }
  1396. //大陆学校
  1397. var chinaApiVisits = apiVisits.Where(x => x.hostName.Equals("大陆站"));
  1398. var chinaSchoolVisits = apiVisits.Where(x => !string.IsNullOrWhiteSpace(x.school) && x.hostName.Equals("大陆站")).GroupBy(x => x.school);
  1399. List<SchoolStick> chinaStickiness = CountSchoolStickiness(chinaSchoolVisits, chinaApiVisits);
  1400. chinaSchoolStickiness.AddRange(chinaStickiness);
  1401. //国际学校
  1402. var globalApiVisits = apiVisits.Where(x => x.hostName.Equals("国际站"));
  1403. var globalSchoolVisits = apiVisits.Where(x => !string.IsNullOrWhiteSpace(x.school) && x.hostName.Equals("国际站")).GroupBy(x => x.school);
  1404. List<SchoolStick> globalStickiness = CountSchoolStickiness(globalSchoolVisits, globalApiVisits);
  1405. globalSchoolStickiness.AddRange(globalStickiness);
  1406. }
  1407. return (chinaSchoolStickiness, globalSchoolStickiness);
  1408. }
  1409. public static async Task<(ConcurrentBag<ApiVisit> visits, ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)> uuidInfo)>
  1410. ConvertHttpLog(List<HttpLog> logs, AzureRedisFactory _azureRedis, IPSearcher _ipSearcher, Region2LongitudeLatitudeTranslator _longitudeLatitudeTranslator, DateTimeOffset gmt8Time, bool test = false)
  1411. {
  1412. ConcurrentBag<ApiVisit> visits = new ConcurrentBag<ApiVisit>();
  1413. ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)> uuidInfo = new ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)>();
  1414. object lockObj = new object();
  1415. await Parallel.ForEachAsync(logs, async (log, _) =>
  1416. {
  1417. if (!log.path.Contains("."))
  1418. {
  1419. string uuid = Guid.NewGuid().ToString();
  1420. List<string> schoolMatch = new List<string>();
  1421. List<string> useridMatch = new List<string>();
  1422. var vist = new ApiVisit()
  1423. {
  1424. id=uuid,
  1425. ip=log.ip,
  1426. tid=log.tid,
  1427. time= log.time,
  1428. userId= log.id,
  1429. school= log.school,
  1430. tname= log.name,
  1431. path = log.path,
  1432. client=log.p.Equals("os", StringComparison.OrdinalIgnoreCase) ? "ies5" : log.p,
  1433. scope=log.scope,
  1434. host= log.host,
  1435. hostName=log.hostName,
  1436. l=log.l,
  1437. year = $"{gmt8Time:yyyy}",
  1438. month = $"{gmt8Time:MM}",
  1439. day = $"{gmt8Time:dd}",
  1440. hour =$"{gmt8Time:HH}"
  1441. };
  1442. if (test)
  1443. {
  1444. var time = DateTimeOffset.FromUnixTimeMilliseconds(log.time);
  1445. //本地调试时间
  1446. if (vist.ip.Equals("0.0.0.1"))
  1447. {
  1448. time= time.AddHours(8);
  1449. }
  1450. vist.year = $"{time:yyyy}";
  1451. vist.month = $"{time:MM}";
  1452. vist.day = $"{time:dd}";
  1453. vist.hour =$"{time:HH}";
  1454. }
  1455. if (string.IsNullOrWhiteSpace(vist.userId))
  1456. {
  1457. //var jsonPathContext = new JsonPathContext();
  1458. //jsonPathContext.ValueSystem= new JsonTextValueSystem();
  1459. string path = "$..['id_token','idToken','idtoken','tmdid','id','teacherId','teacher','tid','tId','userid','userId','code','studentId','student','studentid']";
  1460. if (!log.param.ValueKind.Equals(JsonValueKind.Null)) {
  1461. JObject jsonObject = JObject.Parse(log.param.GetRawText());
  1462. //var nodes_path = jsonPathContext.SelectNodes(log.param, path);
  1463. var nodes_path = jsonObject.SelectTokens(path);
  1464. foreach (var node in nodes_path)
  1465. {
  1466. // 只获取是字符串的
  1467. if (node.Type.Equals(JTokenType.String))
  1468. {
  1469. switch (true)
  1470. {
  1471. case bool when node.Path.Contains("id_token")||node.Path.Contains("idToken")||node.Path.Contains("idtoken"):
  1472. {
  1473. try
  1474. {
  1475. var jwt = new JwtSecurityToken($"{node}");
  1476. string id = jwt.Payload.Sub;
  1477. var name = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("name"))?.Value;
  1478. if (!string.IsNullOrWhiteSpace(id) && long.TryParse(id, out long _id))
  1479. {
  1480. vist.userId=id;
  1481. vist.scope="teacher";
  1482. vist.tname=name;
  1483. useridMatch.Add(id);
  1484. }
  1485. }
  1486. catch (Exception ex) { }
  1487. break;
  1488. }
  1489. case bool when node.Path.Contains("tmdid")||node.Path.Contains("id")||node.Path.Contains("teacherId")
  1490. ||node.Path.Contains("teacher")||node.Path.Contains("tid")||node.Path.Contains("tId")||node.Path.Contains("userid")
  1491. ||node.Path.Contains("userId"):
  1492. {
  1493. if (!string.IsNullOrWhiteSpace($"{node}") && long.TryParse($"{node}", out long _id))
  1494. {
  1495. vist.userId=$"{node}";
  1496. vist.scope="teacher";
  1497. useridMatch.Add($"{node}");
  1498. }
  1499. break;
  1500. }
  1501. case bool when node.Path.Contains("studentId")||node.Path.Contains("student")||node.Path.Contains("studentid"):
  1502. {
  1503. if (!string.IsNullOrWhiteSpace($"{node}") && long.TryParse($"{node}", out long _id))
  1504. {
  1505. vist.userId=$"{node}";
  1506. vist.scope="student";
  1507. useridMatch.Add($"{node}");
  1508. }
  1509. break;
  1510. }
  1511. case bool when node.Path.Contains("code"):
  1512. {
  1513. if (!string.IsNullOrWhiteSpace($"{node}"))
  1514. {
  1515. if (long.TryParse($"{node}", out long _id))
  1516. {
  1517. vist.userId=$"{node}";
  1518. useridMatch.Add($"{node}");
  1519. }
  1520. else
  1521. {
  1522. string[] codes = $"{node}".Split("-");
  1523. foreach (var _code in codes)
  1524. {
  1525. if (long.TryParse(_code, out long _codeid))
  1526. {
  1527. vist.userId=$"{node}";
  1528. useridMatch.Add($"{_code}");
  1529. break;
  1530. }
  1531. }
  1532. }
  1533. }
  1534. break;
  1535. }
  1536. }
  1537. }
  1538. }
  1539. if (log.path.Contains("process-notify", StringComparison.OrdinalIgnoreCase))
  1540. {
  1541. string s = log.param.ToJsonString().Replace("\\", "");
  1542. Regex regextmdid = new Regex("\"tmdid\":\"(\\d+)\"");
  1543. Match matchtmdid = regextmdid.Match(s);
  1544. if (matchtmdid.Success)
  1545. {
  1546. var t = matchtmdid.Groups[1].Value;
  1547. if (!string.IsNullOrWhiteSpace(t))
  1548. {
  1549. vist.userId=t;
  1550. vist.scope="teacher";
  1551. useridMatch.Add(t);
  1552. }
  1553. }
  1554. Regex regexname = new Regex("\"tmdname\":\"(.+?)\"");
  1555. Match matchname = regexname.Match(s);
  1556. if (matchname.Success)
  1557. {
  1558. var t = matchname.Groups[1].Value;
  1559. if (!string.IsNullOrWhiteSpace(t))
  1560. {
  1561. vist.tname=t;
  1562. }
  1563. }
  1564. }
  1565. }
  1566. }
  1567. else
  1568. {
  1569. if (string.IsNullOrWhiteSpace($"{vist.scope}"))
  1570. {
  1571. vist.scope="teacher";
  1572. uuidInfo.Add((uuid, log, new List<string>() { vist.userId }, new List<string>()));
  1573. }
  1574. }
  1575. if (string.IsNullOrWhiteSpace(vist.school) || vist.school.Equals("true") || vist.school.Equals("false"))
  1576. {
  1577. vist.school="";
  1578. string path = "$..['school','id','schoolId','schoolid','schoolCode','school_code','schoolcode','code']";
  1579. if (!log.param.ValueKind.Equals(JsonValueKind.Null)) {
  1580. JObject jsonObject = JObject.Parse(log.param.GetRawText());
  1581. var nodes_path = jsonObject.SelectTokens(path);
  1582. foreach (var node in nodes_path)
  1583. {
  1584. // 只获取是字符串的
  1585. if (node.Type.Equals(JTokenType.String))
  1586. {
  1587. switch (true)
  1588. {
  1589. case bool when node.Path.Contains("school")||node.Path.Contains("id")||node.Path.Contains("schoolId")
  1590. ||node.Path.Contains("schoolid")||node.Path.Contains("schoolCode")||node.Path.Contains("school_code")||node.Path.Contains("schoolcode"):
  1591. {
  1592. if (!$"{node}".Contains("-")&& $"{node}".Length<=8 && $"{node}".Length>=1)
  1593. {
  1594. vist.school=$"{node}";
  1595. schoolMatch.Add($"{node}");
  1596. }
  1597. break;
  1598. }
  1599. case bool when node.Path.Contains("code"):
  1600. {
  1601. if (!$"{node}".Contains("-")&& $"{node}".Length<=8&& $"{node}".Length>=1)
  1602. {
  1603. vist.school=$"{node}";
  1604. schoolMatch.Add($"{node}");
  1605. }
  1606. else
  1607. {
  1608. string[] codes = $"{node}".Split("-");
  1609. foreach (var _code in codes)
  1610. {
  1611. if ($"{_code}".Length<=8 && $"{_code}".Length>=1)
  1612. {
  1613. vist.school=$"{_code}";
  1614. schoolMatch.Add($"{_code}");
  1615. break;
  1616. }
  1617. }
  1618. }
  1619. break;
  1620. }
  1621. }
  1622. }
  1623. }
  1624. if (log.path.Contains("process-notify", StringComparison.OrdinalIgnoreCase))
  1625. {
  1626. string s = log.param.ToJsonString().Replace("\\", "");
  1627. Regex regexname = new Regex("\"schoolId\":\"(.+?)\"");
  1628. Match matchname = regexname.Match(s);
  1629. if (matchname.Success)
  1630. {
  1631. var t = matchname.Groups[1].Value;
  1632. if (!string.IsNullOrWhiteSpace(t))
  1633. {
  1634. vist.school=t;
  1635. }
  1636. }
  1637. }
  1638. }
  1639. }
  1640. //处理 client
  1641. {
  1642. if (!string.IsNullOrWhiteSpace(log.client))
  1643. {
  1644. if (log.client.Equals("IES", StringComparison.OrdinalIgnoreCase))
  1645. {
  1646. vist.client="ies5";
  1647. }
  1648. if (log.client.Equals("AClassONE", StringComparison.OrdinalIgnoreCase))
  1649. {
  1650. vist.client="aclassone";
  1651. }
  1652. if (log.client.Equals("BB", StringComparison.OrdinalIgnoreCase))
  1653. {
  1654. vist.client="habb";
  1655. }
  1656. if (log.client.Equals("HiTool", StringComparison.OrdinalIgnoreCase) ||log.client.Equals("HiTools", StringComparison.OrdinalIgnoreCase))
  1657. {
  1658. vist.client="hiscan";
  1659. }
  1660. if (log.client.Equals("HiTA", StringComparison.OrdinalIgnoreCase))
  1661. {
  1662. vist.client="hita";
  1663. vist.scope="teacher";
  1664. }
  1665. if (log.client.Equals("HiTeachCC", StringComparison.OrdinalIgnoreCase))
  1666. {
  1667. vist.client="hiteachcc";
  1668. }
  1669. if (log.client.Equals("HiTeach", StringComparison.OrdinalIgnoreCase))
  1670. {
  1671. vist.scope="teacher";
  1672. vist.client="hiteach";
  1673. }
  1674. if (log.client.Equals("Open", StringComparison.OrdinalIgnoreCase))
  1675. {
  1676. vist.client="open";
  1677. }
  1678. }
  1679. }
  1680. //补全站点
  1681. {
  1682. if (
  1683. (log.host.Equals("wwww.teammodel.net")||log.host.Equals("ies5-rc.teammodel.net")))
  1684. {
  1685. vist.hostName="国际站";
  1686. vist.host="www.teammodel.net";
  1687. vist.client="ies5";
  1688. vist.l="Global";
  1689. }
  1690. else if (
  1691. log.host.Equals("teammodelos-yx.chinacloudsites.cn")||log.host.Equals("teammodelos.chinacloudsites.cn")
  1692. ||log.host.Equals("yx.teammodel.cn")||log.host.Equals("teammodelos-rc.chinacloudsites.cn")||log.host.Equals("rc.teammodel.cn")|| log.host.Equals("www.teammodel.cn"))
  1693. {
  1694. vist.hostName="大陆站";
  1695. vist.host="www.teammodel.cn";
  1696. vist.client="ies5"; vist.l="China";
  1697. }
  1698. else if ((log.host.Contains("localhost") && log.p.Equals("os"))|| log.host.Equals("teammodelos-test.chinacloudsites.cn") ||log.host.Equals("test.teammodel.cn"))
  1699. {
  1700. vist.hostName="测试站";
  1701. vist.host="test.teammodel.cn";
  1702. vist.client="ies5";
  1703. vist.l="China";
  1704. }
  1705. else if (
  1706. (log.host.Equals("scyx.teammodel.cn") ||log.host.Equals("jinniu.teammodel.cn")))
  1707. {
  1708. vist.hostName="研修2.0";
  1709. vist.host="scyx.teammodel.cn";
  1710. vist.client="ability"; vist.l="China";
  1711. }
  1712. else if (
  1713. log.host.Equals("open.teammodel.cn")||log.host.Equals("open-test.teammodel.cn") ||log.host.Equals("zhiyin-test.teammodel.cn"))
  1714. {
  1715. vist.hostName="开放平台";
  1716. vist.host="open.teammodel.cn";
  1717. vist.client="open";
  1718. vist.l="China";
  1719. }
  1720. else if ((log.host.Equals("bi-rc.teammodel.net") || log.host.Equals("bi.teammodel.net")))
  1721. {
  1722. vist.hostName="国际站";
  1723. vist.host="bi.teammodel.net";
  1724. vist.client="bi";
  1725. vist.scope="admin";
  1726. vist.l="Global";
  1727. }
  1728. else if (log.host.Equals("teammodelbi.chinacloudsites.cn") ||log.host.Equals("bi.teammodel.cn"))
  1729. {
  1730. vist.hostName="大陆站";
  1731. vist.host="bi.teammodel.cn";
  1732. vist.client="bi";
  1733. vist.scope="admin";
  1734. vist.l="China";
  1735. }
  1736. else if (log.host.Equals("bitest.teammodel.cn")||log.host.Equals("teammodelbi-test.chinacloudsites.cn")||(log.host.Contains("localhost") && (log.p.Equals("bi"))))
  1737. {
  1738. vist.hostName="测试站";
  1739. vist.host="testbi.teammodel.cn";
  1740. vist.client="bi";
  1741. vist.scope="admin";
  1742. vist.l="China";
  1743. }
  1744. else if (
  1745. log.host.Equals("teamcontest.chinacloudsites.cn")||log.host.Equals("contest.chinacloudsites.cn")||log.host.Equals("contest-test.chinacloudsites.cn"))
  1746. {
  1747. vist.hostName="大陆站";
  1748. vist.host="contest.teammodel.cn";
  1749. vist.client="contest"; vist.l="China";
  1750. }
  1751. else if (
  1752. log.host.Equals("hiteachcc.chinacloudsites.cn"))
  1753. {
  1754. vist.hostName="正式站";
  1755. vist.host="hiteachcc.teammodel.cn";
  1756. vist.client="hiteachcc";
  1757. vist.l="China";
  1758. }
  1759. else if (log.host.Equals("appraisal.chinacloudsites.cn"))
  1760. {
  1761. vist.hostName="大陆站";
  1762. vist.host="appraisal.teammodel.cn";
  1763. vist.client="appraisal";
  1764. vist.l="China";
  1765. }
  1766. else if ((log.host.Contains("localhost") && log.p.Equals("appraisal"))||log.host.Equals("appraisal-test.teammodel.cn") ||log.host.Equals("appraisal-test.chinacloudsites.cn"))
  1767. {
  1768. vist.hostName="测试站";
  1769. vist.host="appraisal-test.teammodel.cn";
  1770. vist.client="appraisal";
  1771. vist.l="China";
  1772. }
  1773. else
  1774. {
  1775. if (!string.IsNullOrWhiteSpace(log.p))
  1776. {
  1777. vist.client=log.p.Equals("os", StringComparison.OrdinalIgnoreCase) ? "ies5" : log.p;
  1778. }
  1779. else
  1780. {
  1781. vist.client="other";
  1782. }
  1783. if (log.host.EndsWith(".cn"))
  1784. {
  1785. vist.hostName="大陆站";
  1786. vist.l="China";
  1787. }
  1788. else if (log.host.EndsWith(".net"))
  1789. {
  1790. vist.hostName="国际站";
  1791. vist.l="Global";
  1792. }
  1793. else
  1794. {
  1795. vist.hostName="其他";
  1796. vist.l="其他";
  1797. }
  1798. }
  1799. }
  1800. //补全产品端
  1801. {
  1802. //研修2.0
  1803. if (log.path.Contains("research") || log.path.Contains("study") || log.path.Contains("standard-file"))
  1804. {
  1805. vist.client="ability";
  1806. }
  1807. if (log.path.Contains("teacher"))
  1808. {
  1809. vist.scope="teacher";
  1810. }
  1811. if (log.path.Contains("tmduser"))
  1812. {
  1813. vist.scope=Constant.ScopeTmdUser;
  1814. }
  1815. if (log.path.Contains("student/login"))
  1816. {
  1817. vist.scope=Constant.ScopeStudent;
  1818. }
  1819. if (log.path.StartsWith("/activity"))
  1820. {
  1821. vist.client="contest";
  1822. }
  1823. //小程序
  1824. if (log.path.Contains("aclassone"))
  1825. {
  1826. vist.client="aclassone";
  1827. }
  1828. // /// <summary>
  1829. /// ExamInfo qamode 書面問答類型 0:書面問答 1:紙本測驗 2:艺术评测
  1830. /// </summary>
  1831. //艺术评测
  1832. if (log.path.Contains("art") ||log.path.Contains("aclassone/find-children-activity") ||log.path.Contains("aclassone/find-teacher-activity") ||log.path.Contains("aclassone/find-summary-activity") ||log.path.Contains("aclassone/upload-all") ||log.path.Contains("aclassone/delete"))
  1833. {
  1834. vist.client="art";
  1835. }
  1836. if (log.path.Contains("common/exam/upsert-record"))
  1837. {
  1838. JObject jobject = JObject.Parse(log.param.GetRawText());
  1839. var jtokens = jobject.SelectTokens("$..artId");
  1840. if (jtokens!=null && jtokens.Count()>0)
  1841. {
  1842. vist.client="art";
  1843. vist.scope=Constant.ScopeStudent;
  1844. }
  1845. }
  1846. if (log.path.Contains("habb"))
  1847. {
  1848. vist.client="habb";
  1849. }
  1850. //阅卷客户端
  1851. if (log.path.Contains("hiscan"))
  1852. {
  1853. vist.client="hiscan";
  1854. }
  1855. if (log.path.Contains("hita"))
  1856. {
  1857. vist.client="hita";
  1858. }
  1859. if (log.path.Contains("hiteachcc"))
  1860. {
  1861. vist.client="hiteachcc";
  1862. }
  1863. if (log.path.Contains("sokrate"))
  1864. {
  1865. vist.client="sokrate";
  1866. }
  1867. if (log.path.Contains("sokrate") || log.path.Contains("score"))
  1868. {
  1869. vist.client="sokrate";
  1870. }
  1871. if (log.path.Contains("hiteach"))
  1872. {
  1873. vist.client="hiteach";
  1874. }
  1875. ///IES开放平台
  1876. if (log.path.Contains("business") || log.path.Contains("biz") || log.path.Contains("openapi-config") || log.path.Contains("open-api"))
  1877. {
  1878. vist.client="open";
  1879. }
  1880. //从token的role 能否获取 开放平台
  1881. //单点登录及第三方接口
  1882. if (log.path.Contains("lepei") || log.path.Contains("sc/") || log.path.Contains("/sso") || log.path.Contains("sc-init")|| log.path.Contains("moofen") || log.path.Contains("data-push") || log.path.Contains("xkw")|| log.path.Contains("tianbo")
  1883. || log.path.Contains("oauth/check-bind")|| log.path.Contains("dingding") || log.path.Contains("wechat")
  1884. )
  1885. {
  1886. vist.client="sso-third";
  1887. }
  1888. }
  1889. //处理IP转地区
  1890. var region = await _ipSearcher.SearchIpAsync(vist.ip);
  1891. if (string.IsNullOrWhiteSpace(region))
  1892. {
  1893. region="未知IP·未知IP·未知IP";
  1894. }
  1895. if (!string.IsNullOrWhiteSpace(region))
  1896. {
  1897. region= region.Replace("省·", "·").Replace("市·", "·").Replace("特别行政区·", "·").Replace("藏族羌族自治州·", "·");
  1898. var regions = region.Split("·");
  1899. if (regions.Length==4)
  1900. {
  1901. vist.area= regions[0];
  1902. vist.province = regions[1];
  1903. vist.city = regions[2];
  1904. }
  1905. if (regions.Length==3)
  1906. {
  1907. vist.area= regions[0];
  1908. vist.province = regions[1];
  1909. }
  1910. if (regions.Length==2)
  1911. {
  1912. vist.area= regions[0];
  1913. vist.province = regions[1];
  1914. }
  1915. if (regions.Length==1)
  1916. {
  1917. vist.area= regions[0];
  1918. }
  1919. }
  1920. //处理地区转经纬度
  1921. {
  1922. IEnumerable<JToken> tokens = default;
  1923. if (!string.IsNullOrWhiteSpace(vist.city) && !string.IsNullOrWhiteSpace(vist.province) && !string.IsNullOrWhiteSpace(vist.area))
  1924. {
  1925. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.province}/i && @.city=~ /.*{vist.city}/i)]");
  1926. if (!(tokens.Any() && tokens.Count()>0))
  1927. {
  1928. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.city}/i && @.city=~ /.*{vist.province}/i)]");
  1929. if (!(tokens.Any() && tokens.Count()>0))
  1930. {
  1931. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.city}/i || @.city=~ /.*{vist.city}/i)]");
  1932. }
  1933. if (!(tokens.Any() && tokens.Count()>0))
  1934. {
  1935. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.province}/i||@.city=~ /.*{vist.province}/i)]");
  1936. }
  1937. }
  1938. }
  1939. else if (string.IsNullOrWhiteSpace(vist.city) && !string.IsNullOrWhiteSpace(vist.province) && !string.IsNullOrWhiteSpace(vist.area))
  1940. {
  1941. if (vist.area.Equals("中国"))
  1942. {
  1943. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.province}/i)]");
  1944. if (!(tokens.Any() && tokens.Count()>0))
  1945. {
  1946. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.city=~ /.*{vist.province}/i)]");
  1947. }
  1948. }
  1949. else
  1950. {
  1951. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.city=~ /.*{vist.province}/i || @.province=~ /.*{vist.province}/i)]");
  1952. }
  1953. }
  1954. else if (!string.IsNullOrWhiteSpace(vist.city) && string.IsNullOrWhiteSpace(vist.province)&& !string.IsNullOrWhiteSpace(vist.area))
  1955. {
  1956. if (vist.area.Equals("中国"))
  1957. {
  1958. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.city}/i)]");
  1959. if (!(tokens.Any() && tokens.Count()>0))
  1960. {
  1961. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.city=~ /.*{vist.province}/i)]");
  1962. }
  1963. }
  1964. else
  1965. {
  1966. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.city=~ /.*{vist.city}/i || @.city=~ /.*{vist.province}/i)]");
  1967. }
  1968. }
  1969. else if (string.IsNullOrWhiteSpace(vist.city) && string.IsNullOrWhiteSpace(vist.province)&& !string.IsNullOrWhiteSpace(vist.area))
  1970. {
  1971. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.country=~ /.*{vist.area}/i && @.m=='1')]");
  1972. }
  1973. if (tokens!=null && tokens.Any())
  1974. {
  1975. List<RegionLngLat> regionLngLats = new List<RegionLngLat>();
  1976. foreach (JToken token in tokens)
  1977. {
  1978. regionLngLats.Add(token.ToString().ToObject<RegionLngLat>());
  1979. }
  1980. var points = regionLngLats.FindAll(x => string.IsNullOrWhiteSpace(x.area));
  1981. if (!points.IsNotEmpty())
  1982. {
  1983. points= regionLngLats.FindAll(x => !string.IsNullOrWhiteSpace(x.m) && x.m.Equals("1"));
  1984. }
  1985. if (string.IsNullOrWhiteSpace(vist.city))
  1986. {
  1987. vist.city=points?.FirstOrDefault()?.city;
  1988. if (!string.IsNullOrWhiteSpace(vist.city) && vist.city.EndsWith("市"))
  1989. {
  1990. vist.city=vist.city.Replace("市", "");
  1991. }
  1992. }
  1993. vist.lat=points?.FirstOrDefault()?.lat;
  1994. vist.lng=points?.FirstOrDefault()?.lng;
  1995. }
  1996. else
  1997. {
  1998. if (vist.area.Equals("内网IP"))
  1999. {
  2000. if (vist.host.Contains(".cn") || vist.host.Contains("localhost"))
  2001. {
  2002. vist.lat="30.655821878416408";
  2003. vist.lng="104.08153351042463";
  2004. vist.area="中国";
  2005. vist.province="四川";
  2006. vist.city="成都";
  2007. }
  2008. else
  2009. {
  2010. vist.lat="25.044332";
  2011. vist.lng="121.509062";
  2012. vist.area="中国";
  2013. vist.province="台湾";
  2014. vist.city="台湾";
  2015. }
  2016. }
  2017. else
  2018. {
  2019. vist.lat="30.655821878416408";
  2020. vist.lng="104.08153351042463";
  2021. vist.area="中国";
  2022. vist.province="四川";
  2023. vist.city="成都";
  2024. }
  2025. }
  2026. }
  2027. uuidInfo.Add((uuid, log, useridMatch, schoolMatch));
  2028. visits.Add(vist);
  2029. }
  2030. }
  2031. );
  2032. List<(string tmd, bool exists, string scope)> tmdexists = new List<(string tmd, bool exists, string scope)>();
  2033. List<(string sch, bool exists)> schexists = new List<(string sch, bool exists)>();
  2034. var tmds = uuidInfo.SelectMany(x => x.tmdid).ToHashSet();
  2035. if (tmds.Any())
  2036. {
  2037. foreach (var tmd in tmds)
  2038. {
  2039. var exists = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Blob:Catalog:{tmd}");
  2040. if (exists)
  2041. {
  2042. tmdexists.Add((tmd, exists, "teacher"));
  2043. }
  2044. else
  2045. {
  2046. exists = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Login:School:hbcn:student:{tmd}");
  2047. tmdexists.Add((tmd, exists, "student"));
  2048. }
  2049. }
  2050. }
  2051. var schs = uuidInfo.SelectMany(x => x.school).ToHashSet();
  2052. if (schs.Any())
  2053. {
  2054. foreach (var sch in schs)
  2055. {
  2056. var exists = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Blob:Catalog:{sch}");
  2057. schexists.Add((sch, exists));
  2058. }
  2059. }
  2060. foreach (var item in uuidInfo)
  2061. {
  2062. foreach (var tmd in item.tmdid)
  2063. {
  2064. var tmdext = tmdexists.Find(x => x.tmd.Equals(tmd));
  2065. if (tmdext.tmd!= null && tmdext.exists)
  2066. {
  2067. var vist = visits.Where(x => x.id.Equals(item.uuid));
  2068. foreach (var vi in vist)
  2069. {
  2070. vi.userId=tmd;
  2071. if (string.IsNullOrWhiteSpace(vi.scope))
  2072. {
  2073. vi.scope=tmdext.scope;
  2074. }
  2075. }
  2076. break;
  2077. }
  2078. }
  2079. foreach (var sch in item.school)
  2080. {
  2081. var schext = schexists.Find(x => x.sch.Equals(sch));
  2082. if (schext.sch!= null && schext.exists)
  2083. {
  2084. var vist = visits.Where(x => x.id.Equals(item.uuid));
  2085. foreach (var vi in vist)
  2086. {
  2087. vi.school=sch;
  2088. }
  2089. break;
  2090. }
  2091. }
  2092. }
  2093. return (visits, uuidInfo);
  2094. }
  2095. public class ApiVisit
  2096. {
  2097. public string id { get; set; }
  2098. public string path { get; set; }
  2099. /// <summary>
  2100. /// 细分
  2101. /// ies5 ,hiteach,hita,cc,bi,contest,open,aclassone,sokrates,ability,art
  2102. /// 产品端
  2103. /// </summary>
  2104. public string client { get; set; }
  2105. /// <summary>
  2106. /// 具体功能
  2107. /// </summary>
  2108. // public string func { get; set; }
  2109. public string userId { get; set; }
  2110. public string scope { get; set; }
  2111. public string tname { get; set; }
  2112. public string school { get; set; }
  2113. public string ip { get; set; }
  2114. //public string region { get; set; }
  2115. public string area { get; set; }
  2116. public string province { get; set; }
  2117. public string city { get; set; } = "";
  2118. public long time { get; set; }
  2119. /// <summary>
  2120. ///
  2121. /// </summary>
  2122. public string host { get; set; }
  2123. public string hostName { get; set; } = "其他";
  2124. /// <summary>
  2125. /// tokenid
  2126. /// </summary>
  2127. public string tid { get; set; }
  2128. public string year { get; set; }
  2129. public string month { get; set; }
  2130. public string day { get; set; }
  2131. public string hour { get; set; }
  2132. //public RegionLngLat point { get; set; }
  2133. public string lat { get; set; }
  2134. public string lng { get; set; }
  2135. public string l { get; set; }
  2136. }
  2137. public class HttpLog
  2138. {
  2139. public string ip { get; set; }
  2140. public long time { get; set; }
  2141. public string host { get; set; }
  2142. public string hostName = "其他";
  2143. public string tid { get; set; }
  2144. public string path { get; set; }
  2145. public string client { get; set; }
  2146. public JsonElement param { get; set; }
  2147. public string id { get; set; }
  2148. public string name { get; set; }
  2149. public string school { get; set; }
  2150. public string p { get; set; }
  2151. // public string ua { get; set; }
  2152. // public string referer { get; set; }
  2153. public string scope { get; set; }
  2154. public string year { get; set; }
  2155. public string month { get; set; }
  2156. public string day { get; set; }
  2157. public string hour { get; set; }
  2158. public string l { get; set; }
  2159. }
  2160. public record RegionLngLat
  2161. {
  2162. public string country { get; set; }
  2163. public string province { get; set; }
  2164. public string city { get; set; }
  2165. public string lat { get; set; }
  2166. public string lng { get; set; }
  2167. public string area { get; set; }
  2168. public string m { get; set; } = "0";
  2169. }
  2170. public class CountData
  2171. {
  2172. public string pk { get; set; } = "";
  2173. public string sk { get; set; } = "";
  2174. public string sp { get; set; } = "";
  2175. public int count { get; set; }
  2176. }
  2177. public class SchoolStick
  2178. {
  2179. /// <summary>
  2180. /// 名字
  2181. /// </summary>
  2182. public string name { get; set; }
  2183. public string picture { get; set; }
  2184. /// <summary>
  2185. /// 编码
  2186. /// </summary>
  2187. public string id { get; set; }
  2188. public string last { get; set; }
  2189. #region tmd
  2190. public ClientStick tmd { get; set; } = new ClientStick();
  2191. #endregion
  2192. #region hiteach
  2193. public ClientStick hiteach { get; set; } = new ClientStick();
  2194. public Indicator lesson { get; set; } = new Indicator();
  2195. #endregion
  2196. #region hita
  2197. public ClientStick hita { get; set; } = new ClientStick();
  2198. #endregion
  2199. #region ies5Teacher
  2200. public ClientStick ies5Tch { get; set; } = new ClientStick();
  2201. #endregion
  2202. #region otherTeacher
  2203. public ClientStick otherTch { get; set; } = new ClientStick();
  2204. #endregion
  2205. #region ies5Student
  2206. public ClientStick ies5Stu { get; set; } = new ClientStick();
  2207. #endregion
  2208. #region otherStudent
  2209. public ClientStick otherStu { get; set; } = new ClientStick();
  2210. #endregion
  2211. public List<TchStick> tchSticks { get; set; } = new List<TchStick>();
  2212. public List<StuStick> stuSticks { get; set; } = new List<StuStick>();
  2213. }
  2214. public class TchStick {
  2215. public string school { get; set; }
  2216. /// <summary>
  2217. /// 名字
  2218. /// </summary>
  2219. public string name { get; set; }
  2220. public string picture { get; set; }
  2221. /// <summary>
  2222. /// 编码
  2223. /// </summary>
  2224. public string id { get; set; }
  2225. public string last { get; set; }
  2226. #region tmd
  2227. public ClientStick tmd { get; set; } = new ClientStick();
  2228. #endregion
  2229. #region hiteach
  2230. public ClientStick hiteach { get; set; } = new ClientStick();
  2231. public Indicator lesson { get; set; } = new Indicator();
  2232. #endregion
  2233. #region hita
  2234. public ClientStick hita { get; set; } = new ClientStick();
  2235. #endregion
  2236. #region ies5Teacher
  2237. public ClientStick ies5Tch { get; set; } = new ClientStick();
  2238. #endregion
  2239. #region otherTeacher
  2240. public ClientStick otherTch { get; set; } = new ClientStick();
  2241. #endregion
  2242. }
  2243. public class StuStick
  2244. {
  2245. public string school { get; set; }
  2246. /// <summary>
  2247. /// 名字
  2248. /// </summary>
  2249. public string name { get; set; }
  2250. public string picture { get; set; }
  2251. /// <summary>
  2252. /// 编码
  2253. /// </summary>
  2254. public string id { get; set; }
  2255. public string last { get; set; }
  2256. #region tmd
  2257. public ClientStick tmd { get; set; } = new ClientStick();
  2258. #endregion
  2259. #region ies5Student
  2260. public ClientStick ies5Stu { get; set; } = new ClientStick();
  2261. #endregion
  2262. #region otherStudent
  2263. public ClientStick otherStu { get; set; } = new ClientStick();
  2264. #endregion
  2265. }
  2266. public class ClientStick
  2267. {
  2268. public int userCount { get; set; }
  2269. public Indicator stick { get; set; } = new Indicator();
  2270. public Indicator duration { get; set; } = new Indicator();
  2271. public Indicator count { get; set; } = new Indicator();
  2272. }
  2273. public class Indicator
  2274. {
  2275. /// <summary>
  2276. /// 值
  2277. /// </summary>
  2278. public double value { get; set; } = 0;
  2279. /// <summary>
  2280. /// 值的增减浮动
  2281. /// </summary>
  2282. public double range { get; set; } = 0;
  2283. /// <summary>
  2284. /// 排名
  2285. /// </summary>
  2286. public double rank { get; set; } = 0;
  2287. /// <summary>
  2288. /// 排名的上升下降
  2289. /// </summary>
  2290. public double updown { get; set; } = 0;
  2291. }
  2292. }
  2293. }