using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Falcon.Extend;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using StaffManagement.Database.Tables;
using StaffManagement.Models;

namespace StaffManagement.Controllers.Api
{
    /// <summary>
    /// 员工操作接口
    /// </summary>
    public class PersonController:ApiControllerBase<PersonController>
    {
        public PersonController(ILogger<PersonController> logger,IServiceProvider service) : base(logger,service) {
        }

        /// <summary>
        /// 获取员工列表
        /// </summary>
        /// <returns>员工数据列表</returns>
        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<Person>),200)]
        public IEnumerable<Person> GetPeople(PersonFilter filter) {
            var qu = this.Db.People.AsQueryable();
            if(filter != null) {
                if(filter.Name.IsNotNullOrEmpty()) {
                    var f = filter.Name;
                    qu = qu.Where(m => m.Name.Contains(f));
                }
            }
            return qu.ToList();
        }
        /// <summary>
        /// 增加新人员信息
        /// </summary>
        /// <param name="p">人员信息</param>
        /// <returns>保存后的信息</returns>
        [HttpPost]
        [ProducesResponseType(typeof(Person),200)]
        public Person AddNewPerson(Person p) {
            this.Db.Entry(p).State = EntityState.Added;
            this.Db.SaveChanges();
            return p;
        }
        /// <summary>
        /// 删除人员信息
        /// </summary>
        /// <param name="id">人员编号</param>
        /// <returns>是否成功</returns>
        [HttpPost]
        [ProducesResponseType(typeof(bool),200)]
        public bool RemovePerson(int id) {
            var p = new Person { Id = id };
            //this.Db.People.Attach(p);
            this.Db.Entry(p).State = EntityState.Deleted;
            this.Db.SaveChanges();
            return true;
        }
    }
}
