using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EquipmentApi.IRepositories;
using MadRunFabric.Model;
using MadRunFabric.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;

namespace EquipmentApi.Controllers
{
    /// <summary>
    /// 人员管理控制器，对人员信息的管理
    /// </summary>
    [Authorize]
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class StaffInfoController : BaseController
    {
        private readonly ILogger<StaffInfoController> _logger;
        private readonly IUser_Pro_StaffRepository _pro_staff_infoRepository;

        public StaffInfoController(ILogger<StaffInfoController> logger, IUser_Pro_StaffRepository pro_staff_infoRepository)
        {
            _logger = logger;
            _pro_staff_infoRepository = pro_staff_infoRepository;
        }

        /// <summary>
        /// 获取人员资料
        /// </summary>
        /// <returns></returns>
        [HttpGet("getall")]
        public async Task<IActionResult> GetAllLists()
        {
            var stafflist =await  _pro_staff_infoRepository.GetAll();
            return Success("获取全部数据成功", stafflist.ToJson());
        }

        /// <summary>
        /// 删除人员资料
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("delete")]
        public async Task<IActionResult> Remove(string[] ids)
        {
            //使用逻辑删除
            //物理删除的数据无法恢复
            var res = 0;
            if (ids != null && ids.Length > 0)
            {
                foreach (var item in ids)
                {
                    var ml =await _pro_staff_infoRepository.GetSingle(item);
                    ml.isdelete = 1;
                    if (_pro_staff_infoRepository.Update(ml).Result)
                        res += 1;
                }
                if (res == ids.Length)
                    return Success("删除成功");
                else if (res > 0 && res < ids.Length)
                    return Error("部分删除失败，请查看后重新操作");
                else
                    return Error("删除失败，请查看后重新操作");
            }
            else
                return Error("请选择要删除的记录");
        }
    }
}