using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
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 TdevDevicePartController : BaseController
    {
        public ILogger<TdevDevicePartController> _logger { get; }
        private readonly ITdevDevicePartService _TdevDevicePartService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="TdevDevicePartService"></param>
        /// <param name="logger"></param>
        public TdevDevicePartController(ITdevDevicePartService TdevDevicePartService, ILogger<TdevDevicePartController> logger)
        {
            _TdevDevicePartService = TdevDevicePartService;
            _logger = logger;
        }

        /// <summary>
        /// 通过id获取设备零配件信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetDevicePartAsync/{id}")]
        public async Task<ApiResult> GetDevicePartAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var content = await _TdevDevicePartService.GetConditionAsync(new TdevDevicePartSearchModel { C_ID = id });
                return new ApiResult<TdevDevicePartViewModel>(content.FirstOrDefault());
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 获取所有设备零配件
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetDevicePartsAsync")]
        public async Task<ApiResult> GetDevicePartsAsync()
        {
            try
            {
                var contentList = await _TdevDevicePartService.GetAllAsync();
                return new ApiResult<IEnumerable<TdevDevicePartViewModel>>(contentList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 通过设备零配件名称条件查询
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        [HttpPost("GetDevicePartsByAsync")]
        public async Task<ApiResult> GetDevicePartsByAsync(TdevDevicePartSearchModel searchModel)
        {
            if (searchModel == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            searchModel.IsPagination = false;
            try
            {
                var contentList = await _TdevDevicePartService.GetConditionAsync(searchModel);
                return new ApiResult<PagesModel<TdevDevicePartViewModel>>(new PagesModel<TdevDevicePartViewModel>(contentList, searchModel));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 创建设备零配件
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        [HttpPost("CreateDevicePartAsync")]
        public async Task<ApiResult> CreateDevicePartAsync(TdevDevicePartViewModel content)
        {
            if (content == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _TdevDevicePartService.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("DeleteDevicePartAsync/{id}")]
        public async Task<ApiResult> DeleteDevicePartAsync(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TdevDevicePartService.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("UpdateDevicePartAsync/{id}")]
        public async Task<ApiResult> UpdateDevicePartAsync(string id, TdevDevicePartUpdateModel updateModel)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _TdevDevicePartService.UpdateAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

    }
}