using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Ropin.Inspection.Api.Common;
using Ropin.Inspection.Api.Controllers;
using Ropin.Inspection.Model;
using Ropin.Inspection.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ropin.Inspection.Api
{

    public class TmtnDevOpsContentController : BaseController
    {
        public ILogger<TmtnDevOpsContentController> _logger { get; }
        private readonly ITmtnDevOpsContentService _TmtnDevOpsContentService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="TmtnDevOpsContentService"></param>
        /// <param name="logger"></param>
        public TmtnDevOpsContentController(ITmtnDevOpsContentService TmtnDevOpsContentService, ILogger<TmtnDevOpsContentController> logger)
        {
            _TmtnDevOpsContentService = TmtnDevOpsContentService;
            _logger = logger;
        }

        /// <summary>
        /// 通过id获取运维内容信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetDevOpsContentAsync/{id}")]
        public async Task<ApiResult> GetDevOpsContentAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TmtnDevOpsContentService.GetConditionAsync(new TmtnDevOpsContentSearchModel { C_ID = id });
                return new ApiResult<TmtnDevOpsContentViewModel>(content.FirstOrDefault());
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 获取所有运维内容
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetDevOpsContentsAsync")]
        public async Task<ApiResult> GetDevOpsContentsAsync()
        {
            try
            {
                var contentList = await _TmtnDevOpsContentService.GetAllAsync();
                return new ApiResult<IEnumerable<TmtnDevOpsContentViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过运维内容名称条件查询
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetDevOpsContentsByAsync")]
        public async Task<ApiResult> GetDevOpsContentsByAsync(TmtnDevOpsContentSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            searchModel.IsPagination = false;
            try
            {
                var contentList = await _TmtnDevOpsContentService.GetConditionAsync(searchModel);
                return new ApiResult<PagesModel<TmtnDevOpsContentViewModel>>(new PagesModel<TmtnDevOpsContentViewModel>(contentList, searchModel));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 创建运维内容
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreateDevOpsContentAsync")]
        public async Task<ApiResult> CreateDevOpsContentAsync(TmtnDevOpsContentViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TmtnDevOpsContentService.CreateOneAsync(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除运维内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteDevOpsContentAsync/{id}")]
        public async Task<ApiResult> DeleteDevOpsContentAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TmtnDevOpsContentService.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 更新运维内容
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        [HttpPut("UpdateDevOpsContentAsync/{id}")]
        public async Task<ApiResult> UpdateDevOpsContentAsync(string id, TmtnDevOpsContentUpdateModel updateModel)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TmtnDevOpsContentService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

    }
}