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

        /// <summary>
        /// 通过code获取区域信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("GetAreaAsync/{code}")]
        public async Task<ApiResult> GetAreaAsync(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TpntAreaService.GetConditionAsync(new TpntAreaSearchModel {C_Code = code });
                return new ApiResult<TpntAreaViewModel>(content.FirstOrDefault());
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

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

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

        /// <summary>
        /// 创建区域
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreateAreaAsync")]
        public async Task<ApiResult> CreateAreaAsync(TpntAreaViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TpntAreaService.CreateOneAsync(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除区域
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpDelete("DeleteAreaAsync/{code}")]
        public async Task<ApiResult> DeleteAreaAsync(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TpntAreaService.DeleteAsync(code);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 更新区域
        /// </summary>
        /// <param name="code"></param>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        [HttpPut("UpdateAreaAsync/{code}")]
        public async Task<ApiResult> UpdateAreaAsync(string code, TpntAreaUpdateModel updateModel)
        {
            if (string.IsNullOrEmpty(code))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TpntAreaService.UpdateAsync(code, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

    }
}