using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Ropin.Inspection.Api.Common;
using Ropin.Inspection.Model.SearchModel;
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
{
    public class TispRegionController : BaseController
    {
        public ILogger<TispRegionController> _logger { get; }
        private readonly ITispRegionService _tispRegionService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tispRegionService"></param>
        /// <param name="logger"></param>
        public TispRegionController(ITispRegionService tispRegionService, ILogger<TispRegionController> logger)
        {
            _tispRegionService = tispRegionService;
            _logger = logger;
        }
        /// <summary>
        /// 通过ID获取巡检区域信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetRegionAsync/{id}")]
        public async Task<ApiResult> GetRegionAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _tispRegionService.GetByIdAsync(id);
                return new ApiResult<TispRegionViewModel>(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 获取所有巡检区域
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetRegionsAsync")]
        public async Task<ApiResult> GetRegionsAsync()
        {
            try
            {
                var contentList = await _tispRegionService.GetAllAsync();
                return new ApiResult<IEnumerable<TispRegionViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过巡检区域名称条件查询
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetRegionsByAsync")]
        public async Task<ApiResult> GetRegionsByAsync(TispRegionSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            searchModel.IsPagination = false;
            try
            {
                var contentList = await _tispRegionService.GetRegionsConditionAsync(searchModel);
                return new ApiResult<IEnumerable<TispRegionViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 创建巡检区域
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreateRegionAsync")]
        public async Task<ApiResult> CreateRegionAsync(TispRegionCreateViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                TispRegionViewModel mode = new TispRegionViewModel { C_Name = content.C_Name, C_ImageUrl = content.C_ImageUrl, C_Remark = content.C_Remark };
                await _tispRegionService.CreateOneAsync(mode);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除巡检区域
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteRegionAsync/{id}")]
        public async Task<ApiResult> DeleteRegionAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tispRegionService.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("UpdateRegionAsync/{id}")]
        public async Task<ApiResult> UpdateRegionAsync(Guid id, TispRegionUpdateViewModel updateModel)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tispRegionService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

    }
}