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 TprdProdSKUController : BaseController
    {
        public ILogger<TprdProdSKUController> _logger { get; }
        private readonly ITprdProdSKUService _TprdProdSKUService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="TprdProdSKUService"></param>
        /// <param name="logger"></param>
        public TprdProdSKUController(ITprdProdSKUService TprdProdSKUService, ILogger<TprdProdSKUController> logger)
        {
            _TprdProdSKUService = TprdProdSKUService;
            _logger = logger;
        }
        /// <summary>
        /// 通过ID获取器具SKU信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetProdSKUAsync/{id}")]
        public async Task<ApiResult> GetProdSKUAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TprdProdSKUService.GetByIdAsync(id);
                return new ApiResult<TprdProdSKUViewModel>(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 获取所有器具SKU
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetProdSKUsAsync")]
        public async Task<ApiResult> GetProdSKUsAsync()
        {
            try
            {
                var contentList = await _TprdProdSKUService.GetAllAsync();
                return new ApiResult<IEnumerable<TprdProdSKUViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过器具SKU名称条件查询
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetProdSKUsByAsync")]
        public async Task<ApiResult> GetProdSKUsByAsync(TprdProdSKUSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            searchModel.IsPagination = false;
            try
            {
                var contentList = await _TprdProdSKUService.GetConditionAsync(searchModel);
                return new ApiResult<IEnumerable<TprdProdSKUViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 创建器具SKU
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreateProdSKUAsync")]
        public async Task<ApiResult> CreateProdSKUAsync(TprdProdSKUViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TprdProdSKUService.CreateOneAsync(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除器具SKU
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteProdSKUAsync/{id}")]
        public async Task<ApiResult> DeleteProdSKUAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TprdProdSKUService.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 更新器具SKU
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        [HttpPut("UpdateProdSKUAsync/{id}")]
        public async Task<ApiResult> UpdateProdSKUAsync(string id, TprdProdSKUUpdateModel updateModel)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TprdProdSKUService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

    }
}