123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.Extensions.Configuration;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.Models;
- using Microsoft.Extensions.Options;
- using System.Text.Json;
- using Microsoft.Azure.Cosmos;
- using System.Text;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelBI.Filter;
- using TEAMModelBI.Tool.Extension;
- using TEAMModelOS.SDK.Context.BI;
- using TEAMModelOS.SDK;
- namespace TEAMModelBI.Controllers.BINormal
- {
- [Route("biabilitymgmt")]
- [ApiController]
- public class AbilityMgmtController : ControllerBase
- {
- //获取配置信息
- private readonly IConfiguration _configuration;
- //数据容器
- private readonly AzureCosmosFactory _azureCosmos;
- //钉钉提示信息
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly AzureStorageFactory _azureStorage;
- public AbilityMgmtController(IConfiguration configuration, AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage)
- {
- _configuration = configuration;
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _option = option?.Value;
- _azureStorage = azureStorage;
- }
- /// <summary>
- /// 依据区级的标准编号获取标准的编号集合 //已对接
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-abilitys")]
- public async Task<IActionResult> GetAbilitysByStandard(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- List<Ability> abilities = new();
- string sqltxt = "select value(c) from c";
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<Ability>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{standard}") }))
- {
- abilities.Add(item);
- }
- return Ok(new { state = 200, abilities });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/get-abilitys \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 依据能力标准的编号查询某个能力标准信息 //已对接
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-abulityid")]
- public async Task<IActionResult> GetAbilityByID(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
- if (!jsonElement.TryGetProperty("abilityId", out JsonElement abilityId)) return BadRequest();
- //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
- var cosmosClient = _azureCosmos.GetCosmosClient();
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- Ability ability = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<Ability>($"{abilityId}", new PartitionKey($"Ability-{standard}"));
- return Ok(new { state = 200, ability = ability });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/get-abulityid \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 依据区标准点和ID 删除册别 //已对接
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist,sales")]
- [HttpPost("del-ability")]
- public async Task<IActionResult> DelAbility(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
- if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
- //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
- var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- if (string.IsNullOrEmpty($"{standard}"))
- {
- return Ok(new { state = 0,message= "standard 参数异常" });
- }
- var cosmosClient = _azureCosmos.GetCosmosClient();
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- ////分开部署,就不需要,一站多用时,取消注释
- //if ($"{site}".Equals(BIConst.Global))
- //{
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
- //}
- List<AbilityTask> syllabus = new();
- string sql = $"select value(c) from c where c.abilityId='{id}'";
- var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Ability-{standard}"));
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<AbilityTask>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilityTask-{standard}") }))
- {
- syllabus.Add(item);
- }
- if (syllabus.IsNotEmpty())
- {
- var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(syllabus.Select(x => x.id).ToList(), $"AbilityTask-{standard}");
- }
- if (response.StatusCode == System.Net.HttpStatusCode.OK)
- {
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "abilityTask-del", $"{_tmdName}【{_tmdId}】删除册别,删除ID:{id}", _dingDing, httpContext: HttpContext);
- return Ok(new { state = 200 });
- }
- else return Ok(new { state = response.StatusCode });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/del-ability \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 更新和新增册别信息 //已经对接
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist,sales")]
- [HttpPost("upd-ability")]
- public async Task<IActionResult> UpdAbility(Ability ability, [FromHeader] string site)
- {
- try
- {
- var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- StringBuilder stringBuilder = new($"{_tmdName}【{_tmdId}】");
- //Ability ability = recordAbility.ability;
- ability.pk = "Ability";
- ability.ttl = -1;
- Ability tempAbility = new Ability();
- if (!string.IsNullOrEmpty(ability.code) && !string.IsNullOrEmpty(ability.standard))
- {
- ability.code = ability.code.StartsWith("Ability-") ? ability.code : $"Ability-{ability.code}";
- }
- else return Ok(new { state = 0, message = "参数异常" });
- var azureClient = _azureCosmos.GetCosmosClient();
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- if ($"{site}".Equals(BIConst.Global))
- {
- azureClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
- }
- string tempType = "";
- //新增册别
- if (string.IsNullOrEmpty(ability.id))
- {
- ability.comid = Guid.NewGuid().ToString();
- StringBuilder sqltxt = new("select value(c) from c where c.status = 1 ");
- if (!string.IsNullOrEmpty(ability.dimension))
- {
- sqltxt.Append($" and c.dimension = '{ability.dimension}' and c.name = '{ability.name}' and c.no = '{ability.no}' ");
- List<Ability> abilities = new List<Ability>();
- await foreach (var item in azureClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<Ability>(queryText: sqltxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(ability.code) }))
- {
- abilities.Add(item);
- }
- if (abilities.Count > 0)
- {
- return Ok(new { state = 1, message = "数据已存在" });
- }
- }
- else
- {
- return Ok(new { state = 2, mesages = "参数异常" });
- }
- ability.id = Guid.NewGuid().ToString();
- ability.creatorId = string.IsNullOrEmpty($"{ability.creatorId}") ? $"{_tmdId}" : ability.creatorId;
- ability.creatorName = string.IsNullOrEmpty($"{ability.creatorName}") ? $"{_tmdName}" : ability.creatorName;
- tempAbility = await azureClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync(ability, new PartitionKey(ability.code));
- stringBuilder.Append($"新增册别信息,册别编号:{tempAbility.id},册别名称:{tempAbility.name}");
- tempType = "ability-add";
- }
- //更新册别
- else
- {
- try
- {
- tempAbility = await azureClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<Ability>(ability, ability.id, new PartitionKey(ability.code));
- stringBuilder.Append($"更新册别信息,册别编号:{tempAbility.id},册别名称:{tempAbility.name}");
- tempType = "ability-update";
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/upd-ability \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, tempType?.ToString(), stringBuilder?.ToString(), _dingDing, httpContext: HttpContext);
- return Ok(new { state = 200, Ability = tempAbility });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} biabilitymgmt/upd-ability \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 修改是否必修的接口 //已对接
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist,sales")]
- [HttpPost("upd-currency")]
- public async Task<IActionResult> UpdateCurrency(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("isRequired", out JsonElement isRequired)) return BadRequest(); //是否是必修集合
- //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
- var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- var currencys = isRequired.ToObject<List<IsRequired>>();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- //if ($"{site}".Equals(BIConst.Global))
- //{
- // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
- // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
- //}
- foreach (var item in currencys)
- {
- Ability ability = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<Ability>(item.abilityId, new PartitionKey($"Ability-{item.standard}"));
- ability.currency = item.currency;
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync(ability, ability.id, new PartitionKey(ability.code));
- }
- //保存操作记录
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "ability-update", $"{_tmdName}【{_tmdId}】设置是否必修状态。标准:{currencys[0].standard}", _dingDing, httpContext: HttpContext);
- return Ok(new { state = 200, currencys });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /biabilitymgmt/upd-currency \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- public record IsRequired()
- {
- public string abilityId { get; set; }
- public string standard { get; set; }
- public int currency { get; set; }
- }
- /// <summary>
- /// 新增和修改是使用
- /// </summary>
- public record RecordAbility()
- {
- public string tmdId { get; set; }
-
- public string tmdName { get; set; }
- public Ability ability { get; set; }
- }
- }
- }
|