Exemple #1
0
        public static async Task <bool> GeneratePackageCodes(int packageId)
        {
            var result  = false;
            var package = GetBeautyServicePackage(packageId);

            if (package != null && package.PackageCodeNum > 0)
            {
                var writeStr = BeautyServicePackageDal.GetTuhuGrouponWriteConnstr();
                using (var dbhelper = new SqlDbHelper(writeStr))
                {
                    dbhelper.BeginTransaction();
                    var key = $"PacakgeCodeGenerateingRate/{packageId}";
                    try
                    {
                        var waitingGenerateCount = package.PackageCodeNum;
                        var batchSize            = 1000;
                        while (waitingGenerateCount > 0)
                        {
                            var generateCount = batchSize < waitingGenerateCount ? batchSize : waitingGenerateCount;
                            var rows          = BeautyServicePackageDal.InsertBeautyServicePackageCodes(dbhelper, package, generateCount);
                            if (generateCount == rows)
                            {
                                waitingGenerateCount -= generateCount;
                            }
                            else
                            {
                                throw new Exception($"兑换码插入数据库的没有成功,rows:{rows},generateCount:{generateCount}");
                            }
                            using (var client = CacheHelper.CreateCacheClient(CacheClientName))
                            {
                                var process = (package.PackageCodeNum - waitingGenerateCount) * 1.0f / package.PackageCodeNum;
                                await client.SetAsync(key, process, TimeSpan.FromMinutes(20));
                            }
                        }
                        if (string.Equals(package.SettlementMethod, "PreSettled"))
                        {
                            var pacakgeDetails    = GetBeautyServicePackageDetails(packageId);
                            var createOrderResult = await OrderManager.CreatePackageCodeOrderForVipUser(package, pacakgeDetails);

                            if (createOrderResult.OrderId <= 0)
                            {
                                throw new Exception($"创建大客户兑换码订单失败,packageId:{packageId},OrderId{createOrderResult.OrderId}");
                            }
                            else
                            {
                                var setOrderIdResult = BeautyServicePackageDal.SetPackageBuyoutOrderId(dbhelper, packageId, createOrderResult.OrderId);
                                if (!setOrderIdResult)
                                {
                                    throw new Exception("设置买断订单Id字段失败");
                                }
                            }
                        }
                        var setResult = BeautyServicePackageDal.SetPackageCodeIsGenerated(dbhelper, packageId);
                        if (!setResult)
                        {
                            throw new Exception("设置是否兑换字段失败");
                        }
                        dbhelper.Commit();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        dbhelper.Rollback();
                        using (var client = CacheHelper.CreateCacheClient(CacheClientName))
                        {
                            await client.SetAsync(key, 0, TimeSpan.FromMinutes(20));
                        }
                        Logger.Error("批量生成兑换码错误", ex);
                    }
                }
            }

            return(result);
        }
Exemple #2
0
        public static async Task <bool> GenerateServiceCodes(int packageDetailId)
        {
            var result = false;
            var hasGenerateServiceCodes = new List <string>();
            var currentProduct          = GetBeautyServicePackageDetail(packageDetailId);
            var package       = GetBeautyServicePackage(currentProduct.PackageId);
            var cooperateUser = new BankMRManager().FetchMrCooperateUserConfigByPKID(currentProduct.CooperateId);

            if ((string.Equals(package?.PackageType, "serviceCode") || currentProduct.PackageId <= 0) && cooperateUser != null)
            {
                var writeStr = BeautyServicePackageDal.GetTuhuGrouponWriteConnstr();
                using (var dbhelper = new SqlDbHelper(writeStr))
                {
                    dbhelper.BeginTransaction();
                    var key = $"ServiceCodeGenerateingRate/{packageDetailId}";
                    try
                    {
                        var waitingGenerateCount = currentProduct.ServiceCodeNum;
                        var betchSize            = 100;
                        while (waitingGenerateCount > 0)
                        {
                            var generateCount = waitingGenerateCount > betchSize ? betchSize : waitingGenerateCount;
                            var serviceCodes  = await GenerateServiceCode(currentProduct.PID, generateCount, currentProduct.Name, 0, currentProduct.ServiceCodeEndTime);

                            if (serviceCodes != null && serviceCodes.Count() == generateCount)
                            {
                                hasGenerateServiceCodes.AddRange(serviceCodes);
                                var rows = BeautyServicePackageDal.InsertBeautyServicePackageDetailCodes(dbhelper, currentProduct, cooperateUser.VipUserId, serviceCodes);
                                if (rows == generateCount)
                                {
                                    waitingGenerateCount -= generateCount;
                                }
                                else
                                {
                                    throw new Exception($"服务码插入数据库的没有成功,rows:{rows},generateCount:{generateCount}");
                                }
                                using (var client = CacheHelper.CreateCacheClient(CacheClientName))
                                {
                                    var process = (currentProduct.ServiceCodeNum - waitingGenerateCount) * 1.0f / currentProduct.ServiceCodeNum;
                                    await client.SetAsync(key, process, TimeSpan.FromMinutes(20));
                                }
                            }
                            else
                            {
                                throw new Exception($"快修服务生成的服务码和请求的数量不相等,PID:{currentProduct.PID},name:{currentProduct.Name}," +
                                                    $"needCount:{generateCount},realCount:{serviceCodes.Count()}");
                            }
                        }
                        if (string.Equals(currentProduct.SettlementMethod, "PreSettled"))
                        {
                            var createOrderResult = await OrderManager.CreateServiceCodeOrderForVip(cooperateUser, currentProduct);

                            if (createOrderResult.OrderId <= 0)
                            {
                                throw new Exception($"创建大客户兑换码订单失败,packageDetailId:{packageDetailId},OrderId{createOrderResult.OrderId}");
                            }
                            else
                            {
                                var setOrderIdResult = BeautyServicePackageDal.SetPackageDetailBuyoutOrderId(dbhelper, packageDetailId, createOrderResult.OrderId);
                                if (!setOrderIdResult)
                                {
                                    throw new Exception("设置买断订单Id字段失败");
                                }
                            }
                        }
                        var setResult = BeautyServicePackageDal.SetServiceCodeIsGenerated(dbhelper, packageDetailId);
                        if (!setResult)
                        {
                            throw new Exception("设置是否生成服务码字段失败");
                        }
                        dbhelper.Commit();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        dbhelper.Rollback();
                        using (var client = CacheHelper.CreateCacheClient(CacheClientName))
                        {
                            await client.SetAsync(key, 0, TimeSpan.FromMinutes(20));
                        }
                        await RevertVOLServiceCode(hasGenerateServiceCodes);

                        Logger.Error("批量生成服务码错误", ex);
                    }
                }
            }

            return(result);
        }