示例#1
0
        public static async Task <ZeroActivityDetailModel> FetchZeroActivityDetailAsync(int period)
        {
            var prefix = await RedisHelper.GetZeroActivityCacheKeyPrefix(GlobalConstant.ZeroActivityDetail);

            Logger.Info($"3缓存key前缀为{prefix}");
            ZeroActivityDetailModel zeroActivity;
            ZeroActivityDetailModel zeroActivityResult = new ZeroActivityDetailModel();

            using (var client = CacheHelper.CreateCacheClient(DefaultClientName))
            {
                var result = await client.GetOrSetAsync(prefix + period.ToString() + "/", () => DalZeroActivity.FetchZeroActivityDetailAsync(period), GlobalConstant.ZeroActivityDetailExpiration);

                Logger.Info($"3redis{result.RealKey};{result.Value?.PID}ProductName:{result.Value?.ProductName}");
                if (result.Success)
                {
                    zeroActivity = result.Value;
                }
                else
                {
                    if (!result.Success)
                    {
                        Logger.Warn($"3缓存redis失败ZeroActivityDetail:{GlobalConstant.ZeroActivityDetail + period.ToString() + "/"};Error:{result.Message}", result.Exception);
                    }
                    zeroActivity = await DalZeroActivity.FetchZeroActivityDetailAsync(period);

                    Logger.Info($"3数据库查询结果{zeroActivity?.PID}");
                }
                if (zeroActivity != null)
                {
                    var product = await FetchProductInfoOnPid(period, zeroActivity.PID);

                    Logger.Info($"3产品详情具体为{product?.ProductId};{product?.DisplayName}");
                    if (product == null)
                    {
                        Logger.Warn($"PID为{zeroActivity.PID}的产品详情失败");
                    }
                    var numOfApplies = await FetchNumOfAppliesOnPeriod(zeroActivity.Period);

                    Logger.Info($"3第{zeroActivity.Period}期活动的申请次数是{numOfApplies}");
                    zeroActivityResult = new ZeroActivityDetailModel
                    {
                        Period            = zeroActivity.Period,
                        ProductName       = product?.DisplayName,
                        NumOfApplications = numOfApplies,
                        NumOfWinners      = zeroActivity.NumOfWinners,
                        SingleValue       = (product == null) ? 0 : (zeroActivity.Quantity / zeroActivity.NumOfWinners) * product.Price,
                        StartDateTime     = zeroActivity.StartDateTime,
                        EndDateTime       = zeroActivity.EndDateTime,
                        StatusOfActivity  = (DateTime.Now < zeroActivity.StartDateTime) ? 0 : ((DateTime.Now < zeroActivity.EndDateTime) ? 1 : ((DateTime.Now < zeroActivity.EndDateTime.AddDays(30)) ? 3 : 4)),
                        Description       = zeroActivity.Description,
                        ImgUrl            = zeroActivity.ImgUrl,
                        PID           = zeroActivity.PID,
                        Quantity      = zeroActivity.Quantity,
                        ProductImages = product?.ImageUrls
                    };
                }
                Logger.Info($"3周期是{zeroActivityResult.Period.ToString()};获奖者人数是{zeroActivityResult.NumOfWinners.ToString()};申请次数是{zeroActivityResult.NumOfApplications.ToString()};产品PID是{zeroActivityResult.PID};产品名称是{zeroActivityResult.ProductName}");
                return(zeroActivityResult);
            }
        }
        public static async Task <int> SubmitZeroActivityApplicationAsync(ZeroActivityRequest requestModel, ZeroActivityDetailModel activityDetail, string userMobile)
        {
            using (var cmd = new SqlCommand(InsertZeroActivityApplication))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@Period", requestModel.Period);
                cmd.Parameters.AddWithValue("@UserID", requestModel.UserId);
                cmd.Parameters.AddWithValue("@UserName", requestModel.UserName);
                cmd.Parameters.AddWithValue("@PID", activityDetail.PID);
                cmd.Parameters.AddWithValue("@ProductName", activityDetail.ProductName);
                cmd.Parameters.AddWithValue("@Quantity", (activityDetail.NumOfWinners == 0 ? 0 : activityDetail.Quantity / activityDetail.NumOfWinners));
                cmd.Parameters.AddWithValue("@ProvinceID", requestModel.ProvinceID);
                cmd.Parameters.AddWithValue("@CityID", requestModel.CityID);
                cmd.Parameters.AddWithValue("@ApplyReason", requestModel.ApplicationReason);
                cmd.Parameters.AddWithValue("@CarID", requestModel.CarID);
                cmd.Parameters.AddWithValue("@Mobile", userMobile);
                var count = Convert.ToInt32(await DbHelper.ExecuteScalarAsync(cmd));
                if (count > 0)
                {
                    await RedisHelper.CreateZeroActivityApplyCache();

                    await RedisHelper.CreateZeroActivityApplyCacheOnPeriod(requestModel.Period);

                    await RedisHelper.CreateZeroActivityApplyCacheOnUserId(requestModel.UserId);
                }
                return(count);
            }
        }