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

namespace DAL.Repositories
{
    /// <summary>
    /// 出货交接单与袋牌关联仓库实现
    /// </summary>
    public class ShippingHandoverFormBagTagRepository : IShippingHandoverFormBagTagRepository
    {
        private readonly ISqlSugarProvider _provider;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="provider">SqlSugar提供器</param>
        public ShippingHandoverFormBagTagRepository(ISqlSugarProvider provider)
        {
            _provider = provider;
            // 初始化表结构
            var db = _provider.GetClient();
            db.CodeFirst.InitTables(typeof(ShippingHandoverFormBagTagEntity));
        }

        /// <summary>
        /// 插入关联记录
        /// </summary>
        /// <param name="relation">关联实体</param>
        /// <returns>影响行数</returns>
        public async Task<int> InsertAsync(ShippingHandoverFormBagTagEntity relation)
        {
            var db = _provider.GetClient();
            return await db.Insertable(relation).ExecuteCommandAsync();
        }

        /// <summary>
        /// 批量插入关联记录
        /// </summary>
        /// <param name="relations">关联实体列表</param>
        /// <returns>影响行数</returns>
        public async Task<int> InsertBatchAsync(List<ShippingHandoverFormBagTagEntity> relations)
        {
            var db = _provider.GetClient();
            return await db.Insertable(relations).ExecuteCommandAsync();
        }

        /// <summary>
        /// 根据出货交接单ID获取关联的袋牌列表
        /// </summary>
        /// <param name="shippingHandoverFormId">出货交接单ID</param>
        /// <returns>袋牌列表</returns>
        public async Task<List<ShippingHandoverFormBagTagEntity>> GetByShippingHandoverFormIdAsync(int shippingHandoverFormId)
        {
            var db = _provider.GetClient();
            return await db.Queryable<ShippingHandoverFormBagTagEntity>()
                .Where(r => r.ShippingHandoverFormId == shippingHandoverFormId)
                .ToListAsync();
        }

        /// <summary>
        /// 根据袋牌ID获取关联的出货交接单
        /// </summary>
        /// <param name="bagTagId">袋牌ID</param>
        /// <returns>关联实体</returns>
        public async Task<ShippingHandoverFormBagTagEntity> GetByBagTagIdAsync(int bagTagId)
        {
            var db = _provider.GetClient();
            return await db.Queryable<ShippingHandoverFormBagTagEntity>()
                .Where(r => r.BagTagId == bagTagId)
                .FirstAsync();
        }

        /// <summary>
        /// 删除关联记录
        /// </summary>
        /// <param name="id">关联ID</param>
        /// <returns>影响行数</returns>
        public async Task<int> DeleteAsync(int id)
        {
            var db = _provider.GetClient();
            return await db.Deleteable<ShippingHandoverFormBagTagEntity>()
                .Where(r => r.Id == id)
                .ExecuteCommandAsync();
        }

        /// <summary>
        /// 根据出货交接单ID删除所有关联记录
        /// </summary>
        /// <param name="shippingHandoverFormId">出货交接单ID</param>
        /// <returns>影响行数</returns>
        public async Task<int> DeleteByShippingHandoverFormIdAsync(int shippingHandoverFormId)
        {
            var db = _provider.GetClient();
            return await db.Deleteable<ShippingHandoverFormBagTagEntity>()
                .Where(r => r.ShippingHandoverFormId == shippingHandoverFormId)
                .ExecuteCommandAsync();
        }

        /// <summary>
        /// 检查袋牌是否已关联到出货交接单
        /// </summary>
        /// <param name="bagTagId">袋牌ID</param>
        /// <returns>是否已关联</returns>
        public async Task<bool> ExistsByBagTagIdAsync(int bagTagId)
        {
            var db = _provider.GetClient();
            return await db.Queryable<ShippingHandoverFormBagTagEntity>()
                .Where(r => r.BagTagId == bagTagId)
                .AnyAsync();
        }
    }
}