using Microsoft.AspNetCore.Authorization;
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 TprdProductController : BaseController
    {
        private readonly ILogger<TprdProductController> _logger;
        private readonly ITprdProductService _TprdProductService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="TprdProductService"></param>
        /// <param name="logger"></param>
        public TprdProductController(ITprdProductService TprdProductService, ILogger<TprdProductController> logger)
        {
            _TprdProductService = TprdProductService;
            _logger = logger;
        }
        /// <summary>
        /// 通过ID获取器具信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetProductAsync/{id}")]
        public async Task<ApiResult> GetProductAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TprdProductService.GetByIdAsync(id);
                return new ApiResult<TprdProductViewModel>(content);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

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

        /// <summary>
        /// 通过各个条件查询器具
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetProductsByAsync")]
        public async Task<ApiResult> GetProductsByAsync(TprdProductSearchModel searchModel)
        {
            if (searchModel == null || string.IsNullOrEmpty(searchModel.C_StoreCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            searchModel.IsPagination = false;
            try
            {
                var contentList = await _TprdProductService.GetConditionAsync(searchModel);
                return new ApiResult<IEnumerable<TprdProductViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 获取器具设备数
        /// </summary>
        /// <param name="storeCode"></param>
        /// <returns></returns>
        [HttpGet("GetDeviceCountByAsync/{storeCode}")]
        [AllowAnonymous]
        public async Task<ApiResult> GetDeviceCountByAsync(string storeCode)
        {
            if (string.IsNullOrEmpty(storeCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var count = await _TprdProductService.GetDeviceCountByAsync(storeCode);
                return new ApiResult<int>(count);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 获取异常器具设备数
        /// </summary>
        /// <param name="storeCode"></param>
        /// <returns></returns>
        [HttpGet("GetAlertDeviceCountByAsync/{storeCode}")]
        [AllowAnonymous]
        public async Task<ApiResult> GetAlertDeviceCountByAsync(string storeCode)
        {
            if (string.IsNullOrEmpty(storeCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var count = await _TprdProductService.GetAlertDeviceCountByAsync(storeCode);
                return new ApiResult<int>(count);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 获取异常器具设备列表
        /// </summary>
        /// <param name="storeCode"></param>
        /// <returns></returns>
        [HttpGet("GetAlertProductsByAsync/{storeCode}")]
        [AllowAnonymous]
        public async Task<ApiResult> GetAlertProductsByAsync(string storeCode)
        {
            if (string.IsNullOrEmpty(storeCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var items = await _TprdProductService.GetAlertProductsByAsync(storeCode);
                return new ApiResult<IEnumerable<TprdProductViewModel>>(items);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 获取超过有效期器具列表
        /// </summary>
        /// <param name="storeCode"></param>
        /// <returns></returns>
        [HttpGet("GetValiDateProductsByAsync/{storeCode}")]
        [AllowAnonymous]
        public async Task<ApiResult> GetValiDateProductsByAsync(string storeCode)
        {
            if (string.IsNullOrEmpty(storeCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var items = await _TprdProductService.GetValiDateProductsByAsync(storeCode);
                return new ApiResult<IEnumerable<TprdProductViewModel>>(items);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 通过code取异常设备信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("GetAlertProductByCodeAsync/{code}")]
        [AllowAnonymous]
        public async Task<ApiResult> GetAlertProductByCodeAsync(Guid code)
        {
            if (Guid.Empty.Equals(code) )
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var items = await _TprdProductService.GetAlertProductByCodeAsync(code);
                return new ApiResult<TprdProductViewModel>(items);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 通过code取过期设备信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet("GetValiDateProductByCodeAsync/{code}")]
        [AllowAnonymous]
        public async Task<ApiResult> GetValiDateProductByCodeAsync(Guid code)
        {
            if (Guid.Empty.Equals(code))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var items = await _TprdProductService.GetValiDateProductByCodeAsync(code);
                return new ApiResult<TprdProductViewModel>(items);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 通过网点取设备及运行的数据
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetProductWithDataByAsync")]
        [AllowAnonymous]
        public async Task<ApiResult>  GetProductWithDataByAsync(TprdProductWithDataSearchModel searchModel)
        {
            if (searchModel == null || string.IsNullOrEmpty(searchModel.C_StoreCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            searchModel.IsPagination = false;
            try
            {
                var contentList = await _TprdProductService.GetProductWithDataByAsync(searchModel);
                return new ApiResult<IEnumerable<AllProductWithDevViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 根据网点区域取设备
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetDeviceByAreaCode")]
        [AllowAnonymous]
        public async Task<ApiResult> GetDeviceByAreaCode(TprdDeviceByAreaSearchModel searchModel)
        {
            if (searchModel == null || string.IsNullOrEmpty(searchModel.C_StoreCode))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var contentList = await _TprdProductService.GetDeviceByAreaCode(searchModel);
                return new ApiResult<IEnumerable<TprdDeviceByAreaViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

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

        /// <summary>
        /// 扫巡二维码获取器具
        /// </summary>
        /// <param name="QRCode"></param>
        /// <param name="storeCode"></param>
        /// <returns></returns>
        [HttpGet("GetProductByQRCodeAsync/{QRCode}/{storeCode}")]
        public async Task<ApiResult> GetProductByQRCodeAsync(string QRCode, string storeCode)
        {
            try
            {
                if (string.IsNullOrEmpty(QRCode) || string.IsNullOrEmpty(storeCode))
                {
                    return new ApiResult(ReturnCode.GeneralError);
                }
                TprdProductViewModel item = await _TprdProductService.GetProductByQRCodeAsync(QRCode, storeCode);
                return new ApiResult<TprdProductViewModel>(item);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 删除器具
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteProductAsync/{id}")]
        public async Task<ApiResult> DeleteProductAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TprdProductService.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("UpdateProductAsync/{id}")]
        public async Task<ApiResult> UpdateProductAsync(Guid id, TprdProductUpdateModel updateModel)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TprdProductService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

    }
}