import { MsgHelper } from 'rh-base/core'; import { ControlValueAccessor } from '@angular/forms'; import { RhSafeAny } from 'rh-base/model'; import { ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output, SimpleChanges, OnChanges } from '@angular/core'; @Component({ selector: 'rh-multiple-choice-date-picker', templateUrl: './multiple-choice-date-picker.component.html', styleUrls: ['./multiple-choice-date-picker.component.less'] }) export class MultipleChoiceDatePickerComponent implements OnInit, ControlValueAccessor, OnChanges { // 最小日期 @Input() minDate!: string; // 最大日期 @Input() maxDate!: string; //显示选择周六或周日 @Input() rhCheckWeekend = false; /** 选中的日期 */ @Output() rhDatePicker = new EventEmitter(); /** 选择周日 */ @Output() rhcheckSunDay = new EventEmitter(); /** 选择周六 */ @Output() rhcheckSutDay = new EventEmitter(); /**是否忽略所有周六 */ @Input() isCheckSutDay!: boolean; /**是否忽略所有周日 */ @Input() isCheckSunDay!: boolean; /** 编辑中所选中的日期 */ @Input() editSelectDates: string[] = []; /** 编辑 or 新建 */ @Input() rhStatus!: number; /** 清空所有选中 */ @Input() rhReset!: number; /** 不清空 */ @Input() rhUnReset = false; /** 是否数据化模式 */ @Input() rhVisualScan!: string; /** 模式变化 */ @Output() rhModeSwitchChange = new EventEmitter(); day = new Date(); // 存放所有日期 firstDatarows: RhSafeAny[] = []; secondDatarows: RhSafeAny[] = []; threeDatarows: RhSafeAny[] = []; fourDatarows: RhSafeAny[] = []; fiveDatarows: RhSafeAny[] = []; sixDatarows: RhSafeAny[] = []; allDateRows: RhSafeAny[] = []; // 当前日期 today!: string; finalday!: number; //最小年份 minYear!: number; //最小月份 minMonth!: number; //最小天数 minDay!: number; //最大年份 maxYear!: number; //最大月份 maxMonth!: number; //最大天数 maxDay!: number; // 当前月 month!: number; date!: number; // 当前年 year!: number; //选择的年份 slectYear!: number; selectday!: string; selectDates: string[] = []; weekday = ['日', '一', '二', '三', '四', '五', '六']; //选中的日期 clickDate: string[] = []; /****** 数据化模式********* */ /** 年份 */ yearNumber = ''; /** 通过年份查询出的日期 */ yearNumberSetDates: string[] = []; /** 月份 */ monthNumber = ''; /** 通过月份查询出的日期 */ monthNumberSetDates: string[] = []; /** 周次 */ weekNumber = ''; /** 通过周次查询出的日期 */ weekNumberSetDates: string[] = []; /** 通过搜索条件查询出的所有日期 */ TableDataset: RhSafeAny[] = []; /** 是否为搜索状态 */ isSubmit = false; /** 月份下拉框 */ monthDataset = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']; /** 周次下拉框 */ weekDataset = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']; _oldValue!: string | number | Date; _value!: string | number | Date; get value() { return this._value; } set value(value: string | number | Date) { this._oldValue = this._value; this._value = value; if (this._oldValue !== this._value) { this.onChange(value); } } onChange: (value: RhSafeAny) => void = () => { // }; onTouched: () => RhSafeAny = () => { // }; constructor(private cdr: ChangeDetectorRef) { // } ngOnInit(): void { this.month = this.getData().get().getMonth(); this.finalday = this.month; this.year = this.getData().get().getFullYear(); this.slectYear = this.year; this.date = this.getData().get().getDate(); this.today = `${this.year}年${this.month + 1 > 9 ? this.month + 1 : `0${this.month + 1}`}月`; this.rhVisualScan = 'scan'; this.dateTableList(); } ngOnChanges(changes: SimpleChanges) { if (changes) { this.getMinAndMax(); this.clickDate = []; if (changes['rhReset']) { this.selectDates = []; } if (changes['rhStatus'] && changes['rhStatus'].currentValue === 1) { this.selectDates = []; if (this.rhUnReset && changes['editSelectDates']) { this.selectDates = changes['editSelectDates'].currentValue; } if (this.rhUnReset && (changes['isCheckSutDay'] || changes['isCheckSunDay'])) { this.isCheckSutDay = changes['isCheckSutDay'].currentValue; this.isCheckSunDay = changes['isCheckSunDay'].currentValue; } } else if (this.rhStatus === 2) { if (changes['editSelectDates']) { this.selectDates = changes['editSelectDates'].currentValue; } if (this.editSelectDates.length) { this.selectDates = []; this.editSelectDates.forEach((ele) => { this.selectDates.push(ele); }); } } if (this.selectDates.length) { this.selectDates.forEach((ele, index) => { if (this.minDate && this.maxDate) { //转成成数组,分别为年,月,日,下同 const Start = this.minDate.split('-'); const End = this.maxDate.split('-'); const selected = ele.split('-'); const startDate = Start[0] + '/' + Start[1] + '/' + Start[2]; const endDate = End[0] + '/' + End[1] + '/' + End[2]; const selectedDate = selected[0] + '/' + selected[1] + '/' + selected[2]; // 找出selectDates中的日期小于最小日期,大于最大日期的数据,删除 if (selectedDate < startDate) { this.selectDates.splice(index, 1); } if (selectedDate > endDate) { this.selectDates.splice(index, 1); } } }); } else { this.selectDates = []; this.clickDate = []; } this.selectDates = [...this.selectDates]; //给选中的日期排序(从小到大排序) this.sortDataArray(this.selectDates); this.rhDatePicker.emit(this.selectDates); let yMdArray: string[] = []; this.selectDates.forEach((ele) => { yMdArray = ele.split('-'); const dayNumber = parseInt(yMdArray[2]); yMdArray[2] = dayNumber.toString(); const selecteddate = yMdArray[0] + '年' + yMdArray[1] + '月' + yMdArray[2]; this.clickDate.push(selecteddate); }); } } /** * 获取日期 */ getData() { let myDate = this.minDate ? new Date(this.minDate) : new Date(); return { get: () => { return myDate; }, set: (date: Date) => { myDate = date; } }; } //给日期数组排序(从小到大) sortDataArray(dateArray: RhSafeAny[]) { //日期排序 return dateArray.sort((a, b) => { return Date.parse(a) - Date.parse(b); }); } clickDay(date: RhSafeAny, today: string) { //每次选择日期之前置空clickDate this.getMinAndMax(); if (!date || (date > this.maxDay && this.month + 1 === this.maxMonth) || (date < this.minDay && this.month + 1 === this.minMonth)) { return; } this.clickDate = []; const year = today.indexOf('年'); const selectYear = today.substring(0, year); const month = today.indexOf('月'); const selectMonth = today.substring(5, month); if (parseInt(date) < 10) { date = '0' + date; } const selectdate = selectYear + '-' + selectMonth + '-' + date; const Index = this.selectDates.findIndex((s) => s === selectdate); if (Index > -1) { //说明已经是选中状态 this.selectDates.splice(Index, 1); } else { //否则选中它 this.selectDates.push(selectdate); } //给选中的日期排序 this.sortDataArray(this.selectDates); this.rhDatePicker.emit(this.selectDates); let yMdArray: string[] = []; this.selectDates.forEach((ele) => { yMdArray = ele.split('-'); const dayNumber = parseInt(yMdArray[2]); yMdArray[2] = dayNumber.toString(); const selecteddate = yMdArray[0] + '年' + yMdArray[1] + '月' + yMdArray[2]; this.clickDate.push(selecteddate); }); } /** * 上个月 */ behindMonth() { this.getMinAndMax(); this.month--; if (this.month + 1 > this.maxMonth || this.month + 1 < this.minMonth) { MsgHelper.ShowWarningMessage('请选择在有效起始日期到有效结束日期之间的日期'); this.month++; return; } if (this.month < 0) { this.month = 11; this.today = `${--this.year}年${this.month + 1 > 9 ? this.month + 1 : `0${this.month + 1}`}月`; this.changeDateTable(this.year, this.month); } else { this.today = `${this.year}年${this.month + 1 > 9 ? this.month + 1 : `0${this.month + 1}`}月`; this.changeDateTable(this.year, this.month); } } /** * 上一年 */ behindyear() { this.getMinAndMax(); this.year--; if (this.year > this.maxYear || this.year < this.minYear) { MsgHelper.ShowWarningMessage('请选择在有效起始日期到有效结束日期之间的日期'); this.year++; return; } this.today = `${this.year}年${this.month + 1 > 9 ? this.month + 1 : `0${this.month + 1}`}月`; this.changeDateTable(this.year, this.month); } /** * 改变日期表格 */ changeDateTable(year: number, month: number) { const firstday = new Date(year, month, 1); const finallday = new Date(year, month + 1, 0); const beforeday = new Date(year, month, 0); let daycount = beforeday.getDate(); for (let i = 1; i <= finallday.getDate(); i++) { this.allDateRows.push(i); } this.firstDatarows = this.allDateRows.splice(0, 7 - firstday.getDay()); for (let i = 0; i < firstday.getDay(); i++) { this.firstDatarows.unshift(''); daycount--; } this.secondDatarows = this.allDateRows.splice(0, 7); this.threeDatarows = this.allDateRows.splice(0, 7); this.fourDatarows = this.allDateRows.splice(0, 7); this.fiveDatarows = this.allDateRows.splice(0, 7); this.sixDatarows = this.allDateRows.splice(0); } /** * 表格列表 */ dateTableList() { const mydate = this.getData().get(); const firstday = new Date(mydate.getFullYear(), mydate.getMonth(), 1); const finallday = new Date(mydate.getFullYear(), mydate.getMonth() + 1, 0); const beforeday = new Date(mydate.getFullYear(), mydate.getMonth(), 0); let daycount = beforeday.getDate(); for (let i = 1; i <= finallday.getDate(); i++) { this.allDateRows.push(i); } for (let i = 0; i < firstday.getDay(); i++) { this.allDateRows.unshift(''); daycount--; } this.firstDatarows = this.allDateRows.splice(0, 7); this.secondDatarows = this.allDateRows.splice(0, 7); this.threeDatarows = this.allDateRows.splice(0, 7); this.fourDatarows = this.allDateRows.splice(0, 7); this.fiveDatarows = this.allDateRows.splice(0, 7); this.sixDatarows = this.allDateRows.splice(0, 7); } /** * 下一年 */ nextyear() { this.getMinAndMax(); this.year++; if (this.year > this.maxYear || this.year < this.minYear) { MsgHelper.ShowWarningMessage('请选择在有效起始日期到有效结束日期之间的日期'); this.year--; return; } this.today = `${this.year}年${this.month + 1 > 9 ? this.month + 1 : `0${this.month + 1}`}月`; this.changeDateTable(this.year, this.month); } /** * 下个月 */ nextMonth() { this.getMinAndMax(); this.month++; if (this.month + 1 > this.maxMonth || this.month + 1 < this.minMonth) { MsgHelper.ShowWarningMessage('请选择在有效起始日期到有效结束日期之间的日期'); this.month--; return; } if (this.month > 11) { this.month = 0; this.today = `${++this.year}年${this.month + 1 > 9 ? this.month + 1 : `0${this.month + 1}`}月`; this.changeDateTable(this.year, this.month); } else { this.today = `${this.year}年${this.month + 1 > 9 ? this.month + 1 : `0${this.month + 1}`}月`; this.changeDateTable(this.year, this.month); } } writeValue(obj: RhSafeAny): void { this.value = obj; this.cdr.markForCheck(); } registerOnChange(fn: (value: RhSafeAny) => void): void { this.onChange = fn; } registerOnTouched(fn: () => RhSafeAny): void { this.onTouched = fn; } checkSutDay(status: boolean) { this.isCheckSutDay = status; this.rhcheckSutDay.emit(status); } checkSunDay(status: boolean) { this.isCheckSunDay = status; this.rhcheckSunDay.emit(status); } getMinAndMax() { if (this.minDate && this.maxDate) { let minDateArray = []; minDateArray = this.minDate.split('-'); this.minYear = parseInt(minDateArray[0]); this.minMonth = parseInt(minDateArray[1]); this.minDay = parseInt(minDateArray[2]); let maxDateArray = []; maxDateArray = this.maxDate.split('-'); this.maxYear = parseInt(maxDateArray[0]); this.maxMonth = parseInt(maxDateArray[1]); this.maxDay = parseInt(maxDateArray[2]); } else { // } } modeSwitchChange(status: boolean) { this.selectDates = [...this.selectDates]; this.rhModeSwitchChange.emit(status); //可视化模式 if (status === false) { this.isSubmit = false; this.reset(); } } /********数据化模式***** */ // 前端搜索 private filterOption(inputValue: string, item: RhSafeAny): boolean { return item.indexOf(inputValue) > -1; } // 通过年份搜索所选日期 yearNumberToSearch() { this.yearNumberSetDates = this.selectDates; this.yearNumberSetDates = this.yearNumberSetDates.filter((item) => { const dateArray = item.split('-'); return this.filterOption(this.yearNumber, dateArray[0]); }); this.yearNumberSetDates = [...this.yearNumberSetDates]; } // 通过月份搜索所选日期 monthNumberToSearch() { this.monthNumberSetDates = this.selectDates; this.monthNumberSetDates = this.monthNumberSetDates.filter((item) => { const dateArray = item.split('-'); return this.filterOption(this.monthNumber, dateArray[1]); }); this.monthNumberSetDates = [...this.monthNumberSetDates]; } // 通过周次搜索所选日期 weekNumberToSearch() { this.weekNumberSetDates = this.selectDates; this.weekNumberSetDates = this.weekNumberSetDates.filter((item) => { return this.filterOption(this.weekNumber, this.getWeekNumber(item)); }); this.weekNumberSetDates = [...this.weekNumberSetDates]; } /*** 通过日期来获取周次 */ getWeekNumber(date: string) { const weekArray = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']; const week = weekArray[new Date(date).getDay()]; return week; } // 搜索 submit() { this.isSubmit = true; // 搜索年份 if (this.yearNumber && this.yearNumber.trim() !== '') { this.yearNumberToSearch(); //是否存在该年份日期,若不存在,则为空 if (this.yearNumberSetDates.length) { this.TableDataset = this.yearNumberSetDates; } else { this.TableDataset = []; } // 搜索月份 if (this.monthNumber && this.monthNumber.trim() !== '') { this.monthNumberToSearch(); //是否存在该年份,月份的日期,若不存在,则为空 if (this.monthNumberSetDates.length) { //获取数组间的交集 this.TableDataset = this.yearNumberSetDates.filter((val) => { return this.monthNumberSetDates.indexOf(val) > -1; }); } else { this.TableDataset = []; } // 搜索周次 if (this.weekNumber && this.weekNumber.trim() !== '') { this.weekNumberToSearch(); // 是否存在该年份,月份,周次的日期 if (this.weekNumberSetDates.length) { this.TableDataset = this.TableDataset.filter((val) => { return this.weekNumberSetDates.indexOf(val) > -1; }); } else { this.TableDataset = []; } } } } //不搜索年份,搜索月份 else if (this.monthNumber && this.monthNumber.trim() !== '') { this.monthNumberToSearch(); // 是否存在该月份的日期 if (this.monthNumberSetDates.length) { this.TableDataset = this.monthNumberSetDates; } else { this.TableDataset = []; } if (this.weekNumber && this.weekNumber.trim() !== '') { this.weekNumberToSearch(); // 是否存在该月份,周次的日期 if (this.weekNumberSetDates.length) { this.TableDataset = this.TableDataset.filter((val) => { return this.weekNumberSetDates.indexOf(val) > -1; }); } else { this.TableDataset = []; } } } //不搜索年份,不搜索月份,搜索周次 else if (this.weekNumber && this.weekNumber.trim() !== '') { this.weekNumberToSearch(); // 是否存在该周次的日期 if (this.weekNumberSetDates.length) { this.TableDataset = this.weekNumberSetDates; } else { this.TableDataset = []; } } else { this.TableDataset = this.selectDates; } this.TableDataset = [...this.TableDataset]; } /** 清空搜索框 */ reset() { this.weekNumber = ''; this.monthNumber = ''; this.yearNumber = ''; } }