using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Ropin.Core.Extensions.Redis;
using Ropin.Inspection.Api.Common;
using Ropin.Inspection.Api.Controllers;
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
{

    public class TdevBoxDevSpotController : BaseController
    {
        public ILogger<TdevBoxDevSpotController> _logger { get; }
        private readonly IRedisBasketRepository _redisService;
        private readonly ITdevBoxDevSpotService _TdevBoxDevSpotService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="TdevBoxDevSpotService"></param>
        /// <param name="logger"></param>
        public TdevBoxDevSpotController(ITdevBoxDevSpotService TdevBoxDevSpotService, IRedisBasketRepository redisService,ILogger<TdevBoxDevSpotController> logger)
        {
            _TdevBoxDevSpotService = TdevBoxDevSpotService;
            _logger = logger;
            _redisService = redisService;
        }

        /// <summary>
        /// 创建盒子设备信息
        /// </summary>
        /// <param name="content">盒子模型</param>
        /// <returns></returns>
        [HttpPost("CreateBoxDevSpotAsync")]
        [AllowAnonymous]
        public async Task<ApiResult> CreateBoxDevSpotAsync(TdevBoxDevSpotViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TdevBoxDevSpotService.CreateOneAsync(content);
                await _redisService.Set("mqtt_BoxDevSpot_IsUpdate", "1", TimeSpan.FromDays(7));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除盒子设备信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteBoxDevSpotAsync/{id}")]
        public async Task<ApiResult> DeleteBoxDevSpotAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TdevBoxDevSpotService.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("UpdateBoxDevSpotAsync/{id}")]
        [AllowAnonymous]
        public async Task<ApiResult> UpdateBoxDevSpotAsync(string id, TdevBoxDevSpotUpdateModel updateModel)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TdevBoxDevSpotService.UpdateAsync(id, updateModel);
                await _redisService.Set("mqtt_BoxDevSpot_IsUpdate", "1", TimeSpan.FromDays(7));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }


        /// <summary>
        /// 通过id获取盒子设备信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetBoxDevSpotAsync")]
        [AllowAnonymous]
        public async Task<ApiResult> GetBoxDevSpotAsync(TdevBoxDevSpotSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var contentList = await _TdevBoxDevSpotService.GetConditionAsync(searchModel);
                return new ApiResult<PagesModel<TdevBoxDevSpotViewModel>>(new PagesModel<TdevBoxDevSpotViewModel>(contentList, searchModel));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[HttpGet("GetBoxDevSpotByCodeAsync/{id}")]
        //[AllowAnonymous]
        //public async Task<ApiResult> GetBoxDevSpotByCodeAsync(string id)
        //{
        //    if (string.IsNullOrEmpty(id))
        //    {
        //        return new ApiResult(ReturnCode.GeneralError);
        //    }
        //    try
        //    {
        //        var content = await _TdevBoxDevSpotService.GetConditionAsync(new TdevBoxDevSpotSearchModel { C_DevSpotCode = id });
        //        var dev = content.ToList();

        //        return new ApiResult<IEnumerable<TdevBoxDevSpotViewModel>>(new List<TdevBoxDevSpotViewModel>(dev));
        //    }
        //    catch (Exception ex)
        //    {
        //        return new ApiResult(ReturnCode.GeneralError, ex.Message);
        //    }
        //}
    }
}