BINoticeController.cs 13 KB

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