using Microsoft.AspNetCore.Authorization;
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
{

    /// <summary>
    /// 巡检内容
    /// </summary>
    public class TispContentController : BaseController
    {
        private readonly ILogger<TispContentController> _logger;
        private readonly ITispContentService _tispContentService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tispContentService"></param>
        /// <param name="logger"></param>
        public TispContentController(ITispContentService tispContentService, ILogger<TispContentController> logger)
        {
            _tispContentService = tispContentService;
            _logger = logger;
        }
        /// <summary>
        /// 通过ID获取巡检内容信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetContentAsync/{id}")]
        public async Task<ApiResult> GetContentAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _tispContentService.GetByIdAsync(id);
                return new ApiResult<TispContentViewModel>(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }


        /// <summary>
        /// 获取所有巡检内容
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetContentsAsync/{storeCode}")]
        public async Task<ApiResult> GetContentsAsync(string storeCode)
        {
            if (string.IsNullOrEmpty(storeCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var contentList = await _tispContentService.GetAllAsync(storeCode);
                return new ApiResult<IEnumerable<TispContentViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 通过内容条件查询,不分页
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetContentsByAsync")]
        public async Task<ApiResult> GetContentsByAsync(TispContentSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            searchModel.IsPagination = false;
            try
            {
                var contentList = await _tispContentService.GetContentsConditionAsync(searchModel);
                return new ApiResult<IEnumerable<TispContentViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 创建巡检内容
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [Route("CreateContentAsync")]
        [HttpPost]
        public async Task<ApiResult> CreateContentAsync([FromBody] TispContentViewModel content)
        {
            if (content == null || string.IsNullOrEmpty(content.C_StoreCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _tispContentService.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("DeleteContentAsync/{id}")]
        public async Task<ApiResult> DeleteContentAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tispContentService.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("UpdateContentAsync/{id}")]
        public async Task<ApiResult> UpdateContentAsync(Guid id, TispContentUpdateViewModel updateModel)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tispContentService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

    }
}