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 TsysPostController : BaseController
    {
        public ILogger<TsysPostController> _logger { get; }
        private readonly ITsysPostService _tsysPostService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tsysPostService"></param>
        /// <param name="logger"></param>
        public TsysPostController(ITsysPostService tsysPostService, ILogger<TsysPostController> logger)
        {
            _tsysPostService = tsysPostService;
            _logger = logger;
        }
        /// <summary>
        /// 通过岗位ID获取岗位信息
        /// </summary>
        /// <param name="PostId"></param>
        /// <returns></returns>
        [HttpGet("GetPostAsync/{PostId}")]
        public async Task<ApiResult> GetPostAsync(Guid PostId)
        {
            if (Guid.Empty == PostId)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var Post = await _tsysPostService.GetByIdAsync(PostId);
                return new ApiResult<TsysPostViewModel>(Post);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 获取所有岗位
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetPostsAsync")]
        public async Task<ApiResult> GetPostsAsync()
        {
            try
            {
                var PostList = await _tsysPostService.GetAllAsync();
                return new ApiResult<IEnumerable<TsysPostViewModel>>(PostList?.ToList());
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 创建岗位
        /// </summary>
        /// <param name="Post"></param>
        /// <returns></returns>
        [Route("CreatePostAsync")]
        [HttpPost]
        public async Task<ApiResult> CreatePostAsync(TsysPostViewModel Post)
        {
            if (Post == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _tsysPostService.CreateOneAsync(Post);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 删除岗位
        /// </summary>
        /// <param name="PostId"></param>
        /// <returns></returns>
        [HttpDelete("DeletePostAsync/{PostId}")]
        public async Task<ApiResult> DeletePostAsync(Guid PostId)
        {
            if (Guid.Empty == PostId)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tsysPostService.DeleteAsync(PostId);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 更新岗位
        /// </summary>
        /// <param name="PostId"></param>
        /// <param name="updatePost"></param>
        /// <returns></returns>
        [HttpPut("UpdatePostAsync/{PostId}")]
        public async Task<ApiResult> UpdatePostAsync(Guid PostId, TsysPostUpdateViewModel updatePost)
        {
            if (Guid.Empty == PostId)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tsysPostService.UpdateAsync(PostId, updatePost);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
    }
}