import { Component, OnInit, ViewChild } from '@angular/core'; import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms'; import { CodeClassModel, DataResultT, RhBaseQueryDto, RhDynamicTableFieldDto, RhHttpRequestSettingDto, RhSafeAny, RhSelectableDto, RhSelectItem, RhTableButtonInfoDto, RhTableQueryOptionDto } from 'rh-base/model'; import { guid, MsgHelper, RhApiUrlsService } from 'rh-base/core'; import { DynamicFormComponent } from 'rh-base/shared'; import { WebApiInvokeLog } from 'rh-syc/syc-models'; import { RhToolKitService } from './../../tool-kit.service'; import { Subject, Subscription } from 'rxjs'; import { debounceTime, takeUntil } from 'rxjs/operators'; @Component({ selector: 'rh-esinvokes-exception-compensagion', templateUrl: './esinvokes-exception-compensagion.component.html', styleUrls: ['./esinvokes-exception-compensagion.component.less'] }) export class ESInvokesExceptionCompensagionComponent implements OnInit { @ViewChild('queryForm', { static: false }) queryFormCmt!: DynamicFormComponent; selectedIndex = 0; dataLoading = false; dataresult!: DataResultT; queryDto!: RhBaseQueryDto; defaultFieldDataset: RhDynamicTableFieldDto[] = []; detailsForm!: UntypedFormGroup; selectedItem: RhSafeAny = null; httpRequestSubscription!: Subscription; destroy$ = new Subject(); editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto( 'showDetailsView', '查看详情', 'default', false, 'eye', false, null, (item) => this.openDetailsTab(item as WebApiInvokeLog), true, false, '#1890ff' ) ]; constructor(private fb: UntypedFormBuilder, private apiUrl: RhApiUrlsService, private operator: RhToolKitService) { // } ngOnInit(): void { // } openDetailsTab(item: WebApiInvokeLog) { this.selectedItem = item; this.createDetailsForm(item); this.selectedIndex = 1; } closeDetailsTab() { this.selectedIndex = 0; } createDetailsForm(dto: WebApiInvokeLog) { this.detailsForm = this.fb.group({}); } closeEditTab() { this.selectedIndex = 0; } openEditTab() { this.selectedIndex = 1; } setDatas(dataset: DataResultT) { this.dataresult = dataset; if (!this.dataresult?.Success) { MsgHelper.ShowWarningMessage(`搜索无数据!${this.dataresult?.Message}`); } } dataLoadingChange(status: boolean) { this.dataLoading = status; } refreshDatas(option: RhTableQueryOptionDto) { this.getDatas(option); } setInitFieldDataset(item: CodeClassModel) { const tempDatas: RhDynamicTableFieldDto[] = []; item.CodeProperties.forEach((ele) => { const temp = RhDynamicTableFieldDto.create(); temp.name = ele.PropertyName; temp.displayName = ele.Descirption; tempDatas.push(temp); }); this.defaultFieldDataset = [...tempDatas]; } //调用子组件中的接口 private getDatas(option?: RhTableQueryOptionDto) { if (this.queryFormCmt) { this.queryFormCmt.submitDefaultForm(null, option); } } ESInvokeExceptionCompensagionOperation() { const requestDto = this.initRequestDto(this.selectedItem); if (this.selectedItem.TryExecuteTimes < 3) { if (this.httpRequestSubscription) { this.httpRequestSubscription.unsubscribe(); } if (requestDto.type === 'get') { this.httpRequestSubscription = this.apiUrl .get(`${requestDto.prefix}${requestDto.url}`) .pipe(debounceTime(500), takeUntil(this.destroy$)) .subscribe((result: RhSafeAny) => { if (result['Success']) { MsgHelper.ShowWarningMessage('补偿操作成功!'); this.operator.CompletedESInvokeExceptionCompensagionOperation(this.selectedItem).subscribe((res) => { if (res.Success) { MsgHelper.ShowWarningMessage(`更新尝试次数成功!`); } else { MsgHelper.ShowWarningMessage(`更新尝试次数失败!${res.Message}`); } }); } else { MsgHelper.ShowWarningMessage(`补偿操作失败!${result['Message'] ? result['Message'] : null}`); this.operator.UpdateESInvokeExceptionCompensagionTryExcuteTime(this.selectedItem).subscribe((log) => { if (log.Success) { MsgHelper.ShowWarningMessage(`更新尝试次数成功!`); } else { MsgHelper.ShowWarningMessage(`更新尝试次数失败!${log.Message}`); } }); } }); } else { this.httpRequestSubscription = this.apiUrl .post(`${requestDto.prefix}${requestDto.url}`, requestDto.input) .subscribe((result: RhSafeAny) => { if (result['Success']) { MsgHelper.ShowWarningMessage('补偿操作成功!'); this.operator.CompletedESInvokeExceptionCompensagionOperation(this.selectedItem).subscribe((res) => { if (res.Success) { MsgHelper.ShowWarningMessage(`更新尝试次数成功!`); } else { MsgHelper.ShowWarningMessage(`更新尝试次数失败!${res.Message}`); } }); } else { MsgHelper.ShowWarningMessage(`补偿操作失败!${result['Message'] ? result['Message'] : null}`); this.operator.UpdateESInvokeExceptionCompensagionTryExcuteTime(this.selectedItem).subscribe((log) => { if (log.Success) { MsgHelper.ShowWarningMessage(`更新尝试次数成功!`); } else { MsgHelper.ShowWarningMessage(`更新尝试次数失败!${log.Message}`); } }); } }); } } else { MsgHelper.ShowWarningMessage('已尝试补偿3次,不可再次补偿操作!'); } } private initRequestDto(data: WebApiInvokeLog): RhHttpRequestSettingDto { const temp = RhHttpRequestSettingDto.create(); temp.InterfaceId = guid(); temp.controllerName = data.ControllerName; temp.interfaceName = data.MethodName; if (data.HttpMethod == 'POST') { temp.input = JSON.parse(data.CallPara as RhSafeAny); } temp.fullUrl = data.ApiUrl; temp.type = data.HttpMethod ? (data.HttpMethod.toLocaleLowerCase() as RhSafeAny) : 'post'; try { if (data.ApiUrl) { const tempDatas = data.ApiUrl.split('/'); temp.prefix = tempDatas.slice(0, -2).join('/'); temp.url = `/${tempDatas.slice(-2).join('/')}`; temp.baseUrl = `/${tempDatas.slice(3, 5).join('/')}`; const serverPort = tempDatas[2]; temp.server = serverPort.split(':')[0]; temp.port = serverPort.split(':')[1]; } } catch (error) { return temp; } return temp; } }