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

namespace SafeCampus.Web.Core.Controllers.Application.Business;
/// <summary>
/// 考勤事件管理接口
/// </summary>
[Route("/business/[controller]")]
[ApiDescriptionSettings(ApiGroupConsts.SYSTEM_Business, Order = 94,Tag = "考勤事件管理")]
public class AttendanceApi : IDynamicApiController
{
    private readonly IAttendanceService _attendanceService;
    private readonly IDeepelephManager _deepelephManager;

    public AttendanceApi(IDeepelephManager deepelephManager, IAttendanceService attendanceService)
    {
        _deepelephManager = deepelephManager;
        _attendanceService = attendanceService;
    }

    /// <summary>
    /// 考勤查询
    /// </summary>
    /// <param name="search">查询参数</param>
    /// <returns></returns>
    [Obsolete]
    public async Task<dynamic> GetPassengerFlow(ClassRoomCallTaskSearchRequest search)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/attendance/data/query"
            .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.cameraId,
                pageIndex = search.PageNum,
                pageSize = search.PageSize,
            })
            .SetContentType("application/json")
            .PostAsAsync<string>();
        var model = JsonConvert.DeserializeObject<JObject>(str);
        if (!(bool)model["success"]) throw Oops.Oh(model["message"].ToString());
        if (model["data"] != null)
        {
            return new
            {
                pageNum = search.PageNum,
                pageSize = model["data"]["size"],
                total = model["data"]["total"],
                pages = model["data"]["pages"],
                list = model["data"]["items"]
            };
        }
        return new
        {
            pageNum = search.PageNum,
            pageSize = 0,
            total = 0,
            pages = 0,
            list = new List<string>()
        };
    }

    /// <summary>
    /// 添加考勤记录
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<bool> Add(List<AttendanceAddInput> input)
    {
        var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
        foreach (var attendanceAddInput in input)
        {
            var vm = new AttendanceDto
            {
                CreateTime = DateTime.Now,
                TenantCode = appSettings.TenantCode,
                FaceScore = 100,
                IsAuto = false,
                PersonSetId = attendanceAddInput.PersonSetId,
                PersonId = attendanceAddInput.PersonId,
                PoiId = appSettings.PoiId,
                Tick = attendanceAddInput.Tick,
                Similarity = 100,
                EventId = "",
                CameraId = "",
                Extend = "",
                TrackId = "",
                SnapshotUrl = "",
                SnapshotData = "",
                Rects = ""
            };
            await _attendanceService.Add(vm);
        }

        return true;
    }

    /// <summary>
    /// 分页获取考勤列表
    /// </summary>
    /// <param name="search"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<AttendanceList>> GetPageList(AttendanceSearch search)
    {
        return await _attendanceService.GetPageList(search);
    }

    /// <summary>
    /// 不分页获取考勤列表
    /// </summary>
    /// <param name="search"></param>
    /// <returns></returns>
    public async Task<List<AttendanceList>> GetNoPageList(AttendanceSearch search)
    {
        return await _attendanceService.GetNoPageList(search);
    }
    /// <summary>
    /// 根据宿舍楼id获取出入记录
    /// </summary>
    /// <param name="search"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<AttendanceList>> GetPageListByBuild(BuildAttendanceSearch search)
    {
        return await _attendanceService.GetPageListByBuild(search);
    }
}