123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
- using System.Net.Http;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK;
- using TEAMModelOS.Models;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Context.Constant;
- using System.Collections.Generic;
- using System.Text;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelBI.Models;
- using System.Linq;
- using TEAMModelBI.Filter;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- using Azure.Cosmos;
- using System;
- using TEAMModelOS.SDK.Models.Service.BI;
- using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
- using TEAMModelBI.Tool.Extension;
- using TEAMModelOS.SDK.Models;
- namespace TEAMModelBI.Controllers.BICommon
- {
- [Route("notice")]
- [ApiController]
- public class BINoticeController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly AzureStorageFactory _azureStorage;
- private readonly IConfiguration _configuration;
- private readonly AzureServiceBusFactory _serviceBus;
- private readonly IHttpClientFactory _http;
- private readonly CoreAPIHttpService _coreAPIHttpService;
- private readonly IWebHostEnvironment _environment; //读取文件
- private readonly HttpClient _httpClient;
- public BINoticeController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, IWebHostEnvironment hostingEnvironment, HttpClient httpClient)
- {
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _azureStorage = azureStorage;
- _option = option?.Value;
- _configuration = configuration;
- _serviceBus = serviceBus;
- _http = http;
- _coreAPIHttpService = coreAPIHttpService;
- _environment = hostingEnvironment;
- _httpClient = httpClient;
- }
- /// <summary>
- /// 查询账户信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [AuthToken(Roles = "admin,rdc,assist,sales")]
- [HttpPost("get-info")]
- public async Task<IActionResult> GetInfo(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("ids", out JsonElement _ids);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- List<string> ids = new();
- ids = _ids.ToObject<List<string>>();
- List<TmdUserinfo> userinfos = new();
- List<string> noFound = new();
- if (ids.Count > 0)
- {
- var content = new StringContent(ids.ToArray().ToJsonString(), Encoding.UTF8, "application/json");
- string idJson = await _coreAPIHttpService.GetUserInfos(content);
- userinfos = idJson.ToObject<List<TmdUserinfo>>();
- foreach (var item in ids)
- {
- var tempId = userinfos.Where(s => s.id.Equals(item)).ToList();
- var tempMail = userinfos.Where(s => !string.IsNullOrEmpty($"{s.mail}") && s.mail.Equals(item)).ToList();
- var tempmobile = userinfos.Where(s => !string.IsNullOrEmpty($"{s.mobile}") && s.mobile.Equals(item)).ToList();
- if (tempId.Count == 0 && tempMail.Count == 0 && tempmobile.Count == 0)
- noFound.Add(item);
- }
- }
- return Ok(new { state = RespondCode.Ok, userinfos, noFound });
- }
- /// <summary>
- /// 依据学校id查询学校教师信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- //[AuthToken(Roles = "admin,rdc,assist,sales")]
- [HttpPost("get-tchinfos")]
- public async Task<IActionResult> GetSchoolTeacher(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("scId", out JsonElement scId)) return BadRequest();
- List<IdInfo> idInfos = new();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- 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}") }))
- {
- idInfos.Add(item);
- }
- return Ok(new { state = RespondCode.Ok, idInfos });
- }
- /// <summary>
- /// BI发布端外消息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [AuthToken(Roles = "admin,rdc,assist,sales")]
- [HttpPost("push-info")]
- public async Task<IActionResult> PushNotion(JsonElement jsonElement)
- {
- try
- {
- var (_tmdId, _tmdName, _, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- jsonElement.TryGetProperty("type", out JsonElement type);
- jsonElement.TryGetProperty("jumpUrl", out JsonElement jumpUrl);
- jsonElement.TryGetProperty("callbackName", out JsonElement callbackName);
- jsonElement.TryGetProperty("refuseName", out JsonElement refuseName);
- if (!jsonElement.TryGetProperty("theme", out JsonElement theme)) return BadRequest();
- if (!jsonElement.TryGetProperty("content", out JsonElement content)) return BadRequest();
- jsonElement.TryGetProperty("tmdIds", out JsonElement _tmdIds);
- jsonElement.TryGetProperty("schoolIds", out JsonElement _schoolIds);
- jsonElement.TryGetProperty("areaIds", out JsonElement _areaIds);
- if (!jsonElement.TryGetProperty("crowdType", out JsonElement _crowdType)) return BadRequest();
- if (!jsonElement.TryGetProperty("sendTime", out JsonElement sendTime)) return BadRequest();
- jsonElement.TryGetProperty("source", out JsonElement source);
- jsonElement.TryGetProperty("isOnlySave", out JsonElement isOnlySave);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- //DateTimeOffset dateTime = DateTimeOffset.UtcNow;
- List<IdNameCode> idNameCodes = new();
- BINotice bINotice = new();
- List<CrowdInfo> tmdIds = new();
- List<CrowdInfo> schoolIds = new();
- List<CrowdInfo> areaIds = new();
- List<string> tempScIds = new();
- //if (!string.IsNullOrEmpty($"{sendTime}"))
- // dateTime = DateTimeOffset.Parse($"{sendTime}");
- if (string.IsNullOrEmpty($"{_tmdIds}") && string.IsNullOrEmpty($"{_schoolIds}") && string.IsNullOrEmpty($"{_areaIds}"))
- return Ok(new { state = RespondCode.ParamsError, msg = "发送人群不能为空" });
- if (!string.IsNullOrEmpty($"{_tmdIds}"))
- tmdIds = _tmdIds.ToObject<List<CrowdInfo>>();
- if (!string.IsNullOrEmpty($"{_schoolIds}"))
- schoolIds = _schoolIds.ToObject<List<CrowdInfo>>();
- if (!string.IsNullOrEmpty($"{_areaIds}"))
- areaIds = _areaIds.ToObject<List<CrowdInfo>>();
- Crowd crowd = new()
- {
- tmdIds = tmdIds.Count > 0 ? tmdIds : new List<CrowdInfo>(),
- schoolIds = schoolIds.Count > 0 ? schoolIds : new List<CrowdInfo>(),
- areaIds = areaIds.Count > 0 ? areaIds : new List<CrowdInfo>(),
- types = $"{_crowdType}"
- };
- if (tmdIds.Count > 0)
- {
- StringBuilder tchSql = new($"select c.id, c.name,c.code,c.picture,c.nickname from c ");
- if (tmdIds.Count > 0)
- {
- tchSql.Append(BICommonWay.ManyScSql(" where c.id ", tmdIds.Select(s => s.id).ToList()));
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<IdNameCode>(queryText: tchSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
- {
- var tempId = idNameCodes.FindAll(fa => fa.id.Equals(item.id)).ToList();
- if (tempId.Count == 0)
- idNameCodes.Add(item);
- }
- }
- }
- if (areaIds.Count > 0)
- {
- StringBuilder scAreaSql = new($"select value(c.id) from c");
- if (schoolIds.Count > 0)
- {
- scAreaSql.Append(BICommonWay.ManyScSql(" where c.areaId", areaIds.Select(s => s.id).ToList()));
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<string>(queryText: scAreaSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
- {
- var tempScId = schoolIds.FindAll(fa => fa.Equals(item)).ToList();
- if (tempScId.Count == 0)
- tempScIds.Add(item);
- }
- }
- }
- if (schoolIds.Count > 0)
- {
- StringBuilder scTchSql = new($"select c.id, c.name, c.picture,c.nickname from c where c.pk='Teacher'");
- if (schoolIds.Count > 0)
- {
- scTchSql.Append(BICommonWay.ManyScSql(" and c.code", schoolIds.Select(s => s.id).ToList(), $"Teacher-"));
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<IdNameCode>(queryText: scTchSql.ToString(), requestOptions: new QueryRequestOptions() { }))
- {
- var tempId = idNameCodes.FindAll(fa => fa.id.Equals(item.id)).ToList();
- if (tempId.Count == 0)
- idNameCodes.Add(item);
- }
- }
- }
- string tagServiceName = ($"{_crowdType}".ToLower().Contains("hita")) ? "HiTA" : $"{_crowdType}";
- bINotice = new()
- {
- id = Guid.NewGuid().ToString(),
- code = "BINotice",
- type = !string.IsNullOrEmpty($"{type}") ? type.GetInt32() : 0,
- jumpUrl = !string.IsNullOrEmpty($"{jumpUrl}") ? $"{jumpUrl}" : "",
- callbackName = !string.IsNullOrEmpty($"{callbackName}") ? $"{callbackName}" : "",
- refuseName = !string.IsNullOrEmpty($"{refuseName}") ? $"{refuseName}" : "",
- theme = $"{theme}",
- content = $"{content}",
- crowd = crowd,
- crowdIds = idNameCodes.Select(s => $"{s.id}_{tagServiceName}").ToList(),
- createId = _tmdId,
- sendTime = sendTime.GetInt64(),//发布时间待解决
- createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
- source = !string.IsNullOrEmpty($"{source}") ? $"{source}" : "BI"
- };
- bINotice = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync<BINotice>(bINotice, new PartitionKey("BINotice"));
- if (isOnlySave.GetBoolean())
- {
- //BI发送端外通知
- _coreAPIHttpService.BIPushNotify(bINotice, new Dictionary<string, object> { { "tmdid", _tmdIds }, { "sendId", bINotice.crowdIds } }, _option.Location, _configuration, _dingDing);
- }
- return Ok(new { state = RespondCode.Ok, bINotice });
- }
- catch (Exception ex)
- {
- _ = _dingDing.SendBotMsg($"BI,{_option.Location},notion/PushNotion() \n{ex.Message}\n{ex.StackTrace}\n", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 查询发布的消息信息
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [HttpPost("get-infos")]
- [AuthToken(Roles = "admin,rdc,assist,sales")]
- public async Task<IActionResult> GetInfos(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("id", out JsonElement id);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- List<BINotice> bINotices = new();
- StringBuilder sql = new("select value(c) from c");
- if (!string.IsNullOrEmpty($"{id}"))
- sql.Append($" where c.id='{id}'");
- sql.Append($" order by c.sendTime desc ");
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<BINotice>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("BINotice") }))
- {
- bINotices.Add(item);
- }
- return Ok(new { state = RespondCode.Ok, bINotices });
- }
- }
- }
|