using MoYu.FriendlyException;
using MoYu.RemoteRequest.Extensions;
using Newtonsoft.Json.Linq;
using SafeCampus.Application.Manager.DeepelephManager;
using SafeCampus.Application.Services.Business.PassengerFlowService;
using SafeCampus.Application.Services.Business.PassengerFlowService.Dto;

namespace SafeCampus.Web.Core.Controllers.Application.Business;
/// <summary>
/// 客流查询接口
/// </summary>
[Route("/business/[controller]")]
[ApiDescriptionSettings(ApiGroupConsts.SYSTEM_Business, Order = 96,Tag = " 客流查询")]
public class PassengerFlowApi : IDynamicApiController
{
    private readonly IDeepelephManager _deepelephManager;
    private readonly IPassengerFlowService _passengerFlowService;
    public PassengerFlowApi(IDeepelephManager deepelephManager, IPassengerFlowService passengerFlowService)
    {
        _deepelephManager = deepelephManager;
        _passengerFlowService = passengerFlowService;
    }

    /// <summary>
    /// 添加客流查询任务
    /// </summary>
    /// <param name="input">添加参数</param>
    /// <returns></returns>
    public async Task<bool> Add(PassengerFlowDto input)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/passenger-flow/enter/search"
            .SetBody(new
            {
                token = _deepelephManager.GetToken(),
                tenantCode = appSettings.TenantCode,
                poiId = appSettings.PoiId,
                startTime = new DateTimeOffset(input.StartTime).ToUnixTimeMilliseconds(),
                endTime = new DateTimeOffset(input.EndTime).ToUnixTimeMilliseconds(),
                cameraId=input.CameraIds,
                split = input.Split ? "HOUR" : "DAY",
            })
            .SetContentType("application/json")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if ((bool)model["success"])
        {
            input.Total = (int)model["data"]["total"];
            input.ExtJson = model["data"]["data"].ToString();
            await _passengerFlowService.Add(input);
            return true;
        }

        throw Oops.Oh(model["message"].ToString());
    }

    /// <summary>
    /// 分页查询列表
    /// </summary>
    /// <param name="search"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<PassengerFlowList>> GetPageList(PassengerFlowSearch search)
    {
        return await _passengerFlowService.GetPageList(search);
    }
    /// <summary>
    /// 客流查询
    /// </summary>
    /// <param name="search">查询参数</param>
    /// <returns></returns>
    [Obsolete]
    public async Task<dynamic> PostPassengerFlow(PassengerFlowDto search)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/passenger-flow/enter/search"
            .SetBody(new
            {
                token = _deepelephManager.GetToken(),
                tenantCode = appSettings.TenantCode,
                poiId = appSettings.PoiId,
                startTime = new DateTimeOffset(search.StartTime).ToUnixTimeMilliseconds(),
                endTime = new DateTimeOffset(search.EndTime).ToUnixTimeMilliseconds(),
                cameraId=search.CameraIds,
                split = search.Split ? "HOUR" : "DAY",
            })
            .SetContentType("application/json")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if ((bool)model["success"])
        {
            return model["data"];
        }

        throw Oops.Oh(model["message"].ToString());
    }
}