123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510 |
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Threading.Tasks;
- using CoreEntity.DAL;
- using CoreEntity.Entity;
- using CoreEntity.ESEntity;
- using Elasticsearch.Net;
- using JCSoft.WX.Framework.Api;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Caching.Memory;
- using Newtonsoft.Json;
- using PublicLibrary.Model;
- using SupplierWeb.Commonss;
- using SupplierWeb.Service;
- namespace SupplierWeb.Controllers
- {
- [Route("web/ES_MemFriends")]
- public class ES_MemFriendsController : BaseController
- {
- public ES_MemFriendsController(IMemoryCache cache, IApiClient client,
- IEsClientProvider clientProvider) : base(cache, client, clientProvider)
- {
- }
- public IActionResult Index()
- {
- return View();
- }
-
- /// <summary>
- /// 会员好友列表
- /// </summary>
- /// <param name="data"></param>
- /// <param name="filters"></param>
- /// <returns></returns>
- [HttpPost]
- [Route("MemberFriends")]
- public ActionResult MemberFriends([FromBody]dynamic data, QueryFilter[] filters)
- {
- int start = 0;
- Object query = null;
- string filter = "";
- string MemberPhone = "";
- if (data != null)
- {
- filters = data.filters.ToObject<QueryFilter[]>();
- filter = QueryFilter.getFilterString(filters);
- MemberPhone = filters[0].value;
- query = new
- {
- query = "select * from mem_memberfriends where 1=1 " +
- filter
- + " order by MemberFriendsPhone " +
- " limit " + data.pageSize
- };
- }
- #region
- string sql = @"{
- ""size"": 10,
- ""query"": {
- ""bool"": {
- ""must"": [
- {
- ""term"": {
- ""MemUsualPhone"": {
- ""value"": ""{@MemUsualPhone}"",
- ""boost"": 1
- }
- }
- },
- {
- ""nested"": {
- ""query"": {
- ""bool"": {
- ""must_not"": [
- {
- ""term"": {
- ""Friends.MemberFriendsPhone"": {
- ""value"": ""{@MemUsualPhone}"",
- ""boost"": 1
- }
- }
- }
- ],
- ""adjust_pure_negative"": true,
- ""boost"": 1
- }
- },
- ""path"": ""Friends"",
- ""ignore_unmapped"": false,
- ""score_mode"": ""none"",
- ""boost"": 1,
- ""inner_hits"": {
- ""ignore_unmapped"": false,
- ""from"": {@from},
- ""size"": {@size},
- ""version"": false,
- ""seq_no_primary_term"": false,
- ""explain"": false,
- ""track_scores"": false,
- ""_source"": {
- ""includes"": [
- ""Friends.*""
- ],
- ""excludes"": []
- }
- }
- }
- }
- ],
- ""adjust_pure_negative"": true,
- ""boost"": 1
- }
- },
- ""_source"": false,
- ""stored_fields"": ""_none_"",
- ""docvalue_fields"": [
- {
- ""field"": ""MemUsualPhone""
- },
- {
- ""field"": ""MemberNickName""
- }
- ],
- ""sort"": [
- {
- ""_doc"": {
- ""order"": ""asc""
- }
- }
- ]
- }";
- //通过转换的DSL语句,获取数据
- sql = sql.Replace("{@from}", "" + start).Replace("{@size}", "" + data.pageSize)
- .Replace("{@MemUsualPhone}", MemberPhone);
- PostData dsl = sql;
- var result = _LowLevelclient.Search<StringResponse>("mem_memberbase_p", dsl);
- var DynamicObject = JsonConvert.DeserializeObject<dynamic>(result.Body);
- List<dynamic> source = new List<dynamic>();
- //总数
- long totalCount = 0;
- if (DynamicObject.hits.hits.Count > 0) {
- HitsItem<dynamic>[] inerhits = DynamicObject.hits.hits[0].inner_hits.Friends.hits.hits.ToObject<HitsItem<dynamic>[]>();
-
- foreach(HitsItem<dynamic> o in inerhits)
- {
- string[] phones = new string[1];
- phones[0] = o._source.MemberFriendsPhone;
- var phones1 = CommonDAL.getPhoneDecrypt(phones);
- string phoneStars = phones1[0];
- if (phoneStars != null && phoneStars.Length > 8)
- {
- string replaced = phoneStars.Substring(3, 4);
- phoneStars = phoneStars.Replace(replaced, "****");
- }
- o._source.MemberFriendsPhoneCr = o._source.MemberFriendsPhone;
- o._source.MemberFriendsPhone = phoneStars;
- o._source.MemberFriendsPhoneOr = phones1[0];
- o._source._id = o._id;
- source.Add(o._source);
- }
- totalCount = DynamicObject.hits.hits[0].inner_hits.Friends.hits.total.value;
- }
- #endregion
- //FriendID = GetFriends(_source.ToArray());
- //query1 = new { query = "select * from mem_memberbase_p where MemUsualPhone " +
- // "in ('" + FriendID + "')"
- //};
- //var json1 = JsonConvert.SerializeObject(query1);
- ////通过传来的SQL语句,转换成DSL语句
- //string sql1 = Util.ElasticQuery("_sql/translate?format=json", "POST", json1);
- ////通过转换的DSL语句,获取数据
- //var str1 = Util.ElasticQuery("mem_memberbase_p/_search?format=json", "POST", sql1);
- ////JSON转Root对象
- //Root<mem_MemberBase> stobj1 = EsCommon.FromJSON<Root<mem_MemberBase>>(str1);
- ////给filed额外的属性赋值
- //stobj1.hits.hits.ForEach(x =>
- //{
- // x.fields._id = x._id;
- //});
- ////将Root对象中的子对象取出来
- //List<mem_MemberBase> _source1 = new List<mem_MemberBase>();
- //_source1 = stobj1.hits.hits.Select(x => x.entity()).ToList();
- //_source.ForEach(x => {
- // _source1.ForEach(y => {
- // if (x.MemberFriendsPhone.Equals(y.MemUsualPhone))
- // {
- // x.MemberFriendNickName = y.MemberNickName;
- // x.MemberFriendRealName = y.MemberNickName;
- // }
- // });
- //});
- return Json(
- new
- {
- items = source,
- totalCount = totalCount
- });
- }
- // /// <summary>
- // /// JSON转对象
- // /// </summary>
- // /// <typeparam name="T"></typeparam>
- // /// <param name="input"></param>
- // /// <returns></returns>
- // public static T FromJSON<T>(string input)
- // {
- // try
- // {
- // return JsonConvert.DeserializeObject<T>(input);
- // }
- // catch (Exception ex)
- // {
- // string a = ex.Message;
- // return default(T);
- // }
- // }
- /// <summary>
- /// 根据电话号码,获取他所有的订单编号
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public string GetFriends(mem_MemberFriends[] data)
- {
- string code = "";
- for (int i = data.Count() - 1; i >= 0; i--)
- {
- if (i > 0)
- {
- code += "" + data[i].MemberFriendsPhone + "','";
- }
- else
- {
- code += "" + data[i].MemberFriendsPhone + "";
- }
- }
- return code;
- }
- // #region
- // public class _shards
- // {
- // /// <summary>
- // ///
- // /// </summary>
- // public int total { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public int successful { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public int skipped { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public int failed { get; set; }
- // }
- // public class Total
- // {
- // /// <summary>
- // ///
- // /// </summary>
- // public int value { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public string relation { get; set; }
- // }
- // public class _source
- // {
- // /// <summary>
- // ///
- // /// </summary>
- // public string MemDesc { get; set; }
- // }
- // public class Fields
- // {
- // public string _id { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<int> OriginType { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> RegisterYear { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemUsualProvinceCode { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemberNickName { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> HasOrder { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemEmail { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemUsualAddress { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemUsualPhone { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemberId { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> RegisterTime { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<int> MemberGender { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemberHeaPic { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<int> MemberKey { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> RegisterDay { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> OrdersCode { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemUsualCityCode { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> RegisterMonth { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemPhone1 { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemPhone2 { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<string> MemPhone3 { get; set; }
- // }
- // public class HitsItem
- // {
- // /// <summary>
- // ///
- // /// </summary>
- // public string _index { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public string _type { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public string _id { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public string _score { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public _source _source { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public Fields fields { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<int> sort { get; set; }
- // }
- // public class Hits
- // {
- // /// <summary>
- // ///
- // /// </summary>
- // public Total total { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public string max_score { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<HitsItem> hits { get; set; }
- // }
- // public class Root
- // {
- // /// <summary>
- // ///
- // /// </summary>
- // public int took { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public string timed_out { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public _shards _shards { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public Hits hits { get; set; }
- // }
- // #endregion
- // #region Friend实体类
- // public class Friend
- // {
- // /// <summary>
- // ///
- // /// </summary>
- // public string name { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public string type { get; set; }
- // }
- // public class FriendCode
- // {
- // /// <summary>
- // ///
- // /// </summary>
- // public List<Friend> columns { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<List<string>> rows { get; set; }
- // }
- // #endregion
- // #region
- // public class ColumnsItem
- // {
- // /// <summary>
- // ///
- // /// </summary>
- // public string name { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public string type { get; set; }
- // }
- // public class Num
- // {
- // /// <summary>
- // ///
- // /// </summary>
- // public List<ColumnsItem> columns { get; set; }
- // /// <summary>
- // ///
- // /// </summary>
- // public List<List<int>> rows { get; set; }
- // }
- // #endregion
- //
- }
- }
|