using CommonLibrary;
using Dapper;
using EasyNetQ;
using Microsoft.Extensions.Configuration;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;

namespace Bp.Sync.BackEnd.Factories
{
    public interface ISyncFactory
    {
        Task SyncTransactions();

        Task SyncBill();

        Task SyncBatch();

        Task SyncOutboundBill();

        Task SyncInboundBill();

        Task SyncInventoryBill();

        Task SyncInvoice();

        Task SyncProduct();

        Task SyncCategory();

        Task SyncPromotion();

        Task SyncPromotionProduct();

        Task SyncUserDistrict();

        Task SyncProductGroup();

        Task SyncProductType();

        Task SyncProductBill();

        Task SyncProductInventory();

        Task SyncProductInvoice();

        Task SyncOrder();

        Task SyncProductOrder();

        Task SyncRetailBill();

        Task SyncBranch();

        Task SyncBaseConfiguration();

        Task SyncContractDiscountLevel();

        Task SyncDiscountBranchDetailConfig();

        Task SyncWholesalePriceList();

        Task SyncUnit();

        Task SyncReason();

        Task SyncProductUnit();

        Task SyncCustomer();

        Task SyncSupplier();

        Task SyncDebt();

        Task SyncGeneralLedger();

        Task SyncAccount();

        Task SyncRetailStatistic();

        Task SyncInboundStatistic();

        Task SyncFcProductInbound();

        Task SyncOutboundStatistic();

        Task SyncFcCustomer();

        Task SyncFcInboundBill();

        Task SyncFcOutboundBill();

        Task SyncFcReceiptBill();

        Task SyncFcProductStorage();

        Task SyncFcProductShipment();

        Task SyncFcProductSummary();

        Task SyncFcUnitExchange();
    }

    public class SyncFactory : ISyncFactory
    {
        private readonly IBus _bus;
        private readonly IDbConnection _dbDefaultConnection;
        private readonly IDbConnection _dbFinanceConnection;
        private readonly IDbConnection _dbInventoryConnection;
        private readonly IDbConnection _dbFcBusinessConnection;
        private readonly IDbConnection _dbFcCustomerConnection;
        private readonly IConfiguration _configuration;

        public SyncFactory(IConfiguration configuration, IBus bus)
        {
            var defaultConnectionString = configuration.GetConnectionString("DefaultConnection");
            _dbDefaultConnection = new SqlConnection(defaultConnectionString);
            var financeConnectionString = configuration.GetConnectionString("FinanceConnection");
            _dbFinanceConnection = new SqlConnection(financeConnectionString);
            var inventoryConnectionString = configuration.GetConnectionString("InventoryConnection");
            _dbInventoryConnection = new SqlConnection(inventoryConnectionString);
            var fcBusinessConnectionString = configuration.GetConnectionString("FcBusinessConnection");
            _dbFcBusinessConnection = new SqlConnection(fcBusinessConnectionString);
            var fcCustomerConnectionString = configuration.GetConnectionString("FcCustomerConnection");
            _dbFcCustomerConnection = new SqlConnection(fcCustomerConnectionString);

            _bus = bus;
            _configuration = configuration;
        }

