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

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

        public FireManagementController(FireManagementApp app)
        {
            _app = app;
        }

        /// <summary>
        /// 下发防火线索任务
        /// </summary>
        /// <param name="info">防火任信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> IssuedFireClueTask(FmFireclueTask info)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.IssuedFireClueTask(info);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 查询接收人员
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<SysUserResp>>> LoadFireClueUser(string username, string unitname)
        {
            Response<List<SysUserResp>> response = new Response<List<SysUserResp>>();
            try
            {
                return await _app.LoadFireClueUser(username, unitname);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 查询任务列表
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<FmFireclueTask>>> GetTaskList()
        {
            Response<List<FmFireclueTask>> response = new Response<List<FmFireclueTask>>();
            try
            {
                return await _app.GetTaskList();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 查询人员单位信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<FmUserUnit>>> GetUserUnit()
        {
            Response<List<FmUserUnit>> response = new Response<List<FmUserUnit>>();
            try
            {
                return await _app.GetUserUnit();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #region 人员单位管理
        /// <summary>
        /// 添加人员单位
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddUserUnit(FmUserUnit info)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.AddUserUnit(info);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 编辑人员单位
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> EditUserUnit(FmUserUnit info)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.EditUserUnit(info);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 删除人员单位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteUserUnit(long id)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.DeleteUserUnit(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 获取单条人员单位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<FmUserUnit>> LoadUserUnitById(long id)
        {
            Response<FmUserUnit> response = new Response<FmUserUnit>();
            try
            {
                return await _app.LoadUserUnitById(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion
        #region 火情线索
        /// <summary>
        /// 添加火情线索
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddFireClueInfo(FmFireclueInfo info)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.AddFireClueInfo(info);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 查询火情线索
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<FmFireclueInfo>> LoadClueInfoById(long id)
        {
            Response<FmFireclueInfo> response = new Response<FmFireclueInfo>();
            try
            {
                return await _app.LoadClueInfoById(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 查询火情线索列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<FmFireclueInfo>>> GetFireClueList()
        {
            Response<List<FmFireclueInfo>> response = new Response<List<FmFireclueInfo>>();
            try
            {
                return await _app.GetFireClueList();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 按日期统计火情线索
        /// </summary>
        /// <param name="type">1-月，2-年</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<FireInfoForChart>>> GetFireClueStatistics(int type)
        {
            Response<List<FireInfoForChart>> response = new Response<List<FireInfoForChart>>();
            try
            {
                return await _app.GetFireClueStatistics(type);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 按状态统计火情线索
        /// </summary>
        /// <param name="type">1-日，2-月，3-年</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<List<FireInfoStateForChart>>> GetFireClueStatisticsByState(int type)
        {
            Response<List<FireInfoStateForChart>> response = new Response<List<FireInfoStateForChart>>();
            try
            {
                return await _app.GetFireClueStatisticsByState(type);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 按等级统计火情线索
        /// </summary>
        /// <param name="type">1-日，2-月，3-年</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<List<FireInfoDegreeForChart>>> GetFireClueStatisticsByDegreeType(int type)
        {
            Response<List<FireInfoDegreeForChart>> response = new Response<List<FireInfoDegreeForChart>>();
            try
            {
                return await _app.GetFireClueStatisticsByDegreeType(type);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion

    }
}
