using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Ropin.Inspection.Api.Common;
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 TsecRecordController : BaseController
    {
        private readonly ILogger<TsecRecordController> _logger;
        private readonly ITsecRecordService _tsecRecordService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tsecRecordService"></param>
        /// <param name="logger"></param>
        public TsecRecordController(ITsecRecordService tsecRecordService, ILogger<TsecRecordController> logger)
        {
            _tsecRecordService = tsecRecordService;
            _logger = logger;
        }
        /// <summary>
        /// 通过ID获取安检记录信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetRecordAsync/{id}")]
        public async Task<ApiResult> GetRecordAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var spot = await _tsecRecordService.GetByIdAsync(id);
                return new ApiResult<TsecRecordViewModel>(spot);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        ///// <summary>
        ///// 获取所有安检记录
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet("GetRecordsAsync")]
        //public async Task<ApiResult> GetRecordsAsync()
        //{
        //    try
        //    {
        //        var spotList = await _tsecRecordService.GetAllAsync();
        //        return new ApiResult<IEnumerable<TsecRecordViewModel>>(spotList);
        //    }
        //    catch (Exception ex)
        //    {
        //        return new ApiResult(ReturnCode.GeneralError, ex.Message);
        //    }
        //}

        /// <summary>
        /// 创建安检记录
        /// </summary>
        /// <param name="spot"></param>
        /// <returns></returns>
        [Route("CreateRecordAsync")]
        [HttpPost]
        public async Task<ApiResult> CreateRecordAsync([FromBody] TsecRecordViewModel spot)
        {
            if (spot == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _tsecRecordService.CreateOneAsync(spot);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除安检记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteRecordAsync/{id}")]
        public async Task<ApiResult> DeleteRecordAsync(Guid id)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tsecRecordService.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("UpdateRecordAsync/{id}")]
        public async Task<ApiResult> UpdateRecordAsync(Guid id, TsecRecordUpdateViewModel updateModel)
        {
            if (Guid.Empty == id)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tsecRecordService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
    }
}