using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using OpenAuth.App;
using OpenAuth.App.FormModule;
using OpenAuth.App.Request;
using OpenAuth.App.ServiceApp;
using OpenAuth.App.ServiceApp.DroneCaseInfo;
using OpenAuth.App.ServiceApp.DroneCaseInfo.Request;
using OpenAuth.App.ServiceApp.Request;
using OpenAuth.App.ServiceApp.Response;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.ServiceControllers;

/// <summary>
/// 重点问题2
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
class DroneCaseInfoZdwt2Controller : ControllerBase
{
    readonly DroneCaseInfoZdwt2App _app;

    public DroneCaseInfoZdwt2Controller(DroneCaseInfoZdwt2App app)
    {
        _app = app;

    }
    /// <summary>
    /// 查询案件列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [NonAction]
    public async Task<Response<PageInfo<List<DroneCaseInfoZdwt2>>>> LoadCaseInfoList([FromQuery] QueryCaseInfoListReq req)
    {
        return await _app.LoadCaseInfoListInterface(req);
    }

    /// <summary>
    /// 案件详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<AddOrUpdateDroneCaseInfoReqExtCurrent>> GetCaseInfo(string id)
    {
        var response = new Response<AddOrUpdateDroneCaseInfoReqExtCurrent>();
        try
        {
            return await _app.GetCaseInfo(id);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return response;
    }

    [HttpGet]
    public async Task<Response<DroneCaseInfoZdwt2>> GetCaseInfoById(string id)
    {
        var response = new Response<DroneCaseInfoZdwt2>();
        try
        {
            return await _app.GetCaseInfoById(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]
    [NonAction]
    public Response<string> DroneCaseInfoExport(string id)
    {
        Response<string> response = new Response<string>();
        try
        {
            response.Result = _app.DroneCaseInfoExport(id);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return response;
    }

    [HttpGet]
    [AllowAnonymous]
    public void GenModel()
    {
        _app.GenModel();
    }

    /// <summary>
    /// 查询待办任务统计数据（审核监督第一层接口） 审核监督/填报审核（illegal 1）/整改审核
    /// </summary>
    /// <param name="year">年份</param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<List<dynamic>>> LoadTaskCount(int year, string tubanlaiyuan, string picihao, int illegal)
    {
        return await _app.LoadTaskCount(year, tubanlaiyuan, picihao, illegal);
    }

    /// <summary>
    /// 查询任务列表数据（原审核监督第二层接口） 审核监督-填报审核
    /// </summary>
    /// <param name="areaid">区域id</param>
    /// <param name="level">等级</param>
    /// <param name="year">年</param>
    /// <param name="is_build_complete">标注类型</param>
    /// <param name="geomid">图斑号</param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<PageInfo<List<dynamic>>>> LoadTaskDetailList([FromQuery] TaskDetailReq req)
    {
        return await _app.LoadTaskDetailList(req);
    }


    /// <summary>
    /// 查询案件信息统计数据
    /// </summary>
    /// <param name="year">年份</param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<List<dynamic>>> LoadDroneCaseInfoCount(int year)
    {
        return await _app.LoadDroneCaseInfoCount(year);
    }

    /// <summary>
    /// 分页获取案件信息列表数据
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<PageInfo<List<dynamic>>>> LoadDroneCaseInfoDetail([FromQuery] DroneInfoStatisticsReq req)
    {
        return await _app.LoadDroneCaseInfoDetail(req);
    }





    [HttpPost]
    public Response<string> UpdateDroneCaseInfo(DronCaseSingleCityUpdateReq req)
    {
        Response<string> response = new Response<string>();
        try
        {
            _app.UpdateDroneCaseInfo(req);
            response.Result = "修改成功";
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return response;


    }
    [HttpGet]
    public Response<string> DroneCaseInfoFavorite(string CaseNo, int type)
    {
        Response<string> response = new Response<string>();
        try
        {
            response.Result = _app.dronecaseinfoFavarite(CaseNo, type);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }

        return response;
    }

    [HttpGet]
    public async Task<Response<PageInfo<List<DroneCaseFavorite>>>> getFavoriteList(int page = 1, int limit = 10)
    {
        return await _app.getFavariteList(page, limit);
    }


    [HttpPost]
    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;
    }

    [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;
    }

    [HttpPost]
    public async Task<Response<bool>> AddtaskFavorite([FromBody] DroneTaskFavorite model)
    {
        Response<bool> response = new Response<bool>();
        try
        {
            response = await _app.AddTaskFavorite(model);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }

    [HttpPost]
    public async Task<Response<bool>> DeleteTaskCase([FromBody] string id)
    {
        Response<bool> response = new Response<bool>();
        try
        {
            response = await _app.DeleteTaskFavoriteCase(id);
        }
        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<dynamic>>>> LoadTasklList([FromQuery] TaskListReq req)
    {
        return await _app.LoadTaskList(req);
    }



    [HttpGet]
    public async Task<Response<Dictionary<string, object>>> LoadMyUncompletedTask([FromQuery] TaskUncompletReq req)
    {
        return await _app.LoadMyUncompletedTask(req);
    }



    [HttpGet]
    public Response<List<dynamic>> loadStreet()
    {
        return _app.loadStreet();
    }
    /// <summary>
    /// 整改审核第二层接口
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<PageInfo<List<dynamic>>>> LoadTaskIllegalDetailList([FromQuery] TaskDetailReq req)
    {
        return await _app.LoadTaskIllegalDetailList(req);
    }
    /// <summary>
    /// 图斑列表
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<PageInfo<List<dynamic>>>> LoadCaseInfoTuBanList([FromQuery] CaseInfoTuBanReq req)
    {
        return await _app.LoadCaseInfoTuBanList(req);
    }
    [HttpGet]
    public async Task<Response<Dictionary<string, object>>> LoadCaseInfoTuBanListApp([FromQuery] CaseInfoTuBanReq req)
    {
        return await _app.LoadCaseInfoTuBanListApp(req);
    }
    /// <summary>
    /// 导出案件shp文件
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpPost]
    public IActionResult ExportCaseInfoShapefile([FromQuery] CaseInfoTuBanReq req)
    {
        try
        {
            string shpFilePath = Path.Combine(Path.GetTempPath(), $"图斑信息{DateTime.Now:yyyyMMddHHmmss}.shp");
            string shpFilePathzip = Path.Combine(Path.GetTempPath(), $"图斑信息{DateTime.Now:yyyyMMddHHmmss}.zip");
            _app.ExportCaseInfoShapefile(req, shpFilePath, shpFilePathzip);
            byte[] fileBytes = System.IO.File.ReadAllBytes(shpFilePathzip);
            return File(fileBytes, "application/octet-stream", $"图斑信息{DateTime.Now:yyyyMMddHHmmss}.zip");
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal server error: {ex.Message}");
        }

    }
    [HttpGet]
    public async Task<Response<PageInfo<List<dynamic>>>> LoadCaseInfoIllegalList([FromQuery] CaseInfoTuBanReq req)
    {
        return await _app.LoadCaseInfoIllegalList(req);
    }
    [HttpPost]
    public IActionResult ExportCaseInfoTuBanList([FromQuery] CaseInfoTuBanReq req)
    {
        var res = new Response();
        //获取列表数
        Response<List<CaseInfoTubanExport>> response = _app.ExprotLoadCaseInfoTuBanList(req);
        //获取模板数据
        List<ModuleColumn> headers = new List<ModuleColumn>();
        ModuleColumn m01 = new ModuleColumn();
        m01.key = "xuhao";
        m01.value = "序号";
        headers.Add(m01);
        ModuleColumn m1 = new ModuleColumn();
        m1.key = "tubanlaiyuan";
        m1.value = "图斑来源";
        headers.Add(m1);
        ModuleColumn m411 = new ModuleColumn();
        m411.key = "picihao";
        m411.value = "批次";
        headers.Add(m411);
        ModuleColumn m2 = new ModuleColumn();
        m2.key = "countyname";
        m2.value = "县区名称";
        headers.Add(m2);
        ModuleColumn m3 = new ModuleColumn();
        m3.key = "streetname";
        m3.value = "乡镇名称";
        headers.Add(m3);
        ModuleColumn m31 = new ModuleColumn();
        m31.key = "communityname";
        m31.value = "社区/村名称";
        headers.Add(m31);
        ModuleColumn m4 = new ModuleColumn();
        m4.key = "caseNo";
        m4.value = "图斑编号";
        headers.Add(m4);
        ModuleColumn m41 = new ModuleColumn();
        m41.key = "typename";
        m41.value = "图斑类型";
        headers.Add(m41);
        ModuleColumn m5 = new ModuleColumn();
        m5.key = "identificationTime";
        m5.value = "下发时间";
        headers.Add(m5);
        ModuleColumn m6 = new ModuleColumn();
        m6.key = "area";
        m6.value = "图斑面积";
        headers.Add(m6);
        ModuleColumn m711 = new ModuleColumn();
        m711.key = "nongyongdiarea";
        m711.value = "农用地面积";
        headers.Add(m711);
        ModuleColumn m7 = new ModuleColumn();
        m7.key = "gengdiArea";
        m7.value = "耕地面积";
        headers.Add(m7);
        ModuleColumn m71 = new ModuleColumn();
        m71.key = "yongjiujibennongtianarea";
        m71.value = "永农面积";
        headers.Add(m71);
        ModuleColumn m72 = new ModuleColumn();
        m72.key = "shengtaibaohuhongxianarea";
        m72.value = "生态保护红线面积";
        headers.Add(m72);
        ModuleColumn m8 = new ModuleColumn();
        m8.key = "isillegal";
        m8.value = "判定结果";
        headers.Add(m8);
        ModuleColumn m9 = new ModuleColumn();
        m9.key = "handleStatusName";
        m9.value = "当前状态";
        headers.Add(m9);
        ModuleColumn m111 = new ModuleColumn();
        m111.key = "jieshou_people";
        m111.value = "接收人";
        headers.Add(m111);
        ModuleColumn m121 = new ModuleColumn();
        m121.key = "jieshou_time";
        m121.value = "接收时间";
        headers.Add(m121);
        ModuleColumn m11 = new ModuleColumn();
        m11.key = "xiangmumc";
        m11.value = "项目名称";
        headers.Add(m11);
        ModuleColumn m12 = new ModuleColumn();
        m12.key = "xiangmuzhuti";
        m12.value = "项目主体";
        headers.Add(m12);
        ModuleColumn m13 = new ModuleColumn();
        m13.key = "actualUseTo";
        m13.value = "实际用途";
        headers.Add(m13);
        ModuleColumn m14 = new ModuleColumn();
        m14.key = "qita_qksm";
        m14.value = "其他情况说明";
        headers.Add(m14);
        ModuleColumn m221 = new ModuleColumn();
        m221.key = "pandingyijushuoming";
        m221.value = "判定依据说明";
        headers.Add(m221);
        ModuleColumn m15 = new ModuleColumn();
        m15.key = "is_chuli";
        m15.value = "是否对人处理";
        headers.Add(m15);
        ModuleColumn m151 = new ModuleColumn();
        m151.key = "chuliqingkuangsm";
        m151.value = "处理情况说明";
        headers.Add(m151);
        ModuleColumn m10 = new ModuleColumn();
        m10.key = "measureName";
        m10.value = "整改措施";
        headers.Add(m10);
        ModuleColumn m131 = new ModuleColumn();
        m131.key = "examiner_name";
        m131.value = "填报人";
        headers.Add(m131);
        ModuleColumn m141 = new ModuleColumn();
        m141.key = "examine_time";
        m141.value = "填报时间";
        headers.Add(m141);
        ModuleColumn m152 = new ModuleColumn();
        m152.key = "remark";
        m152.value = "备注";
        headers.Add(m152);
        ModuleColumn m23 = new ModuleColumn();
        m23.key = "xjshenhejieguo";
        m23.value = "县级审核结果";
        headers.Add(m23);
        ModuleColumn m24 = new ModuleColumn();
        m24.key = "xianjiyijian";
        m24.value = "县级审核意见";
        headers.Add(m24);
        ModuleColumn m25 = new ModuleColumn();
        m25.key = "verifyuser";
        m25.value = "县级审核人";
        headers.Add(m25);
        ModuleColumn m26 = new ModuleColumn();
        m26.key = "verifytime";
        m26.value = "县级审核时间";
        headers.Add(m26);
        ModuleColumn m27 = new ModuleColumn();
        m27.key = "sjshenhejieguo";
        m27.value = "市级审核结果";
        headers.Add(m27);
        ModuleColumn m28 = new ModuleColumn();
        m28.key = "shijiyijian";
        m28.value = "市级审核意见";
        headers.Add(m28);
        ModuleColumn m29 = new ModuleColumn();
        m29.key = "hexiaoren";
        m29.value = "市级审核人";
        headers.Add(m29);
        ModuleColumn m30 = new ModuleColumn();
        m30.key = "hexiaotime";
        m30.value = "市级审核时间";
        headers.Add(m30);
        ModuleColumn m301 = new ModuleColumn();
        m301.key = "case_description";
        m301.value = "图斑描述";
        headers.Add(m301);
        var excelRes = _app.ListToExcelTuban(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);
    }

    [HttpPost]
    public IActionResult ExportCaseInfoIllegalList([FromQuery] CaseInfoTuBanReq req)
    {
        var res = new Response();
        //获取列表数
        Response<List<CaseInfoTubanExport>> response = _app.ExprotLoadCaseInfoIllegalList(req);
        //获取模板数据
        List<ModuleColumn> headers = new List<ModuleColumn>();

        ModuleColumn m2 = new ModuleColumn();
        m2.key = "countyname";
        m2.value = "区县";
        headers.Add(m2);
        ModuleColumn m3 = new ModuleColumn();
        m3.key = "streetname";
        m3.value = "乡镇";
        headers.Add(m3);
        ModuleColumn m4 = new ModuleColumn();
        m4.key = "caseNo";
        m4.value = "图斑编号";
        headers.Add(m4);
        ModuleColumn m5 = new ModuleColumn();
        m5.key = "identificationTime";
        m5.value = "下发时间";
        headers.Add(m5);
        ModuleColumn m6 = new ModuleColumn();
        m6.key = "area";
        m6.value = "图斑面积";
        headers.Add(m6);
        ModuleColumn m7 = new ModuleColumn();
        m7.key = "gengdiArea";
        m7.value = "耕地面积";
        headers.Add(m7);
        ModuleColumn m8 = new ModuleColumn();
        m8.key = "isillegal";
        m8.value = "判定结果";
        headers.Add(m8);
        ModuleColumn m9 = new ModuleColumn();
        m9.key = "handleStatusName";
        m9.value = "当前状态";
        headers.Add(m9);

        ModuleColumn m11 = new ModuleColumn();
        m11.key = "xiangmumc";
        m11.value = "项目名称";
        headers.Add(m11);
        ModuleColumn m12 = new ModuleColumn();
        m12.key = "xiangmuzhuti";
        m12.value = "项目主体";
        headers.Add(m12);
        ModuleColumn m14 = new ModuleColumn();
        m14.key = "weifaleixing";
        m14.value = "违法类型";
        headers.Add(m14);
        ModuleColumn m13 = new ModuleColumn();
        m13.key = "measureName";
        m13.value = "整改措施";
        headers.Add(m13);
        ModuleColumn m15 = new ModuleColumn();
        m15.key = "resultName";
        m15.value = "处理方式";
        headers.Add(m15);
        ModuleColumn m16 = new ModuleColumn();
        m16.key = "registrNumber";
        m16.value = "立案号";
        headers.Add(m16);
        ModuleColumn m17 = new ModuleColumn();
        m17.key = "illegalContact";
        m17.value = "违法联系人";
        headers.Add(m17);
        ModuleColumn m18 = new ModuleColumn();
        m18.key = "illegalShenfenzhenghao";
        m18.value = "违法人身份证号";
        headers.Add(m18);

        var excelRes = _app.ListToExcelIllegal(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);
    }

    [HttpPost]
    public Response<string> dealIllegalCaseInfo([FromBody] DealIllegalCaseInfo model)
    {
        Response<string> response = new Response<string>();
        try
        {
            response.Result = _app.dealIllegalCaseInfo(model);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }

    [HttpPost]
    [AllowAnonymous]
    public Response<string> getShpDataInfo(QueryShpDataInfo shpDataInfo)
    {
        _app.getShpDataInfo(shpDataInfo);
        return null;
    }

    #region 统计
    /// <summary>
    /// 综合统计
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [NonAction]
    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]
    [NonAction]
    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]
    [NonAction]
    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]
    [NonAction]
    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

    #region 无人机发现违法行为情况统计明细表
    /// <summary>
    /// 无人机发现违法行为情况统计明细表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [NonAction]
    public Response<List<OffenceResp>> CaseOffence(DateTime startTime, DateTime endTime)
    {
        Response<List<OffenceResp>> response = new Response<List<OffenceResp>>();
        try
        {
            response = _app.CaseOffence(startTime, endTime);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }
    /// <summary>
    /// 获取穿透案件信息 // 1 - 下发图斑，2 - 合法，3 - 违法小计，4 - 其他，5 - 非粮化，6 - 补办手续，7 - 拆除 ,8 - 核实后， 9 - 整改后，10 - 非农化
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpGet]
    [NonAction]
    public async Task<Response<PageInfo<List<DroneCaseInfoZdwt2>>>> LoadCaseInfoListOffence([FromQuery] QueryCaseOffencetReq req)
    {
        return await _app.LoadCaseInfoListOffence(req);
    }
    /// <summary>
    /// 导出无人机发现违法行为情况统计明细表
    /// </summary>
    /// <param name="startTime"></param>
    /// <param name="endTime"></param>
    /// <returns></returns>
    [HttpPost]
    [NonAction]
    public IActionResult CaseOffenceToExcel(DateTime startTime, DateTime endTime)
    {
        var res = new Response();
        var data = _app.CaseOffence(startTime, endTime).Result;

        var excelRes = _app.CaseOffenceToExcel(data);
        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 = excelRes.Message;
        }
        return Ok(res);
    }
    /// <summary>
    /// 导出图斑明细
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpPost]
    [NonAction]
    public IActionResult ExprotCaseListByType([FromQuery] QueryCaseOffencetReq req)
    {
        var res = new Response();

        // 获取列表数据
        Response<List<DroneCaseByTypeResp>> response = _app.ExprotCaseListByType(req);
        if (response.Code != 200)
        {
            res.Code = response.Code;
            res.Message = "获取数据失败";
            return Ok(res);
        }
        // 获取模板数据
        List<ModuleColumn> headers = GetHeaders();
        // 导出Excel
        var excelRes = _app.ListToExcelByCaseType(response.Result, headers);
        if (excelRes.Code == 200)
        {
            return File(excelRes.Result.ToArray(),
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                $"图斑信息{DateTime.Now:yyyyMMddHHmmss}.xls");
        }
        else
        {
            res.Code = excelRes.Code;
            res.Message = "导出失败";
        }

        return Ok(res);
    }

    private List<ModuleColumn> GetHeaders()
    {
        var headers = new List<ModuleColumn>
        {
            new() { key = "xuhao", value = "序号" },
            new() { key = "tubanlaiyuan", value = "图斑来源" },
            new() { key = "picihao", value = "批次号" },
            new() { key = "caseNo", value = "图斑编号" },
            new() { key = "countyname", value = "县区名称" },
            new() { key = "streetname", value = "乡镇名称" },
            new() { key = "communityname", value = "村名称" },
            new() { key = "case_description", value = "图斑描述" },
            new() { key = "typename", value = "图斑类型" },
            new() { key = "synchronoustime", value = "下发时间" },
            new() { key = "area", value = "图斑面积" },
            new() { key = "nongyongdiArea", value = "农用地面积" },
            new() { key = "gengdiArea", value = "耕地面积" },
            new() { key = "yongnongArea", value = "永农面积" },
            new() { key = "isillegal", value = "判定结果" },
            new() { key = "handleStatusName", value = "当前状态" },
            new() { key = "jieshou_people", value = "接收人" },
            new() { key = "jieshou_time", value = "接收时间" },
            new() { key = "xiangmumc", value = "项目名称" },
            new() { key = "xiangmuzhuti", value = "项目主体" },
            new() { key = "qtsjyt", value = "实际用途" },
            new() { key = "weifaleixing", value = "违法类型" },
            new() { key = "pandingyijushuoming", value = "判定依据说明" },
            new() { key = "examiner_name", value = "填报人" },
            new() { key = "examine_time", value = "填报时间" },
            new() { key = "measureName", value = "整改措施" },
            new() { key = "transactor_name", value = "办理人" },
            new() { key = "transact_time", value = "办理时间" },
            new() { key = "remark", value = "备注" },
            new() { key = "xjshenhejieguo", value = "县级审核结果" },
            new() { key = "xianjiyijian", value = "县级审核意见" },
            new() { key = "verifyuser", value = "县级审核人" },
            new() { key = "verifytime", value = "县级审核时间" },
            new() { key = "sjshenhejieguo", value = "市级审核结果" },
            new() { key = "shijiyijian", value = "市级审核意见" },
            new() { key = "hexiaoren", value = "市级审核人" },
            new() { key = "hexiaotime", value = "市级审核时间" },

        };
        return headers;
    }

    /// <summary>
    /// 导出shp文件
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpPost]
    [NonAction]
    public IActionResult ExportShapefile([FromQuery] QueryCaseOffencetReq req)
    {
        try
        {
            // 获取列表数据
            Response<List<DroneCaseByTypeResp>> response = _app.ExprotCaseListByType(req);
            string shpFilePath = Path.Combine(Path.GetTempPath(), $"图斑信息{DateTime.Now:yyyyMMddHHmmss}.shp");
            string shpFilePathzip = Path.Combine(Path.GetTempPath(), $"图斑信息{DateTime.Now:yyyyMMddHHmmss}.zip");
            _app.ExportToShapefile(req, shpFilePath, shpFilePathzip);
            byte[] fileBytes = System.IO.File.ReadAllBytes(shpFilePathzip);
            return File(fileBytes, "application/octet-stream", $"图斑信息{DateTime.Now:yyyyMMddHHmmss}.zip");
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal server error: {ex.Message}");
        }
    }
    /// <summary>
    /// 导出土地卫片新增违法占用等地情况统计表
    /// </summary>
    /// <param name="startTime"></param>
    /// <param name="endTime"></param>
    /// <returns></returns>
    [HttpPost]
    [NonAction]
    public IActionResult CaseOffenceSimpleToExcel(DateTime startTime, DateTime endTime)
    {
        var res = new Response();
        var excelRes = _app.CaseOffenceSimpleToExcel(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 = excelRes.Message;
        }
        return Ok(res);
    }
    /// <summary>
    /// 导出无人机全域巡查违法用地情况统计表
    /// </summary>
    /// <param name="startTime"></param>
    /// <param name="endTime"></param>
    /// <returns></returns>
    [HttpPost]
    [NonAction]
    public IActionResult CaseOffenceSimpleToExcel2(DateTime startTime, DateTime endTime)
    {
        var res = new Response();
        var excelRes = _app.CaseOffenceSimpleToExcel2(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 = excelRes.Message;
        }
        return Ok(res);
    }

