using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;
using Org.BouncyCastle.Ocsp;
using SqlSugar;

namespace OpenAuth.WebApi.Controllers
{
    public partial class DroneCaseinfoController1
    {
        #region 案件处理

        /// <summary>
        /// 案件核查
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> AddOrUpdateExamineDeal(AddOrUpdateDroneCaseDealReq req)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                req.CheckReqIsNull();
                if (string.IsNullOrEmpty(req.info.caseid))
                {
                    throw new Exception("图斑主键不能为空");
                }
                if (string.IsNullOrEmpty(req.info.Id))
                {
                    //新增
                    response = _app.AddExamineDeal(req);
                }
                else
                {
                    //修改
                    response = _app.UpdateExamineDeal(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<bool> AddDroneCaseDeal([FromBody] AddOrUpdateDroneCaseDealReq req)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                req.CheckReqIsNull();
                if (string.IsNullOrEmpty(req.info.caseid))
                {
                    throw new Exception("图斑主键不能为空");
                }
                if (string.IsNullOrEmpty(req.info.Id))
                {
                    //新增
                    response = _app.AddDroneCaseDeal(req);
                }
                else
                {
                    //修改
                    response = _app.UpdateDroneCaseDeal(req);
                }
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 处理查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<PageInfo<List<DroneCaseDeal>>> LoadDroneCaseDeal([FromQuery] QueryDroneCaseDealReq req)
        {
            var response = new Response<PageInfo<List<DroneCaseDeal>>>();
            try
            {
                response.Result = _app.LoadDroneCaseDeal(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 Response<AddOrUpdateDroneCaseDealReq> GetDroneCaseDeal(string id, string caseid)
        {
            Response<AddOrUpdateDroneCaseDealReq> response = new Response<AddOrUpdateDroneCaseDealReq>();
            try
            {
                response.Result = _app.GetDroneCaseDeal(id, caseid);
            }
            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 Response<bool> DeleteDroneCaseDeal(string[] ids)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _app.DeleteDroneCaseDeal(ids);
            }
            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]
        public Response<PageInfo<List<DroneFlowInstance>>> LoadDroneFlowInstance([FromQuery] QueryDroneFlowInstanceReq req)
        {
            var response = new Response<PageInfo<List<DroneFlowInstance>>>();
            try
            {
                response.Result = _app.LoadDroneFlowInstance(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 Response<DroneFlowInstance> GetDroneFlowInstance(string id)
        {
            Response<DroneFlowInstance> response = new Response<DroneFlowInstance>();
            try
            {
                response.Result = _app.GetDroneFlowInstance(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 新增或修改无人机流程
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<bool> AddOrUpdateDroneFlowInstance([FromBody] DroneFlowInstance model)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                if (string.IsNullOrEmpty(model.Id))
                    response = _app.AddDroneFlowInstance(model);
                else
                    response = _app.UpdateDroneFlowInstance(model);

            }
            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 Response<bool> DeleteDroneFlowInstance(string[] ids)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _app.DeleteDroneFlowInstance(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 Response<bool> BlockUpDroneFlowInstance(string[] ids)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _app.BlockUpDroneFlowInstance(ids);
            }
            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<bool> DroneFlowInstanceBindRole([FromBody] AddOrUpdateDroneFlowBindRoleReq req)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _app.DroneFlowInstanceBindRole(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 Response<List<string>> LoadDroneFlowRoleBindData(string id)
        {
            Response<List<string>> response = new Response<List<string>>();
            try
            {
                response.Result = _app.LoadDroneFlowRoleBindData(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 查询待审核
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<PageInfo<List<DroneCaseInfo>>> LoadWaitToVerifyCaseList([FromQuery] QueryCaseInfoListReq req)
        {
            var response = new Response<PageInfo<List<DroneCaseInfo>>>();
            try
            {
                response.Result = _app.LoadWaitToVerifyCaseList(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 Response<bool> VerfiyCaseData(string id)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _app.VerfiyCaseData(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 Response<bool> RecallCase(string id)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _app.RecallCase(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 Response<bool> RecallCaseToUnCheck(string id, int? type)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                if (type == 1)
                {
                    //撤回到待核查
                    response = _app.RecallCaseToUnCheck(id);
                }
                else if (type == 2)
                {
                    //撤回到待办理
                    response = _app.RecallCaseToUnDeal(id);
                }
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 案件退回
        /// </summary>
        /// <param name="id"></param>
        /// <param name="drawbackReason"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<bool> DrawbackCase([FromQuery] DrawBackCaseReq req)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _app.DrawbackCase(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 Response<bool> SetCaseChecked(string id)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _app.SetCaseChecked(id);
            }
            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 Response<bool> VerificatCase(string[] ids)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _app.VerificatCase(ids);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 暂结案件
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<bool> TentativeCase(string id)
        {
            Response<bool> response = new Response<bool>();
            try
            {
                response = _app.TentativeCase(id);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        #endregion

        #region 用户

        /// <summary>
        /// 获取当前用户的信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<JToken> GetCurrentUser()
        {
            Response<JToken> response = new Response<JToken>();
            try
            {
                response.Result = _app.GetCurrentUser();
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        ///// <summary>
        ///// 乡镇管理员查询用户接口
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public Response<List<Sugar_Users>> GetUserListByStreetManager()
        //{
        //    Response<List<Sugar_Users>> response = new Response<List<Sugar_Users>>();
        //    try
        //    {
        //        response.Result = _app.GetUserListByStreetManager();
        //    }
        //    catch (Exception ex)
        //    {
        //        response.Code = 500;
        //        response.Message = ex.InnerException?.Message ?? ex.Message;
        //    }
        //    return response;

        //}

        #endregion

        #region 统计

        /// <summary>
        /// 获取核查定性、处置办理、监督审核及归档的相关统计数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<CaseProcessStatData> CaseProcessStatData([FromQuery] QueryCaseProcessStatDataReq req)
        {
            Response<CaseProcessStatData> response = new Response<CaseProcessStatData>();
            try
            {
                response.Result = _app.GetCaseProcessStatData(req);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        #endregion

        #region 案件专题

        /// <summary>
        /// 专题分页
        /// </summary>
        /// <param name="pageReq"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<TopicResponse>>>> LoadCaseTopic([FromQuery] PageReq pageReq)
        {
            var response = new Response<PageInfo<List<TopicResponse>>>();
            try
            {
                response.Result = await _app.LoadCaseTopic(pageReq);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 为专题分配案件类型
        /// </summary>
        /// <param name="alloDroneTopic"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddTopicCaseType(AlloDroneTopic alloDroneTopic)
        {
            var response = new Response<bool>();
            try
            {
                response = await _app.AddTopicCaseType(alloDroneTopic);

            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }


        /// <summary>
        /// 为专题分配角色
        /// </summary>
        /// <param name="alloDroneTopic"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> AddTopicRole(AlloDroneTopic alloDroneTopic)
        {
            var response = new Response<bool>();
            try
            {
                response = await _app.AddTopicRole(alloDroneTopic);
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }

        /// <summary>
        /// 某专题对应已分配资源
        /// </summary>
        /// <param name="topicId"></param>
        /// <param name="code">TopicCaseType/TopicRole</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<string>>> LoadTopicRelevance(string topicId, string code)
        {
            var response = new Response<List<string>>();
            try
            {
                response.Result = await _app.LoadTopicRelevance(topicId, code);
                response.Message = "获取分配资源成功";
            }
            catch (Exception ex)
            {
                response.Code = 500;
                response.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return response;
        }
        #endregion
    }
}
