mem_prescriptionproductController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.Entity;
  7. using JCSoft.WX.Framework.Api;
  8. using MemberWeb.Commonss.EsDAL;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Microsoft.Extensions.Caching.Memory;
  11. using Newtonsoft.Json;
  12. using PublicLibrary.Model;
  13. using SupplierWeb.Commonss;
  14. namespace SupplierWeb.Controllers
  15. {
  16. [Route("web/mem_prescriptionproduct")]
  17. public class mem_prescriptionproductController : BaseController
  18. {
  19. public mem_prescriptionproductController(IMemoryCache cache, IApiClient client) : base(cache, client)
  20. {
  21. }
  22. public IActionResult Index()
  23. {
  24. return View();
  25. }
  26. [HttpPost]
  27. [Route("index")]
  28. public ActionResult index([FromBody]dynamic data, QueryFilter[] filters)
  29. {
  30. string code = "";
  31. int start = 0;
  32. string filter = "";
  33. Object query = null;
  34. if (data != null)
  35. {
  36. filters = data.filters.ToObject<QueryFilter[]>();
  37. filter = QueryFilter.getFilterString(filters);
  38. if (data.pageIndex == 1)
  39. {
  40. start = 0;
  41. }
  42. else
  43. {
  44. start = ((data.pageIndex - 1) * data.pageSize) + 1;
  45. }
  46. //根据会员电话号码,先找到他的所有订单
  47. code = OrderDAL.GetPrescriptionOderscode(data, filter);
  48. if (code == "")
  49. {
  50. return Json(
  51. new
  52. {
  53. items = new Object[] { },
  54. totalCount = 0
  55. });
  56. }
  57. //找到属于处方产品的订单
  58. query = new { query = "select * from mem_prescriptionproduct where OrderCode in(" + code + ") " +
  59. " order by OrderCode " +
  60. " limit "+data.pageSize };
  61. }
  62. //将参数转换成JSON数据
  63. var json = JsonConvert.SerializeObject(query);
  64. //通过传来的SQL语句,转换成DSL语句
  65. string sql = Util.ElasticQuery("_sql/translate?format=json", "POST", json);
  66. string from = "\"from\":" + start + ",";
  67. sql = sql.Insert(1, from);
  68. //通过转换的DSL语句,获取数据
  69. var str = Util.ElasticQuery("mem_prescriptionproduct/_search?format=json", "POST", sql);
  70. //JSON转Root对象
  71. Root stobj = FromJSON<Root>(str);
  72. //将Root对象中的子对象取出来
  73. List<_source> _source = new List<_source>();
  74. _source = stobj.hits.hits.Select(x => x._source).ToList();
  75. List<Fields> Fields = new List<Fields>();
  76. Fields = stobj.hits.hits.Select(x => x.fields).ToList();
  77. //给filed额外的属性赋值
  78. stobj.hits.hits.ForEach(x =>
  79. {
  80. x._source.ProductCode = x.fields.ProductCode[0].ToString();
  81. //x.fields.Manufacturer = x._source.Manufacturer;
  82. });
  83. //总数
  84. long totalcount = 0;
  85. var countdata = new { query = "select count(1) from mem_prescriptionproduct where OrderCode in(" + code + ") " };
  86. var jsons = JsonConvert.SerializeObject(countdata);
  87. string obj = Util.ElasticQuery("_sql?format=json", "POST", jsons);
  88. Num num = FromJSON<Num>(obj);
  89. totalcount = num.rows[0][0];
  90. var jsonData = JsonConvert.SerializeObject(_source);
  91. return Json(
  92. new
  93. {
  94. items = JsonConvert.DeserializeObject(jsonData),
  95. totalCount = totalcount
  96. });
  97. }
  98. /// <summary>
  99. /// JSON转对象
  100. /// </summary>
  101. /// <typeparam name="T"></typeparam>
  102. /// <param name="input"></param>
  103. /// <returns></returns>
  104. public static T FromJSON<T>(string input)
  105. {
  106. try
  107. {
  108. return JsonConvert.DeserializeObject<T>(input);
  109. }
  110. catch (Exception ex)
  111. {
  112. string a = ex.Message;
  113. return default(T);
  114. }
  115. }
  116. #region 列表实体类
  117. public class _shards
  118. {
  119. /// <summary>
  120. ///
  121. /// </summary>
  122. public int total { get; set; }
  123. /// <summary>
  124. ///
  125. /// </summary>
  126. public int successful { get; set; }
  127. /// <summary>
  128. ///
  129. /// </summary>
  130. public int skipped { get; set; }
  131. /// <summary>
  132. ///
  133. /// </summary>
  134. public int failed { get; set; }
  135. }
  136. public class Total
  137. {
  138. /// <summary>
  139. ///
  140. /// </summary>
  141. public int value { get; set; }
  142. /// <summary>
  143. ///
  144. /// </summary>
  145. public string relation { get; set; }
  146. }
  147. public class _source
  148. {
  149. /// <summary>
  150. ///
  151. /// </summary>
  152. public string ProductCode { get; set; }
  153. /// <summary>
  154. /// [{"ProductName":"万通 乳酸左氧氟沙星分散片 0.1g*12片","Amount":"1","Pack":"po 0.2g bid","IsPrescription":"1","ProductCode":"726583"}]
  155. /// </summary>
  156. public string Contents { get; set; }
  157. /// <summary>
  158. /// 1张
  159. /// </summary>
  160. public string AtomUnit { get; set; }
  161. /// <summary>
  162. /// @21 药师服务卡(京东) 1张
  163. /// </summary>
  164. public string ProductName { get; set; }
  165. /// <summary>
  166. ///
  167. /// </summary>
  168. public string ProductPacking { get; set; }
  169. /// <summary>
  170. ///
  171. /// </summary>
  172. public string OrderCode { get; set; }
  173. /// <summary>
  174. /// 张
  175. /// </summary>
  176. public string Unit { get; set; }
  177. /// <summary>
  178. /// 王朝约
  179. /// </summary>
  180. public string Name { get; set; }
  181. /// <summary>
  182. ///
  183. /// </summary>
  184. public string Dossage { get; set; }
  185. }
  186. public class Fields
  187. {
  188. /// <summary>
  189. ///
  190. /// </summary>
  191. public List<string> IsDelete { get; set; }
  192. /// <summary>
  193. ///
  194. /// </summary>
  195. public List<int> ProductCode { get; set; }
  196. /// <summary>
  197. ///
  198. /// </summary>
  199. public List<int> Amount { get; set; }
  200. }
  201. public class HitsItem
  202. {
  203. /// <summary>
  204. ///
  205. /// </summary>
  206. public string _index { get; set; }
  207. /// <summary>
  208. ///
  209. /// </summary>
  210. public string _type { get; set; }
  211. /// <summary>
  212. ///
  213. /// </summary>
  214. public string _id { get; set; }
  215. /// <summary>
  216. ///
  217. /// </summary>
  218. public string _score { get; set; }
  219. /// <summary>
  220. ///
  221. /// </summary>
  222. public _source _source { get; set; }
  223. /// <summary>
  224. ///
  225. /// </summary>
  226. public Fields fields { get; set; }
  227. /// <summary>
  228. ///
  229. /// </summary>
  230. public List<int> sort { get; set; }
  231. }
  232. public class Hits
  233. {
  234. /// <summary>
  235. ///
  236. /// </summary>
  237. public Total total { get; set; }
  238. /// <summary>
  239. ///
  240. /// </summary>
  241. public string max_score { get; set; }
  242. /// <summary>
  243. ///
  244. /// </summary>
  245. public List<HitsItem> hits { get; set; }
  246. }
  247. public class Root
  248. {
  249. /// <summary>
  250. ///
  251. /// </summary>
  252. public int took { get; set; }
  253. /// <summary>
  254. ///
  255. /// </summary>
  256. public string timed_out { get; set; }
  257. /// <summary>
  258. ///
  259. /// </summary>
  260. public _shards _shards { get; set; }
  261. /// <summary>
  262. ///
  263. /// </summary>
  264. public Hits hits { get; set; }
  265. }
  266. #endregion
  267. #region 总数实体类
  268. public class ColumnsItem
  269. {
  270. /// <summary>
  271. ///
  272. /// </summary>
  273. public string name { get; set; }
  274. /// <summary>
  275. ///
  276. /// </summary>
  277. public string type { get; set; }
  278. }
  279. public class Num
  280. {
  281. /// <summary>
  282. ///
  283. /// </summary>
  284. public List<ColumnsItem> columns { get; set; }
  285. /// <summary>
  286. ///
  287. /// </summary>
  288. public List<List<int>> rows { get; set; }
  289. }
  290. #endregion
  291. #region 订单code实体类
  292. public class Condition
  293. {
  294. /// <summary>
  295. ///
  296. /// </summary>
  297. public string name { get; set; }
  298. /// <summary>
  299. ///
  300. /// </summary>
  301. public string type { get; set; }
  302. }
  303. public class Orderscode
  304. {
  305. /// <summary>
  306. ///
  307. /// </summary>
  308. public List<Condition> columns { get; set; }
  309. /// <summary>
  310. ///
  311. /// </summary>
  312. public List<List<string>> rows { get; set; }
  313. }
  314. #endregion
  315. }
  316. }