|
@@ -0,0 +1,268 @@
|
|
1
|
+import { Component, OnInit, Output, Input } from '@angular/core';
|
|
2
|
+import { EventEmitter } from '@angular/core';
|
|
3
|
+import { differenceInCalendarDays, endOfMonth, endOfYear, startOfDay, format, endOfDay, startOfMonth, startOfYear } from "date-fns";
|
|
4
|
+import { DateService } from 'src/app/services/date.service';
|
|
5
|
+
|
|
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 {
|
|
12
|
+ @Output() customChangeDateEmit = new EventEmitter();
|
|
13
|
+ @Input() queryType: number;
|
|
14
|
+ constructor(
|
|
15
|
+ private dateService: DateService,
|
|
16
|
+ ) { }
|
|
17
|
+
|
|
18
|
+ dateType: string = "day"; //选中时间维度
|
|
19
|
+ dateTypes: any = [
|
|
20
|
+ {
|
|
21
|
+ label: "按天",
|
|
22
|
+ value: "day",
|
|
23
|
+ },
|
|
24
|
+ {
|
|
25
|
+ label: "按月",
|
|
26
|
+ value: "month",
|
|
27
|
+ },
|
|
28
|
+ {
|
|
29
|
+ label: "按年",
|
|
30
|
+ value: "year",
|
|
31
|
+ },
|
|
32
|
+ ]; //时间维度
|
|
33
|
+ defRange = "1"; //默认上周
|
|
34
|
+ defRanges = [
|
|
35
|
+ {
|
|
36
|
+ label: "上周",
|
|
37
|
+ id: 1,
|
|
38
|
+ },
|
|
39
|
+ {
|
|
40
|
+ label: "上月",
|
|
41
|
+ id: 2,
|
|
42
|
+ },
|
|
43
|
+ {
|
|
44
|
+ label: "上年",
|
|
45
|
+ id: 3,
|
|
46
|
+ },
|
|
47
|
+ ]; //时间默认区间
|
|
48
|
+
|
|
49
|
+ dateRange: any = []; //发起时间区间 天
|
|
50
|
+ monthRangeStart: any; //发起时间 月 起
|
|
51
|
+ monthRangeEnd: any; //发起时间 月 止
|
|
52
|
+ yearRangeStart: any; //发起时间 年 起
|
|
53
|
+ yearRangeEnd: any; //发起时间 年 止
|
|
54
|
+
|
|
55
|
+ ngOnInit() {}
|
|
56
|
+
|
|
57
|
+ // this.customChangeDateEmit.emit({queryType, hospital, duty, parent});
|
|
58
|
+
|
|
59
|
+ // 修改时间展示维度
|
|
60
|
+ changeDateType(res) {
|
|
61
|
+ console.log(res, this.dateType);
|
|
62
|
+ this.dateType = res;
|
|
63
|
+ switch (res) {
|
|
64
|
+ case "day":
|
|
65
|
+ this.defRanges = [
|
|
66
|
+ {
|
|
67
|
+ label: "上周",
|
|
68
|
+ id: 1,
|
|
69
|
+ },
|
|
70
|
+ {
|
|
71
|
+ label: "上月",
|
|
72
|
+ id: 2,
|
|
73
|
+ },
|
|
74
|
+ {
|
|
75
|
+ label: "上年",
|
|
76
|
+ id: 3,
|
|
77
|
+ },
|
|
78
|
+ ]; //时间默认区间
|
|
79
|
+ this.defRange = "1"; //默认上周
|
|
80
|
+ this.changeDateRange("1");
|
|
81
|
+ break;
|
|
82
|
+ case "month":
|
|
83
|
+ this.defRanges = [
|
|
84
|
+ {
|
|
85
|
+ label: "上月",
|
|
86
|
+ id: 2,
|
|
87
|
+ },
|
|
88
|
+ {
|
|
89
|
+ label: "上年",
|
|
90
|
+ id: 3,
|
|
91
|
+ },
|
|
92
|
+ ]; //时间默认区间
|
|
93
|
+ this.defRange = "2"; //上月
|
|
94
|
+ this.changeDateRange("2");
|
|
95
|
+ break;
|
|
96
|
+ case "year":
|
|
97
|
+ this.defRanges = [
|
|
98
|
+ {
|
|
99
|
+ label: "上年",
|
|
100
|
+ id: 3,
|
|
101
|
+ },
|
|
102
|
+ ]; //时间默认区间
|
|
103
|
+ this.defRange = "3"; //默认上周
|
|
104
|
+ this.changeDateRange("3");
|
|
105
|
+ break;
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ // 禁选日期
|
|
110
|
+ disabledDate = (current: Date): boolean => {
|
|
111
|
+ // Can not select days before today and today
|
|
112
|
+ return differenceInCalendarDays(current, this.today) > 0;
|
|
113
|
+ };
|
|
114
|
+
|
|
115
|
+ // 禁选月份开始
|
|
116
|
+ disabledMonthStart = (current: Date): boolean => {
|
|
117
|
+ // Can not select days before today and today
|
|
118
|
+ let cur = differenceInCalendarDays(current, endOfMonth(this.today)) > 0;
|
|
119
|
+ let staEnd = differenceInCalendarDays(current, this.monthRangeEnd) > 0;
|
|
120
|
+ return cur || staEnd;
|
|
121
|
+ };
|
|
122
|
+ // 禁选月份结束
|
|
123
|
+ disabledMonthEnd = (current: Date): boolean => {
|
|
124
|
+ // Can not select days before today and today
|
|
125
|
+ let cur = differenceInCalendarDays(current, endOfMonth(this.today)) > 0;
|
|
126
|
+ let staEnd = differenceInCalendarDays(this.monthRangeStart, current) > 0;
|
|
127
|
+ return cur || staEnd;
|
|
128
|
+ };
|
|
129
|
+
|
|
130
|
+ // 禁选年份开始
|
|
131
|
+ disabledYearStart = (current: Date): boolean => {
|
|
132
|
+ // Can not select days before today and today
|
|
133
|
+ let cur = differenceInCalendarDays(current, endOfYear(this.today)) > 0;
|
|
134
|
+ let staEnd = differenceInCalendarDays(current, this.yearRangeEnd) > 0;
|
|
135
|
+ return cur || staEnd;
|
|
136
|
+ };
|
|
137
|
+
|
|
138
|
+ // 禁选年份结束
|
|
139
|
+ disabledYearEnd = (current: Date): boolean => {
|
|
140
|
+ // Can not select days before today and today
|
|
141
|
+ let cur = differenceInCalendarDays(current, endOfYear(this.today)) > 0;
|
|
142
|
+ let staEnd = differenceInCalendarDays(this.yearRangeStart, current) > 0;
|
|
143
|
+ return cur || staEnd;
|
|
144
|
+ };
|
|
145
|
+
|
|
146
|
+ // 日期选择 日
|
|
147
|
+ startDate: string; //发起时间开始
|
|
148
|
+ endDate: string; //发起时间结束
|
|
149
|
+ changeDate(result?): void {
|
|
150
|
+ console.log(this.dateRange);
|
|
151
|
+ console.log(result);
|
|
152
|
+ this.dateRange = result;
|
|
153
|
+ if (!this.quick) {
|
|
154
|
+ // 不是快捷选择
|
|
155
|
+ this.defRange = null;
|
|
156
|
+ }
|
|
157
|
+ if (!result || !result.length) {
|
|
158
|
+ this.startDate = this.endDate = "";
|
|
159
|
+ return;
|
|
160
|
+ }
|
|
161
|
+ this.startDate =
|
|
162
|
+ result[0].getFullYear() +
|
|
163
|
+ "-" +
|
|
164
|
+ (result[0].getMonth() + 1) +
|
|
165
|
+ "-" +
|
|
166
|
+ result[0].getDate();
|
|
167
|
+ this.endDate =
|
|
168
|
+ result[1].getFullYear() +
|
|
169
|
+ "-" +
|
|
170
|
+ (result[1].getMonth() + 1) +
|
|
171
|
+ "-" +
|
|
172
|
+ result[1].getDate();
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ // 月份选择
|
|
176
|
+ changeMonthStart(result?) {
|
|
177
|
+ console.log(result);
|
|
178
|
+ this.monthRangeStart = result;
|
|
179
|
+ if (!this.quick) {
|
|
180
|
+ // 不是快捷选择
|
|
181
|
+ this.defRange = null;
|
|
182
|
+ }
|
|
183
|
+ if (!result) {
|
|
184
|
+ this.startDate = this.endDate = "";
|
|
185
|
+ return;
|
|
186
|
+ }
|
|
187
|
+ this.startDate = format(startOfMonth(result), 'yyyy-MM-dd');
|
|
188
|
+ }
|
|
189
|
+ changeMonthEnd(result?) {
|
|
190
|
+ console.log(result);
|
|
191
|
+ this.monthRangeEnd = result;
|
|
192
|
+ if (!this.quick) {
|
|
193
|
+ // 不是快捷选择
|
|
194
|
+ this.defRange = null;
|
|
195
|
+ }
|
|
196
|
+ if (!result) {
|
|
197
|
+ this.startDate = this.endDate = "";
|
|
198
|
+ return;
|
|
199
|
+ }
|
|
200
|
+ this.endDate = format(endOfMonth(result), 'yyyy-MM-dd');
|
|
201
|
+ }
|
|
202
|
+ // 年份选择
|
|
203
|
+ changeYearStart(result?) {
|
|
204
|
+ console.log(result);
|
|
205
|
+ this.yearRangeStart = result;
|
|
206
|
+ if (!this.quick) {
|
|
207
|
+ // 不是快捷选择
|
|
208
|
+ this.defRange = null;
|
|
209
|
+ }
|
|
210
|
+ if (!result) {
|
|
211
|
+ this.startDate = this.endDate = "";
|
|
212
|
+ return;
|
|
213
|
+ }
|
|
214
|
+ this.startDate = format(startOfYear(result), 'yyyy-MM-dd');
|
|
215
|
+ // this.endDate = result.getFullYear() + '-01-01';
|
|
216
|
+ }
|
|
217
|
+ changeYearEnd(result?) {
|
|
218
|
+ console.log(result);
|
|
219
|
+ this.yearRangeEnd = result;
|
|
220
|
+ if (!this.quick) {
|
|
221
|
+ // 不是快捷选择
|
|
222
|
+ this.defRange = null;
|
|
223
|
+ }
|
|
224
|
+ if (!result) {
|
|
225
|
+ this.startDate = this.endDate = "";
|
|
226
|
+ return;
|
|
227
|
+ }
|
|
228
|
+ this.endDate = format(endOfYear(result), 'yyyy-MM-dd');
|
|
229
|
+ }
|
|
230
|
+
|
|
231
|
+ // 日期选择 快速修改时间区间
|
|
232
|
+ today = new Date();
|
|
233
|
+ quick: boolean = false;
|
|
234
|
+ changeDateRange(res) {
|
|
235
|
+ console.log(res);
|
|
236
|
+ this.quick = true;
|
|
237
|
+ switch (res) {
|
|
238
|
+ case "1":
|
|
239
|
+ // 上周
|
|
240
|
+ let lastweekstartdate = this.dateService.date().lastWeekStartDate;
|
|
241
|
+ let lastweekenddate = this.dateService.date().lastWeekEndDate;
|
|
242
|
+ console.log(lastweekstartdate, lastweekenddate);
|
|
243
|
+ this.changeDate([lastweekstartdate, lastweekenddate]);
|
|
244
|
+ break;
|
|
245
|
+ case "2":
|
|
246
|
+ // 上月
|
|
247
|
+ let lastmonthstartdate = this.dateService.date().lastMonthStartDate;
|
|
248
|
+ let lastmonthenddate = this.dateService.date().lastMonthEndDate;
|
|
249
|
+ console.log(lastmonthstartdate, lastmonthenddate);
|
|
250
|
+ this.changeDate([lastmonthstartdate, lastmonthenddate]);
|
|
251
|
+ this.changeMonthStart(lastmonthstartdate);
|
|
252
|
+ this.changeMonthEnd(lastmonthenddate);
|
|
253
|
+ break;
|
|
254
|
+ case "3":
|
|
255
|
+ // 上年
|
|
256
|
+ let lastyearstartdate = this.dateService.date().lastYearStartDate;
|
|
257
|
+ let lastyearenddate = this.dateService.date().lastYearEndDate;
|
|
258
|
+ console.log(lastyearstartdate, lastyearenddate);
|
|
259
|
+ this.changeDate([lastyearstartdate, lastyearenddate]);
|
|
260
|
+ this.changeMonthStart(lastyearstartdate);
|
|
261
|
+ this.changeMonthEnd(lastyearenddate);
|
|
262
|
+ this.changeYearStart(lastyearstartdate);
|
|
263
|
+ this.changeYearEnd(lastyearenddate);
|
|
264
|
+ break;
|
|
265
|
+ }
|
|
266
|
+ this.quick = false;
|
|
267
|
+ }
|
|
268
|
+}
|