using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Ropin.Inspection.Api.Common;
using Ropin.Inspection.Model.SearchModel.LGS;
using Ropin.Inspection.Model.ViewModel.LGS;
using Ropin.Inspection.Model;
using Ropin.Inspection.Service.LGS.Interface;
using System.Threading.Tasks;
using System;

namespace Ropin.Inspection.Api.Controllers.LGS
{
    public class LargeScreenEventController : BaseController
    {
        private readonly ILargeScreenEventService _repository;
        private static readonly ILog log = LogManager.GetLogger(typeof(LargeScreenEventController));
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        public LargeScreenEventController(ILargeScreenEventService repository)
        {
            _repository = repository;
        }

        /// <summary>
        /// 创建大屏控件事件
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreateLargeScreenEventAsync")]
        public async Task<ApiResult> CreateLargeScreenEventAsync(LargeScreenEventViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _repository.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("DeleteLargeScreenEventAsync/{id}")]
        public async Task<ApiResult> DeleteLargeScreenEventAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _repository.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("UpdateLargeScreenEventAsync/{id}")]
        [AllowAnonymous]
        public async Task<ApiResult> UpdateLargeScreenEventAsync(string id, LargeScreenEventViewModel updateModel)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _repository.UpdateAsync(updateModel, id);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 获取大屏控件事件列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetLargeScreenEventAsync")]
        [AllowAnonymous]
        public async Task<ApiResult> GetLargeScreenEventAsync(LargeScreenEventSearch searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                var contentList = await _repository.GetConditionAsync(searchModel);
                return new ApiResult<PagesModel<LargeScreenEventViewModel>>(new PagesModel<LargeScreenEventViewModel>(contentList, searchModel));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过id获取大屏控件事件详情
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetLargeScreenEventByIDAsync")]
        [AllowAnonymous]
        public async Task<ApiResult> GetLargeScreenEventByIDAsync(string Id)
        {
            if (string.IsNullOrEmpty(Id))
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                LargeScreenEventViewModel data = await _repository.GetByIdAsync(Id);
                return new ApiResult<LargeScreenEventViewModel>(data);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
    }
}