    /// <summary>
    /// 案件分宗/图斑分割/图斑拆分/案件分割
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<string>> CaseSplit([FromBody] CaseSplitReq req)
    {

        Response<string> response = new Response<string>();
        try
        {
            return await _app.CaseSplit(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>> CaseRecover([FromBody] CaseRecoverReq req)
    {

        Response<string> response = new Response<string>();
        try
        {
            return await _app.CaseRecover(req);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }

    #endregion

    #region 添加案件图片
    [HttpPost]
    [AllowAnonymous]
    public Response<bool> AddCaseImg(List<DroneImageRef> info)
    {

        Response<bool> response = new Response<bool>();
        try
        {
            return _app.AddCaseImg(info);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }
    /// <summary>
    /// 获取图片信息
    /// </summary>
    /// <param name="caseid"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<List<DroneImageRef>>> LoadCaseImgList(string caseid)
    {
        return await _app.LoadCaseImgList(caseid);
    }
    [HttpGet]
    [AllowAnonymous]
    public Response<bool> DeleteCaseImg(string path, string fieldName, string caseid, string dataTable)
    {

        Response<bool> response = new Response<bool>();
        try
        {
            return _app.DeleteCaseImg(path, fieldName, caseid, dataTable);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }
    #endregion

    #region 获取图斑
    [HttpGet]
    [AllowAnonymous]
    public async Task<Response<List<DroneShpData>>> LoadDroneShpDataById(string caseid)
    {
        return await _app.LoadDroneShpDataById(caseid);
    }
    /// <summary>
    /// 获取图斑中心点
    /// </summary>
    /// <param name="tablename">表名</param>
    /// <param name="filter">筛选条件</param>
    /// <returns></returns>
    [HttpGet]
    public Response<dynamic> GetCenterPoints(string tablename, string filter)
    {
        Response<dynamic> response = new Response<dynamic>();
        try
        {
            response.Result = _app.GetCenterPoints(tablename, filter);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }

    /// <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");
    }
    #endregion

    /// <summary>
    /// 案件信息列表
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<PageInfo<List<dynamic>>>> LoadCaseInfoCheckTuBanList([FromQuery] CaseInfoTypeName req)
    {
        return await _app.LoadCaseInfoCheckTuBanList(req);
    }

    #region 案件关闭--重点问题2
    /// <summary>
    /// 案件关闭--重点问题2
    /// </summary>
    /// <param name="id">案件id</param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> CloseCaseInfoZdwt2(CloseCaseInfoReq req)
    {
        Response<bool> response = new Response<bool>();
        try
        {
            response = await _app.CloseCaseInfoZdwt2(req.id, req.closereason);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }
    #endregion
}