TestController.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using Azure.Storage.Blobs.Models;
  4. using HTEXLib.Helpers.ShapeHelpers;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.International.Converters.PinYinConverter;
  10. using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
  11. using Newtonsoft.Json;
  12. using StackExchange.Redis;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Net;
  18. using System.Net.Http;
  19. using System.Net.Http.Json;
  20. using System.Text;
  21. using System.Text.Json;
  22. using System.Text.RegularExpressions;
  23. using System.Threading.Tasks;
  24. using TEAMModelOS.SDK;
  25. using TEAMModelOS.SDK.DI;
  26. using TEAMModelOS.SDK.Extension;
  27. using TEAMModelOS.SDK.Models;
  28. using TEAMModelOS.SDK.Models.Cosmos.Common;
  29. using TEAMModelOS.Services.Common;
  30. namespace TEAMModelOS.Controllers
  31. {
  32. [Route("test")]
  33. [ApiController]
  34. public class TestController : ControllerBase
  35. {
  36. public IWebHostEnvironment _environment { get; set; }
  37. private readonly AzureStorageFactory _azureStorage;
  38. private readonly AzureRedisFactory _azureRedis;
  39. private readonly AzureCosmosFactory _azureCosmos;
  40. private readonly DingDing _dingDing;
  41. private readonly AzureServiceBusFactory _serviceBus;
  42. public IConfiguration _configuration { get; set; }
  43. public TestController(IWebHostEnvironment environment, AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, IConfiguration configuration, AzureServiceBusFactory serviceBus, DingDing dingDing)
  44. {
  45. _azureCosmos = azureCosmos;
  46. _azureRedis = azureRedis;
  47. _azureStorage = azureStorage;
  48. _dingDing = dingDing;
  49. _serviceBus = serviceBus; _configuration = configuration;
  50. _environment = environment;
  51. }
  52. public static List<string> comeRemoveStr = new List<string>() { "省", "市", "区", "州", "县", "旗", "盟", "自治" };
  53. public static List<string> prvcRemoveStr = new List<string>() { "回族", "维吾尔", "壮族", };
  54. public static List<string> cityRemoveStr = new List<string>() { "蒙古族", "地区", "壮族", "朝鲜族", "直辖" };
  55. public static List<string> nationRemoveStr = new List<string> {
  56. "维吾尔","回族", "自治", "满族", "蒙古", "壮族", "苗族" , "侗族", "瑶族",
  57. "达斡尔","鄂温克","朝鲜","畲族","土家","各族","仫佬","毛南","羌族","彝族","仡佬","布依","水族",
  58. "傣族","纳西","哈尼","拉祜","佤族","傈僳","独龙","怒族","白族","普米","固族","哈萨克","土族","撒拉","景颇","族"
  59. };
  60. public static List<string> schooRemoveStr = new List<string>() {"(",")", "(",")","省", "市", "校区", "区", "州", "县", "旗", "盟", "自治",
  61. "第","学校","校园","镇","村","乡","街","路","部","楼","与","站","馆","街道","办事处","义务"
  62. };
  63. public static List<KeyValuePair<string, string>> areaRemoveStr = new List<KeyValuePair<string, string>>{
  64. new KeyValuePair<string, string>("国际", "际") ,
  65. new KeyValuePair<string, string>("中国", ""),
  66. new KeyValuePair<string, string>("国家", ""),
  67. new KeyValuePair<string, string>("新区", ""),
  68. new KeyValuePair<string, string>("高新", "高"),
  69. new KeyValuePair<string, string>("园区", ""),
  70. new KeyValuePair<string, string>("产业", "产"),
  71. new KeyValuePair<string, string>("经济", "经"),
  72. new KeyValuePair<string, string>("开发", "开"),
  73. new KeyValuePair<string, string>("中心", "") ,
  74. new KeyValuePair<string, string>("集团", "") ,
  75. new KeyValuePair<string, string>("公司", "") ,
  76. new KeyValuePair<string, string>("有限", "") ,
  77. new KeyValuePair<string, string>("股份", "") ,
  78. new KeyValuePair<string, string>("服务", "") ,
  79. new KeyValuePair<string, string>("基地", "") ,
  80. new KeyValuePair<string, string>("社区", "社") ,
  81. new KeyValuePair<string, string>("职工", "") ,
  82. new KeyValuePair<string, string>("专修", "")
  83. ,
  84. };
  85. public static List<KeyValuePair<string, string>> schooReplaceStr = new List<KeyValuePair<string, string>> {
  86. new KeyValuePair<string, string>("职业技术学院", "职技") ,
  87. new KeyValuePair<string, string>("创新创业", "创") ,
  88. new KeyValuePair<string, string>("就业创业", "就") ,
  89. new KeyValuePair<string, string>("应用核技术", "核") ,
  90. new KeyValuePair<string, string>("职业学院", "职") ,
  91. new KeyValuePair<string, string>("幼儿园", "幼") ,
  92. new KeyValuePair<string, string>("幼儿", "幼") ,
  93. new KeyValuePair<string, string>("小学", "小") ,
  94. new KeyValuePair<string, string>("中学", "中") ,
  95. new KeyValuePair<string, string>("中专", "中") ,
  96. new KeyValuePair<string, string>("初中", "初") ,
  97. new KeyValuePair<string, string>("初级", "初") ,
  98. new KeyValuePair<string, string>("高中", "高") ,
  99. new KeyValuePair<string, string>("高级", "高") ,
  100. new KeyValuePair<string, string>("大学", "大") ,
  101. new KeyValuePair<string, string>("学院", "院") ,
  102. new KeyValuePair<string, string>("职业", "职") ,
  103. new KeyValuePair<string, string>("技术", "技") ,
  104. new KeyValuePair<string, string>("专科", "专") ,
  105. new KeyValuePair<string, string>("外国语", "外") ,
  106. new KeyValuePair<string, string>("实验", "验") ,
  107. new KeyValuePair<string, string>("年制", "制") ,
  108. new KeyValuePair<string, string>("高等", "高") ,
  109. new KeyValuePair<string, string>("分校", "分") ,
  110. new KeyValuePair<string, string>("分园", "分") ,
  111. new KeyValuePair<string, string>("分院", "分") ,
  112. new KeyValuePair<string, string>("院校", "院") ,
  113. new KeyValuePair<string, string>("初等", "小") ,
  114. new KeyValuePair<string, string>("附属", "附"),
  115. new KeyValuePair<string, string>("五年", "五") ,
  116. new KeyValuePair<string, string>("卫生", "卫") ,
  117. new KeyValuePair<string, string>("九年", "九") ,
  118. new KeyValuePair<string, string>("创业", "创") ,
  119. new KeyValuePair<string, string>("继续", "继") ,
  120. new KeyValuePair<string, string>("开放", "开") ,
  121. new KeyValuePair<string, string>("技师", "技") ,
  122. new KeyValuePair<string, string>("成人", "成") ,
  123. new KeyValuePair<string, string>("教育", "教") ,
  124. new KeyValuePair<string, string>("科技", "科") ,
  125. new KeyValuePair<string, string>("行政", "政") ,
  126. new KeyValuePair<string, string>("生物", "生") ,
  127. new KeyValuePair<string, string>("美术", "美") ,
  128. new KeyValuePair<string, string>("设计", "设") ,
  129. new KeyValuePair<string, string>("传播", "传") ,
  130. new KeyValuePair<string, string>("计算机", "算") ,
  131. new KeyValuePair<string, string>("土木", "土") ,
  132. new KeyValuePair<string, string>("石油", "油") ,
  133. new KeyValuePair<string, string>("科普", "普") ,
  134. new KeyValuePair<string, string>("电信", "信") ,
  135. new KeyValuePair<string, string>("联合", "联") ,
  136. new KeyValuePair<string, string>("电商", "商") ,
  137. new KeyValuePair<string, string>("纺织", "织") ,
  138. new KeyValuePair<string, string>("服装", "服") ,
  139. new KeyValuePair<string, string>("新闻", "新") ,
  140. new KeyValuePair<string, string>("网络", "网") ,
  141. new KeyValuePair<string, string>("应用", "应") ,
  142. new KeyValuePair<string, string>("畜牧", "畜") ,
  143. new KeyValuePair<string, string>("兽医", "兽") ,
  144. new KeyValuePair<string, string>("师范", "师") ,
  145. new KeyValuePair<string, string>("人文", "文") ,
  146. new KeyValuePair<string, string>("创新", "创") ,
  147. new KeyValuePair<string, string>("法官", "法") ,
  148. new KeyValuePair<string, string>("邮电", "邮") ,
  149. new KeyValuePair<string, string>("文化", "文") ,
  150. new KeyValuePair<string, string>("函授", "函") ,
  151. new KeyValuePair<string, string>("科学", "科") ,
  152. new KeyValuePair<string, string>("信息", "息") ,
  153. new KeyValuePair<string, string>("水利", "利") ,
  154. new KeyValuePair<string, string>("水电", "电") ,
  155. new KeyValuePair<string, string>("电力", "力") ,
  156. new KeyValuePair<string, string>("环境", "环") ,
  157. new KeyValuePair<string, string>("建材", "材") ,
  158. new KeyValuePair<string, string>("机电", "机") ,
  159. new KeyValuePair<string, string>("航空", "空") ,
  160. new KeyValuePair<string, string>("航天", "天") ,
  161. new KeyValuePair<string, string>("警察", "警") ,
  162. new KeyValuePair<string, string>("警官", "警") ,
  163. new KeyValuePair<string, string>("财贸", "财") ,
  164. new KeyValuePair<string, string>("电子", "电") ,
  165. new KeyValuePair<string, string>("建筑", "筑") ,
  166. new KeyValuePair<string, string>("艺术", "艺") ,
  167. new KeyValuePair<string, string>("体育", "体") ,
  168. new KeyValuePair<string, string>("城市", "城") ,
  169. new KeyValuePair<string, string>("地质", "地") ,
  170. new KeyValuePair<string, string>("医药", "药") ,
  171. new KeyValuePair<string, string>("政法", "法") ,
  172. new KeyValuePair<string, string>("铁路", "铁") ,
  173. new KeyValuePair<string, string>("铁道", "道") ,
  174. new KeyValuePair<string, string>("轨道", "轨") ,
  175. new KeyValuePair<string, string>("交通", "通") ,
  176. new KeyValuePair<string, string>("医学院", "医") ,
  177. new KeyValuePair<string, string>("医学", "医") ,
  178. new KeyValuePair<string, string>("传媒", "媒") ,
  179. new KeyValuePair<string, string>("工程", "程") ,
  180. new KeyValuePair<string, string>("临床", "临") ,
  181. new KeyValuePair<string, string>("化工", "化") ,
  182. new KeyValuePair<string, string>("林业", "林") ,
  183. new KeyValuePair<string, string>("老年", "老") ,
  184. new KeyValuePair<string, string>("旅游", "游") ,
  185. new KeyValuePair<string, string>("护理", "护") ,
  186. new KeyValuePair<string, string>("贸易", "贸") ,
  187. new KeyValuePair<string, string>("中医药", "中医") ,
  188. new KeyValuePair<string, string>("资源", "源") ,
  189. new KeyValuePair<string, string>("冶金", "冶") ,
  190. new KeyValuePair<string, string>("能源", "能") ,
  191. new KeyValuePair<string, string>("汽车", "车") ,
  192. new KeyValuePair<string, string>("医科", "医") ,
  193. new KeyValuePair<string, string>("机械", "械") ,
  194. new KeyValuePair<string, string>("应用", "应") ,
  195. new KeyValuePair<string, string>("电气", "电") ,
  196. new KeyValuePair<string, string>("材料", "材") ,
  197. new KeyValuePair<string, string>("劳动", "劳") ,
  198. new KeyValuePair<string, string>("轻工", "轻") ,
  199. new KeyValuePair<string, string>("农业", "农") ,
  200. new KeyValuePair<string, string>("委员会", "委") ,
  201. new KeyValuePair<string, string>("广播", "广"),
  202. new KeyValuePair<string, string>("电视", "电") ,
  203. new KeyValuePair<string, string>("工商", "商") ,
  204. new KeyValuePair<string, string>("工业", "工") ,
  205. new KeyValuePair<string, string>("管理", "管") ,
  206. new KeyValuePair<string, string>("社会主义", "社") ,
  207. new KeyValuePair<string, string>("社会", "社") ,
  208. new KeyValuePair<string, string>("自动化", "自") ,
  209. };
  210. public static List<KeyValuePair<string, string>> proReplaceStr = new List<KeyValuePair<string, string>> {
  211. new KeyValuePair<string, string>("北京","京") ,
  212. new KeyValuePair<string, string>("天津","津") ,
  213. new KeyValuePair<string, string>("河北","冀") ,
  214. new KeyValuePair<string, string>("山西","晋") ,
  215. new KeyValuePair<string, string>("内蒙古","蒙") ,
  216. new KeyValuePair<string, string>("辽宁","辽") ,
  217. new KeyValuePair<string, string>("吉林","吉") ,
  218. new KeyValuePair<string, string>("黑龙江","黑") ,
  219. new KeyValuePair<string, string>("上海","沪") ,
  220. new KeyValuePair<string, string>("江苏","苏") ,
  221. new KeyValuePair<string, string>("浙江","浙") ,
  222. new KeyValuePair<string, string>("安徽","皖") ,
  223. new KeyValuePair<string, string>("福建","闽") ,
  224. new KeyValuePair<string, string>("江西","赣") ,
  225. new KeyValuePair<string, string>("山东","鲁") ,
  226. new KeyValuePair<string, string>("河南","豫") ,
  227. new KeyValuePair<string, string>("湖北","鄂") ,
  228. new KeyValuePair<string, string>("湖南","湘") ,
  229. new KeyValuePair<string, string>("广东","粤") ,
  230. new KeyValuePair<string, string>("广西","桂") ,
  231. new KeyValuePair<string, string>("海南","琼") ,
  232. new KeyValuePair<string, string>("四川","川") ,
  233. new KeyValuePair<string, string>("贵州","贵") ,
  234. new KeyValuePair<string, string>("云南","云") ,
  235. new KeyValuePair<string, string>("重庆","渝") ,
  236. new KeyValuePair<string, string>("西藏","藏") ,
  237. new KeyValuePair<string, string>("陕西","陕") ,
  238. new KeyValuePair<string, string>("甘肃","甘") ,
  239. new KeyValuePair<string, string>("青海","青") ,
  240. new KeyValuePair<string, string>("宁夏","宁") ,
  241. new KeyValuePair<string, string>("新疆","新") ,
  242. new KeyValuePair<string, string>("香港","港") ,
  243. new KeyValuePair<string, string>("澳门","澳") ,
  244. new KeyValuePair<string, string>("台湾","台")
  245. };
  246. // new List<string>() {"省", "市", "校区", "区", "州", "县", "旗", "盟", "自治","小学","第","学校","中学","大学","初中","高中", "学院", "职业","技术","幼儿园","(",")", "(",")","分校","分园","附属","校园","镇","村","乡","街","路","部"
  247. public class SchoolData{
  248. public string province { get; set; }
  249. public string id { get; set; }
  250. public string name { get; set; }
  251. public string city { get; set; }
  252. public string address { get; set; }
  253. }
  254. /// <summary>
  255. /// 测试blob多线程写入同一个文件
  256. /// </summary>
  257. /// <returns></returns>
  258. [ProducesDefaultResponseType]
  259. [HttpPost("generate-school-code")]
  260. public async Task<IActionResult> Servicebus(JsonElement json)
  261. {
  262. string path = $"{_environment.ContentRootPath}/JsonFile/Core/region.json";
  263. var schools = json.GetProperty("schools").ToObject<List<SchoolData>>();
  264. schools= schools.Where((x, i) => schools.FindIndex(n => n.name.Equals(x.name)) == i).ToList();
  265. StreamReader streamReader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  266. StringBuilder stringBuilder = new StringBuilder();
  267. string text;
  268. while ((text = streamReader.ReadLine()) != null)
  269. {
  270. stringBuilder.Append(text.ToString());
  271. }
  272. streamReader.Close();
  273. string input = stringBuilder.ToString();
  274. List<Region> region = input.ToObject<List<Region>>(); ;
  275. try
  276. {
  277. schools.ForEach(x =>
  278. {
  279. x.province= ChineseConverter.Convert(x.province, ChineseConversionDirection.TraditionalToSimplified);
  280. x.city = ChineseConverter.Convert(x.city, ChineseConversionDirection.TraditionalToSimplified);
  281. x.name = ChineseConverter.Convert(x.name, ChineseConversionDirection.TraditionalToSimplified);
  282. if (!string.IsNullOrEmpty(x.address)) {
  283. x.address = ChineseConverter.Convert(x.address, ChineseConversionDirection.TraditionalToSimplified);
  284. }
  285. comeRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  286. prvcRemoveStr.ForEach(c => { x.province = x.province.Replace(c, ""); });
  287. var province = region.Find(r => r.name.Contains(x.province));
  288. string tmpprovince = x.province;
  289. string tmpcity = x.city;
  290. if (province != null)
  291. {
  292. x.province = province.name;
  293. comeRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  294. cityRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  295. nationRemoveStr.ForEach(c => { x.city = x.city.Replace(c, ""); });
  296. tmpcity = x.city;
  297. var city = province.children.Find(r => r.name.Contains(x.city));
  298. if (city == null)
  299. {
  300. city = province.children.Find(r => r.name.Contains(x.city));
  301. if (city == null)
  302. {
  303. city = province.children.SelectMany(x => x.children).ToList().Find(r => r.name.Contains(x.city));
  304. }
  305. }
  306. if (city != null)
  307. {
  308. x.city = city.name;
  309. }
  310. }
  311. string name = x.name;
  312. string[] names = name.Split("(");
  313. if (names.Length > 1)
  314. {
  315. name = $"{names[0]}{names[1].Substring(0, 1)}";
  316. }
  317. names = name.Split("(");
  318. if (names.Length > 1)
  319. {
  320. name = $"{names[0]}{names[1].Substring(0, 1)}";
  321. }
  322. name = Regex.Replace(name, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "");
  323. name = Regex.Replace(name, @"\s", "");
  324. name = name.Replace("\\", "");
  325. if (name.Length > 6)
  326. {
  327. //新区,高新,产业等非新政单位
  328. areaRemoveStr.ForEach(str =>
  329. {
  330. name = name.Replace(str.Key, str.Value);
  331. });
  332. //去除冗余的学校信息,只保留地名和校名关键信息
  333. schooRemoveStr.ForEach(str =>
  334. {
  335. name = name.Replace(str, "");
  336. });
  337. //替换民族信息词
  338. if (name.Length > 6)
  339. {
  340. nationRemoveStr.ForEach(str =>
  341. {
  342. name = name.Replace(str, "");
  343. });
  344. }
  345. //替换学校信息的副词
  346. if (name.Length > 6)
  347. {
  348. foreach (var str in schooReplaceStr) {
  349. if (name.Length <= 6)
  350. {
  351. break;
  352. }
  353. name = name.Replace(str.Key, str.Value);
  354. }
  355. }
  356. //替换省简称信息词
  357. string tmpname = name;
  358. if (name.Length > 6)
  359. {
  360. proReplaceStr.ForEach(str =>
  361. {
  362. name = name.Replace(str.Key, str.Value);
  363. });
  364. //如果替换后仍然大于6位,则取消,直接替换省份完整的词
  365. if (name.Length > 6)
  366. {
  367. name = tmpname.Replace(tmpprovince, "");
  368. }
  369. }
  370. //替换城市信息词
  371. if (name.Length > 6)
  372. {
  373. name = name.Replace(tmpcity, "");
  374. }
  375. }
  376. int len = name.Length;
  377. if (len >= 6)
  378. {
  379. name = $"{name.Substring(0, 1)}{name.Substring(len-5, 3)}{name.Substring(len - 2, 2)}";
  380. }
  381. //if (len <= 7 && len > 6)
  382. //{
  383. // name = name.Substring(len - 6);
  384. //}
  385. if (len <= 4)
  386. {
  387. name = $"{x.city.Substring(0, 2)}{name}";
  388. }
  389. if (len == 5)
  390. {
  391. name = $"{x.city.Substring(0, 1)}{name}";
  392. }
  393. string code = ToPinYin(name);
  394. x.address = name;
  395. x.id = code;
  396. });
  397. }
  398. catch (Exception ex)
  399. {
  400. await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  401. }
  402. string a_z = "abcdefghijklmnopqrstuvwxyz";
  403. var data= schools.GroupBy(x=>x.id).Select(x=>new {key= x.Key,more=x.ToList().Count>2, count = x.ToList().Count, list= x.ToList()}).ToList();
  404. data.ForEach(x =>
  405. {
  406. if (x.count > 1)
  407. {
  408. var first = x.list.First();
  409. var same = x.list.Skip(1).Take(x.count - 1).ToList();
  410. var fs = first.name.Select(x => x).ToArray();
  411. same.ForEach(z =>
  412. {
  413. var sp = z.name.Select(x => x).ToArray();
  414. int len = sp.Length;
  415. if (sp.Length >= fs.Length)
  416. {
  417. len = fs.Count();
  418. }
  419. for (int index = 0; index < len; index++)
  420. {
  421. if (!$"{sp[index]}".Equals($"{fs[index]}"))
  422. {
  423. short st = ChineseChar.GetStrokeNumber(sp[index]);
  424. if (st > 0)
  425. {
  426. var ins = st % 27;
  427. var insch = a_z[ins];
  428. if (z.name.EndsWith($"{insch}"))
  429. {
  430. ins = (st + 1) % 27;
  431. insch = a_z[ins];
  432. }
  433. z.id = $"{z.id.Substring(0, z.id.Length - 1)}{insch}";
  434. break;
  435. }
  436. }
  437. }
  438. });
  439. }
  440. });
  441. return Ok(data);
  442. }
  443. public static string ToPinYin(string val)
  444. {
  445. val = Regex.Replace(val, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!??::•`·、。,;,.;\"‘’“”-]", "");
  446. val = Regex.Replace(val, @"\s", "");
  447. val = val.Replace("\\", "");
  448. StringBuilder sb = new StringBuilder();
  449. val.ToCharArray().ToList().ForEach(x =>
  450. {
  451. if (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9'))
  452. {
  453. sb.Append(x);
  454. }
  455. else
  456. {
  457. try
  458. {
  459. ChineseChar cc = new ChineseChar(x);
  460. if (cc.Pinyins.Count > 0 && cc.Pinyins[0].Length > 0)
  461. {
  462. sb.Append(cc.Pinyins[0][0]);
  463. }
  464. }
  465. catch (Exception ex)
  466. {
  467. }
  468. }
  469. });
  470. return sb.ToString().ToLower();
  471. }
  472. /// <summary>
  473. /// 测试blob多线程写入同一个文件
  474. /// </summary>
  475. /// <returns></returns>
  476. [ProducesDefaultResponseType]
  477. [HttpPost("servicebus")]
  478. public async Task<IActionResult> Servicebus(ItemCondDto jsonMsg)
  479. {
  480. var messageBlob = new ServiceBusMessage(jsonMsg.ToJsonString());
  481. await _serviceBus.GetServiceBusClient().SendMessageAsync("itemcond", messageBlob);
  482. return Ok();
  483. }
  484. /// <summary>
  485. /// 测试blob多线程写入同一个文件
  486. /// </summary>
  487. /// <returns></returns>
  488. [ProducesDefaultResponseType]
  489. [HttpPost("blobroot")]
  490. public async Task<IActionResult> MultipleBlob(JsonElement jsonMsg)
  491. {
  492. if (jsonMsg.TryGetProperty("name", out JsonElement name) && name.ValueKind == JsonValueKind.String
  493. && jsonMsg.TryGetProperty("root", out JsonElement root) && root.ValueKind == JsonValueKind.String)
  494. {
  495. List<Dictionary<string, double?>> list = new List<Dictionary<string, double?>>();
  496. string u = System.Web.HttpUtility.UrlDecode($"{root}", Encoding.UTF8).Split("/")[0];
  497. var client = _azureStorage.GetBlobContainerClient($"{name}");
  498. var size = await client.GetBlobsSize(u);
  499. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", u);
  500. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", u, size.HasValue ? size.Value : 0);
  501. var scores = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{name}");
  502. double blobsize = 0;
  503. if (scores != default && scores != null)
  504. {
  505. foreach (var score in scores)
  506. {
  507. blobsize = blobsize + score.Score;
  508. list.Add(new Dictionary<string, double?>() { { score.Element.ToString(), score.Score } });
  509. }
  510. }
  511. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", new RedisValue($"{name}"), new RedisValue($"{blobsize}"));
  512. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob() 容器:{name}使用:{root},文件分类:{list.ToJsonString()}",
  513. GroupNames.成都开发測試群組);
  514. return Ok(list);
  515. }
  516. else
  517. {
  518. return Ok();
  519. }
  520. }
  521. private async Task<int> SendNotification()
  522. {
  523. HttpClient _httpClient = new HttpClient();
  524. // _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
  525. var pa = new { grant_type = "device", client_id = "9794e418-c4ef-4fd5-a42d-accaa2d96d6e", client_secret = "ruL?I79h0w1AZaZXtBaZeZuQLQXLa=:-" };
  526. HttpResponseMessage responseMessage = await _httpClient.PostAsJsonAsync("https://api2-rc.teammodel.net/oauth2/token", pa);
  527. if (responseMessage.StatusCode == HttpStatusCode.OK)
  528. {
  529. return 200;
  530. }
  531. else if (responseMessage.StatusCode == HttpStatusCode.Unauthorized)
  532. {
  533. return 401;
  534. }
  535. else
  536. {
  537. return 500;
  538. }
  539. }
  540. /// <summary>
  541. /// 测试redis通配符
  542. /// </summary>
  543. /// <param name="request"></param>
  544. /// <returns></returns>
  545. [ProducesDefaultResponseType]
  546. [HttpGet("test-delete-read")]
  547. public async Task<IActionResult> TestDelete()
  548. {
  549. foreach (var cnt in _azureStorage.GetBlobServiceClient().GetBlobContainers())
  550. {
  551. Console.WriteLine(cnt.Name);
  552. }
  553. await SendNotification();
  554. var client = _azureCosmos.GetCosmosClient();
  555. string aaa = "0";
  556. try
  557. {
  558. ItemResponse<Student> a = await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemAsync<Student>("1111111", new PartitionKey($"Course-111111"));
  559. Ok(a.GetRawResponse().Status);
  560. }
  561. catch (CosmosException ex)
  562. {
  563. if (ex.Response.Status == 404)
  564. {
  565. aaa = "404";
  566. }
  567. }
  568. try
  569. {
  570. ItemResponse<Student> a = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<Student>("1111111", new PartitionKey($"Course-111111"));
  571. Ok(a.GetRawResponse().Status);
  572. }
  573. catch (CosmosException ex)
  574. {
  575. if (ex.Response.Status == 404)
  576. {
  577. aaa = aaa + " 404";
  578. }
  579. }
  580. try
  581. {
  582. var a = await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemStreamAsync("1111111", new PartitionKey($"Course-111111"));
  583. Ok(a.Status);
  584. }
  585. catch (CosmosException ex)
  586. {
  587. if (ex.Response.Status == 404)
  588. {
  589. aaa = "404";
  590. }
  591. }
  592. try
  593. {
  594. var a = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemStreamAsync("1111111", new PartitionKey($"Course-111111"));
  595. Ok(a.Status);
  596. }
  597. catch (CosmosException ex)
  598. {
  599. if (ex.Response.Status == 404)
  600. {
  601. aaa = aaa + " 404";
  602. }
  603. }
  604. return Ok(new { aaa });
  605. }
  606. /// <summary>
  607. /// 测试redis通配符
  608. /// </summary>
  609. /// <param name="request"></param>
  610. /// <returns></returns>
  611. [ProducesDefaultResponseType]
  612. [HttpGet("test-redis")]
  613. public async Task<IActionResult> TestRedis()
  614. {
  615. try
  616. {
  617. var client = _azureCosmos.GetCosmosClient();
  618. List<ItemInfo> items = new List<ItemInfo>();
  619. var queryslt = $"SELECT value(c) FROM c where c.pid = null ";
  620. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ItemInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-hbcn") }))
  621. {
  622. items.Add(item);
  623. }
  624. List<ItemCond> itemConds = new List<ItemCond>();
  625. items.GroupBy(x => x.periodId).Select(y => new { key = y.Key, list = y.ToList() }).ToList().ForEach(z =>
  626. {
  627. ItemCond cond = new ItemCond() { id = z.key, code = $"ItemCond-hbcn", pk = "ItemCond", ttl = -1, count = z.list.Count, grades = new List<GradeCount>(), subjects = new List<SubjectCount>() };
  628. z.list.ForEach(y =>
  629. {
  630. ItemService.CountItemCond(y, null, cond);
  631. });
  632. itemConds.Add(cond);
  633. });
  634. itemConds.ForEach(async cond =>
  635. {
  636. await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(cond, new PartitionKey(cond.code));
  637. });
  638. return Ok(new { itemConds });
  639. }
  640. catch (Exception ex) { await _dingDing.SendBotMsg($"TEAMModelFunction,ActivityHttpTrigger,fix-itemcond()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組); }
  641. return Ok(new { });
  642. }
  643. /// <summary>
  644. /// 删除
  645. /// </summary>
  646. /// <param name="request"></param>
  647. /// <returns></returns>
  648. [ProducesDefaultResponseType]
  649. //[AuthToken(Roles = "teacher")]
  650. [HttpPost("fix-blob-content")]
  651. public async Task<IActionResult> FixBlobContent(JsonElement request)
  652. {
  653. try
  654. {
  655. if (!request.TryGetProperty("name", out JsonElement name)) return BadRequest();
  656. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  657. var client = _azureCosmos.GetCosmosClient();
  658. List<string> prefixs = new List<string>() { "audio", "doc", "image", "other", "res", "video", "thum" };
  659. var ContainerClient = _azureStorage.GetBlobContainerClient($"{name}");
  660. string scope = "private";
  661. scope = $"{_scope}";
  662. var tb = "Teacher";
  663. if (scope != "private")
  664. {
  665. tb = "School";
  666. }
  667. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  668. foreach (var prefix in prefixs)
  669. {
  670. List<string> items = await ContainerClient.List(prefix);
  671. foreach (var item in items)
  672. {
  673. var urlsSize = await ContainerClient.GetBlobsSize(item);
  674. Bloblog bloblog = new Bloblog { id = Guid.NewGuid().ToString(), code = $"Bloblog-{name}", pk = "Bloblog", time = now, size = urlsSize != null && urlsSize.HasValue ? urlsSize.Value : 0, type = prefix };
  675. await client.GetContainer(Constant.TEAMModelOS, tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
  676. }
  677. }
  678. return new OkObjectResult(new { });
  679. }
  680. catch (Exception ex)
  681. {
  682. await _dingDing.SendBotMsg($"TEAMModelFunction,ActivityHttpTrigger,fix-blob-content()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  683. return new BadRequestResult();
  684. }
  685. }
  686. /// <summary>
  687. /// 删除
  688. /// </summary>
  689. /// <param name="request"></param>
  690. /// <returns></returns>
  691. [ProducesDefaultResponseType]
  692. //[AuthToken(Roles = "teacher")]
  693. [HttpPost("get-data")]
  694. public async Task<IActionResult> GetData(JsonElement request)
  695. {
  696. try
  697. {
  698. var client = _azureCosmos.GetCosmosClient();
  699. return Ok(new { code = 1 });
  700. }
  701. catch (Exception e)
  702. {
  703. return BadRequest(e.StackTrace);
  704. }
  705. }
  706. }
  707. }