/// <summary> /// 其他礼包新增 /// </summary> /// <param name="giftData"></param> /// <returns></returns> public int AddandUpdateGift_All(GiftBaseData giftData) { if (_giftRespostitory.GetList(m => m.GiftName == giftData.GiftName).Count == 0) { return(_giftRespostitory.Insert(giftData)); } else { var model = _giftRespostitory.GetList(m => m.GiftName == giftData.GiftName && m.GiftType != 1 && m.GiftType != 2 && m.GiftType != 3 && m.GiftType != 4 && m.GiftType != 5 ).FirstOrDefault(); if (model != null) { model.GiftName = giftData.GiftName; model.GiftStatus = giftData.GiftStatus; model.GiftType = giftData.GiftType; model.GiftStatusName = giftData.GiftStatusName; model.GiftTypeName = giftData.GiftTypeName; model.PlateForm = giftData.PlateForm; model.Url = giftData.Url; model.ValidateTime = giftData.ValidateTime; model.Remain = giftData.Remain; _giftRespostitory.Update(model); } return(1); } }
private bool All_DayCollectAsync(CollectConfigModel model, Action <GiftBaseData, bool> CallBack) { bool isSucceed = false; var startTime = DateTime.Now; IList <GameDataModel> data = null; try { SendNotify_Ex("采集开始……"); data = CollectContext.HttpGetResult(model, CallBack); SendNotify_Ex("采集结束,开始同步数据……"); if (data != null && data.Count > 0) { int i = 0; foreach (var item in data) { i++; GiftBaseData basedata = new GiftBaseData { GiftName = item.Name, GiftStatus = item.GiftStatus.GetHashCode(), GiftStatusName = item.GiftStatusName, GiftType = item.GiftType.GetHashCode(), GiftTypeName = item.GiftTypeName, PlateForm = item.PlateForm, Remain = item.Remain, Url = item.Url, ValidateTime = item.ValidateTime }; isSucceed = _giftbll.AddandUpdateGift_All(basedata) > 0; if (isSucceed) { if (i == data.Count) { CallBack(basedata, true); } CallBack(basedata, false); } } } return(isSucceed); } catch (Exception) { throw; } }
private void SendNotify(GiftBaseData model, bool state) { if (model != null) { if (this.InvokeRequired) { this.Invoke(new Action <GiftBaseData, bool>((p, q) => { SendNotify(p, q); }), model, state); } else { if (state) { this.lsbInfo.Items.Add(model.GiftTypeName + " 同步结束:" + "当前时间:" + DateTime.Now.ToString()); } else { this.lsbInfo.Items.Add("礼包分类:" + model.GiftTypeName + " 礼包名称:" + model.GiftName + "剩余:" + model.Remain); } } } }
public static IList <GameDataModel> HttpGetResult(CollectConfigModel item, Action <GiftBaseData, bool> CallBack) { var gameList = new List <GameDataModel>(); if (string.IsNullOrEmpty(item.Url)) { return(null); } var status = EnumHelper.GetDescription(item.GiftStatus); var giftType = EnumHelper.GetDescription(item.GiftType); var plateform = EnumHelper.GetDescription(item.GiftPlatForm); //处理分页问题 var totalRegex = new Regex(ConstantHelper.TotalGifts); //获取总条数采集地址 var requsetUrl = string.Format(item.Url, plateform, giftType, status); string totalResults = new HttpHleper().GetString(requsetUrl, "UTF-8"); var totalCounts = totalRegex.Matches(totalResults); if (totalCounts.Count > 0) { item.Count = int.Parse(totalCounts[0].Value); item.Page = (item.Count + item.PageSize - 1) / item.PageSize; } for (int i = 1; i <= item.Count; i++) { //检测Url为空返回 string result = ""; //result = await new HttpHleper().GetStringAsync(requsetUrl, "UTF-8"); result = new HttpHleper().GetString(string.Concat(requsetUrl, string.Format("&p={0}", i)), "UTF-8"); //result = new HttpHleper().GetHtml(requsetUrl, "", null, @"<table class=""newGifts"">", "</table>", false); //正则匹配数据 var ScopeRegex = new Regex(ConstantHelper.ContentScopeRegx); var nameRegx = new Regex(ConstantHelper.NameRegx); var validateTimeRegex = new Regex(ConstantHelper.ValidateTimeRegx); var remainRegex = new Regex(ConstantHelper.RemainRegx); var urlRegex = new Regex(ConstantHelper.UrlRegx); //获取内容 var searchMatchData = ScopeRegex.Matches(result); //int count = searchMatchData.Count; foreach (Match NextMatch in searchMatchData) { var str = NextMatch.Value; if (nameRegx.IsMatch(str)) { var model = new GameDataModel(); try { model.PlateForm = plateform; model.GiftType = item.GiftType; model.GiftTypeName = giftType; model.GiftStatus = item.GiftStatus; model.GiftStatusName = status; //1.匹配名称 var nameData = nameRegx.Matches(str); if (nameData.Count > 0) { model.Name = nameData[0].Value; } //2.提取有效时间 var validateTimeData = validateTimeRegex.Matches(str); if (validateTimeData.Count > 0) { model.ValidateTime = validateTimeData[0].Value; } //3.提取礼包剩余量 var remainData = remainRegex.Matches(str); if (remainData.Count > 0) { model.Remain = remainData[0].Value; } //4.提取游戏抢包详细页面 var urlData = urlRegex.Matches(str); if (urlData.Count > 0) { model.Url = urlData[0].Value; } if (!string.IsNullOrEmpty(model.Name)) { gameList.Add(model); GiftBaseData basedata = new GiftBaseData { GiftName = model.Name, GiftStatus = model.GiftStatus.GetHashCode(), GiftStatusName = model.GiftStatusName, GiftType = model.GiftType.GetHashCode(), GiftTypeName = model.GiftTypeName, PlateForm = model.PlateForm, Remain = model.Remain, Url = model.Url, ValidateTime = model.ValidateTime }; CallBack(basedata, false); } } catch (Exception) { throw; } } } } return(gameList); }