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 Nest;
using Newtonsoft.Json;
using PublicLibrary.Model;
using SupplierWeb.Commonss;
using SupplierWeb.Service;
namespace SupplierWeb.Controllers
{
[Route("web/mem_memberhealth")]
public class mem_memberhealthController : BaseController
{
public mem_memberhealthController(IMemoryCache cache, IApiClient client,
IEsClientProvider clientProvider) : base(cache, client, clientProvider)
{
}
public IActionResult Index()
{
return View();
}
///
/// 会员中心列表
///
///
///
///
[HttpPost]
[Route("index")]
public ActionResult index([FromBody]dynamic data, QueryFilter[] filters)
{
Object query = null;
if (data != null)
{
query = new { query = "select PatientId,PatientKey,PatientName,PatientAge,PatientBirthDay,PatientMariageAt,PatientHeight,PatientWeight,PatientGender,PatientPhone from mem_memberbase_p where MemUsualPhone = '" + data .MemberPhone+ "'" };
}
//将参数转换成JSON数据
var json = JsonConvert.SerializeObject(query);
//通过传来的SQL语句,转换成DSL语句
string sql = Util.ElasticQuery("_sql/translate?format=json", "POST", json);
sql = sql.Replace("MemberId.keyword\":", "_id\":");
int sourceIndex = sql.IndexOf("\"_source\":{");
int sourceIndexEnd = sql.IndexOf("},", sourceIndex);
string sourcestr = sql.Substring(sourceIndex, sourceIndexEnd - sourceIndex + 1);
sql = sql.Replace(sourcestr, "\"_source\":{\"includes\":[\"PatientId\",\"PatientKey\",\"PatientName\",\"PatientAge\",\"PatientBirthDay\",\"PatientMariageAt\",\"PatientHeight\",\"PatientWeight\",\"PatientGender\",\"PatientPhone\"], \"excludes\": []}");
int fieldIndex = sql.IndexOf("\"docvalue_fields\":[");
int fieldIndexEnd = sql.IndexOf("],", fieldIndex);
sql = sql.Remove(fieldIndex, fieldIndexEnd - fieldIndex + 2);
//通过转换的DSL语句,获取数据
PostData dsl = sql;
var result = _LowLevelclient.Search("mem_memberbase_p", dsl);
var DynamicObject = JsonConvert.DeserializeObject(result.Body);
List source = new List();
//总数
long totalCount = 0;
//JSON转Root对象
//将Root对象中的子对象取出来
if (DynamicObject.hits.hits.Count > 0)
{
HitsItem[] inerhits = DynamicObject.hits.hits.ToObject[]>();
//List _source = new List(0);
//_source = inerhits.Select(x => x.entity()).ToList();
foreach (HitsItem o in inerhits)
{
string[] phones = new string[1];
phones[0] = o._source.PatientPhone;
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.PatientPhoneCr = o._source.PatientPhone;
o._source.PatientPhone = phoneStars;
o._source.PatientPhoneOr = phones1[0];
o._source._id = o._id;
source.Add(o._source);
}
totalCount = DynamicObject.hits.total.value;
//var jsonData = JsonConvert.SerializeObject(_source);
}
return Json(
new
{
items = source,
success=true
});
}
[HttpPost]
[Route("EditMemberHealthDoc")]
public ActionResult EditMemberHealthDoc([FromBody]dynamic data)
{
var success = false;
string docId = data._id;
data.Remove("_id");
Dictionary doc = null;
if (data.update == "true" && data.updateField != null
&& data.updateFieldValue != null)
{
var response = _esclient.Get(docId, idx => idx.Index("mem_memberbase_p")); // returns an IGetResponse mapped 1-to-1 with the Elasticsearch JSON response
var Source = response.Source; // the original document
Dictionary memdoc = new Dictionary();
string updateField = data.updateField;
string updateFieldValue = data.updateFieldValue;
memdoc[updateField] = updateFieldValue;
doc = memdoc;
}
else
{
doc = data.ToObject>();
}
data["LastUpdatedDate"] = DateTime.UtcNow;
var jsons = JsonConvert.SerializeObject(new
{
doc = doc
}
);
var param = new UpdateRequestParameters();
param.Refresh = Refresh.True;
var Response = _LowLevelclient.Update("mem_memberbase_p", docId, jsons, param);
dynamic t = JsonConvert.DeserializeObject(
Response.Body
);
if (t.result == "updated")
{
success = true;
}
else
{
success = false;
}
return Json(
new
{
msg = "",
success = success
});
}
}
}