using IP2Region.Net.XDB;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;

namespace SafeCampus.Application.Services.Business.AttendanceService;

public class AttendanceService:DbRepository<Attendance>, IAttendanceService
{
    public async Task<bool> Add(AttendanceDto input)
    {
        var buildingInfo = ChangeRepository<DbRepository<BuildingInfo>>();//切换仓储
        var build = await buildingInfo.GetListAsync();
        var model = input.Adapt<Attendance>();
        //if (string.IsNullOrEmpty(model.PersonId))
        //{
        //    if (await IsAnyAsync(x => x.TaskId == model.TaskId && x.PersonId == model.PersonId && x.PersonSetId == model.PersonSetId))
        //    {
        //        //该批次点名任务已经出现过这个人，不在重复添加
        //        return true;
        //    }
        //}
        //else
        //{
        //    if (await IsAnyAsync(x => x.TaskId == model.TaskId && x.TrackId == model.TrackId))
        //    {
        //        //没有匹配到人员，但是所属同一跟踪id
        //        //该批次点名任务已经出现过这个人，不在重复添加
        //        return true;
        //    }
        //}
        model.InsOut = build.Any(x=>x.InsCameraId==model.CameraId);
        await InsertAsync(model);
        return true;
    }

    public async Task<SqlSugarPagedList<AttendanceList>> GetPageList(AttendanceSearch search)
    {
        var query = Context.Queryable<Attendance>()
            //.Includes(x => x.CameraInfoItem)
            .WhereIF(search.IsAuto.HasValue, x => x.IsAuto == search.IsAuto)
            .WhereIF(!string.IsNullOrEmpty(search.PersonSetId), x => x.PersonSetId == search.PersonSetId)
            .WhereIF(!string.IsNullOrEmpty(search.PersonId), x => x.PersonId == search.PersonId)
            .WhereIF(!string.IsNullOrEmpty(search.TrackId), x => x.TrackId == search.TrackId)
            .WhereIF(!string.IsNullOrEmpty(search.CameraId), x => x.CameraId == search.CameraId)
            .WhereIF(search.StartTick.HasValue, x => x.Tick >= search.StartTick)
            .WhereIF(search.EndTick.HasValue, x => x.Tick <= search.EndTick);

        var list = await query.OrderByDescending(x => x.Tick)
            .ToPagedListAsyncMapster<Attendance, AttendanceList>(search.PageNum, search.PageSize);
        return list;
    }

    public async Task<List<AttendanceList>> GetNoPageList(AttendanceSearch search)
    {
        var query = Context.Queryable<Attendance>()
            //.Includes(x => x.CameraInfoItem)
            .Includes(x=>x.PersonInfoItem,x=>x.DormitoryInfoItem)
            .Includes(x=>x.PersonInfoItem,x=>x.PersonSetInfoItem)
            .WhereIF(search.IsAuto.HasValue, x => x.IsAuto == search.IsAuto)
            .WhereIF(!string.IsNullOrEmpty(search.PersonSetId), x => x.PersonSetId == search.PersonSetId)
            .WhereIF(!string.IsNullOrEmpty(search.PersonId), x => x.PersonId == search.PersonId)
            .WhereIF(!string.IsNullOrEmpty(search.TrackId), x => x.TrackId == search.TrackId)
            .WhereIF(!string.IsNullOrEmpty(search.CameraId), x => x.CameraId == search.CameraId)
            .WhereIF(search.CameraIds!=null&&search.CameraIds.Any(),x=>search.CameraIds.Contains(x.CameraId))
            .WhereIF(search.StartTick.HasValue, x => x.Tick >= search.StartTick)
            .WhereIF(search.EndTick.HasValue, x => x.Tick <= search.EndTick);

        var list = await query.OrderByDescending(x => x.Tick)
            .ToListAsync();
        return list.Adapt<List<AttendanceList>>();
    }

    public async Task<SqlSugarPagedList<AttendanceList>> GetPageListByBuild(BuildAttendanceSearch search)
    {
        var buildingInfo = ChangeRepository<DbRepository<BuildingInfo>>();//切换仓储
        var build = await buildingInfo.GetFirstAsync(x => x.Id == search.BuildId);
        var query = Context.Queryable<Attendance>()
            //.Includes(x => x.CameraInfoItem)
            .Where(x=>x.CameraId==build.InsCameraId||x.CameraId==build.OutCameraId)
            .WhereIF(search.IsAuto.HasValue, x => x.IsAuto == search.IsAuto);

        var list = await query.OrderByDescending(x => x.Tick)
            .ToPagedListAsyncMapster<Attendance, AttendanceList>(search.PageNum, search.PageSize);
        return list;
    }
}