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

namespace Ropin.Inspection.Api.Controllers
{
    /// <summary>
    /// 巡检点内容
    /// </summary>
    public class TispSpotContentController : BaseController
    {
        public ILogger<TispSpotContentController> _logger { get; }
        private readonly ITispSpotContentService _tispSpotContentService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tispSpotContentService"></param>
        /// <param name="logger"></param>
        public TispSpotContentController(ITispSpotContentService tispSpotContentService, ILogger<TispSpotContentController> logger)
        {
            _tispSpotContentService = tispSpotContentService;
            _logger = logger;
        }
        /// <summary>
        /// 通过ID获取巡检点内容信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetSpotContentAsync/{id}")]
        public async Task<ApiResult> GetSpotContentAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _tispSpotContentService.GetByIdAsync(id);
                return new ApiResult<TispSpotContentViewModel>(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }


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

        /// <summary>
        /// 创建巡检点内容
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [Route("CreateSpotContentAsync")]
        [HttpPost]
        public async Task<ApiResult> CreateSpotContentAsync([FromBody] TispSpotContentViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _tispSpotContentService.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("DeleteSpotContentAsync/{id}")]
        public async Task<ApiResult> DeleteSpotContentAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tispSpotContentService.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("UpdateSpotContentAsync/{id}")]
        public async Task<ApiResult> UpdateSpotContentAsync(Guid id, TispSpotContentUpdateViewModel updateModel)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tispSpotContentService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

    }
}