using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Spreadsheet;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.App.ServiceApp.MiManager.Request;
using OpenAuth.App.ServiceApp.MiManager.Response;
using OpenAuth.App.ServiceApp.MiManager.Resquest;
using OpenAuth.App.ServiceApp.Request;
using OpenAuth.App.ServiceApp.Response;
using OpenAuth.Repository.Domain;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// 违法上报
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MiViolationReportController : ControllerBase
    {
        private readonly MiViolationReportApp _app;
        public MiViolationReportController(MiViolationReportApp app)
        {
            _app = app;
        }

        #region 查询

        #region 分页
        /// <summary>
        /// 分页
        /// </summary>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadAllPage([FromQuery] MiviolationReq request)
        {
            return await _app.LoadAllPage(request);
        }

        #endregion

        #region 实体
        /// <summary>
        /// 上报详情
        /// </summary>
        [HttpGet]
        public async Task<Response<dynamic>> Get(string id)
        {
            var result = new Response<dynamic>();
            try
            {
                result.Result = await _app.Get(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion
        #endregion

        #region 增删改

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        [HttpPost]
        public async Task<Response<bool>> Add(MiViolationReport model)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.Add(model);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        [HttpPost]
        public async Task<Response<bool>> Update(MiViolationReportRequest model)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.Update(model);

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

            return result;
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public async Task<Response<bool>> Delete([FromBody] List<string> models)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.Delete(models);

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

            return result;
        }
        #endregion

        #region 上报
        /// <summary>
        /// 上报
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> Report(MiViolationReportRequest request)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.Report(request);

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

            return result;
        }
        #endregion

        #region
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> ReportHandle(HandleInfo info)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.ReportHandle(info);

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

            return result;
        }
        #endregion

        #region  查询扣押单信息
        /// <summary>
        /// 根据id查询扣押单信息
        /// </summary>
        /// <param name="reportid">违法上报id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<dynamic>> GetSeizureDocument(string reportid)
        {
            var result = new Response<dynamic>();
            try
            {
                result.Result = await _app.GetSeizureDocument(reportid);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 扣车审核
        /// </summary>
        [HttpPost]
        public async Task<Response<bool>> ReportAudit(ReportAuditReq model)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.ReportAudit(model);

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

            return result;
        }
        #endregion

        #region 统计
        /// <summary>
        /// 巡查台账--人员
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<PunchStatistics>>> GetPunchStatistics(DateTime? begindate, DateTime? enddate)
        {
            var result = new Response<List<PunchStatistics>>();
            try
            {
                result.Result = await _app.GetPunchStatistics(begindate, enddate);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 巡查台账--点位
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<List<PunchPointStatistics>>> GetPointPunchStatistics(DateTime? begindate, DateTime? enddate)
        {
            var result = new Response<List<PunchPointStatistics>>();
            try
            {
                result.Result = await _app.GetPointPunchStatistics(begindate, enddate);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        ///  违法处理台账
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<ViolateReportStatistics>>>> GetReportStatistics(int page=1,int limit=10)
        {
            var result = new Response<PageInfo<List<ViolateReportStatistics>>>();
            try
            {
                result.Result = await _app.GetReportStatistics(page,limit);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 巡查台账清单
        /// </summary>
        [HttpGet]
        public async Task<Response<PageInfo<List<dynamic>>>> LoadPunchRecordPage(int page = 1, int limit = 10)
        {
            var result = new Response<PageInfo<List<dynamic>>>();
            try
            {
                result = await _app.LoadPunchRecordPage(page,limit);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 巡查台账-人员-巡查次数数据获取
        /// </summary>
        /// <param name="userid">人员id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<dynamic>> GetPunchDetailData(long userid, DateTime? begindate, DateTime? enddate)
        {
            var result = new Response<dynamic>();
            try
            {
                result.Result = await _app.GetPunchDetailData(userid, begindate, enddate);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 巡查台账-人员-巡查异常次数
        /// </summary>
        /// <param name="userid">人员id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<dynamic>> GetReportDetailData(long userid, DateTime? begindate, DateTime? enddate)
        {
            var result = new Response<dynamic>();
            try
            {
                result.Result = await _app.GetReportDetailData(userid, begindate, enddate);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 巡查台账-人员-违法上报次数
        /// </summary>
        /// <param name="userid">人员id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<dynamic>> GetDealDetailData(long userid, DateTime? begindate, DateTime? enddate)
        {
            var result = new Response<dynamic>();
            try
            {
                result.Result = await _app.GetDealDetailData(userid, begindate, enddate);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 巡查台账-点位-巡查次数数据获取
        /// </summary>
        /// <param name="pointid">巡查点id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<dynamic>> GetPointPunchDetailData(string pointid, DateTime? begindate, DateTime? enddate)
        {
            var result = new Response<dynamic>();
            try
            {
                result.Result = await _app.GetPointPunchDetailData(pointid, begindate, enddate);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 巡查台账-点位-巡查异常次数
        /// </summary>
        /// <param name="pointid">巡查点id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<dynamic>> GetPointReportDetailData(string pointid, DateTime? begindate, DateTime? enddate)
        {
            var result = new Response<dynamic>();
            try
            {
                result.Result = await _app.GetPointReportDetailData(pointid, begindate, enddate);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 巡查台账-点位-违法上报次数
        /// </summary>
        /// <param name="pointid">巡查点id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<dynamic>> GetPointDealDetailData(string pointid, DateTime? begindate, DateTime? enddate)
        {
            var result = new Response<dynamic>();
            try
            {
                result.Result = await _app.GetPointDealDetailData(pointid, begindate, enddate);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
        #endregion
        #region 导出
        /// <summary>
        /// 巡查台账--人员导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportPunchStatistics(DateTime? begindate, DateTime? enddate)
        {
            var res = new Response();
            var excelRes = await _app.ExportPunchStatistics(begindate, enddate);
            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>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportPointPunchStatistics(DateTime? begindate, DateTime? enddate)
        {
            var res = new Response();
            var excelRes = await _app.ExportPointPunchStatistics(begindate, enddate);
            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>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportPunchRecordDetails()
        {
            var res = new Response();
            var excelRes = await _app.ExportPunchRecordDetails();
            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>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportViolationReportDetails()
        {
            var res = new Response();
            var excelRes = await _app.ExportViolationReportDetails();
            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);
        }
        #endregion
        #endregion
    }
}