Mem_memberinformationController.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 Nest;
  14. using Newtonsoft.Json;
  15. using PublicLibrary.Model;
  16. using SupplierWeb.Commonss;
  17. using SupplierWeb.Service;
  18. namespace SupplierWeb.Controllers
  19. {
  20. [Route("web/Mem_memberinformation")]
  21. public class Mem_memberinformationController : BaseController
  22. {
  23. public Mem_memberinformationController(IMemoryCache cache, IApiClient client,
  24. IEsClientProvider clientProvider) : base(cache, client, clientProvider)
  25. {
  26. }
  27. public IActionResult Index()
  28. {
  29. return View();
  30. }
  31. /// <summary>
  32. /// 会员中心列表
  33. /// </summary>
  34. /// <param name="data"></param>
  35. /// <param name="filters"></param>
  36. /// <returns></returns>
  37. [HttpPost]
  38. [Route("index")]
  39. public ActionResult index([FromBody]dynamic data, QueryFilter[] filters)
  40. {
  41. int start = 0;
  42. string MemberPhone = "";
  43. if (data != null)
  44. {
  45. MemberPhone = data.query;
  46. data = new { query = "select * from mem_memberbase_p where MemUsualPhone='"+ MemberPhone+"'" };
  47. }
  48. //将参数转换成JSON数据
  49. var json = JsonConvert.SerializeObject(data);
  50. //通过传来的SQL语句,转换成DSL语句
  51. string sql = Util.ElasticQuery("_sql/translate?format=json", "POST", json);
  52. //TranslateSqlResponse result = _LowLevelclient.Sql.Translate<TranslateSqlResponse>(PostData.String(json));
  53. //string sql = result.ApiCall.ResponseBodyInBytes();
  54. string from = "\"from\":" + start + ",";
  55. sql = sql.Insert(1, from);
  56. //通过转换的DSL语句,获取数据
  57. //var str = Util.ElasticQuery("mem_memberbase_p/_search?format=json", "POST", sql);
  58. int sourceIndex = sql.IndexOf("\"_source\":{");
  59. int sourceIndexEnd = sql.IndexOf("},", sourceIndex);
  60. string sourcestr = sql.Substring(sourceIndex, sourceIndexEnd - sourceIndex + 1);
  61. sql = sql.Replace(sourcestr, "\"_source\":{\"includes\":[\"*\"], \"excludes\": []}");
  62. int fieldIndex = sql.IndexOf("\"docvalue_fields\":[");
  63. int fieldIndexEnd = sql.IndexOf("],", fieldIndex);
  64. sql = sql.Remove(fieldIndex, fieldIndexEnd - fieldIndex + 2);
  65. PostData dsl = sql;
  66. var result = _LowLevelclient.Search<StringResponse>("mem_memberbase_p", dsl);
  67. var DynamicObject = JsonConvert.DeserializeObject<dynamic>(result.Body);
  68. List<dynamic> source = new List<dynamic>();
  69. //总数
  70. long totalCount = 0;
  71. //JSON转Root对象
  72. //将Root对象中的子对象取出来
  73. if (DynamicObject.hits.hits.Count > 0)
  74. {
  75. HitsItem<dynamic>[] hits = DynamicObject.hits.hits.ToObject<HitsItem<dynamic>[]>();
  76. //List<mem_MemberTerminal> _source = new List<mem_MemberTerminal>(0);
  77. //_source = inerhits.Select(x => x.entity()).ToList();
  78. foreach (HitsItem<dynamic> o in hits)
  79. {
  80. string[] phones = new string[1];
  81. phones[0] = o._source.MemUsualPhone;
  82. var phones1 = CommonDAL.getPhoneDecrypt(phones);
  83. string phoneStars = phones1[0];
  84. if (phoneStars != null && phoneStars.Length > 8) {
  85. string replaced = phoneStars.Substring(3,4);
  86. phoneStars = phoneStars.Replace(replaced,"****");
  87. }
  88. o._source.MemUsualPhoneCr = o._source.MemUsualPhone;
  89. o._source.MemUsualPhone = phoneStars;
  90. o._source.MemUsualPhoneOr = phones1[0];
  91. o._source._id = o._id;
  92. source.Add(o._source);
  93. }
  94. totalCount = DynamicObject.hits.total.value;
  95. //var jsonData = JsonConvert.SerializeObject(_source);
  96. }
  97. return Json(
  98. new
  99. {
  100. items = source,
  101. temsCount = totalCount,
  102. success =true
  103. });
  104. }
  105. [HttpPost]
  106. [Route("EditMemberBase")]
  107. public ActionResult EditMemberBase([FromBody]dynamic data)
  108. {
  109. var success = false;
  110. string docId = data._id;
  111. data.Remove("_id");
  112. data.MemUsualPhone = data.MemUsualPhoneCr;
  113. data.Remove("MemUsualPhoneOr");
  114. data.Remove("MemUsualPhoneCr");
  115. Dictionary<string, Object> doc = null;
  116. if (data.updates !=null && data.updates.Count>0)
  117. {
  118. docId = data.updates[0]._id;
  119. //var response = _esclient.Get<dynamic>(docId, idx => idx.Index("mem_memberbase_p")); // returns an IGetResponse mapped 1-to-1 with the Elasticsearch JSON response
  120. //var Source = response.Source; // the original document
  121. Dictionary<string, Object> memdoc = new Dictionary<string, Object>();
  122. foreach(var data1 in data.updates) {
  123. if (data1.update == "true" && data1.updateField != null
  124. && data1.updateFieldValue != null) {
  125. string updateField = data1.updateField;
  126. string updateFieldValue = data1.updateFieldValue;
  127. memdoc[updateField] = updateFieldValue;
  128. }
  129. }
  130. doc = memdoc;
  131. }
  132. else
  133. {
  134. doc = data.ToObject<Dictionary<string, Object>>();
  135. }
  136. data["LastUpdatedDate"] = DateTime.UtcNow;
  137. var jsons = JsonConvert.SerializeObject(new
  138. {
  139. doc = doc
  140. }
  141. );
  142. var param = new UpdateRequestParameters();
  143. param.Refresh = Refresh.True;
  144. var Response = _LowLevelclient.Update<StringResponse>("mem_memberbase_p", docId, jsons, param);
  145. dynamic t = JsonConvert.DeserializeObject(
  146. Response.Body
  147. );
  148. if (t.result == "updated")
  149. {
  150. success = true;
  151. }
  152. else
  153. {
  154. success = false;
  155. }
  156. return Json(
  157. new
  158. {
  159. msg = "",
  160. success = success
  161. });
  162. }
  163. }
  164. }