using Hotline.Repository.SqlSugar.DataPermissions;
using Hotline.Share.Dtos;
using Hotline.Share.Tools;
using Hotline.Snapshot;
using Hotline.Snapshot.IRepository;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Domain.Authentications;
using XF.Domain.Dependency;

namespace Hotline.Repository.SqlSugar.Snapshot;
public class OrderSnapshotRepository : BaseRepository<OrderSnapshot>, IOrderSnapshotRepository, IScopeDependency
{
    private readonly ISessionContext _sessionContext;
    public OrderSnapshotRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder, ISessionContext sessionContext, IServiceProvider serviceProvider) : base(uow, dataPermissionFilterBuilder, serviceProvider)
    {
        _sessionContext = sessionContext;
    }

    public async Task<OrderSnapshot> GetByNetworkENumberAsync(string appealNumber)
    {
        return await Queryable().Where(m => m.NetworkENumber == appealNumber).FirstAsync();
    }

    public bool HasOrder(string orderId)
    {
        return Queryable().Any(m => m.Id == orderId);
    }

    public async Task<OrderSnapshot> UpdateSafetyAsync(string orderId, bool isSafety, string remark)
    {
        OrderSnapshot order = null;
        await GetAsync(orderId)
            .Then(async orderSnapshot =>
            {
                orderSnapshot.IsSafetyDepartment = isSafety;
                orderSnapshot.SignUserId = _sessionContext.UserId;
                orderSnapshot.SignUserName = _sessionContext.UserName;
                orderSnapshot.SignTime = DateTime.Now;
                orderSnapshot.SignRemark = remark;
                await UpdateAsync(orderSnapshot, CancellationToken.None);
                order = orderSnapshot;
            });
        return order;
    }
}
