using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Threading.Tasks; using CoreEntity.Entity; using JCSoft.WX.Framework.Api; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using Newtonsoft.Json; using PublicLibrary.Model; using SupplierWeb.Commonss; namespace SupplierWeb.Controllers { [Route("web/ES_Memproduct")] public class ES_MemproductController : BaseController { public ES_MemproductController(IMemoryCache cache, IApiClient client) : base(cache, client) { } public IActionResult Index() { return View(); } [HttpPost] [Route("index")] public ActionResult index([FromBody]dynamic data, QueryFilter[] filters) { int start = 0; string field = ""; string fieldvalue = ""; if (data != null) { if (data.pageIndex == 1) { start = 0; } else { start = ((data.pageIndex - 1) * data.pageSize) + 1; } if (data.filters.Count != 0) { field = data.filters[0].field; fieldvalue = data.filters[0].value; data = new { query = "select * from mem_product1 where " + field + " like '%" + fieldvalue + "%' limit " + data.pageSize }; } else { data = new { query = "select * from mem_product1 a limit " + data.pageSize }; } } //将参数转换成JSON数据 var json = JsonConvert.SerializeObject(data); //通过传来的SQL语句,转换成DSL语句 string sql = Util.ElasticQuery("_sql/translate?format=json", "POST", json); string from = "\"from\":" + start + ","; sql = sql.Insert(1, from); //通过转换的DSL语句,获取数据 var str = Util.ElasticQuery("mem_product1/_search?format=json", "POST", sql); #region //dynamic dyn = new System.Dynamic.ExpandoObject(); //var obj = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(str, new //{ // hits = new // { // hits = new[] // { // new { // _id =string.Empty, // _source = new // { // Manufacturer =String.Empty, // }, // fields = dyn // } // } // } //}); //foreach(var item in obj.hits.hits) //{ //} #endregion //JSON转Root对象 Root stobj = FromJSON(str); //将Root对象中的子对象取出来 List<_source> _source = new List<_source>(); _source = stobj.hits.hits.Select(x => x._source).ToList(); List Fields = new List(); Fields = stobj.hits.hits.Select(x => x.fields).ToList(); //给filed额外的属性赋值 stobj.hits.hits.ForEach(x => { x.fields._id = x._id; x.fields.Manufacturer = x._source.Manufacturer; }); //总数 long totalcount = 0; data = new { query = "select count(1) from mem_product1" }; var jsons = JsonConvert.SerializeObject(data); string obj = Util.ElasticQuery("_sql?format=json", "POST", jsons); Num num = FromJSON(obj); totalcount =num.rows[0][0]; var jsonData = JsonConvert.SerializeObject(Fields); return Json( new { items = JsonConvert.DeserializeObject(jsonData), totalCount = totalcount }); } /// /// JSON转对象 /// /// /// /// public static T FromJSON(string input) { try { return JsonConvert.DeserializeObject(input); } catch (Exception ex) { string a = ex.Message; return default(T); } } #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; } } public class Filters { /// /// /// public int field { get; set; } /// /// /// public int value { get; set; } /// /// /// public int operate { get; set; } } 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 ProudctDescription { get; set; } /// /// 如与其他药物同时使用可能会发生药物相互作用,详情请咨询医师或药师。 /// public string DrugInteractions { get; set; } /// /// 药用PVC硬片,药品包装用PTP铝箔。10粒x2板/盒。 /// public string PackingProduct { get; set; } /// /// 辅仁 糖尿乐胶囊 0.3g*20粒 /// public string ProductName { get; set; } /// /// 贪食症 糖尿病 /// public string ProductTag { get; set; } /// /// 本品为胶囊剂,内容物为灰褐色粉末;味辛、微苦。 /// public string Characters { get; set; } /// /// /// public string Manual { get; set; } /// /// /// public string PregnantWoman { get; set; } /// /// /// public string Pharmaco { get; set; } /// /// 口服,一次3—4粒,一日3次。 /// public string Dosage { get; set; } /// /// 部颁标准中药成方制剂第十三册 /// public string Standards { get; set; } /// /// /// public string OlderPatients { get; set; } /// /// 密封。 /// public string Storage { get; set; } /// /// 滋阴补肾,益气润肺,和胃生津,调节代谢机能。用于消渴症引起的多食、多饮、多尿、四肢无力等症,降低血糖、尿糖。 /// public string Introduction { get; set; } /// /// /// public string ChemicalName { get; set; } /// /// 河南辅仁堂制药有限公司 /// public string Manufacturer { get; set; } /// /// 尚不明确。 /// public string Taboo { get; set; } /// /// 尚不明确。 /// public string Adverse { get; set; } /// /// /// public string Overdose { get; set; } /// /// 严忌含糖食物及烟酒。 /// public string Precautions { get; set; } /// /// /// public string Pharmacokinetics { get; set; } /// /// /// public string Child { get; set; } /// /// /// public string EnglishName { get; set; } } public class Fields { /// /// /// public string _id { get; set; } /// /// /// public string Manufacturer { get; set; } /// /// /// public List CreationDate { get; set; } /// /// /// public List SubProductLine { get; set; } /// /// /// public List IsRecommend { get; set; } /// /// /// public List FinancialType { get; set; } /// /// /// public List LastModified { get; set; } /// /// /// public List WapManual { get; set; } /// /// /// public List GmpCode { get; set; } /// /// /// public List AppreciationPrice { get; set; } /// /// /// public List Packing { get; set; } /// /// /// public List ProductImageUrl { get; set; } /// /// /// public List IsLimitPrice { get; set; } /// /// /// public List ToErp { get; set; } /// /// /// public List CommonTitle { get; set; } /// /// /// public List recordState { get; set; } /// /// /// public List ProductLine { get; set; } /// /// /// public List CommonTitlePinyin { get; set; } /// /// /// public List MerchantManageCode { get; set; } /// /// /// public List PackingNumber { get; set; } /// /// /// public List ProductType { get; set; } /// /// /// public List IsControl { get; set; } /// /// /// public List PinyinFullCode { get; set; } /// /// /// public List ControlNumber { get; set; } /// /// /// public List OutOfStockRecommendCodes { get; set; } /// /// /// public List PostageLogo { get; set; } /// /// /// public List ManufacturerCode { get; set; } /// /// /// public List ProductCodeForOutOfStock { get; set; } /// /// /// public List PlaceCode { get; set; } /// /// /// public List ApprovalNumber { get; set; } /// /// /// public List PurchasePrice { get; set; } /// /// /// public List CategoryType { get; set; } /// /// /// public List MassDate { get; set; } /// /// /// public List ProductAttribute { get; set; } /// /// /// public List DivisionCode { get; set; } /// /// /// public List Molecular { get; set; } /// /// /// public List ProductRecommend { get; set; } /// /// /// public List GiftCategory { get; set; } /// /// /// public List Structure { get; set; } /// /// /// public List HealthInsuranceType { get; set; } /// /// /// public List IsDrugRecord { get; set; } /// /// /// public List ShowMall { get; set; } /// /// /// public List ConsumeDays { get; set; } /// /// /// public List PlureProductStatusType { get; set; } /// /// /// public List IsSuit { get; set; } /// /// /// public List CheckPendingPrice { get; set; } /// /// /// public List IsVisible { get; set; } /// /// /// public List UsageCode { get; set; } /// /// /// public List ThumbnailUrl { get; set; } /// /// /// public List Productusecrowd { get; set; } /// /// /// public List BtManual { get; set; } /// /// /// public List BrandCode { get; set; } /// /// /// public List ProductusecrowdCode { get; set; } /// /// /// public List SFDAGmpCertificateDate { get; set; } /// /// /// public List Unit { get; set; } /// /// /// public List Formula { get; set; } /// /// /// public List ProductHotType { get; set; } /// /// /// public List Formulation { get; set; } /// /// /// public List Views { get; set; } /// /// /// public List PinyinCode { get; set; } /// /// /// public List OurPrice { get; set; } /// /// /// public List Img180 { get; set; } /// /// /// public List Inspection { get; set; } /// /// /// public List SyncStatus { get; set; } /// /// /// public List SFDACode { get; set; } /// /// /// public List Weight { get; set; } /// /// /// public List Img100 { get; set; } /// /// /// public List GmpCertificateUrl { get; set; } /// /// /// public List ClassCode { get; set; } /// /// /// public List MinMarketPrice { get; set; } /// /// /// public List StandardCode { get; set; } /// /// /// public List BranchCode { get; set; } /// /// /// public List PriceSectorPricing { get; set; } /// /// /// public List FormulationCode { get; set; } /// /// /// public List CQuantity { get; set; } /// /// /// public List BarCode { get; set; } /// /// /// public List ExternalID { get; set; } /// /// /// public List Productmainmaterial { get; set; } /// /// /// public List BusinessIds { get; set; } /// /// /// public List ControlAreaRegion { get; set; } /// /// /// public List XQuantity { get; set; } /// /// /// public List PrescriptionType { get; set; } /// /// /// public List QcClass { get; set; } /// /// /// public List ProfitLevel { get; set; } /// /// /// public List PurchaseWhile { get; set; } /// /// /// public List SuitDescription { get; set; } /// /// /// public List MarketPrice { get; set; } /// /// /// public List ProductStatusType { get; set; } /// /// /// public List SFDAStatus { get; set; } /// /// /// public List Composition { get; set; } /// /// /// public List UserName { get; set; } /// /// /// public List IsGifts { get; set; } /// /// /// public List Quantity { get; set; } /// /// /// public List Img320 { get; set; } /// /// /// public List SFDAApprovalValidDate { get; set; } /// /// /// public List Displaytab { 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 } }