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;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using Infrastructure.Helpers.Excel.Model;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Infrastructure.Helpers.Excel;

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]
        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]
        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]
        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]
        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]
        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]
        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

        #region 获取导出数据
        /// <summary>
        /// 获取导出数据
        /// </summary>
        /// <param name="mid">功能id</param>
        /// <param name="id">id数据</param>
        /// <param name="query">查询参数</param>
        /// <param name="code">编号</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Export([FromQuery] TaxBuildingReq req)
        {
            var res = new Response();
            //获取列表数据
            var result = await _comApp.LoadBuildingPageExport(req);
            List<ModuleColumn> columns = new List<ModuleColumn>
            {
                new ModuleColumn { key = "gid", value = "gid" },
                new ModuleColumn { key = "RowNumber", value = "序号" },
                new ModuleColumn { key = "jzwbm", value = "建筑物编码" },
                new ModuleColumn { key = "jzwmc", value = "建筑物名称" },
                new ModuleColumn { key = "jzwjg", value = "建筑物结构" },
                new ModuleColumn { key = "jzsyqr", value = "建筑物所有权人" },
                new ModuleColumn { key = "jzwsjmj", value = "建筑物实际占地面积" },
                new ModuleColumn { key = "jzwfzmj", value = "建筑物发证面积" },
                new ModuleColumn { key = "jgrq", value = "竣工日期" },
                new ModuleColumn { key = "dcrq", value = "调查日期" },
                new ModuleColumn { key = "bz", value = "备注" },
            };
            var excelRes = ExcelHelper.ListToExcel(result.Result, columns);
            if (excelRes.Code == 200)
            {
                return File(excelRes.Result.ToArray(),
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    "企业用房信息" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            }
            else
            {
                res.Code = excelRes.Code;
                res.Message = "导出失败";
            }
            return Ok(res);
        }
        #endregion

    }
}
