using New_College.IServices;
using New_College.Model;
using New_College.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace New_College.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class Sys_TenantController : ControllerBase
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly ISys_TenantServices _sys_TenantServices;

        public Sys_TenantController(ISys_TenantServices Sys_TenantServices)
        {
            _sys_TenantServices = Sys_TenantServices;
        }

        [HttpGet]
        public async Task<MessageModel<PageModel<Sys_Tenant>>> Get(int page = 1, string key = "", int intPageSize = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            Expression<Func<Sys_Tenant, bool>> whereExpression = a => a.Id > 0;

            return new MessageModel<PageModel<Sys_Tenant>>()
            {
                msg = "获取成功",
                success = true,
                response = await _sys_TenantServices.QueryPage(whereExpression, page, intPageSize)
            };

        }

        [HttpGet("{id}")]
        public async Task<MessageModel<Sys_Tenant>> Get(int id = 0)
        {
            return new MessageModel<Sys_Tenant>()
            {
                msg = "获取成功",
                success = true,
                response = await _sys_TenantServices.QueryById(id)
            };
        }

        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] Sys_Tenant request)
        {
            var data = new MessageModel<string>();

            var id = await _sys_TenantServices.Add(request);
            data.success = id > 0;

            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }

            return data;
        }

        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] Sys_Tenant request)
        {
            var data = new MessageModel<string>();
            if (request.Id > 0)
            {
                data.success = await _sys_TenantServices.Update(request);
                if (data.success)
                {
                    data.msg = "更新成功";
                    data.response = request?.Id.ObjToString();
                }
            }

            return data;
        }

        [HttpDelete("{id}")]
        public async Task<MessageModel<string>> Delete(int id = 0)
        {
            var data = new MessageModel<string>();
            if (id > 0)
            {
                var detail = await _sys_TenantServices.QueryById(id);

                detail.IsDelete = true;

                if (detail != null)
                {
                    data.success = await _sys_TenantServices.Update(detail);
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = id.ObjToString();
                    }
                }
            }

            return data;
        }
    }
}