using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.ServiceApp.TaxBuildingManage.Request;
using OpenAuth.App.ServiceApp.TaxBuildingManage;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.ServiceController
{
    /// <summary>
    /// 用房信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TaxBuildingController : ControllerBase
    {
        TaxBuildingApp _comApp;
        public TaxBuildingController(
            TaxBuildingApp comApp)
        {
            _comApp = comApp;
        }

        #region 获取分页数据列表
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadBuildingPageList([FromQuery] TaxBuildingReq req)
        {
            var result = new Response<PageInfo<List<dynamic>>>();
            try
            {
                var data = await _comApp.LoadBuildingPageList(req);
                result = data;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据id获取单个用房信息
        /// </summary>
        /// <param name="gid">企业用房gid</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<TaxBuilding>> GetBuildingInfoById(int gid)
        {
            var result = new Response<TaxBuilding>();
            try
            {
                var data = await _comApp.GetBuildingInfoById(gid);
                result.Result = data;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        #endregion
        #region
        /// <summary>
        /// 添加用房信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> AddBuilding(TaxBuilding req)
        {
            var result = new Response<bool>();
            try
            {
                var data = await _comApp.AddBuilding(req);
                result = data;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 编辑用房信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> UpdateBuilding(TaxBuilding req)
        {
            var result = new Response<bool>();
            try
            {
                var data = await _comApp.UpdateBuilding(req);
                result = data;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除用房信息
        /// </summary>
        /// <param name="ids">企业用房gid</param>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> DeleteBuilding(int[] ids)
        {
            var result = new Response<bool>();
            try
            {
                var data = await _comApp.DeleteBuilding(ids);
                result = data;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        #endregion

        #region 企业用房数据导入
        /// <summary>
        /// 上传企业用房数据
        /// </summary>
        /// <param name="formFiles">文件</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public Response<bool> ImportBuildingInfo(IFormFileCollection formFiles)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _comApp.ImportBuildingInfo(formFiles);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion
    }
}
