using Infrastructure;
using Microsoft.AspNetCore.Mvc;
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.Response;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.ServiceControllers;

/// <summary>
/// 矿产专题（卫片下发）
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class DroneCaseInfoSatelliteController : ControllerBase
{
    readonly DroneCaseInfoSatelliteApp _app;

    public DroneCaseInfoSatelliteController(DroneCaseInfoSatelliteApp app)
    {
        _app = app;
    }

    /// <summary>
    /// 案件详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<DroneCaseInfoSatellite>> GetCaseInfoById(string id)
    {
        var response = new Response<DroneCaseInfoSatellite>();
        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="year">年份</param>
    /// <returns></returns>
    [HttpGet]
    public async Task<Response<List<dynamic>>> LoadReportCaseCount(int year, string tubanlaiyuan, string picihao)
    {
        var illegal = 1;
        return await _app.LoadReportCaseCount(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>>> LoadReformCaseCount(int year, string tubanlaiyuan, string picihao)
    {
        var illegal = 0;
        return await _app.LoadReformCaseCount(year, tubanlaiyuan, picihao, illegal);
    }

    /// <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>>>> LoadCaiKuangTaskList([FromQuery] TaskListReq req)
    {
        return await _app.LoadCaiKuangTaskList(req);
    }


    /// <summary>
    /// 收藏
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> AddCaiKuangTaskFavorite([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>> DeleteCaiKuangTaskCase([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>>>> LoadCaseInfoWpxfTuBanList([FromQuery] CaseInfoTuBanReq req)
    {
        return await _app.LoadCaseInfoTuBanList(req);
    }

    /// <summary>
    /// 导出案件shp文件
    /// </summary>
    /// <param name="req"></param>
    /// <returns></returns>
    [HttpPost]
    public IActionResult ExportCaseInfoWpxfShapefile([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}");
        }
    }

    [HttpPost]
    public IActionResult ExportCaseInfoWpxfTuBanList([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 m7 = new ModuleColumn();
        m7.key = "gengdiArea";
        m7.value = "耕地面积";
        headers.Add(m7);
        ModuleColumn m71 = new ModuleColumn();
        m71.key = "yongjiujibennongtianarea";
        m71.value = "永农面积";
        headers.Add(m71);
        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 = "weifaleixing";
        m14.value = "违法类型";
        headers.Add(m14);
        ModuleColumn m221 = new ModuleColumn();
        m221.key = "pandingyijushuoming";
        m221.value = "判定依据说明";
        headers.Add(m221);
        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 m10 = new ModuleColumn();
        m10.key = "measureName";
        m10.value = "整改措施";
        headers.Add(m10);
        ModuleColumn m15 = new ModuleColumn();
        m15.key = "transactor_name";
        m15.value = "办理人";
        headers.Add(m15);
        ModuleColumn m151 = new ModuleColumn();
        m151.key = "transact_time";
        m151.value = "办理时间";
        headers.Add(m151);
        ModuleColumn m1511 = new ModuleColumn();
        m1511.key = "remark";
        m1511.value = "备注";
        headers.Add(m1511);
        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);
    }
    [HttpGet]
    public async Task<Response<Dictionary<string, object>>> LoadMyUncompletedCaiKuangTask([FromQuery] TaskUncompletReq req)
    {
        return await _app.LoadMyUncompletedTask(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;
    }
    /// <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;
    }

    #region 案件关闭--矿产卫片下发
    /// <summary>
    /// 案件关闭--矿产卫片下发
    /// </summary>
    /// <param name="id">案件id</param>
    /// <returns></returns>
    [HttpPost]
    public async Task<Response<bool>> CloseCaseInfoSatellite(CloseCaseInfoReq req)
    {
        Response<bool> response = new Response<bool>();
        try
        {
            response = await _app.CloseCaseInfoSatellite(req.id, req.closereason);
        }
        catch (Exception ex)
        {
            response.Code = 500;
            response.Message = ex.InnerException?.Message ?? ex.Message;
        }
        return response;
    }
    #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);
    }

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