using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Common.Page;
using GxPress.Repository.Interface.Organization;
using GxPress.Request.OrganizationUser;
using GxPress.Result.Organization;
using GxPress.Service.Interface.OrganizationUser;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AdminControllers
{
    [Route("api/admin/organization")]
    [ApiController]
    [Authorize]
    public class AdminOrganizationController : Controller
    {
        private readonly IOrganizationRepository repository;
        private readonly IOrganizationUserService service;

        private readonly IOrganizationUserRepository organizationUserRepository;
        public AdminOrganizationController(IOrganizationRepository repository, IOrganizationUserService service, IOrganizationUserRepository organizationUserRepository)
        {
            this.repository = repository;
            this.service = service;
            this.organizationUserRepository = organizationUserRepository;
        }
        /// <summary>
        /// 添加机构用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("add-user")]
        public async Task<bool> InsertOrganizationUserAsync(Entity.tede2.Organization.OrganizationUser model)
        {
            if (model.OrganizationId == 0)
                throw new Common.Exceptions.BusinessException("机构ID必填");
            if (string.IsNullOrEmpty(model.LoginAccountNumber))
                throw new Common.Exceptions.BusinessException("登陆账号必填");
            if (string.IsNullOrEmpty(model.UserName))
                throw new Common.Exceptions.BusinessException("用户名必填");
            if (string.IsNullOrEmpty(model.Phone))
                throw new Common.Exceptions.BusinessException("手机号码必填");
            if (model.Phone.Length != 11)
                throw new Common.Exceptions.BusinessException("手机号码有误");
            return await service.InsertAsync(model);
        }
        /// <summary>
        /// 机构用户启用停用
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("user-updat-disable")]
        public async Task<bool> UpdateDisableAsync(OrganizationUserDisableUpdateRequest request)
        {
            return await organizationUserRepository.UpdateDisableAsync(request);
        }
        /// <summary>
        /// 机构用户删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("user-delete")]
        public async Task<bool> UserDeleteAsync(List<int> ids)
        {
            return await service.DeleteAsync(ids);
        }
        /// <summary>
        /// 机构用户列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("user-list")]
        public async Task<PagedList<Entity.tede2.Organization.OrganizationUser>> GetSearchAllAsync(OrganizationUserSearchRequest request)
        {
            return await organizationUserRepository.GetSearchAllAsync(request);
        }
        /// <summary>
        ///  机构用户修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("user-updat")]
        public async Task<bool> UpdateAsync(OrganizationUserUpdateRequest request)
        {
            return await organizationUserRepository.UpdateAsync(request);
        }

        /// <summary>
        ///  机构列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PagedList<Entity.tede2.Organization.Organization>> GetAllAsync(OrganizationListRequest request)
        {
            return await repository.GetAllAsync(request);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> UpdateAsync(OrganizationUpdateRequest request)
        {
            return await repository.UpdateAsync(request);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> DeleteAsync(List<int> ids)
        {
            return await repository.DeleteAsync(ids);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("update")]
        public async Task<bool> UpdateIsDisableAsync(OrganizationUpdateDisableRequest request)
        {
            return await repository.UpdateIsDisableAsync(request);
        }
        /// <summary>
        /// 添加
        /// /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<bool> InsertAsync(Entity.tede2.Organization.Organization model)
        {
            return await repository.InsertAsync(model);
        }
    }
}