ES_DataStaisController.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Common.Model;
  9. using CoreEntity.Entity;
  10. using CoreEntity.ESEntity;
  11. using JCSoft.WX.Framework.Api;
  12. using MemberWeb.Commonss;
  13. using Microsoft.AspNetCore.Hosting;
  14. using Microsoft.AspNetCore.Mvc;
  15. using Microsoft.Extensions.Caching.Memory;
  16. using Newtonsoft.Json;
  17. using Newtonsoft.Json.Linq;
  18. using NPOI.HSSF.UserModel;
  19. using PublicLibrary.Model;
  20. using SupplierWeb.Commonss;
  21. using ZcPeng.PublicLibrary;
  22. using Num = CoreEntity.ESEntity.Num;
  23. namespace SupplierWeb.Controllers
  24. {
  25. [Route("web/ES_DataStais")]
  26. public class ES_DataStaisController : BaseController
  27. {
  28. public ES_DataStaisController(IMemoryCache cache, IApiClient client) : base(cache, client)
  29. {
  30. }
  31. public IActionResult Index()
  32. {
  33. return View();
  34. }
  35. [HttpPost]
  36. [Route("index")]
  37. public ActionResult index([FromBody]dynamic data, QueryFilter[] filters,string sortField, Int32 sortDirection, Int32 pageIndex, Int32 pageSize,string scrollId)
  38. {
  39. int start = 0;
  40. string filter = "";
  41. string redisKey = "1_redis_stais";//缓存Key
  42. Object query = null;
  43. DateTime dateTime = DateTime.Parse(DateTime.Now.ToString());
  44. string orderStr = "{\"ym"+ dateTime.AddDays(-60).ToString("yyyyMM") + ".RM\":{\"order\":\"desc\"}}";
  45. if (data != null)
  46. {
  47. filters = data.filters.ToObject<QueryFilter[]>();
  48. pageIndex = data.pageIndex;
  49. pageSize = data.pageSize;
  50. sortField = data.sortField;
  51. sortDirection = data.sortDirection;
  52. }
  53. string dslStr = "\"query\":{ \"bool\": { \"must\": { \"exists\": {\"field\": \"F1*\"}}}}";
  54. string dslCount = "\"_source\":{\"include\":[\" * \"]}";
  55. string totalStr = "";//按选择月份.统计
  56. string totalRing = "";//用户选择平台
  57. //拼接组装dsl条件
  58. if (filters != null && filters.Length > 0)
  59. {
  60. string str = "";
  61. foreach (var item in filters) {
  62. if (item.field.Contains("tagKey"))
  63. {
  64. if (item.field == "tagKey1" || item.field == "tagKey3")
  65. {
  66. if (item.field == "tagKey1")
  67. {
  68. item.value = "ym" + item.value.Replace("-", "");
  69. str += "{ \"exists\": {\"field\": \"" + item.value + "\"}},";
  70. redisKey += item.value;//时间
  71. totalStr = item.value;
  72. }
  73. else
  74. {
  75. var key1 = item.value.Split(",");
  76. foreach (string tag in key1)
  77. {
  78. str += "{ \"exists\": {\"field\": \"OriginType-" + tag + "\"}},";
  79. redisKey += tag;
  80. }
  81. totalRing = item.value;//平台
  82. }
  83. }
  84. else
  85. {
  86. var key0 = item.value.Split(",");
  87. foreach (string tag in key0)
  88. {
  89. str += "{\"nested\": {\"path\":\"Tags\",\"query\": {\"bool\": {\"must\": [{\"match\": {\"Tags.TagId\":" + tag + "}}]}}}},";
  90. redisKey += tag;
  91. }
  92. ;
  93. }
  94. }
  95. else {
  96. if (item.value!=""&&item.value.Length>0) {
  97. str += "{\"match_phrase\": { \""+item.field+"\": \""+item.value+"\"}},";
  98. redisKey +=item.value;
  99. }
  100. }
  101. }
  102. if (str != "" && str.Length > 0) {
  103. dslStr = "\"query\": {\"bool\": {\"should\": [" + str.Substring(0, str.Length - 1) + "]}}";
  104. dslCount = "\"query\": {\"bool\": {\"should\": [" + str.Substring(0, str.Length - 1) + "]}}";
  105. }
  106. }
  107. if (filters != null && filters.Length > 0 && filters[0].field.Equals("scrollId"))
  108. {
  109. scrollId = filters[0].value;
  110. }
  111. string redisKey1 = redisKey;
  112. //排序
  113. string direct = "desc";
  114. if (data.sortDirection != 1)
  115. {
  116. direct = "asc";
  117. }
  118. if (sortField != null && (sortField.Equals("M1")|| sortField.Equals("F1"))) {
  119. orderStr = "{\"" + sortField + "\":{\"order\":\"" + direct + "\"}}";
  120. redisKey1 += sortField + direct;
  121. }
  122. if (data.pageIndex == 1)
  123. {
  124. start = 0;
  125. }
  126. else
  127. {
  128. start = ((data.pageIndex - 1) * data.pageSize) + 1;
  129. }
  130. //数据列表
  131. redisKey1 += Convert.ToString(start);
  132. string jsonTag = "";
  133. string sqlTag = RedisUtil.getRedisEsUtils(redisKey1 + "_6");//获取缓存
  134. if (start >= 10000)
  135. {
  136. if (scrollId != null && scrollId.Length > 0)
  137. {//游标继续获取
  138. jsonTag = "{\"scroll_id\": \""+ scrollId + "\"}";
  139. sqlTag = Util.ElasticQuery("mem_repurchaseinfo/_search?scroll=2m", "POST", jsonTag);
  140. }
  141. else
  142. {//初始获取数据scroll_id
  143. jsonTag = "{" + dslStr + ",\"sort\":[" + orderStr + "],\"size\": " + data.pageSize + "}";
  144. sqlTag = Util.ElasticQuery("mem_repurchaseinfo/_search?scroll=2m", "POST", jsonTag);
  145. }
  146. }
  147. else {
  148. if (sqlTag == null || sqlTag.Length <= 0)
  149. {
  150. jsonTag = "{" + dslStr + ",\"sort\":[" + orderStr + "],\"from\":" + start + ",\"size\": " + data.pageSize + "}";
  151. //没有缓存,加入缓存
  152. sqlTag = Util.ElasticQuery("mem_repurchaseinfo/_search?format=json", "POST", jsonTag);
  153. RedisUtil.addRedisEsUtils(sqlTag, redisKey1 + "_6");//加入缓存
  154. }
  155. }
  156. Root<Mem_dataStais> stobj = EsCommon.FromJSON<Root<Mem_dataStais>>(sqlTag);
  157. //屏蔽4位
  158. stobj.hits.hits.ForEach(x =>
  159. {
  160. string MemPhone = getPhoneDesEncrypt(x._id.Trim());
  161. x._source._id = MemPhone;
  162. x._source.AConsigneePhone2 = MemPhone.Substring(0, 3)+"****"+ MemPhone.Substring(MemPhone.Length-4, 4);
  163. x._source.M1 = Convert.ToDouble(x._source.M1)/100;
  164. });
  165. List<dynamic> _source = new List<dynamic>();
  166. _source = stobj.hits.hits.Select(x => x._source).ToList();
  167. DateTime endTime = Convert.ToDateTime(DateTime.Now.ToLocalTime().ToString());
  168. _source.ForEach(x =>
  169. {
  170. dynamic age = x.age;
  171. string age1 =JsonConvert.SerializeObject(age);
  172. if (age1 != null&& !age1.Contains("null"))
  173. {
  174. DateTime startTime = Convert.ToDateTime(age1.Replace("\"", ""));
  175. x.age = CalculateAge(startTime, endTime);
  176. }
  177. else {
  178. x.age = "0";
  179. }
  180. });
  181. string sqlTag2=RedisUtil.getRedisEsUtils(redisKey);//获取缓存
  182. if (sqlTag2==null || sqlTag2.Length<=0) {//没有缓存,加入缓存
  183. string F = "F1";
  184. string M = "M1";
  185. if (totalStr.Length > 0)
  186. {
  187. F = totalStr + ".RN";
  188. M = totalStr + ".RM";
  189. }
  190. string msum = ",\"M_COUNT\": {\"cardinality\": {\"field\": \"_id\"}},\"M_COUNTS\": {\"filter\":{ \"range\":{ \"" + F + "\":{\"gte\": \"2\"} }},\"aggs\":{\"M_COUNTS\":{\"cardinality\":{\"field\":\"_id\"}} }}";
  191. //获取统计数据
  192. string sumStr = "\"F_SUMS\": {\"filter\":{ \"range\":{ \"" + F + "\":{\"gte\": \"2\"} }},\"aggs\":{\"F_SUMS\":{\"sum\":{\"field\":\"" + F + "\"}} }},\"M_SUMS\": {\"filter\":{ \"range\":{ \"" + F + "\":{\"gte\": \"2\"} }},\"aggs\":{\"M_SUMS\":{\"sum\":{\"field\":\"" + M + "\"}} }}";
  193. jsonTag = "{" + dslCount + ",\"aggs\": {\"F_SUM\": {\"sum\": {\"field\": \"" + F + "\"}},\"M_SUM\": {\"sum\": {\"field\": \"" + M + "\"}}," + sumStr + msum + "},\"size\": 0}";
  194. System.Diagnostics.Debug.WriteLine("--" + jsonTag);
  195. sqlTag2 = Util.ElasticQuery("mem_repurchaseinfo/_search?format=json", "POST", jsonTag);
  196. RedisUtil.addRedisEsUtils(sqlTag2, redisKey);//加入缓存
  197. }
  198. Root<Mem_dataStais> stobj2 = EsCommon.FromJSON<Root<Mem_dataStais>>(sqlTag2);
  199. //总数
  200. long totalcount = stobj.hits.total.value;
  201. var jsonData = JsonConvert.SerializeObject(_source);
  202. var jsonSum= JsonConvert.SerializeObject(stobj2.aggregations);
  203. dynamic agger = JsonConvert.DeserializeObject(jsonSum);
  204. dynamic agerTotle = agger.M_COUNT.value;
  205. string agertl = JsonConvert.SerializeObject(agerTotle);
  206. if (agertl != null && !agertl.Contains("null"))
  207. {
  208. totalcount = Convert.ToInt64(agertl);
  209. }
  210. return Json(
  211. new
  212. {
  213. items = JsonConvert.DeserializeObject(jsonData),
  214. totalCount = totalcount,
  215. jsonSum= JsonConvert.DeserializeObject(jsonSum),
  216. _scroll_id = stobj._scroll_id
  217. });
  218. }
  219. //计算年龄
  220. public static int CalculateAge(DateTime birthDate, DateTime now)
  221. {
  222. int age = now.Year - birthDate.Year;
  223. if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
  224. {
  225. age--;
  226. }
  227. if (age >= 100) { age = 0; } else if (age<=0) { age = 0; }
  228. return age;
  229. }
  230. //解密
  231. public static string getPhoneDesEncrypt(string MemPhone) {
  232. DataTable dt = new DataTable();
  233. string result;
  234. List<SqlParameter> parameters = new List<SqlParameter>();
  235. bool success = DataAccess.GetValuesV2("select (dbo.DesDecryptFixKey('123456','123456e10adc3949ba59abbe56e057f20f883e','"+ MemPhone + "')) as phone ", ref dt, parameters.ToArray(), out result);
  236. if (dt.Rows.Count>0) {
  237. DataRow dr = dt.Rows[0];
  238. MemPhone = Convert.ToString(dr["phone"]);
  239. }
  240. return MemPhone;
  241. }
  242. //加密
  243. public static string getPhoneEncDesrypt(string MemPhone)
  244. {
  245. DataTable dt = new DataTable();
  246. string result;
  247. List<SqlParameter> parameters = new List<SqlParameter>();
  248. bool success = DataAccess.GetValuesV2("select (dbo.DesEncryptFixKey('123456','123456e10adc3949ba59abbe56e057f20f883e','" + MemPhone + "')) as phone ", ref dt, parameters.ToArray(), out result);
  249. if (dt.Rows.Count > 0)
  250. {
  251. DataRow dr = dt.Rows[0];
  252. MemPhone = Convert.ToString(dr["phone"]);
  253. }
  254. return MemPhone;
  255. }
  256. /// <summary>
  257. /// 会员统计
  258. /// </summary>
  259. /// <param name="data"></param>
  260. /// <param name=""></param>
  261. /// <returns></returns>
  262. [HttpGet]
  263. [Route("memCount")]
  264. public ActionResult memCount(string tagKey,string zKey,string sexkey) {
  265. string sqlStr = "";
  266. string memKey = "redis_mem";//性别缓存Key
  267. if (tagKey!=null && !tagKey .Equals("0")) {
  268. List<mem_MemberTag> _sourceTag=ES_UsageMemberController.TagIDList(tagKey);//获取标签会员
  269. string countstr = "";
  270. foreach (mem_MemberTag memtag in _sourceTag)
  271. {
  272. countstr += "'"+memtag.MemberPhone + "',";
  273. memKey += memtag.MemberPhone;
  274. }
  275. sqlStr = sqlStr + " and MemPhone2 IN(" + countstr.Substring(0, countstr.Length - 1) + ")";
  276. }
  277. if (zKey!=null && !zKey.Equals("0")) {
  278. sqlStr = sqlStr + " and OriginType IN(" + zKey + ")";
  279. memKey += zKey;
  280. }
  281. if (sexkey!=null) {
  282. sqlStr = sqlStr + " and MemberGender ="+ sexkey;
  283. memKey += sexkey;
  284. }
  285. //性别
  286. var jsonStr = RedisUtil.getRedisEsUtils("2_sex"+memKey);
  287. if (jsonStr==null|| jsonStr.Length<=0) {//缓存
  288. var sexStr = "{\"query\":\"" + "select MemberGender,count(MemUsualPhone) as cn from crm_memberbase_phoneid where 1=1 " + sqlStr + " group by MemberGender\"}";
  289. jsonStr = Util.ElasticQuery("_xpack/sql?format=json", "POST", sexStr);
  290. RedisUtil.addRedisEsUtils(jsonStr, "2_sex" + memKey);
  291. }
  292. //根据地区获取会员
  293. var str= RedisUtil.getRedisEsUtils("2_are" + memKey);
  294. if (str==null||str.Length<=0) {//缓存
  295. var json = "{\"query\":\"" + "select MemUsualProvinceCode,count(MemUsualPhone) as cn from crm_memberbase_phoneid where 1=1 " + sqlStr + " group by MemUsualProvinceCode ORDER BY cn desc\"}";
  296. str = Util.ElasticQuery("_xpack/sql?format=json", "POST", json);
  297. RedisUtil.addRedisEsUtils(str, "2_are" + memKey);
  298. }
  299. var sexJson = (JObject)JsonConvert.DeserializeObject(jsonStr);
  300. var jo = (JObject)JsonConvert.DeserializeObject(str);
  301. //获取省级
  302. var resultCate = Util.SqlQuery("select* from areamap where Level = 1 ");
  303. Dictionary<string, string> map = new Dictionary<string, string>();
  304. var listRow = new List<string[]>();
  305. foreach (var cate in resultCate.GetValue("rows"))
  306. {
  307. if (map.ContainsKey(cate[2].ToString()) == false)
  308. {
  309. map.Add(cate[2].ToString(), cate[0].ToString());
  310. }
  311. }
  312. foreach (var prod in jo.GetValue("rows"))
  313. {
  314. var row = new string[3];
  315. if (map.ContainsKey(prod[0].ToString()))
  316. {
  317. row[2] = prod[0].ToString();
  318. row[1] = prod[1].ToString();
  319. row[0] = map[prod[0].ToString()];
  320. listRow.Add(row);
  321. }
  322. }
  323. var listJson = JsonConvert.SerializeObject(listRow);
  324. return Json(
  325. new
  326. {
  327. itmes = JsonConvert.DeserializeObject(listJson),
  328. sexjson = sexJson.GetValue("rows")
  329. });
  330. }
  331. //获取新增会员统计
  332. [HttpGet]
  333. [Route("memDay")]
  334. public ActionResult memDay(string mDay)
  335. {
  336. DateTime dateTime = DateTime.Parse(DateTime.Now.ToString());
  337. DateTime date1 = dateTime.AddDays(-1);
  338. string sqlStr =" and RegisterTime <='" + date1.ToString("yyyy-MM-dd hh:mm:ss") + "'";
  339. //指定几天前的时间
  340. if (mDay != null)
  341. {
  342. DateTime date = dateTime.AddDays(-Convert.ToDouble(mDay));
  343. sqlStr += " and RegisterTime >='" + date.ToString("yyyy-MM-dd hh:mm:ss") + "'";
  344. }
  345. else {
  346. DateTime date = dateTime.AddDays(-15);
  347. sqlStr += " and RegisterTime >='" + date.ToString("yyyy-MM-dd hh:mm:ss") + "'";
  348. }
  349. var dayStr = "{\"query\":\"" + "select HISTOGRAM(RegisterTime, INTERVAL 1 DAYS) AS h,count(*) as cn from crm_memberbase_phoneid where 1=1 "+sqlStr+" group by h ORDER BY h desc\"}";
  350. var jsonStr = Util.ElasticQuery("_xpack/sql?format=json", "POST", dayStr);
  351. var dayJson = (JObject)JsonConvert.DeserializeObject(jsonStr);
  352. return Json(
  353. new
  354. {
  355. dayJson = dayJson.GetValue("rows")
  356. });
  357. }
  358. [HttpGet]
  359. [Route("orderProvCount")]
  360. public ActionResult orderProvCount(string tagKey, string zKey)
  361. {
  362. string str1 = "";
  363. string redisKey = "3_order_prov";
  364. if (zKey != null)//日期
  365. {
  366. if (!zKey.Equals("0"))
  367. {
  368. zKey = "ym" + zKey.Replace("-", "");
  369. str1 = "{ \"exists\": {\"field\": \"" + zKey + "\"}}";
  370. redisKey += zKey;
  371. }
  372. }
  373. var jsonStr = RedisUtil.getRedisEsUtils(redisKey);
  374. if (jsonStr == null || jsonStr.Length <= 0)
  375. {//缓存
  376. if (str1 != null && str1.Length > 0)
  377. {
  378. str1 = ",\"filter\":" + str1;
  379. }
  380. var dayStr = "{\"query\":\"select LEFT(RegionCode,2) AS c, count(distinct id) as cn from mem_repurchaseinfo group by c\"" + str1 + "}";
  381. jsonStr = Util.ElasticQuery("_xpack/sql?format=json", "POST", dayStr);
  382. RedisUtil.addRedisEsUtils(jsonStr, redisKey);
  383. }
  384. var jo = (JObject)JsonConvert.DeserializeObject(jsonStr);
  385. //获取省级
  386. var resultCate = Util.SqlQuery("select* from areamap where Level = 1 ");
  387. Dictionary<string, string> map = new Dictionary<string, string>();
  388. var listRow = new List<string[]>();
  389. foreach (var cate in resultCate.GetValue("rows"))
  390. {
  391. if (map.ContainsKey(cate[2].ToString()) == false)
  392. {
  393. map.Add(cate[2].ToString(), cate[0].ToString());
  394. }
  395. }
  396. foreach (var prod in jo.GetValue("rows"))
  397. {
  398. var row = new string[3];
  399. if (map.ContainsKey(prod[0].ToString() + "0000"))
  400. {
  401. row[2] = prod[0].ToString() + "0000";
  402. row[1] = prod[1].ToString();
  403. row[0] = map[prod[0].ToString() + "0000"];
  404. listRow.Add(row);
  405. }
  406. }
  407. var listJson = JsonConvert.SerializeObject(listRow);
  408. return Json(
  409. new
  410. {
  411. listJson = JsonConvert.DeserializeObject(listJson)
  412. });
  413. }
  414. /// <summary>
  415. /// 订单统计
  416. /// </summary>
  417. /// <param name="data"></param>
  418. /// <param name=""></param>
  419. /// <returns></returns>
  420. [HttpGet]
  421. [Route("orderCount")]
  422. public ActionResult orderCount(string tagKey,string zKey)
  423. {
  424. string str = "";
  425. string str1 = "";
  426. string dateStr = "";
  427. if (tagKey != null)//平台
  428. {
  429. var key1 = tagKey.Split(",");
  430. if (!key1[0].Equals("0"))
  431. {
  432. foreach (string tag in key1)
  433. {
  434. str += tag + ",";
  435. }
  436. str = " and OriginType in ("+ str .Substring(0,str.Length-1)+ ") ";
  437. }
  438. }
  439. if (zKey != null)//日期
  440. {
  441. if (!zKey.Equals("0"))
  442. {
  443. var datasp = zKey.Split("-");
  444. int dayCount = getSurplusDays(Convert.ToInt32(datasp[0]), Convert.ToInt32(datasp[1]));//本月天数
  445. dateStr = " and OrderTime>= '"+ zKey + "-01 00:00:00' and OrderTime<='"+ zKey + "-"+Convert.ToString(dayCount) +" 23:59:59'";
  446. }
  447. }
  448. string commandText = " select a.fsm,a.fcn,b.sm,b.cn from ( "+
  449. " select SUM(Sum/ 100.0) as fsm,toInt64(COUNT(1)) as fcn ,1 as cid from Orders WHERE AConsigneePhone2 global in " +
  450. " (select AConsigneePhone2 from Orders WHERE OrderStatus not in(0, 100, 200) and IsReturn = 0 "+
  451. " AND RIGHT(OrdersCode,2) <> 'BF' group by AConsigneePhone2 having count(AConsigneePhone2) > 1) "+
  452. dateStr+ str+
  453. " ) a FULL JOIN " +
  454. "(SELECT SUM(Sum / 100) as sm, toInt64(COUNT(1)) as cn, 1 as cid FROM Orders WHERE OrderStatus not in (0, 100, 200) and IsReturn = 0 " + dateStr + str + " )b " +
  455. " ON a.cid = b.cid FORMAT JSON ";
  456. var dugObj = ClickHouseUtil.HttpClickHouseGet(commandText);
  457. var obj = (JObject)JsonConvert.DeserializeObject(dugObj);
  458. dynamic data = JsonConvert.DeserializeObject(Convert.ToString(obj.GetValue("data")));
  459. dynamic sm = data[0].sm;
  460. string sm1 = JsonConvert.SerializeObject(sm);
  461. dynamic cn = data[0].cn;
  462. string cn1 = JsonConvert.SerializeObject(cn).Replace("\"","");
  463. dynamic fsm = data[0].fsm;
  464. string fsm1 = JsonConvert.SerializeObject(fsm);
  465. dynamic fcn = data[0].fcn;
  466. string fcn1 = JsonConvert.SerializeObject(fcn).Replace("\"", "");
  467. Object query = new
  468. {
  469. sm = sm1,
  470. cn=cn1,
  471. fsm=fsm1,
  472. fcn=fcn1
  473. };
  474. //更改es数据
  475. return Json(
  476. new
  477. {
  478. jsonSum = query
  479. });
  480. }
  481. /// <summary>
  482. /// 获取最近销售
  483. /// </summary>
  484. /// <param name="mDay"></param>
  485. /// <returns></returns>
  486. [HttpGet]
  487. [Route("orderDay")]
  488. public ActionResult orderDay(string mDay)
  489. {
  490. DateTime dateTime = DateTime.Parse(DateTime.Now.ToString());
  491. DateTime date = dateTime.AddDays(-16);
  492. DateTime date1 = dateTime.AddDays(-1);
  493. //指定几天前的时间
  494. if (mDay != null)
  495. {
  496. date = dateTime.AddDays(-Convert.ToDouble(mDay));
  497. }
  498. DataTable dt = new DataTable();
  499. string result;
  500. List<SqlParameter> parameters = new List<SqlParameter>();
  501. IList<order_Count> permss = new List<order_Count>(0);
  502. //终端
  503. string commandText3 = " select COUNT(DISTINCT o.OrdersCode) AS orCnt,CONVERT(varchar(100), o.CreationDate, 23) AS orDate,Sum(CONVERT(bigint, p.MarketPrice) * CONVERT(bigint, p.Amount)) AS orSum FROM Orders o LEFT JOIN OrderProducts p ON p.OrdersCode=o.OrdersCode WHERE" +
  504. " o.CreationDate>='"+ date.ToString("yyyy-MM-dd") + "' AND o.CreationDate<='" + date1.ToString("yyyy-MM-dd") + "' AND p.CreationDate>='"+ date.ToString("yyyy-MM-dd") + "' AND p.CreationDate<='" + date1.ToString("yyyy-MM-dd") + "' GROUP BY CONVERT(varchar(100), o.CreationDate, 23) order by CONVERT(varchar(100), o.CreationDate, 23)";
  505. bool success = DataAccess.GetValuesV2(commandText3, ref dt, parameters.ToArray(), out result);
  506. if (dt != null && dt.Rows.Count > 0)
  507. {
  508. permss = ModelConvertHelper<order_Count>.ConvertToModel(dt);
  509. }
  510. var jsonSum = JsonConvert.SerializeObject(permss);
  511. return Json(
  512. new
  513. {
  514. jsonSum = JsonConvert.DeserializeObject(jsonSum)
  515. });
  516. }
  517. /// <summary>
  518. /// 复购月份导出
  519. /// </summary>
  520. /// <param name="mDay"></param>
  521. /// <returns></returns>
  522. [HttpGet]
  523. [Route("memorderExport")]
  524. public ActionResult memorderExport([FromServices]IHostingEnvironment env, string staffId)
  525. {
  526. IList<Dictionary<string, string>> _sourceCount = new List<Dictionary<string, string>>();
  527. string sqlEs = "";
  528. Int32 monthFirstday = Convert.ToInt32(DateTime.Now.ToString("MM"));
  529. MemoryStream m = new MemoryStream();//创建表
  530. var wor = new HSSFWorkbook();
  531. var sheet = wor.CreateSheet();
  532. var shea = sheet.CreateRow(0);//1行
  533. int Index = 1;
  534. int staprice = 0;//金额区间
  535. int endprice = 1000;
  536. shea.CreateCell(0).SetCellValue("下单用户/复购用户");
  537. for (int j = 0; j < 11; j++) {//首行金额
  538. shea.CreateCell(j + 1).SetCellValue(staprice + "<¥≤" + endprice);
  539. staprice += 1000;
  540. if (staprice >= 10000)
  541. {
  542. endprice += 1000000;
  543. }
  544. else
  545. {
  546. endprice += 1000;
  547. }
  548. }
  549. for (int i = 1; i < monthFirstday; i++)
  550. {
  551. staprice = 0;//金额区间
  552. endprice = 1000;
  553. var newRow = sheet.CreateRow(i);//月度行
  554. newRow.CreateCell(0).SetCellValue(DateTime.Now.ToString("yyyy") + Convert.ToString(i));//列
  555. for (int j = 0; j < 11; j++)
  556. {
  557. newRow.CreateCell(j+1).SetCellValue(j);//列
  558. string dayStr = Convert.ToString(i);
  559. if (dayStr.Length < 2)
  560. {
  561. dayStr = "0" + dayStr;
  562. }
  563. sqlEs = "{\"query\":{\"range\":{\"ym" + DateTime.Now.ToString("yyyy") + dayStr + ".RM\":{\"gt\":" + (staprice * 100) + ",\"lte\":" + (endprice * 100) + "}}},\"aggs\":{\"mCount\":{\"cardinality\":{\"field\":\"_id\"}},\"count\":{\"filter\":{\"range\":{\"ym" + DateTime.Now.ToString("yyyy") + dayStr + ".RN\":{\"gte\":\"2\"}}},\"aggs\":{\"fCount\":{\"cardinality\":{\"field\":\"_id\"}}}}},\"size\":0}";
  564. staprice += 1000;
  565. if (staprice>=10000)
  566. {
  567. endprice += 1000000;
  568. }
  569. else
  570. {
  571. endprice += 1000;
  572. }
  573. var memstr = Util.ElasticQuery("mem_repurchaseinfo/_search?format=json", "POST", sqlEs);
  574. Root<mem_Export> stobj2 = EsCommon.FromJSON<Root<mem_Export>>(memstr);
  575. //总数
  576. var jsonSum = JsonConvert.SerializeObject(stobj2.aggregations);
  577. dynamic agger = JsonConvert.DeserializeObject(jsonSum);
  578. dynamic mCount = agger.mCount.value;
  579. dynamic fCount = agger.count.doc_count;
  580. string mCount1 = JsonConvert.SerializeObject(mCount);
  581. string fCount1 = JsonConvert.SerializeObject(fCount);
  582. newRow.CreateCell(j+1).SetCellValue(mCount1 + "/" + fCount1);
  583. }
  584. }
  585. wor.Write(m);
  586. m.Flush();
  587. m.Position = 0;
  588. sheet = null;
  589. shea = null;
  590. wor = null;
  591. return File(m, "application/ms-excel", "下单统计"+DateTime.Now.ToString("MMddHHmmss") + ".xls");
  592. }
  593. [HttpGet]
  594. [Route("getPhoneencDes")]
  595. public string getPhoneencDes(string staffId,string desPhone) {
  596. if (desPhone!=null) {
  597. desPhone = getPhoneEncDesrypt(desPhone);
  598. }
  599. return desPhone;
  600. }
  601. public static int getSurplusDays(int year, int month)
  602. {
  603. int days = 0;
  604. if (month != 2)
  605. {
  606. switch (month)
  607. {
  608. case 1:
  609. case 3:
  610. case 5:
  611. case 7:
  612. case 8:
  613. case 10:
  614. case 12:
  615. days = 31;
  616. break;
  617. case 4:
  618. case 6:
  619. case 9:
  620. case 11:
  621. days = 30;
  622. break;
  623. }
  624. }
  625. else
  626. {
  627. // 闰年
  628. if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
  629. days = 29;
  630. else
  631. days = 28;
  632. }
  633. return days;
  634. }
  635. }
  636. }