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);
        }
Beispiel #2
0
        protected override CommandResult <string> OnExecute(object commandParameter)
        {
            var result = new CommandResult <string>();
            var param  = commandParameter as CreatePinParameter;

            using (CoreContext context = new CoreContext())
            {
                //使用zlopenid,因为此时没有登录
                var ZlOpenId = context.MemberInfo.Where(m => m.AccountId == param.MemberAccount).Select(m => m.ZlOpenId).FirstOrDefault();
                var pinCofig = context.PinConfig.Where(p => p.PingId == param.PinId && DateTime.Now >= p.StartDate && p.EndDate >= DateTime.Now).FirstOrDefault();
                if (pinCofig == null)
                {
                    result.ErrorCode    = -1;
                    result.ErrorMessage = "找不到拼团活动";
                    return(result);
                }

                var    configArry = JsonConvert.DeserializeObject <JArray>(pinCofig.Config);
                string config     = "";
                foreach (var item in configArry)
                {
                    if (item["ProductSkuNo"].Value <string>() == param.ProductSkuNo)
                    {
                        config = JsonConvert.SerializeObject(item);
                        break;
                    }
                }
                using (var tran = context.Database.BeginTransaction())
                {
                    PinInfo pinInfo  = null;
                    int     pinCount = 0;
                    if (param.MainId > 0)
                    {
                        pinInfo = context.PinInfo.Where(p => p.Recid == param.MainId && p.PingId == param.PinId && p.EndDate >= DateTime.Now).FirstOrDefault();
                        if (pinInfo != null && pinInfo.MemberAccount == param.MemberAccount)
                        {
                            result.ErrorCode    = -1;
                            result.ErrorMessage = "不能参与自己发起的团";
                            return(result);
                        }
                        pinCount = context.PinOrder.Where(p => p.MainId == param.MainId && p.Status == 1).Count();
                        if (pinCount == 0)
                        {
                            result.ErrorCode    = -1;
                            result.ErrorMessage = "参数错误";
                            return(result);
                        }
                    }
                    if (pinInfo == null)
                    {
                        var endDate = DateTime.Now.AddDays(Convert.ToInt32(pinCofig.MaxDate));
                        if (DateTime.Now.ToString("yyyy-MM-dd") == Convert.ToDateTime(pinCofig.EndDate).ToString("yyyy-MM-dd"))
                        {
                            endDate = Convert.ToDateTime(pinCofig.EndDate);
                        }
                        pinInfo = new PinInfo()
                        {
                            MaxDate       = pinCofig.MaxDate,
                            PingId        = pinCofig.PingId,
                            Status        = 1,
                            Config        = config,
                            CreateDate    = DateTime.Now,
                            EndDate       = endDate,
                            MemberAccount = param.MemberAccount,
                            MinCount      = pinCofig.MinCount
                        };
                        context.PinInfo.Add(pinInfo);
                        context.SaveChanges();
                    }
                    if (pinInfo.Status == 9)
                    {
                        result.ErrorCode    = -1;
                        result.ErrorMessage = "拼团失败,该团已满人";
                        return(result);
                    }
                    if (pinInfo.Status == -1)
                    {
                        result.ErrorCode    = -1;
                        result.ErrorMessage = "拼团失败,该团已失效";
                        return(result);
                    }
                    if (pinInfo.EndDate < DateTime.Now)
                    {
                        result.ErrorCode    = -1;
                        result.ErrorMessage = "拼团失败,该团已失效";
                        return(result);
                    }


                    try
                    {
                        var pinOrder = new PinOrder()
                        {
                            Status        = 0,
                            ProductConfig = pinInfo.Config,
                            CreateDate    = DateTime.Now,
                            MainId        = pinInfo.Recid,
                            MemberAccount = param.MemberAccount
                        };
                        context.PinOrder.Add(pinOrder);
                        //创建商城订单
                        JObject productconfig = JsonConvert.DeserializeObject <JObject>(pinInfo.Config);
                        var     res           = new CreateShopOrderCommand().Execute(new CreateShopOrderParameter()
                        {
                            ZlOpenId     = ZlOpenId,
                            ProductSkuNo = productconfig["ProductSkuNo"].Value <string>(),
                            AddressId    = param.AddressId,
                            Id           = pinCofig.PingId
                        });
                        if (!string.IsNullOrEmpty(res.Data))
                        {
                            pinOrder.OrderNo = res.Data;
                        }
                        else
                        {
                            result.ErrorCode    = -1;
                            result.ErrorMessage = res.ErrorMessage;
                            return(result);
                        }

                        context.SaveChanges();
                        result.Data = pinOrder.OrderNo;

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        result = ErrorResult <string> .ParameterError;
                        result.ErrorMessage = ex.Message;
                        return(result);
                    }

                    //未成团锁定商城订单状态
                    var confirmRes = ZlanAPICaller.ExecuteSys("Sys.ChangeOrderConfirm", new
                    {
                        OrderNo = new List <string> {
                            result.Data
                        },
                        Type = "AWAIT"
                    });
                    if (!confirmRes["ErrorCode"].Value <string>().Equals("0000"))
                    {
                        LogUtil.Log("CreatePinOrder", result.Data, confirmRes["ErrorMsg"].Value <string>());

                        result.ErrorCode    = -1;
                        result.ErrorMessage = "锁定商城订单状态失败";
                        return(result);
                    }
                }
            }

            return(result);
        }