using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenAuth.App;
using OpenAuth.App.FormModule;
using OpenAuth.App.FormScheme.Request;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using OpenAuth.WebApi.Model.CustomAttribute;
using SqlSugar;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 无人机执法
    /// </summary>
    //[Route("api/[controller]/[action]")]
    //[ApiController]
    public partial class DroneCaseinfoController1 : ControllerBase
    {
        IConfiguration _configuration;
        DroneCaseinfoApp _app;
        IAuth _auth;
        ILogger<DroneCaseinfoController> _logger;
        WFProcessApp _wfProcessApp;
        public DroneCaseinfoController1(
            IConfiguration configuration,
            DroneCaseinfoApp app,
            IAuth auth,
            ILogger<DroneCaseinfoController> logger,
            WFProcessApp wFProcessApp)
        {
            _configuration = configuration;
            _app = app;
            _auth = auth;
            _logger = logger;
            _wfProcessApp = wFProcessApp;
        }

        #region 案件
        /// <summary>
        /// 收藏案件
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> AddCaseFavorite([FromBody] DroneCaseFavorite model)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = await _app.AddCaseFavorite(model);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 加载收藏的案件信息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<DroneCaseInfo>>>> LoadFavoriteCases([FromQuery] PageReq req)
        {
            try
            {
                return await _app.LoadFavoriteCases(req);
            }
            catch (Exception ex)
            {
                return new Response<PageInfo<List<DroneCaseInfo>>>()
                {
                    Result = null,
                    Code = 500,
                    Message = "获取数据失败"
                };
            }

        }
        /// <summary>
        /// 获取收藏列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<dynamic>> FavoriteCaseList(string uid)
        {

            try
            {
                return await _app.FavoriteCaseList(uid);
            }
            catch (Exception ex)
            {
                return new Response<dynamic>()
                {
                    Result = null,
                    Code = 500,
                    Message = "获取数据失败"
                };
            }
        }
        /// <summary>
        /// 批量删除收藏的案件
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteFavoriteCases([FromBody] string[] ids)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = await _app.DeleteFavoriteCases(ids);
            }
            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>> DeleteFavoriteCase([FromBody] string id)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = await _app.DeleteFavoriteCase(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 获取案件记录
        /// </summary>
        /// <param name="caseid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<List<DroneCaseOperationLog>>> LoadDroneCaseLog(string caseid)
        {
            Response<List<DroneCaseOperationLog>> response = new Response<List<DroneCaseOperationLog>>();
            try
            {
                response = await _app.LoadDroneCaseLog(caseid);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 无人机添加案件
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<string>> AddDroneCaseByDrone([FromBody] AddDroneCaseByDroneReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                response = await _app.BackhaulDroneCaseByDrone(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 上报案件
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> AddDroneCaseInfo([FromBody] AddOrUpdateDroneCaseInfoReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                if (string.IsNullOrEmpty(req.info.Id))
                {
                    //新增
                    response = await _app.AddDroneCaseInfo(req);
                }
                else
                {
                    //修改
                    response = await _app.UpdateDroneCaseInfo(req);
                }
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 案件判读
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> UpdateDroneCaseInfoIntact([FromBody] AddOrUpdateDroneCaseInfoReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                response = await _app.UpdateDroneCaseInfoIntact(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 分配案件
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DispenseDroneCaseData([FromBody] AddOrUpdateDispenseDroneCaseDataReq req)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = await _app.DispenseDroneCaseData(req);
            }
            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<string[]>> GetDispenseDroneCaseData(string id)
        {
            Response<string[]> response = new Response<string[]>();
            try
            {
                response = await _app.GetDispenseDroneCaseData(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<bool>> CloseDroneCaseInfo(string id)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = await _app.CloseDroneCaseInfo(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<bool>> VerifyCaseInfoSuccess(string id)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = await _app.VerifyCaseInfoSuccess(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 审核改进
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> VerifyCaseInfoFailed(AddOrUpdateVerifyCaseSuccessReq req)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = await _app.VerifyCaseInfoFailed(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 上报事件
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> AddCaseEvent([FromBody] AddOrUpdateDroneCaseEventReq req)
        {
            var response = new Response<string>();
            try
            {
                response = await _app.AddCaseEvent(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 上报案件临时接口-分配给自己
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> AddTemporaryDroneCaseInfo([FromBody] AddOrUpdateDroneCaseInfoReq req)
        {
            Response<string> response = new Response<string>();
            try
            {
                response = await _app.AddTemporaryDroneCaseInfo(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion

        #region 案件列表
        /// <summary>
        /// 查询案件和处理信息列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<CaseInfoAndDealInfoResponse>>>> LoadCaseInfoAndDealInfoList([FromQuery] QueryCaseInfoListReq req)
        {
            try
            {
                return await _app.LoadCaseInfoAndDealInfoList(req);
            }
            catch (Exception ex)
            {
                return new Response<PageInfo<List<CaseInfoAndDealInfoResponse>>>()
                {
                    Code = 500,
                    Message = "获取数据失败"
                };
            }
        }
        /// <summary>
        /// 查询案件列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<DroneCaseInfoExt>>>> LoadCaseInfoList([FromQuery] QueryCaseInfoListReq req)
        {
            try
            {
                return await _app.LoadCaseInfoListInterface(req);
            }
            catch (Exception ex)
            {
                return new Response<PageInfo<List<DroneCaseInfoExt>>>()
                {
                    Code = 500,
                    Message = "获取数据失败"
                };
            }
        }
        /// <summary>
        /// 查询判读案件列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<DroneCaseInfo>>>> LoadIntactCaseInfoList([FromQuery] QueryIntactCaseInfoListReq obj)
        {
            try
            {
                QueryCaseInfoListReq req = obj.MapTo<QueryCaseInfoListReq>();
                return await _app.LoadCaseInfoList(req);
            }
            catch (Exception ex)
            {
                return new Response<PageInfo<List<DroneCaseInfo>>>()
                {
                    Code = 500,
                    Message = "获取数据失败"
                };
            }
        }
        /// <summary>
        /// 查询处理案件列表
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<DroneCaseInfoExt>>>> LoadDealCaseInfoList([FromQuery] QueryCaseInfoListReq obj)
        {
            try
            {
                return await _app.LoadDealCaseInfoList(obj);
            }
            catch (Exception ex)
            {
                return new Response<PageInfo<List<DroneCaseInfoExt>>>()
                {
                    Code = 500,
                    Message = "获取数据失败"
                };
            }
        }
        /// <summary>
        /// 根据案件分配查询案件
        /// </summary>
        /// <param name="handle_status_id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<DroneCaseInfoExt>>>> LoadDealCaseInfoListByLaw([FromQuery] QueryCaseInfoListReq obj)
        {
            try
            {
                return await _app.LoadDealCaseInfoListByLaw(obj);
            }
            catch (Exception ex)
            {
                return new Response<PageInfo<List<DroneCaseInfoExt>>>()
                {
                    Code = 500,
                    Message = "获取数据失败"
                };
            }
        }

        #endregion

        #region 案件数量统计App查询案件 全部 未办理 办理中 已办结数量
        /// <summary>
        /// 案件数量统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<JObject>> CaseCountCensus()
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                QueryCaseInfoListReq req = new QueryCaseInfoListReq();
                req.is_dealer = 1;
                response = await _app.CaseCountCensus(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 待审核案件及未读消息数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<JObject> UnreadMsgAndCaseCount()
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                return _app.UnreadMsgAndCaseCount();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 首页通知统计数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<JObject> GetIndexCensusDataApp()
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                return _app.GetIndexCensusDataApp();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 根据分配案件 进行案件数量统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<JObject>> CaseCountCensusByLaw()
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                //判断是否是乡镇管理员和审核管理员
                var currentUser = _auth.GetCurrentUser();
                var _count = currentUser.Roles.Where(c => c.Name.Contains("案件处理")).Count();
                if (_count == 0)
                {
                    return await CaseCountCensus();
                }
                else
                {
                    QueryCaseInfoListReq req = new QueryCaseInfoListReq();
                    req.dispense_dealer = 1;
                    return await _app.CaseCountCensus(req);
                }
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 根据分配案件 进行案件数量统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<object>> CaseCountCensusByLawDetail()
        {
            Response<object> response = new Response<object>();
            try
            {
                return await _app.GetDroneTypeCount("", 1);
            }
            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<AddOrUpdateDroneCaseInfoReqExt>> GetCaseInfo(string id)
        {
            Response<AddOrUpdateDroneCaseInfoReqExt> response = new Response<AddOrUpdateDroneCaseInfoReqExt>();
            try
            {
                return await _app.GetCaseInfo(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<CaseInfoAndDealInfoByIdResponse>> GetCaseInfoAndDealInfo(string id)
        {
            Response<CaseInfoAndDealInfoByIdResponse> response = new Response<CaseInfoAndDealInfoByIdResponse>();
            try
            {
                return await _app.GetCaseInfoAndDealInfo(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<List<CaseFlowLogModel>>> GetCaseFlowLog(string id)
        {
            Response<List<CaseFlowLogModel>> response = new Response<List<CaseFlowLogModel>>();
            try
            {
                return await _app.GetCaseFlowLog(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<object>> LoadCaseInfoData()
        {
            Response<object> response = new Response<object>();
            try
            {
                return await _app.LoadCaseInfoData();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 删除案件接口
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteDroneCaseInfo(string[] ids)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.DeleteDroneCaseInfo(ids);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteDroneFiles(string[] ids)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.DeleteDroneFiles(ids);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 删除文件 - 通过路径删除
        /// </summary>
        /// <param name="paths"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteDroneFilesByPath(string[] paths)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.DeleteDroneFilesByPath(paths);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 获取所有案件中心点
        /// APP获取中心点和图层的坐标点集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<JToken>> GetAllDroneCaseCenterApp(string id)
        {
            Response<JToken> response = new Response<JToken>();
            try
            {
                return await _app.GetAllDroneCaseCenterApp(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> AddDroneCasePic([FromBody] AddFroneCasePicReq req)
        {
            var res = new Response<string>();
            try
            {
                return await _app.AddDroneCasePic(req);
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }
        /// <summary>
        /// 添加案件处理图片
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> AddDroneDealPic([FromBody] AddDroneDealPicReq req)
        {
            var res = new Response<string>();
            try
            {
                return await _app.AddDroneDealPic(req);
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> DeleteDroneCasePic(string id)
        {
            var res = new Response<bool>();
            try
            {
                return await _app.DeleteDroneCasePic(id);
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }
        #endregion

        #region 通知
        /// <summary>
        /// 查询所有通知信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<JToken>>> LoadMessageList([FromQuery] QueryDroneCaseinfoReq req)
        {
            try
            {
                return await _app.LoadMessageList(req);
            }
            catch (Exception ex)
            {
                return new Response<PageInfo<JToken>>()
                {
                    Result = null,
                    Code = 500,
                    Message = "获取数据失败"
                };
            }
        }
        ///// <summary>
        ///// 查询通知详情
        ///// </summary>
        ///// <param name="id">通知信息id</param>
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<Response<DroneMessage>> LoadMessage(string id)
        //{
        //    var result = new Response<DroneMessage>();
        //    try
        //    {
        //        return await _app.LoadMessage(id);
        //    }
        //    catch (Exception ex)
        //    {
        //        result.Code = 500;
        //        result.Message = ex.InnerException?.Message ?? ex.Message;
        //    }
        //    return result;
        //}
        /// <summary>
        /// 查询通知详情
        /// </summary>
        /// <param name="id">通知信息id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<QueryDroneCaseRecipientUserReq>> LoadMessage(string id)
        {
            var result = new Response<QueryDroneCaseRecipientUserReq>();
            try
            {
                return await _app.LoadMessage(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 根据权限查看通知详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<DroneMessage>> LoadMessageUser(string id)
        {
            var result = new Response<DroneMessage>();
            try
            {
                return await _app.LoadMessageUser(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 添加通知
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> AddMessage([FromBody] QueryDroneCaseRecipientUserReq req)
        {
            var result = new Response<string>();
            try
            {
                return await _app.AddMessage(req);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 添加通知-部门角色
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> AddMessRoleOrg([FromBody] QueryDroneCaseRecipientOrgRoleReq req)
        {
            var result = new Response<string>();
            try
            {
                return await _app.AddMessRoleOrg(req);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 编辑通知
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> UpdateMessage([FromBody] DroneMessage obj)
        {
            var result = new Response<string>();
            try
            {
                return await _app.UpdateMessage(obj);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 更新删除通知信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> UpdateDelMessage(string id)
        {
            var result = new Response<bool>();
            try
            {
                return await _app.UpdateDelMessage(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Result = false;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 设置通知已读
        /// </summary>
        /// <param name="messageid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<bool>> SetMessageRead(string messageid)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.SetMessageRead(messageid);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion

        #region 图层
        /// <summary>
        /// 根据表名获取矢量切片
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public IActionResult QueryVectorTileByTable([FromQuery] QueryVectorTileByTableReq req)
        {
            var result = _app.QueryVectorTileByTable(req);
            return File(result, "application/octet-stream");
        }
        /// <summary>
        /// 添加违建图层
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> AddIllegalBuildeLayer([FromBody] DroneShpData model)
        {
            var res = new Response<string>();
            try
            {
                return await _app.AddIllegalBuildeLayer(model);
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }
        /// <summary>
        /// 编辑违建图层
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<string>> UpdateIllegalBuildeLayer([FromBody] DroneShpData model)
        {
            var res = new Response<string>();
            try
            {
                return await _app.UpdateIllegalBuildeLayer(model);
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }
        /// <summary>
        /// 删除图斑
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<bool>> DeleteDroneLayers(int gid)
        {
            var res = new Response<bool>();
            try
            {
                return await _app.DeleteDroneLayers(gid);
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }
        /// <summary>
        /// 查询主页违法图层数据
        /// 获取所有图斑，主页用
        /// </summary>
        /// <param name="reqObj"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<JToken> LoadAllDroneGeoJson([FromQuery] QueryAllDroneGeoJsonReq reqObj)
        {
            var res = new Response<JToken>();
            try
            {
                res.Result = _app.LoadAllDroneGeoJson(reqObj);
            }
            catch (Exception ex)
            {
                res.Code = 500;
                res.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return res;
        }
        /// <summary>
        /// 获取GeoJson
        /// PC获取图层的GeoJson（判读页面用）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public Response<JToken> GetDroneGeoJson(string id)
        {
            Response<JToken> response = new Response<JToken>();
            try
            {
                response.Result = _app.GetDroneGeoJson(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// app获取GeoJson
        /// 替代app获取中心点和图层坐标点集合
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<JToken>> GetDroneGeoJsonApp(string id)
        {
            Response<JToken> response = new Response<JToken>();
            try
            {
                return await _app.GetDroneGeoJsonApp(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 根据经纬度获取组织机构
        /// </summary>
        /// <param name="lng"></param>
        /// <param name="lat"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<JObject>> GetOrgAreaByPoint(decimal lng, decimal lat)
        {
            Response<JObject> response = new Response<JObject>();
            try
            {
                return await _app.GetOrgAreaByPoint(lng, lat);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 计算点与shp之间的距离
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<bool>> GetPointShpDistance(string lng, string lat, string caseid)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                return await _app.GetPointShpDistance(lng, lat, caseid);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion

        #region 统计
        /// <summary>
        /// 综合统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]

        public async Task<Response<JArray>> CaseSynthesisCensusSingle([FromQuery] QueryCaseInfoListReq req)
        {
            Response<JArray> response = new Response<JArray>();
            try
            {
                return await _app.CaseSynthesisCensusSingle(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        [HttpPost]
        public async Task<IActionResult> ExportCaseSynthesisCensusStreet([FromQuery]  QueryCaseInfoListReq req)
        {
            var res = new Response();
            //获取列表数据
            Response<JArray> response =  await _app.CaseSynthesisCensusSingle(req);
            //获取模板数据
            List<ModuleColumn> headers = new List<ModuleColumn>();
            ModuleColumn m1 = new ModuleColumn();
            m1.key = "streetname";
            m1.value = "乡镇";
            headers.Add(m1);
            ModuleColumn m2 = new ModuleColumn();
            m2.key = "allCount";
            m2.value = "无人机发现数量";
            headers.Add(m2);
            ModuleColumn m3 = new ModuleColumn();
            m3.key = "handleStatus";
            m3.value = "完成外业核查数量";
            headers.Add(m3);
            ModuleColumn m4 = new ModuleColumn();
            m4.key = "notDealHour24";
            m4.value = "24h内未核查";
            headers.Add(m4);
            ModuleColumn m5 = new ModuleColumn();
            m5.key = "typeFanxinCount";
            m5.value = "房屋翻新";
            headers.Add(m5);
            ModuleColumn m6 = new ModuleColumn();
            m6.key = "typeJiagaiCount";
            m6.value = "房屋加盖";
                headers.Add(m6);
            ModuleColumn m7 = new ModuleColumn();
            m7.key = "typeFanjianCount";
            m7.value = "房屋翻建";
            headers.Add(m7);
            ModuleColumn m8 = new ModuleColumn();
            m8.key = "typeCunliangCount";
            m8.value = "存量建设";
            headers.Add(m8);
            ModuleColumn m9 = new ModuleColumn();
            m9.key = "typeTuituCount";
            m9.value = "推土";
            headers.Add(m9);    
            ModuleColumn m10 = new ModuleColumn();
            m10.key = "typeOtherCount";
            m10.value = "其他";
            headers.Add(m10);   
            ModuleColumn m11 = new ModuleColumn();
            m11.key = "illegal0Count";
            m11.value = "合法";
            headers.Add(m11);
            ModuleColumn m12 = new ModuleColumn();
            m12.key = "illegal1Count";
            m12.value = "违法";
            headers.Add(m12);   
            ModuleColumn m13 = new ModuleColumn();
            m13.key = "illegal2Count";
            m13.value = "伪变化";
            headers.Add(m13);   
            ModuleColumn m14 = new ModuleColumn();
            m14.key = "illegalNullCount";
            m14.value = "外业核实确定违法数";
            headers.Add(m14);    
            ModuleColumn m15 = new ModuleColumn();
            m15.key = "illegalHandleAreaList";
            m15.value = "违法面积";
            headers.Add(m15);
            ModuleColumn m16 = new ModuleColumn();
            m16.key = "illegalHandle2Status";
            m16.value = "整改销号数";
            headers.Add(m16);
            ModuleColumn m17 = new ModuleColumn();
            m17.key = "illegalHandle01Status"; 
            m17.value = "尚未整改宗数";
            headers.Add(m17);
            ModuleColumn m18 = new ModuleColumn();
            m18.key = "illegalHandle2AreaList";
            m18.value = "销号面积";
            headers.Add(m18);
            ModuleColumn m19 = new ModuleColumn();
            m19.key = "illegalHandle01AreaList";
            m19.value = "未整改面积";
            headers.Add(m19);
            ModuleColumn m20 = new ModuleColumn();
            m20.key = "notComplete3";
            m20.value = "3日内未整改完成";
            headers.Add(m20);
            ModuleColumn m21 = new ModuleColumn();
            m21.key = "notComplete7";
            m21.value = "7日内未整改完成";
            headers.Add(m21);
            ModuleColumn m22 = new ModuleColumn();
            m22.key = "notComplete30";
            m22.value = "30日内未整改完成";
            headers.Add(m22);
            ModuleColumn m23 = new ModuleColumn();
            m23.key = "notComplete30More";
            m23.value = "30日以上未整改完成";
            headers.Add(m23);

            var excelRes = _app.ListToExcel(response.Result, headers);
            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);
        }

        [HttpGet]
        public async Task<Response<JArray>> CaseSynthesisCensus([FromQuery] QueryCaseInfoListReq req)
        {
            Response<JArray> response = new Response<JArray>();
            try
            {
                return await _app.CaseSynthesisCensus(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        /// <summary>
        /// 综合统计，统计镇权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<JArray> CaseSynthesisCensusStreet([FromQuery] QueryCaseInfoListReq req)
        {
            Response<JArray> response = new Response<JArray>();
            try
            {
                response.Result = _app.CaseSynthesisCensusStreet(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion
    }
}
