public ActionResult PluginsLottery(Guid Id, string t) { try { //Log4NetImpl.Write("抽奖开始:1"); if (Id == Guid.Empty) { return(Json(new { isOK = false, error_code = "ERR_ArgNotExist" })); //message = "参数有误,请检查!" }); } if (MpUserID == Guid.Empty) { string rawUrl = "http://wx.ssrcb.com/pluginsScene/PluginsLottery?id=" + WebHelper.GetGuid("Id", Guid.Empty); if (string.IsNullOrEmpty(RQuery["openid"])) { return(Json(new { isOK = false, isLogin = true, error_code = "ERR_NotLogin", message = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri=http://wx.ssrcb.com/mpuser/autologin?refUrl={1}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect", MpCenterCache.AppID, rawUrl) })); } } Plugin plugin = _pluginService.GetById(Id); if (plugin == null) { return(Json(new { isOK = false, error_code = "ERR_PluginNotExits" }));//message = "对不起,不存在该插件或未开通,请检查!" }); } //设置布局页 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(plugin.Value); XmlNode pluginNode = xmlDoc.SelectSingleNode("plugin"); GetDataToViewData(pluginNode); if (ViewBag.Cst_Plugin_Title == null) { return(Json(new { isOK = false, error_code = "ERR_PluginNotExits" }));// message = "对不起,您还未配置或配置错误,请到插件中心进行配置下!" }); } if (!string.IsNullOrEmpty(ViewBag.Cst_Plugin_StartDate)) { DateTime startDate = TypeConverter.StrToDateTime(ViewBag.Cst_Plugin_StartDate.ToString()); if (startDate > DateTime.Now) { return(Json(new { isOK = false, error_code = "ERR_PluginNotStart" }));// message = "对不起,抽奖还未开始!" }); } else { if (!string.IsNullOrEmpty(ViewBag.Cst_Plugin_EndDate)) { DateTime endDate = TypeConverter.StrToDateTime(ViewBag.Cst_Plugin_EndDate.ToString()); if (endDate < DateTime.Now) { return(Json(new { isOK = false, error_code = "ERR_PluginIsEnd" }));// message = "对不起,抽奖已经结束!" }); } } } } //PluginSceneVerifyCodeDetail verifyCodeDetail = _pluginSceneVerifyCodeDetailService.GetUnique("from SceneVerifyCodeDetail where ClientMPID='" + this.MpUserID.ToString() + "' and convert(char(10),(CreateDate),120) = '" + DateTime.Now.ToString("yyyy-MM-dd") + "'"); PluginSceneVerifyCodeDetail verifyCodeDetail = _pluginSceneVerifyCodeDetailService.GetUnique("from SceneVerifyCodeDetail where ClientMPID='" + this.MpUserID.ToString() + "' and cast(DATEPART(YEAR, CreateDate) as varchar(4))+'-'+ cast(DATEPART(MONTH, CreateDate) as varchar(2)) = '" + DateTime.Now.ToString("yyyy-MM") + "'"); if (verifyCodeDetail == null) { return(Json(new { isOK = false, isVertfy = true, error_code = "ERR_UserNotVerify" })); } if (verifyCodeDetail.LeaveCount <= 0) { return(Json(new { isOK = false, error_code = "ERR_UserUseOutChance" })); } int prizeCount = TypeConverter.StrToInt(ViewBag.Cst_Plugin_PrizeCount, 12); int prizeItemCount = TypeConverter.StrToInt(ViewBag.Cst_Plugin_Items_Count, 1); if (prizeCount != prizeItemCount) { Log4NetImpl.Write("奖项设置不正确"); return(Json(new { isOK = false, error_code = "ERR_ArgNotExist" })); } IList <PrizeGift> prizeGift = new List <PrizeGift>(prizeItemCount); IList <Double> prizeItems = new List <Double>(prizeItemCount); IList <string> prizeItemsName = new List <string>(prizeCount); IList <int> prizeAngle = new List <int>(12); prizeItemsName.Add("一"); prizeItemsName.Add("二"); prizeItemsName.Add("三"); prizeItemsName.Add("四"); prizeItemsName.Add("五"); prizeItemsName.Add("六"); prizeItemsName.Add("七"); prizeItemsName.Add("八"); prizeItemsName.Add("九"); prizeItemsName.Add("十"); prizeItemsName.Add("十一"); prizeItemsName.Add("十二"); for (int i = 0; i < prizeItemCount; i++) { PrizeGift gift = new PrizeGift(); gift.ID = i; gift.Name = ViewData.GetString("Cst_Plugin_Items_PrizeName" + i.ToString()); gift.Alias_Name = ViewData.GetString("Cst_Plugin_Items_PrizeAliasName" + i.ToString()); gift.Odds = ViewData.GetDouble("Cst_Plugin_Items_PrizePercent" + i.ToString(), 0.00) / 100.00; gift.Pic = ViewData.GetString("Cst_Plugin_Items_PrizePic" + i.ToString()); gift.Quantity = ViewData.GetInt("Cst_Plugin_Items_PrizeNum" + i.ToString()); if (ViewData["Cst_Plugin_Items_PrizeUsedNum" + i.ToString()] != null) { gift.Leavings_Quantity = ViewData.GetInt("Cst_Plugin_Items_PrizeUsedNum" + i.ToString()); } else { gift.Leavings_Quantity = gift.Quantity; } gift.Angle = ViewData.GetInt("Cst_Plugin_Items_PrizeAngle" + i.ToString()); prizeAngle.Add(gift.Angle); gift.Prize_Name = prizeItemsName[i] + "等奖"; if (!string.IsNullOrEmpty(gift.Alias_Name)) { gift.Prize_Name = gift.Alias_Name; } gift.Has_Gift = ViewData.GetInt("Cst_Plugin_Items_Has_Gift" + i.ToString()); gift.Users = ViewData.GetString("Cst_Plugin_Items_SiteUsers" + i.ToString()); gift.UsersName = ViewData.GetString("Cst_Plugin_Items_SiteUsersName" + i.ToString()); prizeItems.Add(gift.Odds); prizeGift.Add(gift); } int averageAngle = 360 / prizeCount; prizeItems.OrderBy(d => d); int result = -1; string resultStr = ""; verifyCodeDetail.LeaveCount = verifyCodeDetail.LeaveCount - 1; verifyCodeDetail.UseDate = DateTime.Now; _pluginSceneVerifyCodeDetailService.Update(verifyCodeDetail); int count = _pluginSceneResultService.GetCount("from SceneResult where pluginId='" + Id.ToString() + "' and MpUserId='" + this.MpUserID.ToString() + "' and cast(DATEPART(YEAR, CreateDate) as varchar(4))+'-'+ cast(DATEPART(MONTH, CreateDate) as varchar(2)) = '" + DateTime.Now.ToString("yyyy-MM") + "'"); if (ViewData.GetInt("Cst_Plugin_ZeroPrize") == 1 && count > 0) { return(Json(new { isOK = true, error_code = "ERR_RunOutOfGift" })); } if (prizeGift.Where(g => g.Has_Gift == 1 && g.Leavings_Quantity > 0).Count() == 0) { return(Json(new { isOK = true, error_code = "ERR_RunOutOfGift" }));//new PrizeGift() { ID = -1, Name = "未中奖", Odds = 0.00d, Pic = "", Quantity = 100000, Angle = 300, Prize_Name = "继续加油" }); } result = LotteryUtils.Lottery(prizeItems, prizeGift, prizeCount, this.MpUserID, Id); if (result == -1) { return(Json(new { isOK = true, error_code = "ERR_RunOutOfGift" }));//Json(new PrizeGift() { ID = -1, Name = "未中奖", Odds = 0.00d, Pic = "", Quantity = 100000, Angle = 240, Prize_Name = "继续加油" }); } else if (result == -2) { return(Json(new { isOK = true, error_code = "ERR_RunOutOfGift" }));// Json(new PrizeGift() { ID = -2, Name = "未中奖", Odds = 0.00d, Pic = "", Quantity = 100000, Angle = 180, Prize_Name = "继续加油" }); } else if (result == -3) { return(Json(new { isOK = true, error_code = "ERR_RunOutOfGift" }));// Json(new PrizeGift() { ID = -2, Name = "未中奖", Odds = 0.00d, Pic = "", Quantity = 100000, Angle = 180, Prize_Name = "继续加油" }); } else { PrizeGift hasPrizeGift = prizeGift[result]; hasPrizeGift.Alias_Name = hasPrizeGift.Alias_Name ?? ""; if (hasPrizeGift.Has_Gift == 1 && hasPrizeGift.Leavings_Quantity > 0) { //中奖业务处理 PluginSceneResult pluginSceneResult = new PluginSceneResult(); pluginSceneResult.Address = ""; pluginSceneResult.CreateDate = DateTime.Now; pluginSceneResult.Email = ""; pluginSceneResult.IsUse = 0; pluginSceneResult.MobilePhone = ""; pluginSceneResult.Name = ""; pluginSceneResult.Phone = ""; pluginSceneResult.PluginId = Id; pluginSceneResult.MpUserId = this.MpUserID; pluginSceneResult.SN = ""; pluginSceneResult.UserName = (string.IsNullOrEmpty(this.Name) ? this.NickName : this.Name); if (this.MpUserArr != null) { pluginSceneResult.Summary = "恭喜用户[" + (string.IsNullOrEmpty(this.Name) ? this.NickName : this.Name) + "]获得" + hasPrizeGift.Prize_Name + "[" + hasPrizeGift.Name + "]"; } else { pluginSceneResult.Summary = "恭喜匿名用户获得" + hasPrizeGift.Prize_Name + "[" + hasPrizeGift.Name + "]"; } pluginSceneResult.Value = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PluginSubmit><Cst_Plugin_ItemIndex>" + result.ToString() + "</Cst_Plugin_ItemIndex><Cst_Plugin_PrizeLevel>" + hasPrizeGift.Prize_Name + "</Cst_Plugin_PrizeLevel><Cst_Plugin_PrizeName>" + hasPrizeGift.Name + "</Cst_Plugin_PrizeName></PluginSubmit>"; #region 更新中奖数量 XmlNode itemsNode = pluginNode.SelectSingleNode("Cst_Plugin_Items"); XmlNode node = itemsNode.SelectSingleNode("Cst_Plugin_Items_PrizeUsedNum" + result.ToString()); if (node == null) { node = xmlDoc.CreateElement("Cst_Plugin_Items_PrizeUsedNum" + result.ToString()); node.InnerText = (hasPrizeGift.Quantity - 1).ToString(); itemsNode.AppendChild(node); } else { node.InnerText = (hasPrizeGift.Leavings_Quantity - 1).ToString(); } StringWriter sw = new StringWriter(); XmlTextWriter tx = new XmlTextWriter(sw); xmlDoc.WriteTo(tx); string strXmlText = sw.ToString(); plugin.Value = strXmlText; _pluginService.Update(plugin); #endregion _pluginSceneResultService.Insert(pluginSceneResult); hasPrizeGift.ResultID = pluginSceneResult.Id; } return(Content(JsonConvert.SerializeObject(hasPrizeGift), "text/javascript")); } } catch (Exception ex) { throw new OceanException(ex.Message, ex); } }
public ActionResult PluginsLottery(Guid Id, string t, string OpenID) { try { if (Id == Guid.Empty) { Log4NetImpl.Write("PluginsLottery:ajax-ERR_ArgNotExist"); return(Json(new { isOK = false, error_code = "ERR_ArgNotExist" })); } Plugin plugin = _pluginService.GetById(Id); if (plugin == null) { return(Json(new { isOK = false, error_code = "ERR_PluginNotExits" })); } //设置布局页 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(plugin.Value); XmlNode pluginNode = xmlDoc.SelectSingleNode("plugin"); GetDataToViewData(pluginNode); if (ViewBag.Cst_Plugin_Title == null) { return(Json(new { isOK = false, error_code = "ERR_PluginNotExits" }));// message = "对不起,您还未配置或配置错误,请到插件中心进行配置下!" }); } if (!string.IsNullOrEmpty(ViewBag.Cst_Plugin_StartDate)) { DateTime startDate = TypeConverter.StrToDateTime(ViewBag.Cst_Plugin_StartDate.ToString()); if (startDate > DateTime.Now) { return(Json(new { isOK = false, error_code = "ERR_PluginNotStart" }));// message = "对不起,抽奖还未开始!" }); } else { if (!string.IsNullOrEmpty(ViewBag.Cst_Plugin_EndDate)) { DateTime endDate = TypeConverter.StrToDateTime(ViewBag.Cst_Plugin_EndDate.ToString()); if (endDate < DateTime.Now) { return(Json(new { isOK = false, error_code = "ERR_PluginIsEnd" }));// message = "对不起,抽奖已经结束!" }); } } } } XYPluginUser xyPluginUser = _xyPluginUserService.GetUnique("from XYPluginUser where OpenId='" + OpenID + "'"); if (xyPluginUser == null) { Log4NetImpl.Write("PluginsLottery:ajax-ERR_UserNotVerify"); return(Json(new { isOK = false, isVertfy = true, error_code = "ERR_UserNotVerify" })); } if (xyPluginUser.LeftCount <= 0) { Log4NetImpl.Write("PluginsLottery:ajax-ERR_UserUseOutChance"); return(Json(new { isOK = false, error_code = "ERR_UserUseOutChance" })); } int prizeCount = TypeConverter.StrToInt(ViewBag.Cst_Plugin_PrizeCount, 12); int prizeItemCount = TypeConverter.StrToInt(ViewBag.Cst_Plugin_Items_Count, 1); if (prizeCount != prizeItemCount) { Log4NetImpl.Write("PluginsLottery:ajax-奖项设置不正确"); return(Json(new { isOK = false, error_code = "ERR_ArgNotExist" })); } IList <PrizeGift> prizeGift = new List <PrizeGift>(prizeItemCount); IList <Double> prizeItems = new List <Double>(prizeItemCount); IList <string> prizeItemsName = new List <string>(prizeCount); IList <int> prizeAngle = new List <int>(12); prizeItemsName.Add("一"); prizeItemsName.Add("二"); prizeItemsName.Add("三"); prizeItemsName.Add("四"); prizeItemsName.Add("五"); prizeItemsName.Add("六"); prizeItemsName.Add("七"); prizeItemsName.Add("八"); prizeItemsName.Add("九"); prizeItemsName.Add("十"); prizeItemsName.Add("十一"); prizeItemsName.Add("十二"); for (int i = 0; i < prizeItemCount; i++) { PrizeGift gift = new PrizeGift(); gift.ID = i; gift.Name = ViewData.GetString("Cst_Plugin_Items_PrizeName" + i.ToString()); gift.Alias_Name = ViewData.GetString("Cst_Plugin_Items_PrizeAliasName" + i.ToString()); gift.Odds = ViewData.GetDouble("Cst_Plugin_Items_PrizePercent" + i.ToString(), 0.00) / 100.00; gift.Pic = ViewData.GetString("Cst_Plugin_Items_PrizePic" + i.ToString()); gift.Quantity = ViewData.GetInt("Cst_Plugin_Items_PrizeNum" + i.ToString()); if (ViewData["Cst_Plugin_Items_PrizeUsedNum" + i.ToString()] != null) { gift.Leavings_Quantity = ViewData.GetInt("Cst_Plugin_Items_PrizeUsedNum" + i.ToString()); } else { gift.Leavings_Quantity = gift.Quantity; } gift.Angle = ViewData.GetInt("Cst_Plugin_Items_PrizeAngle" + i.ToString()); prizeAngle.Add(gift.Angle); gift.Prize_Name = prizeItemsName[i] + "等奖"; if (!string.IsNullOrEmpty(gift.Alias_Name)) { gift.Prize_Name = gift.Alias_Name; } gift.Has_Gift = ViewData.GetInt("Cst_Plugin_Items_Has_Gift" + i.ToString());//>1为奖品,2为刮刮卡 gift.Users = ViewData.GetString("Cst_Plugin_Items_SiteUsers" + i.ToString()); gift.UsersName = ViewData.GetString("Cst_Plugin_Items_SiteUsersName" + i.ToString()); prizeItems.Add(gift.Odds); prizeGift.Add(gift); } int averageAngle = 360 / prizeCount; prizeItems.OrderBy(d => d); int result = -1; xyPluginUser.LeftCount = xyPluginUser.LeftCount - 1; _xyPluginUserService.Update(xyPluginUser); int count = _pluginSceneResultService.GetCount("from SceneResult where pluginId='" + Id.ToString() + "' and MpUserId='" + xyPluginUser.Id + "'"); if (ViewData.GetInt("Cst_Plugin_ZeroPrize") == 1 && count > 0) { Log4NetImpl.Write("PluginsLottery:ajax-ERR_RunOutOfGift"); return(Json(new { isOK = true, error_code = "ERR_RunOutOfGift" })); } if (prizeGift.Where(g => g.Has_Gift > 1 && g.Leavings_Quantity > 0).Count() == 0) { Log4NetImpl.Write("PluginsLottery:ajax-ERR_RunOutOfGift"); return(Json(new { isOK = true, error_code = "ERR_RunOutOfGift" })); } result = LotteryUtils.Lottery(prizeItems, prizeGift, prizeCount, xyPluginUser.Id, Id); if (result == -1) { return(Json(new { isOK = true, error_code = "ERR_RunOutOfGift" })); } else if (result == -2) { return(Json(new { isOK = true, error_code = "ERR_RunOutOfGift" }));// Json(new PrizeGift() { ID = -2, Name = "未中奖", Odds = 0.00d, Pic = "", Quantity = 100000, Angle = 180, Prize_Name = "继续加油" }); } else if (result == -3) { return(Json(new { isOK = true, error_code = "ERR_RunOutOfGift" }));// Json(new PrizeGift() { ID = -2, Name = "未中奖", Odds = 0.00d, Pic = "", Quantity = 100000, Angle = 180, Prize_Name = "继续加油" }); } else { PrizeGift hasPrizeGift = prizeGift[result]; hasPrizeGift.Alias_Name = hasPrizeGift.Alias_Name ?? ""; if (hasPrizeGift.Has_Gift > 0 && hasPrizeGift.Leavings_Quantity > 0) { //中奖业务处理 PluginSceneResult pluginSceneResult = new PluginSceneResult(); pluginSceneResult.Address = ""; pluginSceneResult.CreateDate = DateTime.Now; pluginSceneResult.Email = ""; pluginSceneResult.IsUse = 0; pluginSceneResult.MobilePhone = ""; pluginSceneResult.Name = ""; pluginSceneResult.Phone = ""; pluginSceneResult.PluginId = Id; pluginSceneResult.MpUserId = xyPluginUser.Id; pluginSceneResult.SN = ""; pluginSceneResult.UserName = (string.IsNullOrEmpty(this.Name) ? this.NickName : this.Name); if (hasPrizeGift.Has_Gift == 2)//刮刮卡 { xyPluginUser.GuaGuaKa = 1; _xyPluginUserService.Update(xyPluginUser); } if (this.MpUserArr != null) { pluginSceneResult.Summary = "恭喜用户[" + (string.IsNullOrEmpty(this.Name) ? this.NickName : this.Name) + "]获得" + hasPrizeGift.Prize_Name + "[" + hasPrizeGift.Name + "]"; } else { pluginSceneResult.Summary = "恭喜匿名用户获得" + hasPrizeGift.Prize_Name + "[" + hasPrizeGift.Name + "]"; } pluginSceneResult.Value = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PluginSubmit><Cst_Plugin_ItemIndex>" + result.ToString() + "</Cst_Plugin_ItemIndex><Cst_Plugin_PrizeLevel>" + hasPrizeGift.Prize_Name + "</Cst_Plugin_PrizeLevel><Cst_Plugin_PrizeName>" + hasPrizeGift.Name + "</Cst_Plugin_PrizeName></PluginSubmit>"; #region 更新中奖数量 XmlNode itemsNode = pluginNode.SelectSingleNode("Cst_Plugin_Items"); XmlNode node = itemsNode.SelectSingleNode("Cst_Plugin_Items_PrizeUsedNum" + result.ToString()); if (node == null) { node = xmlDoc.CreateElement("Cst_Plugin_Items_PrizeUsedNum" + result.ToString()); node.InnerText = (hasPrizeGift.Quantity - 1).ToString(); itemsNode.AppendChild(node); } else { node.InnerText = (hasPrizeGift.Leavings_Quantity - 1).ToString(); } StringWriter sw = new StringWriter(); XmlTextWriter tx = new XmlTextWriter(sw); xmlDoc.WriteTo(tx); string strXmlText = sw.ToString(); plugin.Value = strXmlText; _pluginService.Update(plugin); #endregion _pluginSceneResultService.Insert(pluginSceneResult); hasPrizeGift.ResultID = pluginSceneResult.Id; } return(Content(JsonConvert.SerializeObject(hasPrizeGift), "text/javascript")); } } catch (Exception ex) { throw new OceanException(ex.Message, ex); } }