ES_MemFriendsController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using CoreEntity.DAL;
  7. using CoreEntity.Entity;
  8. using CoreEntity.ESEntity;
  9. using Elasticsearch.Net;
  10. using JCSoft.WX.Framework.Api;
  11. using Microsoft.AspNetCore.Mvc;
  12. using Microsoft.Extensions.Caching.Memory;
  13. using Newtonsoft.Json;
  14. using PublicLibrary.Model;
  15. using SupplierWeb.Commonss;
  16. using SupplierWeb.Service;
  17. namespace SupplierWeb.Controllers
  18. {
  19. [Route("web/ES_MemFriends")]
  20. public class ES_MemFriendsController : BaseController
  21. {
  22. public ES_MemFriendsController(IMemoryCache cache, IApiClient client,
  23. IEsClientProvider clientProvider) : base(cache, client, clientProvider)
  24. {
  25. }
  26. public IActionResult Index()
  27. {
  28. return View();
  29. }
  30. /// <summary>
  31. /// 会员好友列表
  32. /// </summary>
  33. /// <param name="data"></param>
  34. /// <param name="filters"></param>
  35. /// <returns></returns>
  36. [HttpPost]
  37. [Route("MemberFriends")]
  38. public ActionResult MemberFriends([FromBody]dynamic data, QueryFilter[] filters)
  39. {
  40. int start = 0;
  41. Object query = null;
  42. string filter = "";
  43. string MemberPhone = "";
  44. if (data != null)
  45. {
  46. filters = data.filters.ToObject<QueryFilter[]>();
  47. filter = QueryFilter.getFilterString(filters);
  48. MemberPhone = filters[0].value;
  49. query = new
  50. {
  51. query = "select * from mem_memberfriends where 1=1 " +
  52. filter
  53. + " order by MemberFriendsPhone " +
  54. " limit " + data.pageSize
  55. };
  56. }
  57. #region
  58. string sql = @"{
  59. ""size"": 10,
  60. ""query"": {
  61. ""bool"": {
  62. ""must"": [
  63. {
  64. ""term"": {
  65. ""MemUsualPhone"": {
  66. ""value"": ""{@MemUsualPhone}"",
  67. ""boost"": 1
  68. }
  69. }
  70. },
  71. {
  72. ""nested"": {
  73. ""query"": {
  74. ""bool"": {
  75. ""must_not"": [
  76. {
  77. ""term"": {
  78. ""Friends.MemberFriendsPhone"": {
  79. ""value"": ""{@MemUsualPhone}"",
  80. ""boost"": 1
  81. }
  82. }
  83. }
  84. ],
  85. ""adjust_pure_negative"": true,
  86. ""boost"": 1
  87. }
  88. },
  89. ""path"": ""Friends"",
  90. ""ignore_unmapped"": false,
  91. ""score_mode"": ""none"",
  92. ""boost"": 1,
  93. ""inner_hits"": {
  94. ""ignore_unmapped"": false,
  95. ""from"": {@from},
  96. ""size"": {@size},
  97. ""version"": false,
  98. ""seq_no_primary_term"": false,
  99. ""explain"": false,
  100. ""track_scores"": false,
  101. ""_source"": {
  102. ""includes"": [
  103. ""Friends.*""
  104. ],
  105. ""excludes"": []
  106. }
  107. }
  108. }
  109. }
  110. ],
  111. ""adjust_pure_negative"": true,
  112. ""boost"": 1
  113. }
  114. },
  115. ""_source"": false,
  116. ""stored_fields"": ""_none_"",
  117. ""docvalue_fields"": [
  118. {
  119. ""field"": ""MemUsualPhone""
  120. },
  121. {
  122. ""field"": ""MemberNickName""
  123. }
  124. ],
  125. ""sort"": [
  126. {
  127. ""_doc"": {
  128. ""order"": ""asc""
  129. }
  130. }
  131. ]
  132. }";
  133. //通过转换的DSL语句,获取数据
  134. sql = sql.Replace("{@from}", "" + start).Replace("{@size}", "" + data.pageSize)
  135. .Replace("{@MemUsualPhone}", MemberPhone);
  136. PostData dsl = sql;
  137. var result = _LowLevelclient.Search<StringResponse>("mem_memberbase_p", dsl);
  138. var DynamicObject = JsonConvert.DeserializeObject<dynamic>(result.Body);
  139. List<dynamic> source = new List<dynamic>();
  140. //总数
  141. long totalCount = 0;
  142. if (DynamicObject.hits.hits.Count > 0) {
  143. HitsItem<dynamic>[] inerhits = DynamicObject.hits.hits[0].inner_hits.Friends.hits.hits.ToObject<HitsItem<dynamic>[]>();
  144. foreach(HitsItem<dynamic> o in inerhits)
  145. {
  146. string[] phones = new string[1];
  147. phones[0] = o._source.MemberFriendsPhone;
  148. var phones1 = CommonDAL.getPhoneDecrypt(phones);
  149. string phoneStars = phones1[0];
  150. if (phoneStars != null && phoneStars.Length > 8)
  151. {
  152. string replaced = phoneStars.Substring(3, 4);
  153. phoneStars = phoneStars.Replace(replaced, "****");
  154. }
  155. o._source.MemberFriendsPhoneCr = o._source.MemberFriendsPhone;
  156. o._source.MemberFriendsPhone = phoneStars;
  157. o._source.MemberFriendsPhoneOr = phones1[0];
  158. o._source._id = o._id;
  159. source.Add(o._source);
  160. }
  161. totalCount = DynamicObject.hits.hits[0].inner_hits.Friends.hits.total.value;
  162. }
  163. #endregion
  164. //FriendID = GetFriends(_source.ToArray());
  165. //query1 = new { query = "select * from mem_memberbase_p where MemUsualPhone " +
  166. // "in ('" + FriendID + "')"
  167. //};
  168. //var json1 = JsonConvert.SerializeObject(query1);
  169. ////通过传来的SQL语句,转换成DSL语句
  170. //string sql1 = Util.ElasticQuery("_sql/translate?format=json", "POST", json1);
  171. ////通过转换的DSL语句,获取数据
  172. //var str1 = Util.ElasticQuery("mem_memberbase_p/_search?format=json", "POST", sql1);
  173. ////JSON转Root对象
  174. //Root<mem_MemberBase> stobj1 = EsCommon.FromJSON<Root<mem_MemberBase>>(str1);
  175. ////给filed额外的属性赋值
  176. //stobj1.hits.hits.ForEach(x =>
  177. //{
  178. // x.fields._id = x._id;
  179. //});
  180. ////将Root对象中的子对象取出来
  181. //List<mem_MemberBase> _source1 = new List<mem_MemberBase>();
  182. //_source1 = stobj1.hits.hits.Select(x => x.entity()).ToList();
  183. //_source.ForEach(x => {
  184. // _source1.ForEach(y => {
  185. // if (x.MemberFriendsPhone.Equals(y.MemUsualPhone))
  186. // {
  187. // x.MemberFriendNickName = y.MemberNickName;
  188. // x.MemberFriendRealName = y.MemberNickName;
  189. // }
  190. // });
  191. //});
  192. return Json(
  193. new
  194. {
  195. items = source,
  196. totalCount = totalCount
  197. });
  198. }
  199. // /// <summary>
  200. // /// JSON转对象
  201. // /// </summary>
  202. // /// <typeparam name="T"></typeparam>
  203. // /// <param name="input"></param>
  204. // /// <returns></returns>
  205. // public static T FromJSON<T>(string input)
  206. // {
  207. // try
  208. // {
  209. // return JsonConvert.DeserializeObject<T>(input);
  210. // }
  211. // catch (Exception ex)
  212. // {
  213. // string a = ex.Message;
  214. // return default(T);
  215. // }
  216. // }
  217. /// <summary>
  218. /// 根据电话号码,获取他所有的订单编号
  219. /// </summary>
  220. /// <param name="data"></param>
  221. /// <returns></returns>
  222. public string GetFriends(mem_MemberFriends[] data)
  223. {
  224. string code = "";
  225. for (int i = data.Count() - 1; i >= 0; i--)
  226. {
  227. if (i > 0)
  228. {
  229. code += "" + data[i].MemberFriendsPhone + "','";
  230. }
  231. else
  232. {
  233. code += "" + data[i].MemberFriendsPhone + "";
  234. }
  235. }
  236. return code;
  237. }
  238. // #region
  239. // public class _shards
  240. // {
  241. // /// <summary>
  242. // ///
  243. // /// </summary>
  244. // public int total { get; set; }
  245. // /// <summary>
  246. // ///
  247. // /// </summary>
  248. // public int successful { get; set; }
  249. // /// <summary>
  250. // ///
  251. // /// </summary>
  252. // public int skipped { get; set; }
  253. // /// <summary>
  254. // ///
  255. // /// </summary>
  256. // public int failed { get; set; }
  257. // }
  258. // public class Total
  259. // {
  260. // /// <summary>
  261. // ///
  262. // /// </summary>
  263. // public int value { get; set; }
  264. // /// <summary>
  265. // ///
  266. // /// </summary>
  267. // public string relation { get; set; }
  268. // }
  269. // public class _source
  270. // {
  271. // /// <summary>
  272. // ///
  273. // /// </summary>
  274. // public string MemDesc { get; set; }
  275. // }
  276. // public class Fields
  277. // {
  278. // public string _id { get; set; }
  279. // /// <summary>
  280. // ///
  281. // /// </summary>
  282. // public List<int> OriginType { get; set; }
  283. // /// <summary>
  284. // ///
  285. // /// </summary>
  286. // public List<string> RegisterYear { get; set; }
  287. // /// <summary>
  288. // ///
  289. // /// </summary>
  290. // public List<string> MemUsualProvinceCode { get; set; }
  291. // /// <summary>
  292. // ///
  293. // /// </summary>
  294. // public List<string> MemberNickName { get; set; }
  295. // /// <summary>
  296. // ///
  297. // /// </summary>
  298. // public List<string> HasOrder { get; set; }
  299. // /// <summary>
  300. // ///
  301. // /// </summary>
  302. // public List<string> MemEmail { get; set; }
  303. // /// <summary>
  304. // ///
  305. // /// </summary>
  306. // public List<string> MemUsualAddress { get; set; }
  307. // /// <summary>
  308. // ///
  309. // /// </summary>
  310. // public List<string> MemUsualPhone { get; set; }
  311. // /// <summary>
  312. // ///
  313. // /// </summary>
  314. // public List<string> MemberId { get; set; }
  315. // /// <summary>
  316. // ///
  317. // /// </summary>
  318. // public List<string> RegisterTime { get; set; }
  319. // /// <summary>
  320. // ///
  321. // /// </summary>
  322. // public List<int> MemberGender { get; set; }
  323. // /// <summary>
  324. // ///
  325. // /// </summary>
  326. // public List<string> MemberHeaPic { get; set; }
  327. // /// <summary>
  328. // ///
  329. // /// </summary>
  330. // public List<int> MemberKey { get; set; }
  331. // /// <summary>
  332. // ///
  333. // /// </summary>
  334. // public List<string> RegisterDay { get; set; }
  335. // /// <summary>
  336. // ///
  337. // /// </summary>
  338. // public List<string> OrdersCode { get; set; }
  339. // /// <summary>
  340. // ///
  341. // /// </summary>
  342. // public List<string> MemUsualCityCode { get; set; }
  343. // /// <summary>
  344. // ///
  345. // /// </summary>
  346. // public List<string> RegisterMonth { get; set; }
  347. // /// <summary>
  348. // ///
  349. // /// </summary>
  350. // public List<string> MemPhone1 { get; set; }
  351. // /// <summary>
  352. // ///
  353. // /// </summary>
  354. // public List<string> MemPhone2 { get; set; }
  355. // /// <summary>
  356. // ///
  357. // /// </summary>
  358. // public List<string> MemPhone3 { get; set; }
  359. // }
  360. // public class HitsItem
  361. // {
  362. // /// <summary>
  363. // ///
  364. // /// </summary>
  365. // public string _index { get; set; }
  366. // /// <summary>
  367. // ///
  368. // /// </summary>
  369. // public string _type { get; set; }
  370. // /// <summary>
  371. // ///
  372. // /// </summary>
  373. // public string _id { get; set; }
  374. // /// <summary>
  375. // ///
  376. // /// </summary>
  377. // public string _score { get; set; }
  378. // /// <summary>
  379. // ///
  380. // /// </summary>
  381. // public _source _source { get; set; }
  382. // /// <summary>
  383. // ///
  384. // /// </summary>
  385. // public Fields fields { get; set; }
  386. // /// <summary>
  387. // ///
  388. // /// </summary>
  389. // public List<int> sort { get; set; }
  390. // }
  391. // public class Hits
  392. // {
  393. // /// <summary>
  394. // ///
  395. // /// </summary>
  396. // public Total total { get; set; }
  397. // /// <summary>
  398. // ///
  399. // /// </summary>
  400. // public string max_score { get; set; }
  401. // /// <summary>
  402. // ///
  403. // /// </summary>
  404. // public List<HitsItem> hits { get; set; }
  405. // }
  406. // public class Root
  407. // {
  408. // /// <summary>
  409. // ///
  410. // /// </summary>
  411. // public int took { get; set; }
  412. // /// <summary>
  413. // ///
  414. // /// </summary>
  415. // public string timed_out { get; set; }
  416. // /// <summary>
  417. // ///
  418. // /// </summary>
  419. // public _shards _shards { get; set; }
  420. // /// <summary>
  421. // ///
  422. // /// </summary>
  423. // public Hits hits { get; set; }
  424. // }
  425. // #endregion
  426. // #region Friend实体类
  427. // public class Friend
  428. // {
  429. // /// <summary>
  430. // ///
  431. // /// </summary>
  432. // public string name { get; set; }
  433. // /// <summary>
  434. // ///
  435. // /// </summary>
  436. // public string type { get; set; }
  437. // }
  438. // public class FriendCode
  439. // {
  440. // /// <summary>
  441. // ///
  442. // /// </summary>
  443. // public List<Friend> columns { get; set; }
  444. // /// <summary>
  445. // ///
  446. // /// </summary>
  447. // public List<List<string>> rows { get; set; }
  448. // }
  449. // #endregion
  450. // #region
  451. // public class ColumnsItem
  452. // {
  453. // /// <summary>
  454. // ///
  455. // /// </summary>
  456. // public string name { get; set; }
  457. // /// <summary>
  458. // ///
  459. // /// </summary>
  460. // public string type { get; set; }
  461. // }
  462. // public class Num
  463. // {
  464. // /// <summary>
  465. // ///
  466. // /// </summary>
  467. // public List<ColumnsItem> columns { get; set; }
  468. // /// <summary>
  469. // ///
  470. // /// </summary>
  471. // public List<List<int>> rows { get; set; }
  472. // }
  473. // #endregion
  474. //
  475. }
  476. }