// GET: SSGiven/SearchReceiveThings 查询受助请求 searchString=查询字符串 public ActionResult SearchReceiveThings(string searchString, int currentPage = 1) { int usrId = (int)HttpContext.Session["usrId"]; List <string> sWords = new List <string>(); //近义词 List <string> oWords = new List <string>(); //原词 var qs = db.Quests.Where(x => x.Status == "等待受助中" && x.ReceiverId != usrId);//基本结果 //地域置顶 var user = db.Users.Where(x => x.UserId == usrId).FirstOrDefault(); var pro = user.Province; var sch = user.School; var qs_u = qs.Join(db.Users.Select(x => new { x.UserId, x.Province, x.School }), q => q.ReceiverId, u => u.UserId, (q, u) => new { q, u }); var qsu1 = qs_u.Where(o => o.u.Province.Contains(pro) && o.u.School.Contains(sch)).Select(n => new SSSearchReceiveThingsViewModel { Quest = n.q, prior = 300 }).ToList(); //同省同校 var qsu2 = qs_u.Where(o => o.u.Province.Contains(pro) && !o.u.School.Contains(sch)).Select(n => new SSSearchReceiveThingsViewModel { Quest = n.q, prior = 200 }).ToList(); //同校不同省 var qsu3 = qs_u.Where(o => !o.u.Province.Contains(pro)).Select(n => new SSSearchReceiveThingsViewModel { Quest = n.q, prior = 100 }).ToList(); //不同省 var qs_place = qsu1.Union(qsu2).Union(qsu3); //已转化为list的视图模型,之后关键字直接foreach匹配 //获取同义分词 AI if (!string.IsNullOrEmpty(searchString)) { string data = TencentAI.tencentAI(searchString); oWords = TencentAI.oriWords(data); sWords = TencentAI.synWords(data); //qs = qs.Where(x => x.Name.Contains(searchString)); //关键字匹配 foreach (var l in qs_place) { if (l.Quest.Name.Contains(searchString)) { l.prior += 10; continue; } for (int i = 0; i < oWords.Count; i++) { if (l.Quest.Name.Contains(oWords[i])) { l.prior += 5; break; } } for (int j = 0; j < sWords.Count; j++) { if (l.Quest.Name.Contains(sWords[j])) { l.prior += 1; break; } } if (l.prior % 100 == 0) { l.prior -= 1000; } } } qs_place = qs_place.Where(x => x.prior >= 0); //分页 int totalThings = qs_place.Count(); //总共条目数目 ViewBag.totalPages = (int)Math.Ceiling(totalThings / (double)numPerPage); //总共页数 int start = (currentPage - 1) * numPerPage; //开始的条目 qs_place = qs_place.OrderByDescending(x => x.prior).ThenByDescending(x => x.Quest.ReleaseTime).Skip(start).Take(numPerPage); ViewBag.searchString = searchString; ViewBag.currentPage = currentPage; return(View(qs_place)); }
// GET: 查询其他用户的捐赠物品 // 修改时间: 2019年5月9日 15点04分 public ActionResult SearchThings(string keywords, int currentPage = 1) { //获取当前用户id int usrId = (int)HttpContext.Session["usrId"]; List <string> sWords = new List <string>(); //近义词 List <string> oWords = new List <string>(); //原词 using (SSDBEntities db = new SSDBEntities()) { //查找[其他用户的][闲置的]物品 var ts = db.Things.Where(x => x.DonatorId != usrId && x.Status == "物品闲置中"); //1.地域置顶 var user = db.Users.Where(x => x.UserId == usrId).FirstOrDefault(); var pro = user.Province; var sch = user.School; var ts_u = ts.Join(db.Users.Select(x => new { x.UserId, x.Province, x.School }), t => t.DonatorId, u => u.UserId, (t, u) => new { t, u }); var tsu1 = ts_u.Where(o => o.u.Province.Contains(pro) && o.u.School.Contains(sch)).Select(n => new ThingsListViewModel { Thing = n.t, prior = 300 }).ToList(); //同省同校 var tsu2 = ts_u.Where(o => o.u.Province.Contains(pro) && !o.u.School.Contains(sch)).Select(n => new ThingsListViewModel { Thing = n.t, prior = 200 }).ToList(); //同校不同省 var tsu3 = ts_u.Where(o => !o.u.Province.Contains(pro)).Select(n => new ThingsListViewModel { Thing = n.t, prior = 100 }).ToList(); //不同省 var ts_place = tsu1.Union(tsu2).Union(tsu3); //已转化为list的视图模型,之后关键字直接foreach匹配 //2.获取同义分词 AI if (!string.IsNullOrEmpty(keywords)) { string data = TencentAI.tencentAI(keywords); oWords = TencentAI.oriWords(data); sWords = TencentAI.synWords(data); //qs = qs.Where(x => x.Name.Contains(searchString)); //关键字匹配 foreach (var l in ts_place) { if (l.Thing.Name.Contains(keywords)) { l.prior += 10; continue; } for (int i = 0; i < oWords.Count; i++) { if (l.Thing.Name.Contains(oWords[i])) { l.prior += 5; break; } } for (int j = 0; j < sWords.Count; j++) { if (l.Thing.Name.Contains(sWords[j])) { l.prior += 1; break; } } if (l.prior % 100 == 0) { l.prior -= 1000; } } } ts_place = ts_place.Where(x => x.prior >= 0); //3.分页 int totalThings = ts.Count(); //总共条目数目 ViewBag.totalPages = (int)Math.Ceiling(totalThings / (double)numPerPage); //总共页数 int start = (currentPage - 1) * numPerPage; //开始的条目 ts_place = ts_place.OrderByDescending(x => x.prior).Skip(start).Take(numPerPage); ViewBag.searchString = keywords; ViewBag.currentPage = currentPage; return(View(ts_place)); } }