        public async Task SyncTransactions()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var transactionIds = await _dbFinanceConnection.QueryAsync<long>("Sync_Transaction_Get_Ids", p, commandType: CommandType.StoredProcedure);
                transactionIds.ToList().Sort();
                int length = transactionIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = transactionIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticTransaction{i}", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncBill()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var transactionIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Bill_Get_Ids", p, commandType: CommandType.StoredProcedure);
                transactionIds.ToList().Sort();
                int length = transactionIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = transactionIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticBill", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncBatch()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var transactionIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Batch_Get_Ids", p, commandType: CommandType.StoredProcedure);
                transactionIds.ToList().Sort();
                int length = transactionIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = transactionIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticBatch", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncOutboundBill()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var transactionIds = await _dbInventoryConnection.QueryAsync<long>("Sync_OutboundBill_Get_Ids", p, commandType: CommandType.StoredProcedure);
                transactionIds.ToList().Sort();
                int length = transactionIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = transactionIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticOutboundBill", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncInboundBill()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var transactionIds = await _dbInventoryConnection.QueryAsync<long>("Sync_InboundBill_Get_Ids", p, commandType: CommandType.StoredProcedure);
                transactionIds.ToList().Sort();
                int length = transactionIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = transactionIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticInboundBill", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncInventoryBill()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var transactionIds = await _dbInventoryConnection.QueryAsync<long>("Sync_InventoryBill_Get_Ids", p, commandType: CommandType.StoredProcedure);
                transactionIds.ToList().Sort();
                int length = transactionIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = transactionIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticInventoryBill", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncInvoice()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var invoiceIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Invoice_Get_Ids", p, commandType: CommandType.StoredProcedure);
                invoiceIds.ToList().Sort();
                int length = invoiceIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = invoiceIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticInvoice{i}", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncProduct()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Product_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticProduct{i}", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncCategory()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Category_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticCategory", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncPromotion()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Promotion_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticPromotion", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncPromotionProduct()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_PromotionProduct_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticPromotionProduct", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncUserDistrict()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_UserDistrict_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticUserDistrict", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncProductGroup()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_ProductGroup_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticProductGroup", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncProductType()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_ProductType_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticProductType", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncProductBill()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_ProductBill_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticProductBill", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncProductInventory()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_ProductInventory_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticProductInventory{i}", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncProductInvoice()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_ProductInvoice_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticProductInvoice{i}", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncOrder()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Order_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticOrder{i}", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncProductOrder()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_ProductOrder_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticProductOrder{i}", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncRetailBill()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_RetailBill_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    int j = 0;
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync($"SyncToElasticRetailBill{i}", timeIds.ConvertListLongToString());
                            }
                        }
                        j++;
                        if (j > 5) j = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncBranch()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Branch_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticBranch", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncBaseConfiguration()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_BaseConfiguration_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticBaseConfiguration", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncContractDiscountLevel()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_ContractDiscountLevel_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticContractDiscountLevel", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncDiscountBranchDetailConfig()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_DiscountBranchDetailConfig_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticDiscountBranchDetailConfig", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncWholesalePriceList()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_WholesalePriceList_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticWholesalePriceList", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncUnit()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Unit_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticUnit", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncReason()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Reason_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticReason", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncProductUnit()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_ProductUnit_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticProductUnit", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncCustomer()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Customer_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticCustomer", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncSupplier()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Supplier_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticSupplier", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncDebt()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Debt_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticDebt", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncGeneralLedger()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_GeneralLedger_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticGeneralLedger", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncAccount()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbInventoryConnection.QueryAsync<long>("Sync_Account_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticAccount", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncRetailStatistic()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_RetailStatistic_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticRetailStatistic0", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncInboundStatistic()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_InboundStatistic_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticInboundStatistic0", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncFcProductInbound()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_ProductInbound_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticFcProductInbound", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncOutboundStatistic()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_OutboundStatistic_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticOutboundStatistic0", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncFcCustomer()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcCustomerConnection.QueryAsync<long>("Sync_FcCustomer_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticFcCustomer", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncFcInboundBill()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_InboundBill_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticFcInboundBill", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncFcOutboundBill()
        {
            try
            {
                var count = 10000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_OutboundBill_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticFcOutboundBill", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncFcReceiptBill()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_ReceiptBill_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticFcReceiptBill", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncFcProductStorage()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_ProductStorage_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticFcProductStorage", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncFcProductShipment()
        {
            try
            {
                var count = 1000;
                var step = 1000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_ProductShipment_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticFcProductShipment", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncFcProductSummary()
        {
            try
            {
                var count = 10000;
                var step = 10000;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_ProductSummary_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticFcProductSummary", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }

        public async Task SyncFcUnitExchange()
        {
            try
            {
                var count = 100;
                var step = 100;
                var p = new DynamicParameters();
                p.Add("@Count", count, DbType.Int32);
                var productIds = await _dbFcBusinessConnection.QueryAsync<long>("Sync_UnitExchange_Get_Ids", p, commandType: CommandType.StoredProcedure);
                productIds.ToList().Sort();
                int length = productIds.Count();
                if (length > 0)
                {
                    for (int i = 0; i <= length / step; i++)
                    {
                        if (length > i * step)
                        {
                            var timeIds = productIds.Skip(i * step).Take(step).ToList();
                            if (timeIds.Count() > 0)
                            {
                                await _bus.SendAsync<string>($"SyncToElasticFcUnitExchange", timeIds.ConvertListLongToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw
            }
        }
    }
}