123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- 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 Azure.Cosmos;
- using HTEXLib.COMM.Helpers;
- using System.Text;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelBI.Filter;
- using TEAMModelBI.Tool.Extension;
- namespace TEAMModelBI.Controllers.BIAbility
- {
- [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();
- List<Ability> abilities = new List<Ability>();
- string sqltxt = "select value(c) from c";
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<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-abilitysbystandard \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();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- 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,assist")]
- [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();
- 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 client = _azureCosmos.GetCosmosClient();
- List<AbilityTask> syllabus = new List<AbilityTask>();
- string sql = $"select value(c) from c where c.abilityId='{id}'";
- var response = await client.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Ability-{standard}"));
- await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<AbilityTask>(queryText: sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilityTask-{standard}") }))
- {
- syllabus.Add(item);
- }
- if (syllabus.IsNotEmpty())
- {
- var sresponse = await client.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(syllabus.Select(x => x.id).ToList(), $"AbilityTask-{standard}");
- }
- if (response.Status == 200)
- {
- //保存操作记录
- await _azureStorage.SaveBILog("abilityTask-del", $"{_tmdName}【{_tmdId}】删除册别,删除ID:{id}", _dingDing, httpContext: HttpContext);
- return Ok(new { state = 200 });
- }
- else return Ok(new { state = response.Status });
- }
- 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,assist")]
- [HttpPost("upd-ability")]
- public async Task<IActionResult> UpdAbility(Ability ability)
- {
- try
- {
- var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- StringBuilder stringBuilder = new StringBuilder($"{_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();
- string tempType = "";
- //新增册别
- if (string.IsNullOrEmpty(ability.id))
- {
- ability.comid = Guid.NewGuid().ToString();
- StringBuilder sqltxt = new StringBuilder("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").GetItemQueryIterator<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 _azureStorage.SaveBILog(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,assist")]
- [HttpPost("upd-currency")]
- public async Task<IActionResult> UpdateCurrency(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("isRequired", out JsonElement isRequired)) return BadRequest(); //是否是必修集合
- var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- var currencys = isRequired.ToObject<List<IsRequired>>();
- foreach (var item in currencys)
- {
- var cosmosClient = _azureCosmos.GetCosmosClient();
- 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 _azureStorage.SaveBILog("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; }
- }
- }
- }
|