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 TmtnSpotDevOpsContentController : BaseController
    {
        public ILogger<TmtnSpotDevOpsContentController> _logger { get; }
        private readonly ITmtnSpotDevOpsContentService _TmtnSpotDevOpsContentService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="TmtnSpotDevOpsContentService"></param>
        /// <param name="logger"></param>
        public TmtnSpotDevOpsContentController(ITmtnSpotDevOpsContentService TmtnSpotDevOpsContentService, ILogger<TmtnSpotDevOpsContentController> logger)
        {
            _TmtnSpotDevOpsContentService = TmtnSpotDevOpsContentService;
            _logger = logger;
        }

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

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

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

        /// <summary>
        /// 创建运维点运维内容
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreateSpotDevOpsContentAsync")]
        public async Task<ApiResult> CreateSpotDevOpsContentAsync(TmtnSpotDevOpsContentViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TmtnSpotDevOpsContentService.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("DeleteSpotDevOpsContentAsync/{id}")]
        public async Task<ApiResult> DeleteSpotDevOpsContentAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TmtnSpotDevOpsContentService.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("UpdateSpotDevOpsContentAsync/{id}")]
        public async Task<ApiResult> UpdateSpotDevOpsContentAsync(string id, TmtnSpotDevOpsContentUpdateModel updateModel)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TmtnSpotDevOpsContentService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

    }
}