BINoticeController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Options;
  6. using System.Net.Http;
  7. using TEAMModelOS.SDK.DI;
  8. using TEAMModelOS.SDK;
  9. using TEAMModelOS.Models;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelOS.SDK.Context.Constant;
  13. using System.Collections.Generic;
  14. using System.Text;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelBI.Models;
  17. using System.Linq;
  18. using TEAMModelBI.Filter;
  19. using TEAMModelOS.SDK.Models.Cosmos.BI;
  20. using Azure.Cosmos;
  21. using System;
  22. using TEAMModelOS.SDK.Models.Service.BI;
  23. using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
  24. using TEAMModelBI.Tool.Extension;
  25. using TEAMModelOS.SDK.Models;
  26. using DocumentFormat.OpenXml.Spreadsheet;
  27. using System.Net.Http.Json;
  28. namespace TEAMModelBI.Controllers.BICommon
  29. {
  30. [Route("notice")]
  31. [ApiController]
  32. public class BINoticeController : ControllerBase
  33. {
  34. private readonly AzureCosmosFactory _azureCosmos;
  35. private readonly DingDing _dingDing;
  36. private readonly Option _option;
  37. private readonly AzureStorageFactory _azureStorage;
  38. private readonly IConfiguration _configuration;
  39. private readonly AzureServiceBusFactory _serviceBus;
  40. private readonly IHttpClientFactory _http;
  41. private readonly CoreAPIHttpService _coreAPIHttpService;
  42. private readonly IWebHostEnvironment _environment; //读取文件
  43. private readonly HttpClient _httpClient;
  44. public BINoticeController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, IWebHostEnvironment hostingEnvironment, HttpClient httpClient)
  45. {
  46. _azureCosmos = azureCosmos;
  47. _dingDing = dingDing;
  48. _azureStorage = azureStorage;
  49. _option = option?.Value;
  50. _configuration = configuration;
  51. _serviceBus = serviceBus;
  52. _http = http;
  53. _coreAPIHttpService = coreAPIHttpService;
  54. _environment = hostingEnvironment;
  55. _httpClient = httpClient;
  56. }
  57. /// <summary>
  58. /// 查询账户信息
  59. /// </summary>
  60. /// <param name="jsonElement"></param>
  61. /// <returns></returns>
  62. [AuthToken(Roles = "admin,rdc,assist,sales")]
  63. [HttpPost("get-info")]
  64. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  65. {
  66. jsonElement.TryGetProperty("ids", out JsonElement _ids);
  67. var cosmosClient = _azureCosmos.GetCosmosClient();
  68. List<string> ids = new();
  69. ids = _ids.ToObject<List<string>>();
  70. List<TmdUserinfo> userinfos = new();
  71. List<string> noFound = new();
  72. if (ids.Count > 0)
  73. {
  74. var content = new StringContent(ids.ToArray().ToJsonString(), Encoding.UTF8, "application/json");
  75. string idJson = await _coreAPIHttpService.GetUserInfos(content);
  76. userinfos = idJson.ToObject<List<TmdUserinfo>>();
  77. foreach (var item in ids)
  78. {
  79. var tempId = userinfos.Where(s => s.id.Equals(item)).ToList();
  80. var tempMail = userinfos.Where(s => !string.IsNullOrEmpty($"{s.mail}") && s.mail.Equals(item)).ToList();
  81. var tempmobile = userinfos.Where(s => !string.IsNullOrEmpty($"{s.mobile}") && s.mobile.Equals(item)).ToList();
  82. if (tempId.Count == 0 && tempMail.Count == 0 && tempmobile.Count == 0)
  83. noFound.Add(item);
  84. }
  85. }
  86. return Ok(new { state = RespondCode.Ok, userinfos, noFound });
  87. }
  88. /// <summary>
  89. /// 依据学校id查询学校教师信息
  90. /// </summary>
  91. /// <param name="jsonElement"></param>
  92. /// <returns></returns>
  93. //[AuthToken(Roles = "admin,rdc,assist,sales")]
  94. [HttpPost("get-tchinfos")]
  95. public async Task<IActionResult> GetSchoolTeacher(JsonElement jsonElement)
  96. {
  97. if (!jsonElement.TryGetProperty("scId", out JsonElement scId)) return BadRequest();
  98. List<IdInfo> idInfos = new();
  99. var cosmosClient = _azureCosmos.GetCosmosClient();
  100. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS,"School").GetItemQueryIterator<IdInfo>(queryText: "select c.id,c.name,c.picture from c",requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{scId}") }))
  101. {
  102. idInfos.Add(item);
  103. }
  104. return Ok(new { state = RespondCode.Ok, idInfos });
  105. }
  106. /// <summary>
  107. /// BI发布端外消息
  108. /// </summary>
  109. /// <param name="jsonElement"></param>
  110. /// <returns></returns>
  111. [AuthToken(Roles = "admin,rdc,assist,sales")]
  112. [HttpPost("push-info")]
  113. public async Task<IActionResult> PushNotion(JsonElement jsonElement)
  114. {
  115. try
  116. {
  117. var (_tmdId, _tmdName, _, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  118. jsonElement.TryGetProperty("type", out JsonElement type);
  119. jsonElement.TryGetProperty("jumpUrl", out JsonElement jumpUrl);
  120. jsonElement.TryGetProperty("callbackUrl", out JsonElement callbackUrl);
  121. if (!jsonElement.TryGetProperty("theme", out JsonElement theme)) return BadRequest();
  122. if (!jsonElement.TryGetProperty("content", out JsonElement content)) return BadRequest();
  123. jsonElement.TryGetProperty("tmdIds", out JsonElement _tmdIds);
  124. jsonElement.TryGetProperty("schoolIds", out JsonElement _schoolIds);
  125. jsonElement.TryGetProperty("areaIds", out JsonElement _areaIds);
  126. if (!jsonElement.TryGetProperty("crowdType", out JsonElement _crowdType)) return BadRequest();
  127. jsonElement.TryGetProperty("sendTime", out JsonElement sendTime);
  128. jsonElement.TryGetProperty("source", out JsonElement source);
  129. var cosmosClient = _azureCosmos.GetCosmosClient();
  130. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  131. List<IdNameCode> idNameCodes = new();
  132. BINotice bINotice = new();
  133. List<string> tmdIds = new();
  134. List<string> schoolIds = new();
  135. List<string> areaIds = new();
  136. if (!string.IsNullOrEmpty($"{sendTime}"))
  137. dateTime = DateTimeOffset.Parse($"{sendTime}");
  138. if (string.IsNullOrEmpty($"{_tmdIds}") && string.IsNullOrEmpty($"{_schoolIds}") && string.IsNullOrEmpty($"{_areaIds}"))
  139. return Ok(new { state = RespondCode.ParamsError, msg = "发送人群不能为空" });
  140. if (!string.IsNullOrEmpty($"{_tmdIds}"))
  141. tmdIds = _tmdIds.ToObject<List<string>>();
  142. if (!string.IsNullOrEmpty($"{_schoolIds}"))
  143. schoolIds = _schoolIds.ToObject<List<string>>();
  144. if (!string.IsNullOrEmpty($"{_areaIds}"))
  145. areaIds = _areaIds.ToObject<List<string>>();
  146. Crowd crowd = new()
  147. {
  148. tmdIds = tmdIds.Count > 0 ? tmdIds : new List<string>(),
  149. schoolIds = schoolIds.Count > 0 ? schoolIds : new List<string>(),
  150. areaIds = areaIds.Count > 0 ? areaIds : new List<string>(),
  151. type = $"{_crowdType}"
  152. };
  153. if (tmdIds.Count > 0)
  154. {
  155. StringBuilder tchSql = new($"select c.id, c.name,c.code,c.picture,c.nickname from c ");
  156. if (tmdIds.Count > 0)
  157. {
  158. tchSql.Append(BICommonWay.ManyScSql(" where c.id ", tmdIds));
  159. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<IdNameCode>(queryText: tchSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  160. {
  161. var tempId = idNameCodes.FindAll(fa => fa.id.Equals(item)).ToList();
  162. if (tempId.Count == 0)
  163. idNameCodes.Add(item);
  164. }
  165. }
  166. }
  167. if (areaIds.Count > 0)
  168. {
  169. StringBuilder scAreaSql = new($"select value(c.id) from c");
  170. if (schoolIds.Count > 0)
  171. {
  172. scAreaSql.Append(BICommonWay.ManyScSql(" where c.areaId", areaIds));
  173. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<string>(queryText: scAreaSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  174. {
  175. var tempScId = schoolIds.FindAll(fa => fa.Equals(item)).ToList();
  176. if (tempScId.Count == 0)
  177. schoolIds.Add(item);
  178. }
  179. }
  180. }
  181. if (schoolIds.Count > 0)
  182. {
  183. StringBuilder scTchSql = new($"select c.id, c.name, c.picture,c.nickname from c where c.pk='Teacher'");
  184. if (schoolIds.Count > 0)
  185. {
  186. scTchSql.Append(BICommonWay.ManyScSql(" and c.code", schoolIds, $"Teacher-"));
  187. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<IdNameCode>(queryText: scTchSql.ToString(), requestOptions: new QueryRequestOptions() { }))
  188. {
  189. var tempId = idNameCodes.FindAll(fa => fa.id.Equals(item)).ToList();
  190. if (tempId.Count == 0)
  191. idNameCodes.Add(item);
  192. }
  193. }
  194. }
  195. bINotice = new()
  196. {
  197. id = Guid.NewGuid().ToString(),
  198. code = "BINotice",
  199. type = !string.IsNullOrEmpty($"{type}") ? type.GetInt32() : 0,
  200. jumpUrl = !string.IsNullOrEmpty($"{jumpUrl}") ? $"{jumpUrl}" : "",
  201. callbackUrl = !string.IsNullOrEmpty($"{callbackUrl}") ? $"{callbackUrl}" : "",
  202. theme = $"{theme}",
  203. content = $"{content}",
  204. crowd = crowd,
  205. crowdIds = tmdIds,
  206. createId = _tmdId,
  207. sendTime = dateTime.ToUnixTimeMilliseconds(),//发布时间待解决
  208. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  209. source = !string.IsNullOrEmpty($"{source}") ? $"{source}" : "BI"
  210. };
  211. bINotice = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync<BINotice>(bINotice, new PartitionKey("BINotice"));
  212. //BI发送端外通知
  213. _coreAPIHttpService.BIPushNotify(bINotice, new Dictionary<string, object> { { "tmdid", _tmdIds }, { "tmdname", _tmdName } }, _option.Location, _configuration, _dingDing);
  214. return Ok(new { state = RespondCode.Ok, bINotice });
  215. }
  216. catch (Exception ex)
  217. {
  218. _ = _dingDing.SendBotMsg($"BI, notion/PushNotion() \n{ex.Message}\n{ex.StackTrace}\n", GroupNames.成都开发測試群組);
  219. return BadRequest();
  220. }
  221. }
  222. /// <summary>
  223. /// 查询发布的消息信息
  224. /// </summary>
  225. /// <param name="jsonElement"></param>
  226. /// <returns></returns>
  227. [AuthToken(Roles = "admin,rdc,assist,sales")]
  228. [HttpPost("get-infos")]
  229. public async Task<IActionResult> GetInfos(JsonElement jsonElement)
  230. {
  231. jsonElement.TryGetProperty("id", out JsonElement id);
  232. var cosmosClient = _azureCosmos.GetCosmosClient();
  233. List<BINotice> bINotices = new();
  234. StringBuilder sql = new("select value(c) from c");
  235. if (!string.IsNullOrEmpty($"{id}"))
  236. sql.Append($" where c.id='{id}'");
  237. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS,"Common").GetItemQueryIterator<BINotice>(queryText:sql.ToString(),requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("BINotice") }))
  238. {
  239. bINotices.Add(item);
  240. }
  241. return Ok(new { state = RespondCode.Ok, bINotices });
  242. }
  243. }
  244. }