using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using TEAMModelOS.Service.Models.Syllabus;
using TEAMModelOS.SDK.Context.Exception;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
using TEAMModelOS.SDK.Extension.SnowFlake;
using TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces;

namespace TEAMModelOS.Controllers.Syllabus
{
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]
    public class SyllabusController : BaseController
    {
        private readonly IAzureCosmosDBRepository azureCosmosDBRepository;
        public SyllabusController(IAzureCosmosDBRepository _azureCosmosDBRepository)
        {
            azureCosmosDBRepository = _azureCosmosDBRepository;
        }
        /// <summary>
        /// 批量保存或更新课纲
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("SaveOrUpdateAll")]
        public async Task<BaseJosnRPCResponse> SaveOrUpdateAll(JosnRPCRequest<SyllabusNode> request)
        {
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            if (string.IsNullOrEmpty(request.@params.id))
            {
                string key = IdWorker.getInstance().NextId() + "";
            }
            List<SyllabusNode> nodes = new List<SyllabusNode>();
            nodes.Add(request.@params);
            List<SyllabusNode> data = await azureCosmosDBRepository.SaveAll(nodes);
            return builder.Data(data).build();
        }
        /// <summary>
        /// 删除课纲
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("Delete")]
        public async Task<BaseJosnRPCResponse> Delete(JosnRPCRequest<SyllabusNode> request)
        {
            bool flag = false;
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            if (string.IsNullOrEmpty(request.@params.id))
            {
                await azureCosmosDBRepository.DeleteAsync<SyllabusNode>(request.@params.id, request.@params.volumeCode);
                flag = true;
            }
            return builder.Data(flag).build();
        }
        /// <summary>
        /// 查找课纲
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("Find")]
        public async Task<BaseJosnRPCResponse> Find(JosnRPCRequest<Dictionary<string, object>> request)
        {
            bool flag = false;
            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
            if (request.@params.TryGetValue("volumeCode", out _))
            {
                await azureCosmosDBRepository.FindByParams<SyllabusNode>(request.@params);
                flag = true;
            }
            else
            {
                throw new BizException("volumeCode is  null!");
            }
            return builder.Data(flag).build();
        }
    }
}
