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 Newtonsoft.Json;
using PublicLibrary.Model;
using SupplierWeb.Commonss;
using SupplierWeb.Service;
namespace SupplierWeb.Controllers
{
[Route("web/ES_MemFriends")]
public class ES_MemFriendsController : BaseController
{
public ES_MemFriendsController(IMemoryCache cache, IApiClient client,
IEsClientProvider clientProvider) : base(cache, client, clientProvider)
{
}
public IActionResult Index()
{
return View();
}
///
/// 会员好友列表
///
///
///
///
[HttpPost]
[Route("MemberFriends")]
public ActionResult MemberFriends([FromBody]dynamic data, QueryFilter[] filters)
{
int start = 0;
Object query = null;
string filter = "";
string MemberPhone = "";
if (data != null)
{
filters = data.filters.ToObject();
filter = QueryFilter.getFilterString(filters);
MemberPhone = filters[0].value;
query = new
{
query = "select * from mem_memberfriends where 1=1 " +
filter
+ " order by MemberFriendsPhone " +
" limit " + data.pageSize
};
}
#region
string sql = @"{
""size"": 10,
""query"": {
""bool"": {
""must"": [
{
""term"": {
""MemUsualPhone"": {
""value"": ""{@MemUsualPhone}"",
""boost"": 1
}
}
},
{
""nested"": {
""query"": {
""bool"": {
""must_not"": [
{
""term"": {
""Friends.MemberFriendsPhone"": {
""value"": ""{@MemUsualPhone}"",
""boost"": 1
}
}
}
],
""adjust_pure_negative"": true,
""boost"": 1
}
},
""path"": ""Friends"",
""ignore_unmapped"": false,
""score_mode"": ""none"",
""boost"": 1,
""inner_hits"": {
""ignore_unmapped"": false,
""from"": {@from},
""size"": {@size},
""version"": false,
""seq_no_primary_term"": false,
""explain"": false,
""track_scores"": false,
""_source"": {
""includes"": [
""Friends.*""
],
""excludes"": []
}
}
}
}
],
""adjust_pure_negative"": true,
""boost"": 1
}
},
""_source"": false,
""stored_fields"": ""_none_"",
""docvalue_fields"": [
{
""field"": ""MemUsualPhone""
},
{
""field"": ""MemberNickName""
}
],
""sort"": [
{
""_doc"": {
""order"": ""asc""
}
}
]
}";
//通过转换的DSL语句,获取数据
sql = sql.Replace("{@from}", "" + start).Replace("{@size}", "" + data.pageSize)
.Replace("{@MemUsualPhone}", MemberPhone);
PostData dsl = sql;
var result = _LowLevelclient.Search("mem_memberbase_p", dsl);
var DynamicObject = JsonConvert.DeserializeObject(result.Body);
List source = new List();
//总数
long totalCount = 0;
if (DynamicObject.hits.hits.Count > 0) {
HitsItem[] inerhits = DynamicObject.hits.hits[0].inner_hits.Friends.hits.hits.ToObject[]>();
foreach(HitsItem o in inerhits)
{
string[] phones = new string[1];
phones[0] = o._source.MemberFriendsPhone;
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.MemberFriendsPhoneCr = o._source.MemberFriendsPhone;
o._source.MemberFriendsPhone = phoneStars;
o._source.MemberFriendsPhoneOr = phones1[0];
o._source._id = o._id;
source.Add(o._source);
}
totalCount = DynamicObject.hits.hits[0].inner_hits.Friends.hits.total.value;
}
#endregion
//FriendID = GetFriends(_source.ToArray());
//query1 = new { query = "select * from mem_memberbase_p where MemUsualPhone " +
// "in ('" + FriendID + "')"
//};
//var json1 = JsonConvert.SerializeObject(query1);
////通过传来的SQL语句,转换成DSL语句
//string sql1 = Util.ElasticQuery("_sql/translate?format=json", "POST", json1);
////通过转换的DSL语句,获取数据
//var str1 = Util.ElasticQuery("mem_memberbase_p/_search?format=json", "POST", sql1);
////JSON转Root对象
//Root stobj1 = EsCommon.FromJSON>(str1);
////给filed额外的属性赋值
//stobj1.hits.hits.ForEach(x =>
//{
// x.fields._id = x._id;
//});
////将Root对象中的子对象取出来
//List _source1 = new List();
//_source1 = stobj1.hits.hits.Select(x => x.entity()).ToList();
//_source.ForEach(x => {
// _source1.ForEach(y => {
// if (x.MemberFriendsPhone.Equals(y.MemUsualPhone))
// {
// x.MemberFriendNickName = y.MemberNickName;
// x.MemberFriendRealName = y.MemberNickName;
// }
// });
//});
return Json(
new
{
items = source,
totalCount = totalCount
});
}
// ///
// /// JSON转对象
// ///
// ///
// ///
// ///
// public static T FromJSON(string input)
// {
// try
// {
// return JsonConvert.DeserializeObject(input);
// }
// catch (Exception ex)
// {
// string a = ex.Message;
// return default(T);
// }
// }
///
/// 根据电话号码,获取他所有的订单编号
///
///
///
public string GetFriends(mem_MemberFriends[] data)
{
string code = "";
for (int i = data.Count() - 1; i >= 0; i--)
{
if (i > 0)
{
code += "" + data[i].MemberFriendsPhone + "','";
}
else
{
code += "" + data[i].MemberFriendsPhone + "";
}
}
return code;
}
// #region
// public class _shards
// {
// ///
// ///
// ///
// public int total { get; set; }
// ///
// ///
// ///
// public int successful { get; set; }
// ///
// ///
// ///
// public int skipped { get; set; }
// ///
// ///
// ///
// public int failed { get; set; }
// }
// public class Total
// {
// ///
// ///
// ///
// public int value { get; set; }
// ///
// ///
// ///
// public string relation { get; set; }
// }
// public class _source
// {
// ///
// ///
// ///
// public string MemDesc { get; set; }
// }
// public class Fields
// {
// public string _id { get; set; }
// ///
// ///
// ///
// public List OriginType { get; set; }
// ///
// ///
// ///
// public List RegisterYear { get; set; }
// ///
// ///
// ///
// public List MemUsualProvinceCode { get; set; }
// ///
// ///
// ///
// public List MemberNickName { get; set; }
// ///
// ///
// ///
// public List HasOrder { get; set; }
// ///
// ///
// ///
// public List MemEmail { get; set; }
// ///
// ///
// ///
// public List MemUsualAddress { get; set; }
// ///
// ///
// ///
// public List MemUsualPhone { get; set; }
// ///
// ///
// ///
// public List MemberId { get; set; }
// ///
// ///
// ///
// public List RegisterTime { get; set; }
// ///
// ///
// ///
// public List MemberGender { get; set; }
// ///
// ///
// ///
// public List MemberHeaPic { get; set; }
// ///
// ///
// ///
// public List MemberKey { get; set; }
// ///
// ///
// ///
// public List RegisterDay { get; set; }
// ///
// ///
// ///
// public List OrdersCode { get; set; }
// ///
// ///
// ///
// public List MemUsualCityCode { get; set; }
// ///
// ///
// ///
// public List RegisterMonth { get; set; }
// ///
// ///
// ///
// public List MemPhone1 { get; set; }
// ///
// ///
// ///
// public List MemPhone2 { get; set; }
// ///
// ///
// ///
// public List MemPhone3 { get; set; }
// }
// public class HitsItem
// {
// ///
// ///
// ///
// public string _index { get; set; }
// ///
// ///
// ///
// public string _type { get; set; }
// ///
// ///
// ///
// public string _id { get; set; }
// ///
// ///
// ///
// public string _score { get; set; }
// ///
// ///
// ///
// public _source _source { get; set; }
// ///
// ///
// ///
// public Fields fields { get; set; }
// ///
// ///
// ///
// public List sort { get; set; }
// }
// public class Hits
// {
// ///
// ///
// ///
// public Total total { get; set; }
// ///
// ///
// ///
// public string max_score { get; set; }
// ///
// ///
// ///
// public List hits { get; set; }
// }
// public class Root
// {
// ///
// ///
// ///
// public int took { get; set; }
// ///
// ///
// ///
// public string timed_out { get; set; }
// ///
// ///
// ///
// public _shards _shards { get; set; }
// ///
// ///
// ///
// public Hits hits { get; set; }
// }
// #endregion
// #region Friend实体类
// public class Friend
// {
// ///
// ///
// ///
// public string name { get; set; }
// ///
// ///
// ///
// public string type { get; set; }
// }
// public class FriendCode
// {
// ///
// ///
// ///
// public List columns { get; set; }
// ///
// ///
// ///
// public List> rows { get; set; }
// }
// #endregion
// #region
// public class ColumnsItem
// {
// ///
// ///
// ///
// public string name { get; set; }
// ///
// ///
// ///
// public string type { get; set; }
// }
// public class Num
// {
// ///
// ///
// ///
// public List columns { get; set; }
// ///
// ///
// ///
// public List> rows { get; set; }
// }
// #endregion
//
}
}