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 MemberWeb.Commonss.EsDAL; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; using Newtonsoft.Json; using PublicLibrary.Model; using SupplierWeb.Commonss; namespace SupplierWeb.Controllers { [Route("web/mem_prescriptionproduct")] public class mem_prescriptionproductController : BaseController { public mem_prescriptionproductController(IMemoryCache cache, IApiClient client) : base(cache, client) { } public IActionResult Index() { return View(); } [HttpPost] [Route("index")] public ActionResult index([FromBody]dynamic data, QueryFilter[] filters) { string code = ""; int start = 0; string filter = ""; Object query = null; if (data != null) { filters = data.filters.ToObject(); filter = QueryFilter.getFilterString(filters); if (data.pageIndex == 1) { start = 0; } else { start = ((data.pageIndex - 1) * data.pageSize) + 1; } //根据会员电话号码,先找到他的所有订单 code = OrderDAL.GetPrescriptionOderscode(data, filter); if (code == "") { return Json( new { items = new Object[] { }, totalCount = 0 }); } //找到属于处方产品的订单 query = new { query = "select * from mem_prescriptionproduct where OrderCode in(" + code + ") " + " order by OrderCode " + " limit "+data.pageSize }; } //将参数转换成JSON数据 var json = JsonConvert.SerializeObject(query); //通过传来的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_prescriptionproduct/_search?format=json", "POST", sql); //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._source.ProductCode = x.fields.ProductCode[0].ToString(); //x.fields.Manufacturer = x._source.Manufacturer; }); //总数 long totalcount = 0; var countdata = new { query = "select count(1) from mem_prescriptionproduct where OrderCode in(" + code + ") " }; var jsons = JsonConvert.SerializeObject(countdata); string obj = Util.ElasticQuery("_sql?format=json", "POST", jsons); Num num = FromJSON(obj); totalcount = num.rows[0][0]; var jsonData = JsonConvert.SerializeObject(_source); 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 _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 ProductCode { get; set; } /// /// [{"ProductName":"万通 乳酸左氧氟沙星分散片 0.1g*12片","Amount":"1","Pack":"po 0.2g bid","IsPrescription":"1","ProductCode":"726583"}] /// public string Contents { get; set; } /// /// 1张 /// public string AtomUnit { get; set; } /// /// @21 药师服务卡(京东) 1张 /// public string ProductName { get; set; } /// /// /// public string ProductPacking { get; set; } /// /// /// public string OrderCode { get; set; } /// /// 张 /// public string Unit { get; set; } /// /// 王朝约 /// public string Name { get; set; } /// /// /// public string Dossage { get; set; } } public class Fields { /// /// /// public List IsDelete { get; set; } /// /// /// public List ProductCode { get; set; } /// /// /// public List Amount { 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 总数实体类 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 #region 订单code实体类 public class Condition { /// /// /// public string name { get; set; } /// /// /// public string type { get; set; } } public class Orderscode { /// /// /// public List columns { get; set; } /// /// /// public List> rows { get; set; } } #endregion } }