using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.Response;
using OpenAuth.App.ServiceApp.FireManagement;
using OpenAuth.App.ServiceApp.FireManagement.Request;
using OpenAuth.App.ServiceApp.FmFireSiteManage;
using OpenAuth.App.ServiceApp.FmFireSiteManage.Response;
using OpenAuth.Repository.Domain.FireManagement;

namespace OpenAuth.WebApi.Controllers.ServiceControllers.FireManagement
{
    /// <summary>
    /// 防火站点管理模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FmFireSiteController : ControllerBase
    {
        private readonly FmFireSiteApp _app;

        public FmFireSiteController(FmFireSiteApp app)
        {
            _app = app;
        }

        #region 站点管理
        /// <summary>
        /// 查询所有站点
        /// </summary>
        /// <param name="siteName">站点名称</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="state">状态（0-未审核，1-审核通过，2-未通过）</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<FmSiteInfo>>>> LoadAllSite(string siteName, int pageIndex, int pageSize, int? state)
        {
            Response<PageInfo<List<FmSiteInfo>>> response = new Response<PageInfo<List<FmSiteInfo>>>();
            try
            {
                return await _app.LoadAllSite(siteName,pageIndex,pageSize,state);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 导出所有站点
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportSite(int? state)
        {
            var res = new Response();
            var excelRes = _app.ExportSite(state);
            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);
        }

        /// <summary>
        /// 站点审核
        /// </summary>
        /// <param name="siteId">站点id</param>
        /// <param name="state">要修改的状态</param>
        /// <param name="content">审核说明</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AuditSite(long siteId, int state, string content)
        {
            Response<bool> response = new();
            try
            {
                return await _app.AuditSite(siteId,state,content);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 获取站点管理人员及消息接收人员
        /// </summary>
        /// <param name="siteId">站点id</param>
        /// <param name="roleid">角色id（站点管理人员角色的id）</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<SiteUserRes>>>> LoadSiteUser(long siteId, long? roleid, int pageIndex, int pageSize)
        {
            Response<PageInfo<List<SiteUserRes>>> response = new Response<PageInfo<List<SiteUserRes>>>();
            try
            {
                return await _app.LoadSiteUser(siteId, roleid,pageIndex, pageSize);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 获取所有站点管理人员
        /// </summary>
        /// <param name="roleid">角色id（站点管理人员角色id）</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadSiteAllUser(long roleid, int pageIndex, int pageSize)
        {
            Response<PageInfo<List<dynamic>>> response = new Response<PageInfo<List<dynamic>>>();
            try
            {
                return await _app.LoadSiteAllUser(roleid,pageIndex, pageSize);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 根据管理员获取站点列表
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadSiteByUserId(string userId, int pageIndex, int pageSize)
        {
            Response<PageInfo<List<dynamic>>> response = new Response<PageInfo<List<dynamic>>>();
            try
            {
                return await _app.LoadSiteByUserId(userId, pageIndex, pageSize);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 注册站点信息
        /// </summary>
        /// <param name="fmSite">站点数据</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> AddSiteInfo(FmSiteInfo fmSite)
        {
            Response<bool> response = new();
            try
            {
                return await _app.AddSite(fmSite);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 站点编辑
        /// </summary>
        /// <param name="fmSite">站点编辑</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> EditSiteInfo(FmSiteInfo fmSite)
        {
            Response<bool> response = new();
            try
            {
                return await _app.EditSite(fmSite);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 获取站点详情
        /// </summary>
        /// <param name="siteId">站点id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<FmSiteRes>> GetSiteInfo(long siteId)
        {
            Response<FmSiteRes> response = new Response<FmSiteRes>();
            try
            {
                return await _app.GetSiteInfo(siteId);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion

        #region
        /// <summary>
        /// 进出山类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public Response<List<EnumItem>> InTypes()
        {
            Response<List<EnumItem>> response = new Response<List<EnumItem>>();
            try
            {
                return  _app.InTypes();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }


        /// <summary>
        /// 获取进出山人员
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="cardNo">身份证号</param>
        /// <param name="siteId">站点id</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<PageInfo<List<EnteringInfoResp>>>> LoadInUser(string userName, string cardNo, long? siteId, int pageIndex, int pageSize, DateTime startTime, DateTime endTime)
        {
            Response<PageInfo<List<EnteringInfoResp>>> response = new Response<PageInfo<List<EnteringInfoResp>>>();
            try
            {
                return await _app.LoadInUser(userName,cardNo,siteId, pageIndex, pageSize,startTime,endTime);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }


        /// <summary>
        /// 导出进出山人员
        /// </summary>
        /// <param name="siteId">站点id</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public IActionResult ExportInUser(long? siteId, DateTime startTime, DateTime endTime)
        {
            var res = new Response();
            var excelRes = _app.ExportInUser(siteId,startTime,endTime);
            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
    }
}
