/// <summary>
        /// 根据服务码查询服务码详情,有用户信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static async Task <Tuple <List <ServiceCodeDetail>, int> > GetServiceCodeDetailByCode(string code)
        {
            var result     = new List <ServiceCodeDetail>();
            var totalCount = 0;

            if (!string.IsNullOrEmpty(code))
            {
                var packageCodeDetails = GetBeautyServicePackageDetailCodesByCodes(new List <string>()
                {
                    code
                });
                var codeDetail = packageCodeDetails?.FirstOrDefault();
                totalCount = codeDetail != null ? 1 : 0;
                if (codeDetail != null)
                {
                    var codeDetails = await GetServiceCodeDetail(new List <string>() { code });

                    if (codeDetails != null && codeDetails.Any())
                    {
                        User user = null;
                        using (var client = new UserAccountClient())
                        {
                            var accountResult = await client.GetUserByIdAsync(codeDetail.UserId);

                            user = accountResult.Result;
                        }
                        foreach (var item in codeDetails)
                        {
                            item.Mobile = user?.MobileNumber;
                            result.Add(item);
                        }
                    }
                }
                if (!result.Any())
                {
                    var bankRecords = BankMRManager.GetBankMRActivityCodeRecordByServiceCode(new List <string>()
                    {
                        code
                    });
                    var bankMrServiceCodeDetails = await BankMRManager.SearchBankMRActivityCodeDetailByRecords(bankRecords);

                    if (bankMrServiceCodeDetails != null && bankMrServiceCodeDetails.Any())
                    {
                        result.AddRange(bankMrServiceCodeDetails);
                    }
                }
            }


            return(Tuple.Create(result, totalCount));
        }
        /// <summary>
        /// 根据手机号查询服务码详情
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static async Task <Tuple <List <ServiceCodeDetail>, int> > GetServiceCodeDetailByMobile(string mobile, int pageIndex, int pageSize)
        {
            var result     = new List <ServiceCodeDetail>();
            var totalCount = 0;

            if (!string.IsNullOrEmpty(mobile))
            {
                Guid userId = Guid.Empty;
                using (var client = new UserAccountClient())
                {
                    var accountResult = await client.GetUserByMobileAsync(mobile);

                    if (accountResult != null && accountResult.Result != null)
                    {
                        userId = accountResult.Result.UserId;
                    }
                }
                if (userId != Guid.Empty)
                {
                    var packageDetailResult = GetBeautyServicePackageDetailCodesByUserId(userId, pageIndex, pageSize);
                    if (packageDetailResult != null)
                    {
                        var packageDetailCodes = packageDetailResult.Item1;
                        totalCount = packageDetailResult.Item2;
                        if (packageDetailCodes != null && packageDetailCodes.Any())
                        {
                            var codeDetail = await GetServiceCodeDetail(packageDetailCodes.Select(t => t.ServiceCode));

                            if (codeDetail != null && codeDetail.Any())
                            {
                                foreach (var item in codeDetail)
                                {
                                    item.Mobile = mobile;
                                    result.Add(item);
                                }
                            }
                        }
                        var bankRecords            = BankMRManager.GetBankMRActivityCodeRecordByUserId(userId);
                        var bankServiceCodeDetails = await BankMRManager.SearchBankMRActivityCodeDetailByRecords(bankRecords);

                        if (bankServiceCodeDetails != null && bankServiceCodeDetails.Any())
                        {
                            result.AddRange(bankServiceCodeDetails);
                        }
                    }
                }
            }
            return(Tuple.Create(result, totalCount));
        }
        /// <summary>
        /// 根据服务码获取服务码详情信息,没有用户信息
        /// </summary>
        /// <param name="serviceCodes"></param>
        /// <returns></returns>
        public static async Task <IEnumerable <ServiceCodeDetail> > GetServiceCodeDetail(IEnumerable <string> serviceCodes)
        {
            IEnumerable <ServiceCodeDetail> result = null;

            if (serviceCodes != null && serviceCodes.Any())
            {
                var packageDetailCodes = GetBeautyServicePackageDetailCodesByCodes(serviceCodes);
                var serviceCodeDetails = await GetServiceCodeDetailsByCodes(serviceCodes);

                if (packageDetailCodes != null && serviceCodeDetails != null && serviceCodeDetails.Any())
                {
                    var packageDetailIds = packageDetailCodes.Select(t => t.PackageDetailId).Distinct();
                    var packageDetails   = BeautyServicePackageManager.GetBeautyServicePackageDetails(packageDetailIds);
                    if (packageDetails != null)
                    {
                        var pids     = packageDetails.Select(t => t.PID).Distinct();
                        var products = new List <BeautyProductModel>();
                        foreach (var pid in pids)
                        {
                            var product = BeautyProductManager.GetBeautyProductByPid(pid);
                            if (product != null)
                            {
                                products.Add(product);
                            }
                        }

                        result = serviceCodeDetails.Select(t =>
                        {
                            var packageDetailCode = packageDetailCodes.FirstOrDefault(h => string.Equals(h.ServiceCode, t.Code));
                            if (packageDetailCode != null)
                            {
                                var packageDetailId = packageDetailCode.PackageDetailId;
                                var packageDetail   = packageDetails.FirstOrDefault(d => d.PKID == packageDetailId);
                                if (packageDetail != null && products.Any())
                                {
                                    var bankManger    = new BankMRManager();
                                    var cooperateUser = bankManger.FetchMrCooperateUserConfigByPKID(packageDetail.CooperateId);
                                    var product       = products.FirstOrDefault(p => string.Equals(p.PID, packageDetail.PID));
                                    if (product != null)
                                    {
                                        return(new ServiceCodeDetail()
                                        {
                                            PackageDetailCodeId = packageDetailCode.PKID,
                                            BatchCode = string.IsNullOrEmpty(packageDetailCode.ImportBatchCode) ? packageDetailCode.PackageDetailId.ToString() :
                                                        packageDetailCode.ImportBatchCode,
                                            ServiceCode = t.Code,
                                            Status = string.Equals(t.Source, "VOLRevert") ? "Reverted" : t.Status.ToString(),
                                            VipCompanyName = cooperateUser?.CooperateName,
                                            VerifyTime = t.VerifyTime,
                                            StartTime = packageDetailCode.StartTime,
                                            EndTime = packageDetailCode.EndTime,
                                            VipSettleMentPrice = packageDetail.VipSettlementPrice,
                                            ShopCommission = packageDetail.ShopCommission,
                                            PID = packageDetail.PID,
                                            RestrictVehicle = BeautyProductManager.GetVehicleTypeDescription(product.RestrictVehicleType),
                                            OrderNo = t.TuhuOrderId > 0 ? $"TH{t.TuhuOrderId}" : null,
                                            VerifyShop = t.InstallShopId?.ToString(),
                                            Type = packageDetail.IsImportUser? "ImportUser": "******"
                                        });
                                    }
                                    else
                                    {
                                        return(new ServiceCodeDetail());
                                    }
                                }
                                else
                                {
                                    return(new ServiceCodeDetail());
                                }
                            }
                            else
                            {
                                return(new ServiceCodeDetail());
                            }
                        }).Where(s => !string.IsNullOrWhiteSpace(s.ServiceCode)).ToList();
                    }
                }
            }

            return(result);
        }
Beispiel #4
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);
        }