import { HttpClient } from '@angular/common/http'; import { DataDisplayDto, RhApiInterfaceDto } from 'rh-itm/shared'; import { Component, Input, OnInit, Output, EventEmitter, ViewChild, ElementRef, SimpleChanges } from '@angular/core'; import { MsgHelper, RhApiUrlsService, RhStorageService } from 'rh-base/core'; import { RhSelectItem, DataResultT, RhLoginedUserDto, RhSafeAny } from 'rh-base/model'; import { ProductionEmployeeDto, WorkShopLineProductionParameter } from '../../business-model'; @Component({ selector: 'rh-employee-data-display', templateUrl: './employee-data-display.component.html', styleUrls: ['./employee-data-display.component.less'] }) export class RhEmployeeDataDisplayComponent implements OnInit { @Input() rhEmployeeInfo: ProductionEmployeeDto | null = null; /** 数据展示信息 */ @Input() rhDisplayDataset!: DataDisplayDto; /** 员工信息展示数据内容 */ displayEmployeeDataset: DataDisplayDto[] = []; employeeImageList = [ // TODO: 后续从接口获取员工头像 { src: 'https://placekitten.com/600/600', w: 600, h: 600 } ]; /** 是否启用上工/登出功能 */ @Input() rhIsUseLogin = true; /** 员工输入 */ @ViewChild('userInput', { static: false }) userInput!: ElementRef; @Output() rhValdateUser = new EventEmitter(); @Output() rhLogOut = new EventEmitter(); @Output() rhClassTypeChange = new EventEmitter(); /** 当前班次 */ currentClassType!: RhSelectItem; classTypes: RhSelectItem[] = []; isShowClassTypeSelectModal = false; // isClassTypeChange = false; /** 当前登录人 */ loginUser: RhLoginedUserDto; // 字段显示控制属性 /** 是否显示部门代码 */ @Input() rhIsShowOrganizationStructureCode = false; /** 是否显示部门名称 */ @Input() rhIsShowOrganizationStructureName = true; /** 是否显示岗位代码 */ @Input() rhIsShowPostCode = false; /** 是否显示岗位名称 */ @Input() rhIsShowPostName = true; /** 是否显示部班次代码 */ @Input() rhIsShowClassTypeCode = false; /** 是否显示班次名称 */ @Input() rhIsShowClassTypeName = false; /** 是否显示技能列表 */ @Input() rhIsShowSkillsDtos = false; /** 登录接口 */ @Input() rhLoginInterface!: RhApiInterfaceDto; /** 登录接口另外需要传的参数 */ @Input() rhLoginInterfaceOptions = {}; /** 登出接口 */ @Input() rhLogoutInterface!: RhApiInterfaceDto; /** 当前工厂的参数配置 */ @Input() rhWorkShopProductionPara!: WorkShopLineProductionParameter; constructor(private apiUrl: RhApiUrlsService, public http: HttpClient, private storage: RhStorageService) { this.loginUser = this.storage.getUserSession()?.User as RhLoginedUserDto; } ngOnInit() { this.getClassTypeList(); this.setDisplayEmployeeDataset(); // console.log('上工扫码组件初始化', guid()); } ngAfterViewInit() { if (this.userInput) { this.userInput.nativeElement.focus(); this.userInput.nativeElement.select(); } } ngOnChanges(changes: SimpleChanges): void { const { rhWorkShopProductionPara } = changes; // 当工厂参数第一次变化 if (rhWorkShopProductionPara?.currentValue) { const { IsUseLoginProduction } = rhWorkShopProductionPara.currentValue; if (this.rhEmployeeInfo?.EmployeeCode) { this.logOut(); } if (!IsUseLoginProduction) { this.validateUser(this.loginUser.UserName as string); } // console.log(rhWorkShopProductionPara); } } /** 登陆员工信息展示配置 */ private setDisplayEmployeeDataset() { this.displayEmployeeDataset = [ new DataDisplayDto('部门代码', 'OrganizationStructureCode', this.rhIsShowOrganizationStructureCode), new DataDisplayDto('部门', 'OrganizationStructureName', this.rhIsShowOrganizationStructureName), new DataDisplayDto('岗位代码', 'PostCode', this.rhIsShowPostCode), new DataDisplayDto('岗位', 'PostName', this.rhIsShowPostName), new DataDisplayDto('班次代码', 'ClassTypeCode', this.rhIsShowClassTypeCode), new DataDisplayDto('班次', 'ClassTypeName', this.rhIsShowClassTypeName), new DataDisplayDto('技能', 'SkillsDtos', this.rhIsShowSkillsDtos) ]; } async validateUser(user: string) { if (!user || !user.trim()) { MsgHelper.ShowWarningModal('请进行上工扫码!'); return; } if (this.rhLoginInterface) { const { type, ctrlName, actionName, localField } = this.rhLoginInterface; const url = this.apiUrl.GetApiOperateUrl(type, ctrlName as string, actionName as string, localField); const dto = { EmployeeCode: user, ...this.rhLoginInterfaceOptions }; try { const result = await this.http.post>(url, dto).toPromise(); if (result?.Success) { this.rhEmployeeInfo = result.Attach; this.rhValdateUser.emit(result.Attach); // console.log('登录', new Date().getTime()); } else { MsgHelper.ShowErrorMessage('员工扫码验证失败!' + result?.Message); } } catch (error) { MsgHelper.ShowErrorMessage('员工扫码验证失败!'); } } else { this.rhValdateUser.emit(user); } } async logOut() { if (this.rhLogoutInterface) { const { type, ctrlName, actionName, localField } = this.rhLogoutInterface; const url = this.apiUrl.GetApiOperateUrl(type, ctrlName, actionName, localField); try { const result = await this.http.post>(url, this.rhEmployeeInfo).toPromise(); if (result?.Success) { // this.rhLogOut.emit(); this.rhEmployeeInfo = null; } else { MsgHelper.ShowErrorMessage('登出失败!' + result?.Message); } // console.log('登出', new Date().getTime()); } catch (error: RhSafeAny) { MsgHelper.ShowErrorMessage('登出失败!' + error?.Message); } } else { this.rhLogOut.emit(); } } editData(code: string) { switch (code) { case 'ClassTypeName': this.openClassTypeSelectModal(); break; default: break; } } // 获取班次列表数据 getClassTypeList() { this.apiUrl.getSettingEntry('MDP', 'FactoryCalendarManagement', 'ClassType').subscribe((result) => { if (result && result.Success) { this.classTypes = result.Attach; } else { this.classTypes = []; } }); } openClassTypeSelectModal() { this.isShowClassTypeSelectModal = true; } closeClassTypeSelectModal() { this.isShowClassTypeSelectModal = false; } onSelectClassType(item: RhSelectItem) { this.currentClassType = item; // this.rhEmployeeInfo.ClassTypeCode = item.Value.toString(); // this.rhEmployeeInfo.ClassTypeName = item.Text.toString(); this.rhClassTypeChange.emit(item); } async ngOnDestroy() { // console.log(this.rhEmployeeInfo); if (this.rhEmployeeInfo?.EmployeeCode && this.rhLogoutInterface) { await this.logOut(); } } }