mem_memberhealthController.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_memberhealth")]
  21. public class mem_memberhealthController : BaseController
  22. {
  23. public mem_memberhealthController(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. Object query = null;
  42. if (data != null)
  43. {
  44. query = new { query = "select PatientId,PatientKey,PatientName,PatientAge,PatientBirthDay,PatientMariageAt,PatientHeight,PatientWeight,PatientGender,PatientPhone from mem_memberbase_p where MemUsualPhone = '" + data .MemberPhone+ "'" };
  45. }
  46. //将参数转换成JSON数据
  47. var json = JsonConvert.SerializeObject(query);
  48. //通过传来的SQL语句,转换成DSL语句
  49. string sql = Util.ElasticQuery("_sql/translate?format=json", "POST", json);
  50. sql = sql.Replace("MemberId.keyword\":", "_id\":");
  51. int sourceIndex = sql.IndexOf("\"_source\":{");
  52. int sourceIndexEnd = sql.IndexOf("},", sourceIndex);
  53. string sourcestr = sql.Substring(sourceIndex, sourceIndexEnd - sourceIndex + 1);
  54. sql = sql.Replace(sourcestr, "\"_source\":{\"includes\":[\"PatientId\",\"PatientKey\",\"PatientName\",\"PatientAge\",\"PatientBirthDay\",\"PatientMariageAt\",\"PatientHeight\",\"PatientWeight\",\"PatientGender\",\"PatientPhone\"], \"excludes\": []}");
  55. int fieldIndex = sql.IndexOf("\"docvalue_fields\":[");
  56. int fieldIndexEnd = sql.IndexOf("],", fieldIndex);
  57. sql = sql.Remove(fieldIndex, fieldIndexEnd - fieldIndex + 2);
  58. //通过转换的DSL语句,获取数据
  59. PostData dsl = sql;
  60. var result = _LowLevelclient.Search<StringResponse>("mem_memberbase_p", dsl);
  61. var DynamicObject = JsonConvert.DeserializeObject<dynamic>(result.Body);
  62. List<dynamic> source = new List<dynamic>();
  63. //总数
  64. long totalCount = 0;
  65. //JSON转Root对象
  66. //将Root对象中的子对象取出来
  67. if (DynamicObject.hits.hits.Count > 0)
  68. {
  69. HitsItem<dynamic>[] inerhits = DynamicObject.hits.hits.ToObject<HitsItem<dynamic>[]>();
  70. //List<mem_MemberTerminal> _source = new List<mem_MemberTerminal>(0);
  71. //_source = inerhits.Select(x => x.entity()).ToList();
  72. foreach (HitsItem<dynamic> o in inerhits)
  73. {
  74. string[] phones = new string[1];
  75. phones[0] = o._source.PatientPhone;
  76. var phones1 = CommonDAL.getPhoneDecrypt(phones);
  77. string phoneStars = phones1[0];
  78. if (phoneStars!=null && phoneStars.Length > 8)
  79. {
  80. string replaced = phoneStars.Substring(3, 4);
  81. phoneStars = phoneStars.Replace(replaced, "****");
  82. }
  83. o._source.PatientPhoneCr = o._source.PatientPhone;
  84. o._source.PatientPhone = phoneStars;
  85. o._source.PatientPhoneOr = phones1[0];
  86. o._source._id = o._id;
  87. source.Add(o._source);
  88. }
  89. totalCount = DynamicObject.hits.total.value;
  90. //var jsonData = JsonConvert.SerializeObject(_source);
  91. }
  92. return Json(
  93. new
  94. {
  95. items = source,
  96. success=true
  97. });
  98. }
  99. [HttpPost]
  100. [Route("EditMemberHealthDoc")]
  101. public ActionResult EditMemberHealthDoc([FromBody]dynamic data)
  102. {
  103. var success = false;
  104. string docId = data._id;
  105. data.Remove("_id");
  106. Dictionary<string, Object> doc = null;
  107. if (data.update == "true" && data.updateField != null
  108. && data.updateFieldValue != null)
  109. {
  110. var response = _esclient.Get<dynamic>(docId, idx => idx.Index("mem_memberbase_p")); // returns an IGetResponse mapped 1-to-1 with the Elasticsearch JSON response
  111. var Source = response.Source; // the original document
  112. Dictionary<string, Object> memdoc = new Dictionary<string, Object>();
  113. string updateField = data.updateField;
  114. string updateFieldValue = data.updateFieldValue;
  115. memdoc[updateField] = updateFieldValue;
  116. doc = memdoc;
  117. }
  118. else
  119. {
  120. doc = data.ToObject<Dictionary<string, Object>>();
  121. }
  122. data["LastUpdatedDate"] = DateTime.UtcNow;
  123. var jsons = JsonConvert.SerializeObject(new
  124. {
  125. doc = doc
  126. }
  127. );
  128. var param = new UpdateRequestParameters();
  129. param.Refresh = Refresh.True;
  130. var Response = _LowLevelclient.Update<StringResponse>("mem_memberbase_p", docId, jsons, param);
  131. dynamic t = JsonConvert.DeserializeObject(
  132. Response.Body
  133. );
  134. if (t.result == "updated")
  135. {
  136. success = true;
  137. }
  138. else
  139. {
  140. success = false;
  141. }
  142. return Json(
  143. new
  144. {
  145. msg = "",
  146. success = success
  147. });
  148. }
  149. }
  150. }