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 CustomerRepository : ICustomerRepository
    {
        private readonly ISqlSugarProvider _provider;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="provider">SqlSugar数据库提供程序</param>
        public CustomerRepository(ISqlSugarProvider provider)
        {
            _provider = provider;
        }

        /// <summary>
        /// 创建客户记录
        /// </summary>
        /// <param name="entity">客户实体</param>
        /// <returns>创建的记录ID</returns>
        public async Task<int> CreateAsync(CustomerEntity entity)
        {
            var db = _provider.GetClient();

            // 确保表存在
            if (!db.DbMaintenance.IsAnyTable("customers"))
            {
                db.CodeFirst.InitTables(typeof(CustomerEntity));
            }

            // 设置时间戳
            entity.CreatedAt = DateTime.UtcNow;
            entity.UpdatedAt = DateTime.UtcNow;

            // 插入数据并返回自增ID
            return await db.Insertable(entity).ExecuteReturnIdentityAsync();
        }

        /// <summary>
        /// 根据ID获取客户记录
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <returns>客户实体</returns>
        public async Task<CustomerEntity?> GetByIdAsync(int id)
        {
            var db = _provider.GetClient();
            return await db.Queryable<CustomerEntity>()
                .Where(x => x.Id == id)
                .FirstAsync();
        }

        /// <summary>
        /// 根据客户代码获取客户记录
        /// </summary>
        /// <param name="customerCode">客户代码</param>
        /// <returns>客户实体</returns>
        public async Task<CustomerEntity?> GetByCustomerCodeAsync(string customerCode)
        {
            var db = _provider.GetClient();
            return await db.Queryable<CustomerEntity>()
                .Where(x => x.CustomerCode == customerCode)
                .FirstAsync();
        }

        /// <summary>
        /// 更新客户记录
        /// </summary>
        /// <param name="entity">客户实体</param>
        /// <returns>更新是否成功</returns>
        public async Task<bool> UpdateAsync(CustomerEntity entity)
        {
            var db = _provider.GetClient();

            // 更新时间戳
            entity.UpdatedAt = DateTime.UtcNow;

            // 更新数据并返回影响行数
            var rows = await db.Updateable(entity).ExecuteCommandAsync();
            return rows > 0;
        }

        /// <summary>
        /// 删除客户记录
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <returns>删除是否成功</returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var db = _provider.GetClient();
            var rows = await db.Deleteable<CustomerEntity>()
                .Where(x => x.Id == id)
                .ExecuteCommandAsync();
            return rows > 0;
        }

        /// <summary>
        /// 获取所有客户记录
        /// </summary>
        /// <returns>客户实体列表</returns>
        public async Task<List<CustomerEntity>> GetAllAsync()
        {
            var db = _provider.GetClient();
            return await db.Queryable<CustomerEntity>()
                .OrderByDescending(x => x.CreatedAt)
                .ToListAsync();
        }

        /// <summary>
        /// 检查客户是否存在
        /// </summary>
        /// <param name="customerCode">客户代码</param>
        /// <returns>是否存在</returns>
        public async Task<bool> ExistsAsync(string customerCode)
        {
            var db = _provider.GetClient();
            return await db.Queryable<CustomerEntity>()
                .Where(x => x.CustomerCode == customerCode)
                .AnyAsync();
        }

        public async Task<List<CustomerEntity>> GetByIdsAsync(List<int> ids)
        {
            if (ids == null || ids.Count == 0)
                return new List<CustomerEntity>();

            var db = _provider.GetClient();
            return await db.Queryable<CustomerEntity>()
                .Where(x => ids.Contains(x.Id))
                .ToListAsync();
        }
    }
}