using System.Collections.Generic;
using System.Threading.Tasks;
using DAL.Interfaces;
using MDL.Models;
using SqlSugar;
using DB.Database;

namespace DAL.Repositories
{
    public class TagInstanceRepository : ITagInstanceRepository
    {
        private readonly SqlSugarScope _db;

        public TagInstanceRepository(ISqlSugarProvider sqlSugarProvider)
        {
            _db = sqlSugarProvider.GetClient();
        }

        public async Task<TagInstanceEntity> CreateAsync(TagInstanceEntity entity)
        {
            await _db.Insertable(entity).ExecuteCommandAsync();
            return entity;
        }

        public async Task<TagInstanceEntity> UpdateAsync(TagInstanceEntity entity)
        {
            await _db.Updateable(entity).ExecuteCommandAsync();
            return entity;
        }

        public async Task<TagInstanceEntity> GetByIdAsync(long id)
        {
            return await _db.Queryable<TagInstanceEntity>().Where(x => x.Id == id).FirstAsync();
        }

        public async Task<List<TagInstanceEntity>> GetByWaybillAsync(string neutralWaybillNumber)
        {
            return await _db.Queryable<TagInstanceEntity>()
                .Where(x => x.NeutralWaybillNumber == neutralWaybillNumber)
                .ToListAsync();
        }

        public async Task<List<TagInstanceEntity>> GetByTagTypeAsync(string tagType)
        {
            return await _db.Queryable<TagInstanceEntity>()
                .Where(x => x.TagType == tagType)
                .ToListAsync();
        }
    }
}
