using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.Repository.Domain;
using OpenAuth.App.ServiceApp.TaxZongdiManage;
using OpenAuth.App.ServiceApp.TaxZongdiManage.Request;
using OpenAuth.App.ServiceApp.TaxBuildingManage.Request;
using SqlSugar;
using Infrastructure.Helpers.Excel.Model;
using Infrastructure.Helpers.Excel;

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

        #region 获取分页数据列表
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadZongdiPageList([FromQuery] TaxZongdiReq req)
        {
            var result = new Response<PageInfo<List<dynamic>>>();
            try
            {
                var data = await _comApp.LoadZongdiPageList(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<TaxZongdi>> GetZongdiInfoById(int gid)
        {
            var result = new Response<TaxZongdi>();
            try
            {
                var data = await _comApp.GetZongdiInfoById(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>> AddZongdi(TaxZongdi req)
        {
            var result = new Response<bool>();
            try
            {
                var data = await _comApp.AddZongdi(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>> UpdateZongdi(TaxZongdi req)
        {
            var result = new Response<bool>();
            try
            {
                var data = await _comApp.UpdateZongdi(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>> DeleteZongdi(int[] ids)
        {
            var result = new Response<bool>();
            try
            {
                var data = await _comApp.DeleteZongdi(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> ImportZongdiInfo(IFormFileCollection formFiles)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _comApp.ImportZongdiInfo(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> ExportZongdi([FromQuery] TaxZongdiReq req)
        {
            var res = new Response();
            //获取列表数据
            var result = await _comApp.LoadZongdiPageExport(req);
            List<ModuleColumn> columns = new List<ModuleColumn>
            {
                new ModuleColumn { key = "gid", value = "gid" },
                new ModuleColumn { key = "RowNumber", value = "序号" },
                new ModuleColumn { key = "zdbm", value = "宗地编号" },
                new ModuleColumn { key = "bdcdyh", value = "不动产单元号" },
                new ModuleColumn { key = "djqmc", value = "乡镇" },
                new ModuleColumn { key = "djzqmc", value = "村" },
                new ModuleColumn { key = "zdfzmj", value = "宗地发证面积" },
                new ModuleColumn { key = "syqsq", value = "使用起始期" },
                new ModuleColumn { key = "tdxz", value = "土地性质" },
                new ModuleColumn { key = "jzfzzmj", value = "建筑发证总面积" },
                new ModuleColumn { key = "tdqdfs", 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
    }
}
