using Infrastructure;
using Microsoft.AspNetCore.Authorization;
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 DroneCaseInfoSTXFController : ControllerBase
    {
        readonly DroneCaseInfoSTXFApp _app;
        public DroneCaseInfoSTXFController(DroneCaseInfoSTXFApp app)
        { 
            _app = app;
        }

        #region 接收办理
        /// <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);
        }

        /// <summary>
        /// 添加收藏
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [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;
        }

        /// <summary>
        /// 删除收藏
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [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;
        }
        #endregion

        #region 填报审核 
        /// <summary>
        /// 查询待办任务统计数据（审核监督第一层接口） 审核监督/填报审核（illegal 1）/整改审核
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="tubanlaiyuan">图斑来源</param>
        /// <param name="picihao">批次号</param>
        /// <param name="illegal"></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] TaskSTXFDetailReq req)
        {
            return await _app.LoadTaskDetailList(req);
        }
        #endregion

        #region 图斑调整
        /// <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;
        }

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

        #region 判读详情
        /// <summary>
        /// 案件详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<AddOrUpdateDroneCaseInfoReqExtCurrent>> GetCaseInfo(string id)
        {
            Response<AddOrUpdateDroneCaseInfoReqExtCurrent> 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;
        }
        #endregion

        #region 案件关闭
        /// <summary>
        /// 案件关闭--生态修复
        /// </summary>
        /// <param name="id">案件id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> CloseSTXFCaseInfo(CloseCaseInfoReq req)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = await _app.CloseSTXFCaseInfo(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>>>> 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>
        /// 代办任务
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>

        [HttpGet]
        public async Task<Response<Dictionary<string, object>>> LoadMyUncompletedTask([FromQuery] TaskUncompletReq req)
        {
            return await _app.LoadMyUncompletedTask(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}");
            }

        }

        [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 m9 = new ModuleColumn();
            m9.key = "handleStatusName";
            m9.value = "当前状态";
            headers.Add(m9);
            ModuleColumn m91 = new ModuleColumn();
            m91.key = "pandingyijushuoming";
            m91.value = "情况说明";
            headers.Add(m91);
            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);
        }
        /// <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="caseid"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<List<DroneImageRef>>> LoadCaseImgList(string caseid, string category)
        {
            return await _app.LoadCaseImgList(caseid, category);
        }
        [HttpGet]
        public async Task<Response<DroneCaseInfoSTXF>> GetCaseInfoById(string id)
        {
            var response = new Response<DroneCaseInfoSTXF>();
            try
            {
                return await _app.GetCaseInfoById(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return response;
        }
        [HttpGet]
        public Response<List<dynamic>> loadStreet()
        {
            return _app.loadStreet();
        }
    }
}
