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

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

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

        /// <summary>
        /// 根据id获取单个企业信息
        /// </summary>
        /// <param name="id">企业id</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<TaxCompany>> GetConpanyInfoById(string id)
        {
            var result = new Response<TaxCompany>();
            try
            {
                var data = await _comApp.GetConpanyInfoById(id);
                result.Result = data;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据社会信用代码获取单个企业信息
        /// </summary>
        /// <param name="id">企业社会信用代码</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<TaxCompany>> GetConpanyInfoByshxydm(string shxydm)
        {
            var result = new Response<TaxCompany>();
            try
            {
                var data = await _comApp.GetConpanyInfoByshxydm(shxydm);
                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>> AddCompany(TaxCompany req)
        {
            var result = new Response<bool>();
            try
            {
                var data = await _comApp.AddCompany(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>> UpdateCompany(TaxCompany req)
        {
            var result = new Response<bool>();
            try
            {
                var data = await _comApp.UpdateCompany(req);
                result = data;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除企业
        /// </summary>
        /// <param name="ids">企业id</param>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> DeleteCompany(string[] ids)
        {
            var result = new Response<bool>();
            try
            {
                var data = await _comApp.DeleteCompany(ids);
                result = data;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        #endregion
        #region
        /// <summary>
        /// 查询乡镇
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<List<dynamic>>> GetStreetInfo()
        {
            var result = new Response<List<dynamic>>();
            try
            {
                var data = await _comApp.GetStreetInfo();
                result.Result = data;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据乡镇编码查询村
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<List<dynamic>>> GetCommunityInfo(string djqdm)
        {
            var result = new Response<List<dynamic>>();
            try
            {
                var data = await _comApp.GetCommunityInfo(djqdm);
                result.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> ImportCompanyInfo(IFormFileCollection formFiles)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _comApp.ImportCompanyInfo(formFiles);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion
    }
}
