Esempio n. 1
0
        protected override CommandResult <int> OnExecute(object commandParameter)
        {
            var result = new CommandResult <int>();
            var param  = commandParameter as SetPinInfoSuccessParameter;

            using (CoreContext context = new CoreContext())
            {
                //设置拼团成功
                result.Data = context.Database.ExecuteSqlCommand("update pin_info set status=9 where recid=@p0 and status!=9 ", param.MainId);
                if (result.Data > 0)
                {
                    foreach (var item in param.OrderNo)
                    {
                        try
                        {
                            var order = context.PinOrder.Where(p => p.OrderNo == item).FirstOrDefault();

                            var zlopenid = context.MemberInfo.Where(m => m.AccountId == order.MemberAccount).Select(m => m.ZlOpenId).FirstOrDefault();
                            var wxopneid = "";
                            using (MySqlConnection conn = new MySqlConnection(ConfigurationUtil.GetSection("ConnectionStrings")["ShopConnectString"]))
                            {
                                conn.Open();
                                MySqlCommand com = new MySqlCommand(@"select wx_open_id  from member_info where account_id=?acc ", conn);
                                com.Parameters.Add(new MySqlParameter("acc", zlopenid));

                                MySqlDataReader reader = com.ExecuteReader();
                                while (reader.Read())
                                {
                                    wxopneid = reader["wx_open_id"] as string;
                                }
                                reader.Close();
                                conn.Close();
                            }
                            if (!string.IsNullOrEmpty(wxopneid))
                            {
                                ActiveMQMessagePusher.Push("Message", new Dictionary <string, string>
                                {
                                    { "MessageKey", "PinSuccess" }
                                }, new
                                {
                                    WxOpenId = wxopneid,
                                    OrderNo  = item,
                                    Product  = JsonConvert.DeserializeObject <Hashtable>(order.ProductConfig)["ProductName"] as string
                                });
                            }
                        }
                        catch (Exception ex)
                        {
                            LogUtil.LogText("sendmessage:MessageKey:PinSuccess", item, ex.Message);
                        }
                    }


                    var confirmRes = ZlanAPICaller.ExecuteSys("Sys.ChangeOrderConfirm", new { param.OrderNo, Type = "SUCCESS" });
                    if (!confirmRes["ErrorCode"].Value <string>().Equals("0000"))
                    {
                        LogUtil.Log("CreatePinOrder", param.MainId.ToString(), confirmRes["ErrorMsg"].Value <string>());

                        result.ErrorCode    = -1;
                        result.ErrorMessage = "解锁商城订单失败";
                        return(result);
                    }
                }
            }
            return(result);
        }
