dateRange.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. 'use strict';
  2. const moment = require('moment');
  3. const utils = require('../util/utils');
  4. module.exports = {
  5. initV: 0,
  6. initC: 0,
  7. kv: {
  8. 0: {
  9. unit: 'days', label: (moment, format) => moment.format(format || 'YYYY年MM月DD日'),
  10. test: moment => { return { year: moment.year(), month: moment.month() + 1, day: moment.date() }; },
  11. },
  12. 1: {
  13. unit: 'month', label: (moment, format) => moment.format(format || 'YYYY年MM月'),
  14. test: moment => { return { year: moment.year(), month: moment.month() + 1 }; },
  15. },
  16. 2: {
  17. unit: 'year', label: (moment, format) => moment.format(format || 'YYYY年'),
  18. test: moment => { return { year: moment.year() }; },
  19. },
  20. },
  21. getInitArr(kv, cond) {
  22. return Object.keys(kv).map(item => {
  23. return { count: this.initC, value: this.initV, key: item, label: kv[item], ...cond };
  24. });
  25. },
  26. getKeyArr(data, key) {
  27. return data.map(item => {
  28. let value = this.initV;
  29. let count = this.initC;
  30. if (Array.isArray(item.value)) {
  31. const obj = item.value.find(item => item._id == key);
  32. value = obj ? obj.value : this.initV;
  33. count = obj ? obj.count : this.initC;
  34. }
  35. return { label: item.label, value, count };
  36. });
  37. },
  38. commonMergeData(startTime, endTime, type, data, config) {
  39. let array = this.range2(startTime, endTime, type, config);
  40. if (data) {
  41. const arr = data.map(item => {
  42. return this.getMomentObj(type, item, config);
  43. }).concat(array);
  44. const res = new Map();
  45. array = arr.filter(a => !res.has(a.label) && res.set(a.label, 1))
  46. .sort((a, b) => parseInt(a.time) - parseInt(b.time));
  47. }
  48. return array;
  49. },
  50. commonMergeData1(startTime, endTime, type, data, config) {
  51. let array = this.range2(startTime, endTime, type, config);
  52. if (data) {
  53. const arr = data.map(item => {
  54. return this.getMomentObj(type, { ...item, count: item.interface_exception_count }, config);
  55. }).concat(array);
  56. const res = new Map();
  57. array = arr.filter(a => !res.has(a.label) && res.set(a.label, 1))
  58. .sort((a, b) => parseInt(a.time) - parseInt(b.time));
  59. }
  60. return array;
  61. },
  62. commonMergeData2(startTime, endTime, type, data, config) {
  63. let array = this.range2(startTime, endTime, type, config);
  64. if (data) {
  65. const arr = data.map(item => {
  66. return this.getMomentObj(type, { ...item, count: item.sys_count }, config);
  67. }).concat(array);
  68. const res = new Map();
  69. array = arr.filter(a => !res.has(a.label) && res.set(a.label, 1))
  70. .sort((a, b) => parseInt(a.time) - parseInt(b.time));
  71. }
  72. return array;
  73. },
  74. getMomentObj(type, item, config) { // config k可以是0,1,2对应的formatter或者count 对应数值的字段
  75. let momentObj;
  76. let year;
  77. let month;
  78. let day;
  79. switch (type) {
  80. case 0:
  81. year = item._id.year;
  82. month = item._id.month;
  83. day = item._id.day;
  84. break;
  85. case 1:
  86. year = item._id.year;
  87. month = item._id.month;
  88. break;
  89. case 2:
  90. year = item._id.year;
  91. break;
  92. }
  93. momentObj = moment().set('year', year || 0).set('month', month ? month - 1 : 0)
  94. .set('date', day || 1)
  95. .set('hour', 0)
  96. .set('minute', 0)
  97. .set('second', 0)
  98. .set('millisecond', 0);
  99. console.log(momentObj, 'momentObj');
  100. let formatter;
  101. let countStr;
  102. const valueObj = {};
  103. let count = item.count;
  104. if (config) {
  105. formatter = config[type];
  106. countStr = config.count;
  107. if (config.value) {
  108. config.value.forEach(str => {
  109. valueObj[str] = item[str].toString().indexOf('.') != -1 ? parseFloat(item[str].toFixed(2)) : item[str];
  110. });
  111. }
  112. }
  113. if (countStr) {
  114. if (utils.isFunction(countStr)) {
  115. count = countStr(item);
  116. } else {
  117. count = item[countStr];
  118. }
  119. }
  120. console.log(count, '我是count');
  121. return {
  122. label: this.kv[type].label(momentObj, formatter),
  123. value: count ? (count.toString().indexOf('.') != -1 ? parseFloat(count.toFixed(2)) : count) : count,
  124. ...valueObj,
  125. time: momentObj.valueOf(),
  126. };
  127. },
  128. range2(s1, s2, type, config) {
  129. if (s1 == '' || s2 == '') {
  130. return [];
  131. }
  132. const arr = [];
  133. const start = moment(s1);
  134. const end = moment(s2);
  135. console.log("start",start);
  136. console.log("end",end);
  137. let isLoop = start.isBefore(end);
  138. while (isLoop) {
  139. // if (arr.length == 0){
  140. // end = end.add(1, this.kv[type].unit);
  141. // }
  142. let formatter;
  143. const iV = this.initV;
  144. const valueObj = {};
  145. if (config) {
  146. formatter = config[type];
  147. if (config.value) {
  148. config.value.forEach(item => {
  149. valueObj[item] = iV;
  150. });
  151. }
  152. }
  153. arr.push({
  154. label: this.kv[type].label(start, formatter), value: iV,
  155. ...valueObj, time: start.valueOf(),
  156. });
  157. start.add(1, this.kv[type].unit);
  158. if (!start.isBefore(end, this.kv[type].unit)) {
  159. isLoop = false;
  160. }
  161. }
  162. return arr;
  163. },
  164. rangeTest(s1, s2, type, config) {
  165. if (s1 == '' || s2 == '') {
  166. return [];
  167. }
  168. const arr = [];
  169. const start = moment(s1);
  170. const end = moment(s2);
  171. console.log("start",start);
  172. console.log("end",end);
  173. let isLoop = start.isBefore(end);
  174. while (isLoop) {
  175. // if (arr.length == 0){
  176. // end = end.add(1, this.kv[type].unit);
  177. // }
  178. let formatter;
  179. const valueObj = {};
  180. if (config) {
  181. formatter = config[type];
  182. if (config.value) {
  183. config.value.forEach(item => {
  184. valueObj[item] = utils.randomNumC();
  185. });
  186. }
  187. }
  188. arr.push({
  189. _id: this.kv[type].test(start, formatter), count: utils.randomNumC(),
  190. ...valueObj, time: start.valueOf(),
  191. });
  192. start.add(1, this.kv[type].unit);
  193. if (!start.isBefore(end, this.kv[type].unit)) {
  194. isLoop = false;
  195. }
  196. }
  197. return arr;
  198. },
  199. rangeTestAdd(s1, s2, type, config) {
  200. if (s1 == '' || s2 == '') {
  201. return [];
  202. }
  203. const arr = [];
  204. const start = moment(s1);
  205. const end = moment(s2);
  206. let isLoop = start.isBefore(end);
  207. let num = 0;
  208. while (isLoop) {
  209. // if (arr.length == 0){
  210. // end = end.add(1, this.kv[type].unit);
  211. // }
  212. let formatter;
  213. const valueObj = {};
  214. num += utils.randomNumC();
  215. if (config) {
  216. formatter = config[type];
  217. if (config.value) {
  218. config.value.forEach(item => {
  219. valueObj[item] = num;
  220. num += utils.randomNumC();
  221. });
  222. }
  223. }
  224. arr.push({
  225. _id: this.kv[type].test(start, formatter), count: num,
  226. ...valueObj, time: start.valueOf(),
  227. });
  228. start.add(1, this.kv[type].unit);
  229. if (!start.isBefore(end, this.kv[type].unit)) {
  230. isLoop = false;
  231. }
  232. }
  233. return arr;
  234. },
  235. rangeTest2(s1, s2, type, kv) {
  236. if (s1 == '' || s2 == '') {
  237. return [];
  238. }
  239. const arr = [];
  240. const start = moment(s1);
  241. const end = moment(s2);
  242. let isLoop = start.isBefore(end);
  243. while (isLoop) {
  244. // if (arr.length == 0){
  245. // end = end.add(1, this.kv[type].unit);
  246. // }
  247. arr.push({
  248. _id: this.kv[type].test(start), data: Object.keys(kv).map(item => {
  249. return { count: utils.randomNumC(), _id: item };
  250. }),
  251. time: start.valueOf(),
  252. });
  253. start.add(1, this.kv[type].unit);
  254. if (!start.isBefore(end, this.kv[type].unit)) {
  255. isLoop = false;
  256. }
  257. }
  258. return arr;
  259. },
  260. rangeTest3(s1, s2, type, kv) {
  261. if (s1 == '' || s2 == '') {
  262. return [];
  263. }
  264. const arr = [];
  265. const start = moment(s1);
  266. const end = moment(s2);
  267. let isLoop = start.isBefore(end);
  268. while (isLoop) {
  269. // if (arr.length == 0){
  270. // end = end.add(1, this.kv[type].unit);
  271. // }
  272. arr.push({
  273. _id: this.kv[type].test(start), data: Object.keys(kv).map(item => {
  274. return { count: utils.randomNumC(), dsmCount: utils.randomNumC(), _id: item };
  275. }),
  276. time: start.valueOf(),
  277. });
  278. start.add(1, this.kv[type].unit);
  279. if (!start.isBefore(end, this.kv[type].unit)) {
  280. isLoop = false;
  281. }
  282. }
  283. return arr;
  284. },
  285. getQuery(s1, s2, type) {
  286. if (s1 == '' || s2 == '') {
  287. return {};
  288. }
  289. const startTime = moment(s1).valueOf();
  290. const endTime = moment(s2).add(1, this.kv[type].unit).valueOf();
  291. return { startTime, endTime, type };
  292. },
  293. getDefaultQuery(type) {
  294. let startTime = 0;
  295. let endTime = 0;
  296. switch (type) {
  297. case 0:
  298. // eslint-disable-next-line no-case-declarations
  299. const s1 = moment().subtract(1, 'day').set('date', 1)
  300. .set('hour', 0)
  301. .set('minute', 0)
  302. .set('second', 0)
  303. .set('millisecond', 0);
  304. startTime = s1.valueOf();
  305. // eslint-disable-next-line no-case-declarations
  306. const e1 = s1.add(1, 'month');
  307. endTime = e1.valueOf();
  308. break;
  309. case 1:
  310. // eslint-disable-next-line no-case-declarations
  311. const s2 = moment().subtract(1, 'day').set('month', 0)
  312. .set('date', 1)
  313. .set('hour', 0)
  314. .set('minute', 0)
  315. .set('second', 0)
  316. .set('millisecond', 0);
  317. startTime = s2.valueOf();
  318. // eslint-disable-next-line no-case-declarations
  319. const e2 = s2.add(1, 'year');
  320. endTime = e2.valueOf();
  321. break;
  322. case 1.1:
  323. // eslint-disable-next-line no-case-declarations
  324. const s21 = moment().subtract(1, 'day').set('month', 0)
  325. .set('date', 1)
  326. .set('hour', 0)
  327. .set('minute', 0)
  328. .set('second', 0)
  329. .set('millisecond', 0);
  330. startTime = s21.valueOf();
  331. // eslint-disable-next-line no-case-declarations
  332. const e21 = s21.add(1, 'month');
  333. endTime = e21.valueOf();
  334. type = 1;
  335. break;
  336. case 1.5:
  337. // eslint-disable-next-line no-case-declarations
  338. const s22 = moment().subtract(1, 'day').set('month', 0)
  339. .set('date', 1)
  340. .set('hour', 0)
  341. .set('minute', 0)
  342. .set('second', 0)
  343. .set('millisecond', 0);
  344. startTime = s22.valueOf();
  345. // eslint-disable-next-line no-case-declarations
  346. const e22 = s22.add(3, 'month');
  347. endTime = e22.valueOf();
  348. type = 1;
  349. break;
  350. case 2:
  351. // eslint-disable-next-line no-case-declarations
  352. const s3 = moment().subtract(1, 'day').set('month', 0)
  353. .set('date', 1)
  354. .set('hour', 0)
  355. .set('minute', 0)
  356. .set('second', 0)
  357. .set('millisecond', 0);
  358. endTime = s3.add(1, 'year').valueOf();
  359. // eslint-disable-next-line no-case-declarations
  360. const e3 = s3.subtract(3, 'year');
  361. startTime = e3.valueOf();
  362. break;
  363. }
  364. return { startTime, endTime, type };
  365. },
  366. };