mem_orderproductsController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 CoreEntity.ESEntity;
  8. using JCSoft.WX.Framework.Api;
  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_orderproducts")]
  17. public class mem_orderproductsController : BaseController
  18. {
  19. public mem_orderproductsController(IMemoryCache cache, IApiClient client) : base(cache, client)
  20. {
  21. }
  22. public IActionResult Index()
  23. {
  24. return View();
  25. }
  26. /// <summary>
  27. /// all订单产品列表
  28. /// </summary>
  29. /// <param name="data"></param>
  30. /// <param name="filters"></param>
  31. /// <returns></returns>
  32. [HttpPost]
  33. [Route("OrderproductsList")]
  34. public ActionResult OrderproductsList([FromBody]dynamic data, QueryFilter[] filters)
  35. {
  36. int start = 0;
  37. string filter = "";
  38. Object query = null;
  39. if (data != null)
  40. {
  41. filters = data.filters.ToObject<QueryFilter[]>();
  42. filter = QueryFilter.getFilterString(filters);
  43. if (data.pageIndex == 1)
  44. {
  45. start = 0;
  46. }
  47. else
  48. {
  49. start = ((data.pageIndex - 1) * data.pageSize) + 1;
  50. }
  51. //根据会员电话号码,先找到他的所有订单
  52. //找到属于处方的订单
  53. query = new { query = "select * from mem_orderproducts where 1=1"+
  54. filter +
  55. " order by CreationDate " +
  56. "limit " + data.pageSize
  57. };
  58. }
  59. //将参数转换成JSON数据
  60. var json = JsonConvert.SerializeObject(query);
  61. //通过传来的SQL语句,转换成DSL语句
  62. string sql = Util.ElasticQuery("_sql/translate?format=json", "POST", json);
  63. string from = "\"from\":" + start + ",";
  64. sql = sql.Insert(1, from);
  65. //通过转换的DSL语句,获取数据
  66. var str = Util.ElasticQuery("mem_orderproducts/_search?format=json", "POST", sql);
  67. //JSON转Root对象
  68. Root<OrderProducts> stobj = EsCommon.FromJSON<Root<OrderProducts>>(str);
  69. //总数
  70. List<OrderProducts> _source = new List<OrderProducts>();
  71. long totalcount = 0;
  72. if (stobj.hits.hits.Count > 0) {
  73. //给filed额外的属性赋值
  74. stobj.hits.hits.ForEach(x =>
  75. {
  76. x._source._id = x._id;
  77. //x._source.ProductCode = x.fields.ProductCode[0].ToString();
  78. x.fields.ActualPrice[0] = x.fields.ActualPrice[0] > 0 ?
  79. x.fields.ActualPrice[0] / 100 : 0;
  80. });
  81. //将Root对象中的子对象取出来
  82. _source = stobj.hits.hits.Select(x => x.entity()).ToList();
  83. var countdata = new { query = "select count(1) from mem_orderproducts where 1=1"+
  84. filter
  85. };
  86. var jsons = JsonConvert.SerializeObject(countdata);
  87. //ES获取SQL执行结果
  88. string obj = Util.ElasticQuery("_sql?format=json", "POST", jsons);
  89. Num num = FromJSON<Num>(obj);
  90. totalcount = num.rows[0][0];
  91. }
  92. var jsonData = JsonConvert.SerializeObject(_source);
  93. return Json(
  94. new
  95. {
  96. items = JsonConvert.DeserializeObject(jsonData),
  97. totalCount = totalcount
  98. });
  99. }
  100. /// <summary>
  101. /// 订单产品列表ByOderCode
  102. /// </summary>
  103. /// <param name="data"></param>
  104. /// <param name="filters"></param>
  105. /// <returns></returns>
  106. [HttpPost]
  107. [Route("ProductsListByOderCode")]
  108. public ActionResult ProductsListByOderCode([FromBody]dynamic data, QueryFilter[] filters)
  109. {
  110. int start = 0;
  111. //string OrdersCode = "JD84666969985";
  112. string OrdersCode = "";
  113. if (data != null)
  114. {
  115. OrdersCode = data.query[0];
  116. data = new { query = "select * from mem_orderproducts where OrdersCode='" + OrdersCode + "' " };
  117. }
  118. //将参数转换成JSON数据
  119. var json = JsonConvert.SerializeObject(data);
  120. //通过传来的SQL语句,转换成DSL语句
  121. string sql = Util.ElasticQuery("_sql/translate?format=json", "POST", json);
  122. string from = "\"from\":" + start + ",";
  123. sql = sql.Insert(1, from);
  124. //通过转换的DSL语句,获取数据
  125. var str = Util.ElasticQuery("mem_orderproducts/_search?format=json", "POST", sql);
  126. //JSON转Root对象
  127. Root<OrderProducts> stobj = FromJSON<Root<OrderProducts>>(str);
  128. //List<Fields> Fields = new List<Fields>();
  129. //Fields = stobj.hits.hits.Select(x => x.fields).ToList();
  130. //给filed额外的属性赋值
  131. stobj.hits.hits.ForEach(x =>
  132. {
  133. x._source._id = x._id;
  134. //x._source.ProductCode = x.fields.ProductCode[0].ToString();
  135. x.fields.ActualPrice[0] = x.fields.ActualPrice[0] > 0 ?
  136. x.fields.ActualPrice[0] / 100 : 0;
  137. });
  138. //将Root对象中的子对象取出来
  139. List<OrderProducts> _source = new List<OrderProducts>();
  140. _source = stobj.hits.hits.Select(x => x.entity()).ToList();
  141. //总数
  142. long totalcount = 0;
  143. data = new { query = "select count(1) from mem_orderproducts where OrdersCode='"+ OrdersCode + "'" };
  144. var jsons = JsonConvert.SerializeObject(data);
  145. //ES获取SQL执行结果
  146. string obj = Util.ElasticQuery("_sql?format=json", "POST", jsons);
  147. Num num = FromJSON<Num>(obj);
  148. totalcount = num.rows[0][0];
  149. var jsonData = JsonConvert.SerializeObject(_source);
  150. return Json(
  151. new
  152. {
  153. items = JsonConvert.DeserializeObject(jsonData),
  154. totalCount = totalcount,
  155. success = true
  156. });
  157. }
  158. /// <summary>
  159. /// JSON转对象
  160. /// </summary>
  161. /// <typeparam name="T"></typeparam>
  162. /// <param name="input"></param>
  163. /// <returns></returns>
  164. public static T FromJSON<T>(string input)
  165. {
  166. try
  167. {
  168. return JsonConvert.DeserializeObject<T>(input);
  169. }
  170. catch (Exception ex)
  171. {
  172. string a = ex.Message;
  173. return default(T);
  174. }
  175. }
  176. //#region 列表实体类
  177. //public class _shards
  178. //{
  179. // /// <summary>
  180. // ///
  181. // /// </summary>
  182. // public int total { get; set; }
  183. // /// <summary>
  184. // ///
  185. // /// </summary>
  186. // public int successful { get; set; }
  187. // /// <summary>
  188. // ///
  189. // /// </summary>
  190. // public int skipped { get; set; }
  191. // /// <summary>
  192. // ///
  193. // /// </summary>
  194. // public int failed { get; set; }
  195. //}
  196. //public class Total
  197. //{
  198. // /// <summary>
  199. // ///
  200. // /// </summary>
  201. // public int value { get; set; }
  202. // /// <summary>
  203. // ///
  204. // /// </summary>
  205. // public string relation { get; set; }
  206. //}
  207. //public class _source
  208. //{
  209. // public string ProductCode { get; set; }
  210. // /// <summary>
  211. // ///
  212. // /// </summary>
  213. // public string CreationDate { get; set; }
  214. // /// <summary>
  215. // /// 仁和 痔速宁片 0.31g*12片*3板
  216. // /// </summary>
  217. // public string ProductName { get; set; }
  218. // /// <summary>
  219. // ///
  220. // /// </summary>
  221. // public string ProductPromotionSet { get; set; }
  222. // /// <summary>
  223. // ///
  224. // /// </summary>
  225. // public string AssociateProudctCode { get; set; }
  226. // /// <summary>
  227. // /// 盒
  228. // /// </summary>
  229. // public string Unit { get; set; }
  230. // /// <summary>
  231. // ///
  232. // /// </summary>
  233. // public string ProfitLevel { get; set; }
  234. // /// <summary>
  235. // ///
  236. // /// </summary>
  237. // public string LastModified { get; set; }
  238. // /// <summary>
  239. // /// 0.31g*12片*3板(仁和)
  240. // /// </summary>
  241. // public string Packing { get; set; }
  242. // /// <summary>
  243. // ///
  244. // /// </summary>
  245. // public string Batch { get; set; }
  246. // /// <summary>
  247. // ///
  248. // /// </summary>
  249. // public string Picture { get; set; }
  250. // /// <summary>
  251. // ///
  252. // /// </summary>
  253. // public string Barcode { get; set; }
  254. // /// <summary>
  255. // /// 江西药都仁和制药有限公司
  256. // /// </summary>
  257. // public string Manufacturer { get; set; }
  258. // /// <summary>
  259. // ///
  260. // /// </summary>
  261. // public string OrdersCode { get; set; }
  262. // /// <summary>
  263. // ///
  264. // /// </summary>
  265. // public string DamagedRemark { get; set; }
  266. // /// <summary>
  267. // ///
  268. // /// </summary>
  269. // public string MemberPhone { get; set; }
  270. // /// <summary>
  271. // ///
  272. // /// </summary>
  273. // public string Id { get; set; }
  274. //}
  275. //public class Fields
  276. //{
  277. // /// <summary>
  278. // ///
  279. // /// </summary>
  280. // public List<int> ActualPrice { get; set; }
  281. // /// <summary>
  282. // ///
  283. // /// </summary>
  284. // public List<int> AvgDiscount { get; set; }
  285. // /// <summary>
  286. // ///
  287. // /// </summary>
  288. // public List<int> MarketPrice { get; set; }
  289. // /// <summary>
  290. // ///
  291. // /// </summary>
  292. // public List<int> ProductCode { get; set; }
  293. // /// <summary>
  294. // ///
  295. // /// </summary>
  296. // public List<int> AvgFreight { get; set; }
  297. // /// <summary>
  298. // ///
  299. // /// </summary>
  300. // public List<int> OurPrice { get; set; }
  301. // /// <summary>
  302. // ///
  303. // /// </summary>
  304. // public List<int> Amount { get; set; }
  305. // /// <summary>
  306. // ///
  307. // /// </summary>
  308. // public List<string> IntegralAmount { get; set; }
  309. // /// <summary>
  310. // ///
  311. // /// </summary>
  312. // public List<int> PurchasePrice { get; set; }
  313. //}
  314. //public class HitsItem
  315. //{
  316. // /// <summary>
  317. // ///
  318. // /// </summary>
  319. // public string _index { get; set; }
  320. // /// <summary>
  321. // ///
  322. // /// </summary>
  323. // public string _type { get; set; }
  324. // /// <summary>
  325. // ///
  326. // /// </summary>
  327. // public string _id { get; set; }
  328. // /// <summary>
  329. // ///
  330. // /// </summary>
  331. // public string _score { get; set; }
  332. // /// <summary>
  333. // ///
  334. // /// </summary>
  335. // public string _routing { get; set; }
  336. // /// <summary>
  337. // ///
  338. // /// </summary>
  339. // public _source _source { get; set; }
  340. // /// <summary>
  341. // ///
  342. // /// </summary>
  343. // public Fields fields { get; set; }
  344. // /// <summary>
  345. // ///
  346. // /// </summary>
  347. // public List<int> sort { get; set; }
  348. //}
  349. //public class Hits
  350. //{
  351. // /// <summary>
  352. // ///
  353. // /// </summary>
  354. // public Total total { get; set; }
  355. // /// <summary>
  356. // ///
  357. // /// </summary>
  358. // public string max_score { get; set; }
  359. // /// <summary>
  360. // ///
  361. // /// </summary>
  362. // public List<HitsItem> hits { get; set; }
  363. //}
  364. //public class Root
  365. //{
  366. // /// <summary>
  367. // ///
  368. // /// </summary>
  369. // public int took { get; set; }
  370. // /// <summary>
  371. // ///
  372. // /// </summary>
  373. // public string timed_out { get; set; }
  374. // /// <summary>
  375. // ///
  376. // /// </summary>
  377. // public _shards _shards { get; set; }
  378. // /// <summary>
  379. // ///
  380. // /// </summary>
  381. // public Hits hits { get; set; }
  382. //}
  383. //#endregion
  384. //#region 总数实体类
  385. //public class ColumnsItem
  386. //{
  387. // /// <summary>
  388. // ///
  389. // /// </summary>
  390. // public string name { get; set; }
  391. // /// <summary>
  392. // ///
  393. // /// </summary>
  394. // public string type { get; set; }
  395. //}
  396. //public class Num
  397. //{
  398. // /// <summary>
  399. // ///
  400. // /// </summary>
  401. // public List<ColumnsItem> columns { get; set; }
  402. // /// <summary>
  403. // ///
  404. // /// </summary>
  405. // public List<List<int>> rows { get; set; }
  406. //}
  407. //#endregion
  408. }
  409. }