Example #1
0
        /// <summary>
        /// 领取优惠券
        /// </summary>
        private void CouponGet()
        {
            try
            {
                int    uid         = GetFormValue("userid", 0);
                int    cpid        = GetFormValue("cpid", 0);
                string username    = GetFormValue("username", "");
                string usermobile  = GetFormValue("usermobile", "");
                string requestSign = GetFormValue("sign", "");

                Dictionary <string, string> paramters = new Dictionary <string, string>();
                paramters.Add("userid", uid.ToString());
                paramters.Add("cpid", cpid.ToString());
                string currentSign = SignatureHelper.BuildSign(paramters, ConstConfig.SECRET_KEY);
                if (!requestSign.Equals(currentSign))
                {
                    json = JsonHelper.JsonSerializer(new ResultModel(ApiStatusCode.未授权));
                }
                else
                {
                    if (!RegexHelper.IsValidMobileNo(usermobile))
                    {
                        json = JsonHelper.JsonSerializer(new ResultModel(ApiStatusCode.无效手机号));
                        return;
                    }

                    //
                    lock (objCouponGetLocked)
                    {
                        CashCouponLogModel logModel = CouponLogic.GetCashCouponLogIDByUserID(uid, cpid);
                        if (logModel != null)
                        {
                            using (TransactionScope scope = new TransactionScope())
                            {
                                bool flag = CouponLogic.UpdateUserCashCouponGetLog(new CashCouponLogModel()
                                {
                                    UserId = uid,
                                    ID     = logModel.ID,
                                    Name   = username,
                                    Mobile = usermobile
                                });
                                if (flag)
                                {
                                    Dictionary <string, object> dict = new Dictionary <string, object>();
                                    dict["couponNo"] = logModel.CouponNo;
                                    json             = JsonHelper.JsonSerializer(new ResultModel(ApiStatusCode.OK, dict));

                                    CouponLogic.DeleteUserCashCoupon(logModel.CouponNo, logModel.CouponId, uid);
                                    var user = UserLogic.GetModel(uid);
                                    if (user != null)
                                    {
                                        CashCouponModel model = CouponLogic.GetModel(cpid, true);
                                        //添加优惠券领取操作日志
                                        LogLogic.AddCouponLog(new LogBaseModel()
                                        {
                                            objId         = model.CouponId,
                                            UserId        = user.UserId,
                                            ShopId        = logModel.ShopId,
                                            OperationType = 1,//0创建 1领取 2使用
                                            Money         = logModel.Money
                                        });
                                    }

                                    try
                                    {
                                        var shopData = ShopLogic.GetShopModel(logModel.ShopId);
                                        if (shopData != null)
                                        {
                                            string errmsg  = "";
                                            string content = string.Format("您收到一张新的{0}元现金券,现金券使用码:{1},您可在{2}前到门店消费使用。门店地址:{3}",
                                                                           logModel.Money,
                                                                           logModel.CouponNo,
                                                                           logModel.StartTime.ToString("yyyy.MM.dd") + "-" + logModel.EndTime.ToString("yyyy.MM.dd"),
                                                                           shopData.ShopProv + shopData.ShopCity + shopData.ShopAddress + shopData.ShopName
                                                                           );
                                            if (!string.IsNullOrEmpty(usermobile) && RegexHelper.IsValidMobileNo(usermobile))
                                            {
                                                SmsLogic.send(1, usermobile, content, out errmsg);
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        LogHelper.Log(string.Format("Message:{0},StackTrace:{1}", ex.Message, ex.StackTrace), LogHelperTag.ERROR);
                                    }
                                }
                                else
                                {
                                    json = JsonHelper.JsonSerializer(new ResultModel(ApiStatusCode.SERVICEERROR));
                                }

                                scope.Complete();
                            }
                        }
                        else
                        {
                            json = JsonHelper.JsonSerializer(new ResultModel(ApiStatusCode.现金券已领完));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log(string.Format("Message:{0},StackTrace:{1}", ex.Message, ex.StackTrace), LogHelperTag.ERROR);
                json = JsonHelper.JsonSerializer(new ResultModel(ApiStatusCode.SERVICEERROR));
            }
        }