custom-change-date.component.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import { Component, OnInit, Input, OnDestroy } from '@angular/core';
  2. import { differenceInCalendarDays, endOfMonth, endOfYear, startOfDay, format, endOfDay, startOfMonth, startOfYear } from "date-fns";
  3. import { DateService } from 'src/app/services/date.service';
  4. import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
  5. import { debounceTime, filter } from 'rxjs/operators';
  6. @Component({
  7. selector: 'app-custom-change-date',
  8. templateUrl: './custom-change-date.component.html',
  9. styleUrls: ['./custom-change-date.component.less']
  10. })
  11. export class CustomChangeDateComponent implements OnInit, OnDestroy {
  12. @Input() isShowType: boolean = true;
  13. // @Input() dateData1: any;
  14. // @Input() dateData2: any;
  15. @Input() name: string = '建单时间';
  16. constructor(
  17. private dateService: DateService,
  18. private router: Router,
  19. ) { }
  20. dateType: string = "day"; //选中时间维度
  21. dateTypes: any = [
  22. {
  23. label: "按天",
  24. value: "day",
  25. },
  26. {
  27. label: "按月",
  28. value: "month",
  29. },
  30. {
  31. label: "按年",
  32. value: "year",
  33. },
  34. ]; //时间维度
  35. defRange = 6; //默认上月
  36. defRanges = [
  37. {
  38. label: "昨日",
  39. id: 1,
  40. },
  41. {
  42. label: "本周",
  43. id: 2,
  44. },
  45. {
  46. label: "本月",
  47. id: 3,
  48. },
  49. {
  50. label: "本年",
  51. id: 4,
  52. },
  53. {
  54. label: "上周",
  55. id: 5,
  56. },
  57. {
  58. label: "上月",
  59. id: 6,
  60. },
  61. {
  62. label: "上年",
  63. id: 7,
  64. },
  65. ]; //时间默认区间
  66. dateRange: any = []; //发起时间区间 天
  67. monthRangeStart: any; //发起时间 月 起
  68. monthRangeEnd: any; //发起时间 月 止
  69. yearRangeStart: any; //发起时间 年 起
  70. yearRangeEnd: any; //发起时间 年 止
  71. ngOnInit() {
  72. this.resetByDate();
  73. // this.router.events
  74. // .pipe(filter(event => event instanceof NavigationEnd))
  75. // .subscribe((event: NavigationEnd) => {
  76. // if(event.urlAfterRedirects.endsWith('/incidentList')){
  77. // this.resetByDate();
  78. // }
  79. // });
  80. }
  81. ngOnDestroy(){
  82. // this.dateData1 = null
  83. // this.dateData2 = null
  84. }
  85. // 根据时间区间重置
  86. resetByDate(){
  87. let newStatistics = JSON.parse(sessionStorage.getItem('newStatistics'));
  88. this.initByDate(newStatistics.defaultDate)
  89. }
  90. // 根据时间区间初始化
  91. initByDate(dateRange:any[], dateNum:any = null){
  92. setTimeout(() => {
  93. this.dateType = 'day';
  94. this.dateRange = dateRange;
  95. this.changeDateType(this.dateType, dateNum);
  96. }, 0)
  97. }
  98. // 修改时间展示维度
  99. changeDateType(res, defRange) {
  100. console.log(res, this.dateType);
  101. this.dateType = res;
  102. switch (res) {
  103. case "day":
  104. this.defRanges = [
  105. {
  106. label: "昨日",
  107. id: 1,
  108. },
  109. {
  110. label: "本周",
  111. id: 2,
  112. },
  113. {
  114. label: "本月",
  115. id: 3,
  116. },
  117. {
  118. label: "上周",
  119. id: 5,
  120. },
  121. {
  122. label: "上月",
  123. id: 6,
  124. },
  125. {
  126. label: "上年",
  127. id: 7,
  128. },
  129. ]; //时间默认区间
  130. this.defRange = defRange === undefined ? this.defRanges[0].id : defRange;
  131. this.changeDateRange(defRange === undefined ? this.defRanges[0].id : defRange);
  132. break;
  133. case "month":
  134. this.defRanges = [
  135. {
  136. label: "本月",
  137. id: 3,
  138. },
  139. {
  140. label: "上月",
  141. id: 6,
  142. },
  143. {
  144. label: "上年",
  145. id: 7,
  146. },
  147. ]; //时间默认区间
  148. this.defRange = defRange === undefined ? this.defRanges[0].id : defRange;
  149. this.changeDateRange(defRange === undefined ? this.defRanges[0].id : defRange);
  150. break;
  151. case "year":
  152. this.defRanges = [
  153. {
  154. label: "本年",
  155. id: 4,
  156. },
  157. {
  158. label: "上年",
  159. id: 7,
  160. },
  161. ]; //时间默认区间
  162. this.defRange = defRange === undefined ? this.defRanges[0].id : defRange;
  163. this.changeDateRange(defRange === undefined ? this.defRanges[0].id : defRange);
  164. break;
  165. }
  166. }
  167. // 禁选日期
  168. disabledDate = (current: Date): boolean => {
  169. // Can not select days before today and today
  170. return false;
  171. };
  172. // 禁选月份开始
  173. disabledMonthStart = (current: Date): boolean => {
  174. // Can not select days before today and today
  175. let cur = differenceInCalendarDays(current, endOfMonth(this.today)) > 0;
  176. let staEnd = differenceInCalendarDays(current, this.monthRangeEnd) > 0;
  177. return cur || staEnd;
  178. };
  179. // 禁选月份结束
  180. disabledMonthEnd = (current: Date): boolean => {
  181. // Can not select days before today and today
  182. let staEnd = differenceInCalendarDays(this.monthRangeStart, current) > 0;
  183. return staEnd;
  184. };
  185. // 禁选年份开始
  186. disabledYearStart = (current: Date): boolean => {
  187. // Can not select days before today and today
  188. let cur = differenceInCalendarDays(current, endOfYear(this.today)) > 0;
  189. let staEnd = differenceInCalendarDays(current, this.yearRangeEnd) > 0;
  190. return cur || staEnd;
  191. };
  192. // 禁选年份结束
  193. disabledYearEnd = (current: Date): boolean => {
  194. // Can not select days before today and today
  195. let staEnd = differenceInCalendarDays(this.yearRangeStart, current) > 0;
  196. return staEnd;
  197. };
  198. // 日期选择 日
  199. startDate: string; //发起时间开始
  200. endDate: string; //发起时间结束
  201. changeDate(result): void {
  202. this.dateRange = result;
  203. if (!this.quick) {
  204. // 不是快捷选择
  205. this.defRange = null;
  206. }
  207. if (!result || !result.length) {
  208. this.startDate = this.endDate = "";
  209. return;
  210. }
  211. this.startDate = format(startOfDay(result[0]), "yyyy-MM-dd HH:mm:ss");
  212. this.endDate = format(endOfDay(result[1]), "yyyy-MM-dd HH:mm:ss");
  213. }
  214. // 月份选择
  215. changeMonthStart(result) {
  216. console.log(result);
  217. this.monthRangeStart = result;
  218. if (!this.quick) {
  219. // 不是快捷选择
  220. this.defRange = null;
  221. }
  222. if (!result) {
  223. this.startDate = this.endDate = "";
  224. return;
  225. }
  226. this.startDate = format(startOfMonth(result), 'yyyy-MM-dd HH:mm:ss');
  227. }
  228. changeMonthEnd(result) {
  229. console.log(result);
  230. this.monthRangeEnd = result;
  231. if (!this.quick) {
  232. // 不是快捷选择
  233. this.defRange = null;
  234. }
  235. if (!result) {
  236. this.startDate = this.endDate = "";
  237. return;
  238. }
  239. this.endDate = format(endOfMonth(result), 'yyyy-MM-dd HH:mm:ss');
  240. }
  241. // 年份选择
  242. changeYearStart(result) {
  243. console.log(result);
  244. this.yearRangeStart = result;
  245. if (!this.quick) {
  246. // 不是快捷选择
  247. this.defRange = null;
  248. }
  249. if (!result) {
  250. this.startDate = this.endDate = "";
  251. return;
  252. }
  253. this.startDate = format(startOfYear(result), 'yyyy-MM-dd HH:mm:ss');
  254. }
  255. changeYearEnd(result) {
  256. console.log(result);
  257. this.yearRangeEnd = result;
  258. if (!this.quick) {
  259. // 不是快捷选择
  260. this.defRange = null;
  261. }
  262. if (!result) {
  263. this.startDate = this.endDate = "";
  264. return;
  265. }
  266. this.endDate = format(endOfYear(result), 'yyyy-MM-dd HH:mm:ss');
  267. }
  268. // 日期选择 快速修改时间区间
  269. today = new Date();
  270. quick: boolean = false;
  271. changeDateRange(res) {
  272. console.log(res);
  273. this.quick = true;
  274. switch (res) {
  275. case 1:
  276. // 昨日
  277. let yesterdaystartdate = this.dateService.date().yesterdayStartDate;
  278. let yesterdayenddate = this.dateService.date().yesterdayEndDate;
  279. console.log(yesterdaystartdate, yesterdayenddate);
  280. this.changeDate([yesterdaystartdate, yesterdayenddate]);
  281. break;
  282. case 2:
  283. // 本周
  284. let thisweekstartdate = this.dateService.date().thisWeekStartDate;
  285. let thisweekenddate = this.dateService.date().thisWeekEndDate;
  286. console.log(thisweekstartdate, thisweekenddate);
  287. this.changeDate([thisweekstartdate, thisweekenddate]);
  288. break;
  289. case 3:
  290. // 本月
  291. let thismonthstartdate = this.dateService.date().thisMonthStartDate;
  292. let thismonthenddate = this.dateService.date().thisMonthEndDate;
  293. console.log(thismonthstartdate, thismonthenddate);
  294. this.changeDate([thismonthstartdate, thismonthenddate]);
  295. this.changeMonthStart(thismonthstartdate);
  296. this.changeMonthEnd(thismonthenddate);
  297. break;
  298. case 4:
  299. // 本年
  300. let thisyearstartdate = this.dateService.date().thisYearStartDate;
  301. let thisyearenddate = this.dateService.date().thisYearEndDate;
  302. console.log(thisyearstartdate, thisyearenddate);
  303. this.changeDate([thisyearstartdate, thisyearenddate]);
  304. this.changeMonthStart(thisyearstartdate);
  305. this.changeMonthEnd(thisyearenddate);
  306. this.changeYearStart(thisyearstartdate);
  307. this.changeYearEnd(thisyearenddate);
  308. break;
  309. case 5:
  310. // 上周
  311. let lastweekstartdate = this.dateService.date().lastWeekStartDate;
  312. let lastweekenddate = this.dateService.date().lastWeekEndDate;
  313. console.log(lastweekstartdate, lastweekenddate);
  314. this.changeDate([lastweekstartdate, lastweekenddate]);
  315. break;
  316. case 6:
  317. // 上月
  318. let lastmonthstartdate = this.dateService.date().lastMonthStartDate;
  319. let lastmonthenddate = this.dateService.date().lastMonthEndDate;
  320. console.log(lastmonthstartdate, lastmonthenddate);
  321. this.changeDate([lastmonthstartdate, lastmonthenddate]);
  322. this.changeMonthStart(lastmonthstartdate);
  323. this.changeMonthEnd(lastmonthenddate);
  324. break;
  325. case 7:
  326. // 上年
  327. let lastyearstartdate = this.dateService.date().lastYearStartDate;
  328. let lastyearenddate = this.dateService.date().lastYearEndDate;
  329. console.log(lastyearstartdate, lastyearenddate);
  330. this.changeDate([lastyearstartdate, lastyearenddate]);
  331. this.changeMonthStart(lastyearstartdate);
  332. this.changeMonthEnd(lastyearenddate);
  333. this.changeYearStart(lastyearstartdate);
  334. this.changeYearEnd(lastyearenddate);
  335. break;
  336. default:
  337. // 回显initByDate
  338. this.changeDate([new Date(this.dateRange[0]), new Date(this.dateRange[1])]);
  339. break;
  340. }
  341. this.quick = false;
  342. }
  343. }