import { HttpErrorResponse } from "@angular/common/http"; import { classToPlain } from "class-transformer"; import { PlanComponent } from "src/app/pages/plan/plan.component"; import { BuildingBasicInfosService } from "src/app/service/babylon/building-basic-infos.service"; import { ObjectsService } from "src/app/service/objects.service"; import { ExportData } from "../model/data/institution/building/export-data"; import { InsitutionDataSimple } from "../model/data/institution/institution-data-simple"; import { AllMarkPlanData, MarkPlanData } from "../model/data/mark/mark-plan-data"; import { DataManager } from "./data-manager"; import { ModeManager, ModeType } from "./mode-manager"; //服务器通信 export class ServeManager { static ngAssetsPath = "assets/";//资源根目录 static instance: ServeManager = null; postFilePath: string;//上传文件的相对路径 static Init(buildingBISrv: BuildingBasicInfosService, objectsSrv: ObjectsService) { ServeManager.instance = new ServeManager(buildingBISrv, objectsSrv); } constructor( private buildingBISrv: BuildingBasicInfosService, private objectsSrv: ObjectsService ) { //this.fileInput = document.getElementById("file"); } //从服务器获取单位信息 getInstitutionData(id: number = 1, onSuccess?: (key: string, data: string ) => void, onError?: (key: string, error: any) => void) { this.buildingBISrv.getBuildingBasicInfos(id) .subscribe(data => { //console.log("得到单位信息" + key, data); if (onSuccess) { onSuccess(id.toString(), JSON.parse((data as any).result)); } }, error => { console.error("From serve:" + id, error); if (onError) { onError(id.toString(), error); } }) } //保存单位信息 saveInstitutionData(institutionData: any, key: string = 'test', institutionID: number = 1, onSuccess?: (key: string, result: string ) => void, onError?: (key: string, error: string) => void) { let l_data = classToPlain(institutionData); // console.log("保存单位", l_data); this.buildingBISrv.postBuildingBasicInfos(key, institutionID, l_data) .subscribe(data => { // console.log("保存单位成功:" + key, l_data); if (onSuccess) { onSuccess(key, data.result); } if (key != "InsList") { PlanComponent.instance.openSnackBar("保存单位成功"); // alert("保存单位成功"); } //暂时没有失败的回调 onError }) } //保存单位列表 saveInstitutionListData(institutionList: InsitutionDataSimple[], onSucess?: () => void) { // console.log("======保存单位"); ServeManager.instance.saveInstitutionData(institutionList, "InsList", -1, () => { //alert("保存单位列表成功"); if (onSucess) { onSucess(); } }) } //#region 态势标会 /** * 获取态势标会信息 * @param institutionID * @param onSuccess * @param onError */ getMarkData(institutionID: number, onSuccess?: (institutionID: number, data: string) => void, onError?: (institutionID: number, error: any) => void) { this.buildingBISrv.getMarkData(institutionID) .subscribe(data => { // ModeManager.log(data); if (onSuccess) { onSuccess(institutionID, JSON.parse((data as any).result)); } }, (error) => { if (error instanceof HttpErrorResponse) { if (error.status === 404) { console.log("没有标绘数据,新建:", error.error); if (ModeManager.currentMode == ModeType.Edit) { onSuccess(institutionID, null); //data 为null ,表示新建 } else { PlanComponent.instance.openSnackBar("暂无数据"); } } else { if (onError) { onError(institutionID, error); } } } if (onError) { onError(institutionID, error); } }) } /** * 保存态势标会信息 */ saveMarkData(allMarkPlanData: AllMarkPlanData, onSuccess?: (key: string, result: string) => void, onError?: (key: string, error: string) => void) { if (allMarkPlanData == null) { console.error("标绘信息为空,无法保存"); } else { let data = classToPlain(allMarkPlanData); this.buildingBISrv.postMarkData(allMarkPlanData.institutionID, DataManager.institutionData_simple.id, data) .subscribe(data => { console.log("保存标绘信息成功:" + allMarkPlanData.institutionID, DataManager.institutionData_simple.id); if (onSuccess) { onSuccess(allMarkPlanData.institutionID, data.result); } //PlanComponent.instance.openSnackBar("保存标绘信息成功"); // alert("保存标绘信息成功"); PlanComponent.instance.openSnackBar("保存成功"); //暂时没有失败的回调 onError }) } } //#endregion //#region 态势标会模板 /** * 获取所有模板 * @param onSuccess */ getAllMarkTemplate(onSuccess?: (data: string) => void) { this.buildingBISrv.getAllSandBoxTemplate() .subscribe(data => { // console.log("得到所有模板", data); if (onSuccess) { onSuccess((data as any).result.items); } }, error => { console.error("From serve:AllMarkTemplate", error); if (onSuccess) { onSuccess(null); } }) } /** * 获取单个模板 */ getOneMarkTemplate(id: number, onSuccess?: (data: string) => void) { this.buildingBISrv.getSandBoxTemplate(id). subscribe(data => { console.log("得到单个模板" + id, data); if (onSuccess) { onSuccess(JSON.parse((data as any).result.value)); } }, error => { console.error("From serve:OneMarkTemplate", error); }) } /** * 保存为模板 * @param id * @param name * @param data * @param onSuccess */ saveOneMarkTemplate(id: number, name: string, data: MarkPlanData, onSuccess?: (data: string) => void) { this.buildingBISrv.postSandBoxTemplate(name, id, data). subscribe(data => { console.log("保存单个模板" + id, data); if (onSuccess) { onSuccess(JSON.parse((data as any).result)); } }, error => { console.error("From serve:OneMarkTemplate", error); }) } /** * 删除模板 */ deleteSandBoxTemplate(id: number, onSuccess?: () => void) { this.buildingBISrv.deleteSandBoxTemplate(id).subscribe(data => { console.log("删除模板" + id, data); if (onSuccess) { onSuccess(); } }, error => { console.error("deleteSandBoxTemplate", error); }) } //#endregion //#region 导出excel的信息 saveGasStationExcelData(exportData: ExportData) { this.buildingBISrv.postGasStationExcelData(DataManager.institutionData_simple.name, DataManager.institutionData_simple.id, exportData) .subscribe(data => { // console.log("保存导出信息成功:" + DataManager.institutionData_simple.id, `'${JSON.stringify(exportData)}'`); }, (error) => { console.log("保存导出信息失败:" + DataManager.institutionData_simple.id, error); }); } //#endregion //#region 文件上传 /** * 队列性,批量上传 * @param index * @param files * @param resPath_out * @param onOneSuccess * @param onEnd */ uploadFile(index: number, files: File[], resPath_out, onOneSuccess: (name: string, path: string, file: File) => void, onEnd: () => void) { if (index < files.length) { console.log("上传文件", files[index].name); ServeManager.instance.openFileSelect(files[index], resPath_out, (name: string, path: string, file: File) => { onOneSuccess(name, path, file); this.uploadFile(index + 1, files, resPath_out, onOneSuccess, onEnd); }); } else { onEnd(); } } //设置文件路径并上传 openFileSelect(file: File, extensionPath: string, onSuccess?: (name: string, path: string, file: File) => void) { this.postFilePath = extensionPath; this.onPostFileSuccess = onSuccess; let fileSize = file.size || null //上传文件的总大小 let shardSize = 5 * 1024 * 1024 //5MB 超过5MB要分块上传 if (fileSize >= shardSize) // 超过5MB要分块上传 { this.postFileByMul(file); } else //普通上传 { this.postFile(file); } } //上传成功 onPostFileSuccess?: (name: string, path: string, file: File) => void; //上传文件 async postFile(file: File) { await new Promise((resolve, reject) => { ServeManager.instance.objectsSrv.postFile(this.postFilePath, file).subscribe(data => { let dataObj = data as any; let fileName = dataObj.fileName; let filePath: string = dataObj.objectName;//此路径不全,前面缺少ObjectsService.baseUrl,这部分不可序列化,要在运行时拼接 filePath = filePath.replace(fileName, "");//去掉结尾的文件名 //filePath = ObjectsService.baseUrl + filePath; console.log("上传完成,地址为", filePath, fileName); ServeManager.instance.onGetPostFileResult(fileName, filePath, file); resolve('success') }); }) } /** * 上传文件的结果 * @param data * @param file */ onGetPostFileResult(fileName: string, filePath: string, file: File) { if (ServeManager.instance.onPostFileSuccess) { ServeManager.instance.onPostFileSuccess(fileName, filePath, file); } } /** * 分块上传 * @param file */ postFileByMul(file: File) { ServeManager.instance.objectsSrv.postFile_MultipartUpload(this.postFilePath, file).then((value) => { let dataObj = value as any; console.log("分块上传完成", dataObj.filePath); dataObj.filePath = dataObj.filePath.replace(dataObj.fileName, ""); //去掉结尾的文件名 console.log(dataObj.filePath, dataObj.fileName, dataObj.name); ServeManager.instance.onGetPostFileResult(dataObj.fileName, dataObj.filePath, file); }); } //#endregion //#region 本地json 加载 /** * 加载Json * @param path 例如: "assets/configs/markplan-1.json" * @param onSuccess */ loadJson(path: string, onSuccess: (data) => void) { this.buildingBISrv.http.get(path) .subscribe(data => { console.log("loadJson", data); onSuccess(data); }); } //#endregion }