import { HttpClient } from "@angular/common/http"; import { Component, ElementRef, OnInit, ViewContainerRef } from "@angular/core"; import { FormBuilder, FormGroup, Validators } from "@angular/forms"; import { Router } from "@angular/router"; import { fromEvent } from "rxjs"; import { debounceTime } from "rxjs/operators"; import * as moment from "moment"; import { NzModalService } from "ng-zorro-antd/modal"; import { NzMessageService } from "ng-zorro-antd/message"; import { TreeService } from "src/app/service/tree.service"; declare var abp: any; import "linqjs"; @Component({ selector: "app-bg-violation", templateUrl: "./bg-violation.component.html", styleUrls: ["./bg-violation.component.scss"], }) export class BgViolationComponent implements OnInit { validateForm!: FormGroup; constructor( private element: ElementRef, private toTree: TreeService, private http: HttpClient, private fb: FormBuilder, private router: Router, private modal: NzModalService, private viewContainerRef: ViewContainerRef, private message: NzMessageService ) {} tableScrollHeight; resizeListener; startdate; enddate; ngOnInit(): void { this.tableScrollHeight = "100px"; // 页面监听 this.resizeListener = fromEvent(window, "resize") .pipe(debounceTime(100)) .subscribe((event) => { let tableHeader = this.element.nativeElement.querySelector( `.ant-table-header` ).clientHeight; this.tableScrollHeight = document.getElementById("tablebox").clientHeight - tableHeader - 10 + "px"; }); //当前日期 let myDate: any = new Date(); let nowY = myDate.getFullYear(); let nowM = myDate.getMonth() + 1; let nowD = myDate.getDate(); this.enddate = nowY + "-" + (nowM < 10 ? "0" + nowM : nowM) + "-" + (nowD < 10 ? "0" + nowD : nowD); //当前日期 //获取三十天前日期 let lw = new Date(myDate - 1000 * 60 * 60 * 24 * 30); //最后一个数字30可改,30天的意思 let lastY = lw.getFullYear(); let lastM = lw.getMonth() + 1; let lastD = lw.getDate(); this.startdate = lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD); //三十天之前日期 this.validateForm = this.fb.group({ // level: [null], // organization: [null], // type: [null], // event: [null], // site: [null], // manufacturer: [null], //厂商 // operation: [null], //操作记录 // disposalState: [null], datePicker: [[this.enddate, this.enddate]], }); this.warningType(); this.tableSpin = true; this.getAllOrganization(); } ngOnDestroy(): void { this.resizeListener.unsubscribe(); } defaultOrId: string; //获取所有组织机构 nodes: any = []; getAllOrganization() { let OrganizationUnitId = sessionStorage.getItem("isGasStation") == "true" ? JSON.parse(sessionStorage.getItem("userdataOfgasstation")) .organization.id : JSON.parse(sessionStorage.getItem("userdata")).organization.id; let params = { OrganizationUnitId: OrganizationUnitId, IsContainsChildren: "true", }; this.http .get("/api/services/app/Organization/GetAll", { params: params, }) .subscribe((data: any) => { data.result.items.forEach((element) => { if (element.id == OrganizationUnitId) { element.parentId = null; } element.key = element.id; element.title = element.displayName; }); this.nodes = [...this.toTree.toTree(data.result.items)]; this.defaultOrId = JSON.parse( sessionStorage.getItem("userdata") ).organization.id; this.validateForm.value.organization = this.defaultOrId; this.getViolateRecordList(); }); } //获得违规记录列表 SkipCount: string = "0"; MaxResultCount: string = "50"; list: any = []; totalCount: string; tableSpin: boolean = false; getViolateRecordList() { let ViolationIds = []; if (this.validateForm.value.event) { ViolationIds.push(this.validateForm.value.event); } if (this.validateForm.value.type && !this.validateForm.value.event) { this.warningTypesDetails.forEach((item) => { item.id ? ViolationIds.push(item.id) : null; }); } let VendorName: any = null; if (this.validateForm.value.manufacturer) { VendorName = this.validateForm.value.manufacturer; } let NotificationState: any = "All"; if (this.validateForm.value.operation) { if (this.validateForm.value.operation === "null") { NotificationState = null; } else { NotificationState = this.validateForm.value.operation; } } let params = { // Level: this.validateForm.value.level, // ViolationIds: ViolationIds, // ViolateArea: this.validateForm.value.site, // OrganizationUnitId: this.validateForm.value.organization, // IsContainsChildren: "true", // CanVerify: "false", ViolateTime: this.validateForm.value.datePicker ? [ moment(this.validateForm.value.datePicker[0]).format("yyyy-MM-DD") + " 00:00:00", moment(this.validateForm.value.datePicker[1]).format("yyyy-MM-DD") + " 23:59:59", ] : null, // SkipCount: this.SkipCount, // MaxResultCount: this.MaxResultCount, //每页50条 // Positive: this.validateForm.value.disposalState, // VendorName: VendorName, // NotificationState: NotificationState, }; this.tableSpin = true; this.http .get( "/api/services/app/ViolateRecordVerification/GetPositiveRateByViolation", { params: params, } ) .subscribe( (data: any) => { this.tableSpin = false; console.log("列表数据", data); this.list = [...data.result.items]; this.totalCount = data.result.totalCount; setTimeout(() => { let tableHeader = this.element.nativeElement.querySelector( `.ant-table-header` ).clientHeight; this.tableScrollHeight = document.getElementById("tablebox").clientHeight - tableHeader - 10 + "px"; }, 0); // console.log("违规记录列表", data); // this.tableSpin = false; // console.log(this.list.length); }, (err) => { this.tableSpin = false; this.message.create("error", err.error.error.details); } ); } ngAfterViewInit(): void { fromEvent( this.element.nativeElement.querySelector(`#tbody`) as HTMLCanvasElement, "scroll" ) .pipe(debounceTime(100)) .subscribe((event: any) => { //监听 DOM 滚动事件 if ( event.target.scrollHeight - (event.target.scrollTop + event.target.clientHeight) <= 10 ) { if (this.totalCount > this.list.length) { console.log("需要加载数据了", event); this.SkipCount = String(Number(this.SkipCount) + 50); this.getViolateRecordList(); } } }); } getThirtyDays() { //获取当前日期 let myDate = new Date(); var nowY = myDate.getFullYear(); var nowM = myDate.getMonth() + 1; var nowD = myDate.getDate(); var enddateStr = nowY + "-" + (nowM < 10 ? "0" + nowM : nowM) + "-" + (nowD < 10 ? "0" + nowD : nowD); //当前日期 var enddate = new Date(enddateStr); //获取三十天前日期 var lw = new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 30); //最后一个数字30可改,30天的意思 var lastY = lw.getFullYear(); var lastM = lw.getMonth() + 1; var lastD = lw.getDate(); var startdateStr = lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-" + (lastD < 10 ? "0" + lastD : lastD); //三十天之前日期 var startDate = new Date(startdateStr); const dateList = []; while (true) { startDate.setDate(startDate.getDate() + 1); if (startDate.getTime() <= enddate.getTime()) { if (startDate.getDate() < 10) { // startDate.getFullYear() 获取年,此处没加上年份 dateList.push(startDate.getMonth() + 1 + ".0" + startDate.getDate()); } else { dateList.push(startDate.getMonth() + 1 + "." + startDate.getDate()); } } else { break; } } return dateList; } submitForm(): void { for (const i in this.validateForm.controls) { this.validateForm.controls[i].markAsDirty(); this.validateForm.controls[i].updateValueAndValidity(); } this.list = []; this.SkipCount = "0"; this.getViolateRecordList(); } resetForm(e: MouseEvent): void { e.preventDefault(); this.validateForm.reset(); for (const key in this.validateForm.controls) { this.validateForm.controls[key].markAsPristine(); this.validateForm.controls[key].updateValueAndValidity(); } this.validateForm.patchValue({ // organization: JSON.parse(sessionStorage.getItem("userdata")).organization // .id, datePicker: [this.enddate, this.enddate], }); this.list = []; this.SkipCount = "0"; this.getViolateRecordList(); } export($event) { const httpOptions = { responseType: "blob" as "json", params: { ViolateTime: this.validateForm.value.datePicker ? [ moment(this.validateForm.value.datePicker[0]).format( "yyyy-MM-DD" ) + " 00:00:00", moment(this.validateForm.value.datePicker[1]).format( "yyyy-MM-DD" ) + " 23:59:59", ] : null, }, }; this.http .get( `/api/services/app/ViolateRecordVerification/ExportPositiveRateByViolation`, httpOptions ) .subscribe( (data: any) => { // console.log('导出成功') // 文件名中有中文 则对文件名进行转码 const link = document.createElement("a"); const blob = new Blob([data], { type: "application/vnd.ms-excel" }); link.setAttribute("href", window.URL.createObjectURL(blob)); link.setAttribute("download", "按照预警类型统计" + ".xls"); link.style.visibility = "hidden"; document.body.appendChild(link); link.click(); document.body.removeChild(link); this.message.create("success", `导出成功`); }, (err) => { this.message.create("error", `导出失败`); } ); } //预警类型接口 warningTypes: any; //预警接口数据 warningTypesDetails: any; warningType() { this.http .get("/api/services/app/Violation/GetAllList") .subscribe((data: any) => { this.warningTypesDetails = data.result; this.warningTypes = (data.result as any).groupBy((t) => { return t.violationType; }); }); } typeChange(e: any) { this.warningTypes.forEach((element) => { if (element.key == e) { this.warningTypesDetails = element; } }); this.validateForm.patchValue({ event: null, }); } }