Esempio n. 2
0
        protected override CommandResult <PrizeModel> OnExecute(object commandParameter)
        {
            var result = new CommandResult <PrizeModel>();
            var param  = commandParameter as NewYearDrawPrizeParameter;

            result.Data = new PrizeModel();
            using (CoreContext context = new CoreContext())
            {
                var act = context.ActivityInfo.Where(a => a.StartTime <= DateTime.Now && a.EndTime >= DateTime.Now && a.Status == 1 && a.Kind == param.Kind).FirstOrDefault();
                if (act == null)
                {
                    result.ErrorCode    = -1;
                    result.ErrorMessage = "活动已结束";
                    return(result);
                }

                Hashtable pconfig = JsonConvert.DeserializeObject <Hashtable>(act.ProductConfig);

                var zlopenid = context.MemberInfo.Where(m => m.AccountId == param.MemberAccount).Select(m => m.ZlOpenId).FirstOrDefault();

                //扣减次数,使用数据库锁
                if (context.Database.ExecuteSqlCommand(@"update member_draw_count set current_count=(current_count-1) 
                                                        where member_account=@p0 and act_id=@p1 and current_count-1>=0", zlopenid, act.Recid) > 0)
                {
                    PrizeModel prize = null;

                    var LogCount = context.ActivityLog.Where(l => l.MemberAccount == zlopenid && l.ActivityId == act.Recid).Count();

                    {
                        var     connect = ConfigurationUtil.GetSection("ConnectionStrings")["ShopConnectString"];
                        decimal payFee  = 0;
                        using (MySqlConnection con = new MySqlConnection(connect))
                        {
                            con.Open();
                            MySqlCommand com1 = new MySqlCommand("select sum(payment_fee) as PayFee from shop_order_info " +
                                                                 "where member_account=?acc and pay_status=1 and order_no like '%00' and date(payment_time)>='2019-01-16' and date(payment_time)<='2019-01-25' ", con);
                            com1.Parameters.Add(new MySqlParameter("?acc", zlopenid));
                            var Reader1 = com1.ExecuteReader();
                            while (Reader1.Read())
                            {
                                if (Reader1["PayFee"] != DBNull.Value)
                                {
                                    payFee = Convert.ToDecimal(Reader1["PayFee"]);
                                }
                            }
                            Reader1.Close();
                            con.Close();
                        }
                        var plist = new List <PrizeModel>();
                        //遍历奖品规则
                        var redisdb = RedisClient.GetDatabase(2);
                        foreach (var item in (pconfig["PayPrize"] as JArray).ToObject <List <Hashtable> >())
                        {
                            JObject condiction = item["Condition"] as JObject;
                            if (payFee >= condiction["Fee"].Value <decimal>())
                            {
                                var _plist = new List <PrizeModel>();
                                foreach (var hash in (item["Prize"] as JArray).ToObject <List <Hashtable> >())
                                {
                                    if (!hash.ContainsKey("end") || DateTime.Parse(hash["start"] as string) <= DateTime.Now && DateTime.Parse(hash["end"] as string) >= DateTime.Now)
                                    {
                                        _plist.Add(new PrizeModel()
                                        {
                                            PrizeCode = hash["PrizeCode"] as string,
                                            PrizeName = hash["PrizeName"] as string,
                                            Kind      = hash["Kind"] as string,
                                            Ratio     = Convert.ToInt32(hash["Ratio"]),
                                            CouponId  = hash.ContainsKey("CouponId")? Convert.ToInt32(hash["CouponId"]):0
                                        });
                                    }
                                }
                                if (_plist.Count > 0)
                                {
                                    var p = GetPrize(_plist);
                                    if (p != null)
                                    {
                                        string key = p.PrizeCode.IndexOf("prize") > -1 ? "RealPrize" : p.PrizeCode;
                                        //判断中奖次数
                                        if (condiction["Limit"] != null && ValidateCount(redisdb, key, param.MemberAccount, Convert.ToInt32(condiction["Limit"])))
                                        {
                                            continue;
                                        }
                                        //处理实物奖品
                                        if (p.Kind == "RealPrize")
                                        {
                                            string RealPrize = "";
                                            RealPrize = redisdb.ListLeftPop("Prize:" + p.PrizeCode);
                                            if (!string.IsNullOrEmpty(RealPrize))
                                            {
                                                prize = JsonConvert.DeserializeObject <PrizeModel>(RealPrize);
                                            }
                                            else
                                            {
                                                continue;//实物不中继续往下抽奖
                                            }
                                        }
                                        else
                                        {
                                            prize = p;
                                        }

                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (prize != null)
                    {
                        result.Data = prize;
                        var p = new PrizeInfo()
                        {
                            Status        = 1,
                            ActId         = act.Recid,
                            CouponId      = prize.CouponId,
                            CreateDate    = DateTime.Now,
                            MemberAccount = zlopenid,
                            PrizeCode     = prize.PrizeCode,
                            PrizeName     = prize.PrizeName,
                            Kind          = prize.Kind,
                        };
                        context.PrizeInfo.Add(p);
                        if (prize.Kind == "Coupon")
                        {
                            //调用获取优惠券
                            var url = ConfigurationUtil.GetSection("CouponUrl").Value as string;
                            //领取抵扣优惠券
                            try
                            {
                                ActiveMQMessagePusher.Push("Command",
                                                           new Dictionary <string, string>
                                {
                                    { "caller", "Restful" },
                                    { "url", url }
                                }, new
                                {
                                    MemberAccount = zlopenid,
                                    RuleId        = Convert.ToInt32(prize.CouponId)
                                }
                                                           );
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    else
                    {
                        result.ErrorMessage = "未中奖,换个姿势再试试吧";
                        result.ErrorCode    = -22;
                    }
                }
                else
                {
                    result.ErrorCode    = -21;
                    result.ErrorMessage = "";
                }
                context.ActivityLog.Add(new ActivityLog()
                {
                    MemberAccount = zlopenid,
                    ActivityId    = act.Recid,
                    CreateTime    = DateTime.Now,
                    Memo          = result.Data != null ? "抽中:" + result.Data.PrizeName : result.ErrorMessage
                });
                context.SaveChanges();
            }

            return(result);
        }
Esempio n. 3
0
        protected override CommandResult <Hashtable> OnExecute(object commandParameter)
        {
            var result = new CommandResult <Hashtable>();
            var param  = commandParameter as DrawPrizeParameter;

            result.Data = new Hashtable();
            using (CoreContext context = new CoreContext())
            {
                var act = context.ActivityInfo.Where(a => a.StartTime <= DateTime.Now && a.EndTime >= DateTime.Now && a.Status == 1 && a.Kind == param.Kind).FirstOrDefault();
                if (act == null)
                {
                    result.ErrorCode    = -1;
                    result.ErrorMessage = "活动已结束";
                    return(result);
                }

                Hashtable pconfig = JsonConvert.DeserializeObject <Hashtable>(act.ProductConfig);

                var zlopenid = context.MemberInfo.Where(m => m.AccountId == param.MemberAccount).Select(m => m.ZlOpenId).FirstOrDefault();

                //扣减次数,使用数据库锁
                if (context.Database.ExecuteSqlCommand(@"update member_draw_count set current_count=(current_count-1) 
                                                        where member_account=@p0 and current_count-1>=0", zlopenid) > 0)
                {
                    var LogCount = context.ActivityLog.Where(l => l.MemberAccount == zlopenid && l.ActivityId == act.Recid).Count();
                    var res      = new NewMemberSkillCommand().Execute(new NewMemberSkillParameter()
                    {
                        MemberAccount = zlopenid,
                        LogCount      = LogCount,
                        Prize         = pconfig
                    });
                    Hashtable prize = null;
                    if (res.ErrorCode == 0 && (Convert.ToBoolean(res.Data["isNew"]) || Convert.ToBoolean(res.Data["is30"])))
                    {
                        result.ErrorCode    = res.ErrorCode;
                        result.ErrorMessage = res.ErrorMessage;
                        prize = res.Data["Prize"] as Hashtable;
                    }
                    else
                    {
                        var     connect = ConfigurationUtil.GetSection("ConnectionStrings")["ShopConnectString"];
                        decimal payFee  = 0;
                        using (MySqlConnection con = new MySqlConnection(connect))
                        {
                            con.Open();
                            MySqlCommand com1 = new MySqlCommand("select sum(payment_fee) as PayFee from shop_order_info " +
                                                                 "where member_account=?acc and pay_status=1 and order_no like '%00' and date(payment_time)>='2018-11-01' and date(payment_time)<='2018-11-11' ", con);
                            com1.Parameters.Add(new MySqlParameter("?acc", zlopenid));
                            var Reader1 = com1.ExecuteReader();
                            while (Reader1.Read())
                            {
                                if (Reader1["PayFee"] != DBNull.Value)
                                {
                                    payFee = Convert.ToDecimal(Reader1["PayFee"]);
                                }
                            }
                            Reader1.Close();
                            con.Close();
                        }
                        //遍历奖品规则
                        var redisdb = RedisClient.GetDatabase(1);
                        foreach (var item in (pconfig["PayPrize"] as JArray).ToObject <List <Hashtable> >())
                        {
                            JObject condiction = item["Condition"] as JObject;

                            string key = "level:" + condiction["Fee"].Value <string>();
                            if (redisdb.HashExists(key, param.MemberAccount))
                            {
                                continue;
                            }
                            if (payFee >= condiction["Fee"].Value <decimal>())
                            {
                                prize = (item["Prize"] as JObject).ToObject <Hashtable>();
                                //处理实物奖品
                                if (prize["Kind"] as string == "RealPrize")
                                {
                                    //var hash = (prize["RealPrize"] as JObject).ToObject<Hashtable>();
                                    var _realprize = new List <PrizeModel>();
                                    foreach (var hash in (prize["RealPrize"] as JArray).ToObject <List <Hashtable> >())
                                    {
                                        if (DateTime.Parse(hash["start"] as string) <= DateTime.Now && DateTime.Parse(hash["end"] as string) >= DateTime.Now)
                                        {
                                            _realprize.Add(new PrizeModel()
                                            {
                                                PrizeCode = hash["PrizeCode"] as string,
                                                PrizeName = hash["PrizeName"] as string,
                                                Kind      = prize["Kind"] as string
                                            });
                                        }
                                    }
                                    string RealPrize = "";

                                    if (_realprize.Count != 0)
                                    {
                                        var r     = new Random(Guid.NewGuid().GetHashCode());
                                        var index = r.Next(0, _realprize.Count);
                                        RealPrize = redisdb.ListLeftPop("Prize:" + _realprize[index].PrizeCode);
                                    }
                                    if (!string.IsNullOrEmpty(RealPrize))
                                    {
                                        prize = JsonConvert.DeserializeObject <Hashtable>(RealPrize);
                                    }
                                    else
                                    {
                                        prize = null;
                                    }
                                }
                                if (prize != null)
                                {
                                    redisdb.HashIncrementAsync(key, param.MemberAccount);
                                }
                                break;
                            }
                        }
                    }
                    if (prize != null)
                    {
                        result.Data = prize;
                        var p = new PrizeInfo()
                        {
                            Status        = 1,
                            ActId         = act.Recid,
                            CouponId      = Convert.ToInt32(prize["CouponId"]),
                            CreateDate    = DateTime.Now,
                            MemberAccount = zlopenid,
                            PrizeCode     = prize["PrizeCode"] as string,
                            PrizeName     = prize["PrizeName"] as string,
                            Kind          = prize["Kind"] as string,
                        };
                        context.PrizeInfo.Add(p);
                        if (prize["Kind"] as string == "Coupon")
                        {
                            //调用获取优惠券
                            var url = ConfigurationUtil.GetSection("CouponUrl").Value as string;
                            //领取抵扣优惠券
                            try
                            {
                                ActiveMQMessagePusher.Push("Command",
                                                           new Dictionary <string, string>
                                {
                                    { "caller", "Restful" },
                                    { "url", url }
                                }, new
                                {
                                    MemberAccount = zlopenid,
                                    RuleId        = Convert.ToInt32(prize["CouponId"])
                                }
                                                           );
                            }
                            catch (Exception)
                            {
                            }
                            //var couponres = ZlanAPICaller.Call<CommandResult<string>>(url, new { MemberAccount = zlopenid, RuleId = Convert.ToInt32(prize["CouponId"]) });
                            //if (couponres.ErrorCode == 0)
                            //{
                            //    couponticket = couponres.Data;
                            //}
                            //else
                            //{
                            //    LogUtil.Log("DrawPrize_TakeCoupon", p.Recid.ToString(), $"input=>{JsonConvert.SerializeObject(new { MemberAccount = zlopenid, RuleId = Convert.ToInt32(prize["CouponId"]) })}#error=>{res.ErrorMessage}");
                            //}
                        }
                    }
                    else
                    {
                        result.ErrorMessage = "未中奖,换个姿势再试试吧";
                        result.ErrorCode    = -22;
                    }
                }
                else
                {
                    result.ErrorCode    = -21;
                    result.ErrorMessage = "每下单68元,即获得一次抽奖机会,上不封顶";
                }
                context.ActivityLog.Add(new ActivityLog()
                {
                    MemberAccount = zlopenid,
                    ActivityId    = act.Recid,
                    CreateTime    = DateTime.Now,
                    Memo          = result.Data.Keys.Count > 0 ? "抽中:" + result.Data["PrizeName"] as string : result.ErrorMessage
                });
                context.SaveChanges();
            }

            return(result);
        }