ES_MemberController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Common.Model;
  9. using CoreEntity.DAL;
  10. using CoreEntity.Entity;
  11. using CoreEntity.ESEntity;
  12. using JCSoft.WX.Framework.Api;
  13. using MemberWeb.Commonss;
  14. using Microsoft.AspNetCore.Mvc;
  15. using Microsoft.Extensions.Caching.Memory;
  16. using Newtonsoft.Json;
  17. using Newtonsoft.Json.Converters;
  18. using Newtonsoft.Json.Linq;
  19. using PublicLibrary.Model;
  20. using SupplierWeb.Commonss;
  21. using ZcPeng.PublicLibrary;
  22. namespace SupplierWeb.Controllers
  23. {
  24. [Route("web/ES_Member")]
  25. public class ES_MemberController : BaseController
  26. {
  27. public ES_MemberController(IMemoryCache cache, IApiClient client) : base(cache, client)
  28. {
  29. }
  30. public IActionResult Index()
  31. {
  32. return View();
  33. }
  34. /// <summary>
  35. /// 会员中心列表
  36. /// </summary>
  37. /// <param name="data"></param>
  38. /// <param name="filters"></param>
  39. /// <returns></returns>
  40. [HttpPost]
  41. [Route("index")]
  42. public ActionResult index([FromBody]dynamic data, QueryFilter[] filters)
  43. {
  44. int start = 0;
  45. string filter = "";
  46. Object query = null;
  47. List<String> phonesSearch = new List<String>(1);
  48. Dictionary<string, List<QueryFilter>> fdic = new Dictionary<string, List<QueryFilter>>();
  49. if (data != null)
  50. {
  51. filters = data.filters.ToObject<QueryFilter[]>();
  52. if(filters !=null && filters.Count() > 0)
  53. {
  54. var j = 0;
  55. foreach(QueryFilter f in filters)
  56. {
  57. f.Index = j;
  58. if (f.field.Equals("MemUsualPhone")&&f.value!=null&&f.value!="null")
  59. {
  60. phonesSearch.Add(f.value);
  61. var phoneCr = CommonDAL.getPhoneEncrypt(phonesSearch.ToArray<String>());
  62. f.value = phoneCr[0];
  63. }
  64. if(f.field.IndexOf(".")>0 && f.value != null && f.value != null && f.value != "null")
  65. {
  66. List<QueryFilter> listF = null;
  67. fdic.TryGetValue(f.field,out listF);
  68. if (listF == null)
  69. listF = new List<QueryFilter>();
  70. else
  71. filters[j] = null;//空字段
  72. if (f.rules != null && f.rules.Count() > 0)
  73. {
  74. if (f.rules[0].value != "")
  75. listF.Add(f);
  76. }
  77. fdic[f.field] = listF;
  78. }
  79. j++;
  80. }
  81. }
  82. filter = QueryFilter.getFilterString(filters);
  83. if (data.pageIndex == 1)
  84. {
  85. start = 0;
  86. }
  87. else
  88. {
  89. start = ((data.pageIndex - 1) * data.pageSize) + 1;
  90. }
  91. query = new { query = "select * from mem_memberbase_p where 1=1 " +
  92. filter +
  93. " order by RegisterTime desc " +
  94. " limit " + data.pageSize };
  95. }
  96. //将参数转换成JSON数据
  97. var json = JsonConvert.SerializeObject(query);
  98. //通过传来的SQL语句,转换成DSL语句
  99. string sql = Util.ElasticQuery("_sql/translate?format=json", "POST", json);
  100. string from = "\"from\":" + start + ",";
  101. sql = sql.Insert(1, from);
  102. int sourceIndex = sql.IndexOf("\"_source\":{");
  103. int sourceIndexEnd = sql.IndexOf("},", sourceIndex);
  104. string sourcestr = sql.Substring(sourceIndex, sourceIndexEnd - sourceIndex + 1);
  105. sql = sql.Replace(sourcestr, "\"_source\":{\"includes\":[\"*\"], \"excludes\": []}");
  106. int fieldIndex = sql.IndexOf("\"docvalue_fields\":[");
  107. int fieldIndexEnd = sql.IndexOf("],", fieldIndex);
  108. sql = sql.Remove(fieldIndex, fieldIndexEnd - fieldIndex + 2);
  109. if (fdic.Count > 0)
  110. {
  111. foreach(var di in fdic)
  112. {
  113. if(fdic[di.Key]!=null && fdic[di.Key].Count > 0)
  114. {
  115. string[] values = new string[fdic[di.Key].Count];
  116. int k = 0;
  117. foreach(var qfilter in fdic[di.Key])
  118. {
  119. if (qfilter.rules != null && qfilter.rules.Count() > 0)
  120. {
  121. if(qfilter.rules[0].value!="")
  122. values[k] = qfilter.rules[0].value;
  123. }
  124. else
  125. {
  126. if (qfilter.value != "")
  127. values[k] = qfilter.value;
  128. }
  129. k++;
  130. }
  131. //miniMatch
  132. StringBuilder nestMinMatch = new StringBuilder(5);
  133. nestMinMatch.Append(@"{ ""bool"": { ""should"": [");
  134. foreach (var value in values) {
  135. var valuestr = "[" + string.Join(",", values) +"]";
  136. var keytoken = di.Key.Split(".");
  137. var path = keytoken[0];
  138. // string tnest = @"{
  139. // ""nested"": {
  140. // ""query"": {
  141. // ""bool"":{
  142. // ""must"":[
  143. // {
  144. // ""script"": {
  145. // ""script"": ""def match = false; for (search_array in [10,71] ) { for (sub_array in doc['Terminals.TerminalCode']){ match = ( search_array == sub_array);if( search_array == sub_array){break;} }} return match;""
  146. // }
  147. // }
  148. // ]
  149. // }
  150. // },
  151. // ""path"": ""Terminals"",
  152. // ""ignore_unmapped"": false,
  153. // ""score_mode"": ""none"",
  154. // ""boost"": 1.0
  155. // }
  156. //tnest = tnest.Replace("[10,71]", valuestr);
  157. //}";
  158. string tnest =
  159. @"{
  160. ""nested"": {
  161. ""query"": {
  162. ""bool"": {
  163. ""must"": [
  164. {
  165. ""term"": {
  166. ""Terminals.TerminalCode"": {
  167. ""value"":""6""
  168. }
  169. }
  170. }
  171. ]
  172. }
  173. },
  174. ""path"": ""Terminals"",
  175. ""ignore_unmapped"": false,
  176. ""score_mode"": ""none"",
  177. ""boost"": 1.0
  178. }
  179. },";
  180. tnest = tnest.Replace(":\"6\"", ":\"" + value+ "\"");
  181. tnest = tnest.Replace("Terminals.TerminalCode", di.Key);
  182. tnest = tnest.Replace("Terminals", path);
  183. nestMinMatch.Append(tnest);
  184. }
  185. nestMinMatch = nestMinMatch.Remove(nestMinMatch.Length-1,1);
  186. nestMinMatch.Append(@"],""minimum_should_match"": "+ values .Length+ "");
  187. nestMinMatch.Append(@"}}" );
  188. string snest = "{\"nested\":{\"query\":{\"term\":{\"Terminals.MemSourcePlatforms\":{\"value\":\"2\",\"boost\":1.0}}},\"path\":\"Terminals\",\"ignore_unmapped\":false,\"score_mode\":\"none\",\"boost\":1.0}}";
  189. var sValue = "";
  190. if (fdic[di.Key][0].rules!=null&& fdic[di.Key][0].rules.Count()>0)
  191. {
  192. sValue = fdic[di.Key][0].rules[0].value;
  193. }
  194. else
  195. {
  196. sValue = fdic[di.Key][0].value;
  197. }
  198. sql = sql.Replace(snest.Replace("Terminals.MemSourcePlatforms", di.Key).Replace("\"2\"", "\""+ sValue + "\""), nestMinMatch.ToString());
  199. }
  200. }
  201. }
  202. //通过转换的DSL语句,获取数据
  203. var str = Util.ElasticQuery("mem_memberbase_p/_search?format=json", "POST", sql);
  204. //JSON转Root对象
  205. Root<mem_MemberBase> stobj = EsCommon.FromJSON<Root<mem_MemberBase>>(str);
  206. //给filed额外的属性赋值
  207. stobj.hits.hits.ForEach(x =>
  208. {
  209. x._source._id = x._id;
  210. });
  211. //将Root对象中的子对象取出来
  212. List<mem_MemberBase> _source = new List<mem_MemberBase>();
  213. _source = stobj.hits.hits.Select(x => x.entity()).ToList();
  214. List<String> phones = new List<String>(_source.Count());
  215. int i = 0;
  216. _source.ForEach(x =>
  217. {
  218. if(x.MemUsualPhone!=null && x.MemUsualPhone.Length == 32) {
  219. phones.Add(x.MemUsualPhone);
  220. i++;
  221. }
  222. });
  223. i = 0;
  224. var phones1 = CommonDAL.getPhoneDecrypt(phones.ToArray<String>());
  225. _source.ForEach(x =>
  226. {
  227. if (x.MemUsualPhone != null && x.MemUsualPhone.Length == 32) {
  228. x.MemUsualPhoneCr = x.MemUsualPhone;
  229. string phoneStars = phones1[i];
  230. if (phoneStars != null && phoneStars.Length > 8)
  231. {
  232. string replaced = phoneStars.Substring(3, 4);
  233. phoneStars = phoneStars.Replace(replaced, "****");
  234. }
  235. x.MemUsualPhone = phoneStars;
  236. x.MemUsualPhoneOr = phones1[i];
  237. i++;
  238. }
  239. });
  240. //总数
  241. long totalcount = 0;
  242. data = new { query = "select count(1) from mem_memberbase_p where 1=1 " +
  243. filter +
  244. "" };
  245. var jsons = JsonConvert.SerializeObject(data);
  246. string obj = Util.ElasticQuery("_sql?format=json", "POST", jsons);
  247. Num num = EsCommon.FromJSON<Num>(obj);
  248. totalcount =num.rows[0][0];
  249. var jsonData = JsonConvert.SerializeObject(_source);
  250. return Json(
  251. new
  252. {
  253. items = JsonConvert.DeserializeObject(jsonData),
  254. totalCount = totalcount
  255. });
  256. }
  257. }
  258. }