package map_service import ( "archive/zip" "context" "encoding/json" "fmt" "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/protocol/consts" uuid "github.com/satori/go.uuid" "io" "io/ioutil" "net/http" "os" "path/filepath" "pji_desktop_http/biz/dal/mysql" "pji_desktop_http/biz/model" "pji_desktop_http/common/config" "pji_desktop_http/common/config/c_log" "pji_desktop_http/common/config/c_pji" "pji_desktop_http/common/entity" "pji_desktop_http/common/util" "pji_desktop_http/pji_client" "strconv" "strings" "time" ) // CheckMapBufConsistency 检查请求id对应的mapBuf文件夹的一致性 // @router /map/checkmapbuf [POST] func CheckMapBufConsistency(ctx context.Context, c *app.RequestContext) { var req []string err := c.BindAndValidate(&req) if err != nil { c.String(consts.StatusBadRequest, err.Error()) return } fmt.Println(req) if len(req) == 0 { // 请求数据为空 c.JSON(consts.StatusBadRequest, entity.HttpResult{Status: false, Code: "", Message: "请求数据为空。"}) } else if len(req) == 1 { // 只有一条数据则直接返回 c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "mapBuf文件夹数据一致。"}) } else { var firstValue int for i, id := range req { // 根据id获取对应的oss文件列表 fileList, err := util.GetExactedMapFileById(id) // 过滤特定后缀的文件列表 fileList = util.FilterBySuffixes(fileList, config.MapBufFiltersuffixes...) //fmt.Println("Filtered Strings:", fileList) if err != nil { c.String(consts.StatusBadRequest, err.Error()) return } // 获取文件列表的总大小 totalSize := calculateTotalFileSize(fileList) fmt.Println("Total Size:", totalSize) // 判断不同文件列表(mapBuf)中文件的总大小是否一致 if i == 0 { firstValue = totalSize } else { if totalSize != firstValue { c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "mapBuf文件夹数据不一致。"}) return } } } c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "mapBuf文件夹数据一致。"}) } } // DownloadOSSFile 根据objectKey下载指定的oss文件 // @router /map/downloadossfile [GET] func DownloadOSSFile(ctx context.Context, c *app.RequestContext) { objectKey := c.Query("objectKey") // 从OSS下载文件 reader, err := config.OssBucket.GetObject(objectKey) if err != nil { c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) return } defer reader.Close() // 设置响应头 c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(objectKey))) c.Response.Header.Set("Content-Type", "binary/octet-stream") // 将文件流式传输回客户端 data, err := io.ReadAll(reader) if err != nil { panic(err) } if _, err := c.Write(data); err != nil { c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) return } } // DownloadMapZipFile 根据请求id从oss拉取并打包下载建图所需要的文件 // @router /map/download/map/zip [GET] func DownloadMapZipFile(ctx context.Context, c *app.RequestContext) { id := c.Query("id") fmt.Println("id", id) deviceNo := c.Query("deviceNo") fmt.Println("deviceNo", deviceNo) ids := strings.Split(id, ",") fmt.Println("ids", ids) sceneId := ids[0] // 根据场景id查询地图id mapId, err := getMapIdById(sceneId) if err != nil || mapId == "" { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取mapId失败"}) return } fmt.Println("mapId", mapId) // 获取朴津地图压缩包url //originUrl deviceMaps, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapId, deviceNo) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备地图失败"}) return } if len(deviceMaps) == 0 { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "未查询到对应设备地图"}) return } deviceMap := deviceMaps[0] originUrl := deviceMap.PjiMapZipURL fmt.Println("originUrl", originUrl) // 获取原始地图map.pgm url originalMaps, err := mysql.QueryOriginalMapByMapIdAndDeviceNo(ctx, mapId, deviceNo) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询原始地图失败"}) return } if len(originalMaps) == 0 { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "未查询到对应原始备地图"}) return } originalMap := originalMaps[0] mapPgmUrl := *originalMap.CicvMapPgmURL // 根据id生成用于地图更新的压缩包 filePath, tmpDir, err := generateMapZipById(ids, originUrl, mapPgmUrl) if err != nil { c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } fmt.Println("filePath", filePath) // 检查文件是否存在 if _, err := os.Stat(filePath); os.IsNotExist(err) { c.JSON(http.StatusNotFound, map[string]string{"error": "File not found"}) return } // 打开文件 f, err := os.Open(filePath) if err != nil { c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to open file"}) return } defer f.Close() // 设置响应头 c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(filePath))) c.Response.Header.Set("Content-Type", "binary/octet-stream") // 将文件流式传输回客户端 data, err := io.ReadAll(f) if err != nil { panic(err) } if _, err := c.Write(data); err != nil { c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) return } defer os.RemoveAll(tmpDir) } // DownloadMapBagFile 根据请求id从oss拉取并下载解析后的map.bag文件 // @router /map/downloadmapbagfile [GET] func DownloadMapBagFile(ctx context.Context, c *app.RequestContext) { id := c.Query("id") fmt.Println("id: ", id) // 根据id获取对应的oss文件列表 fileList, err := util.GetExactedMapFileById(id) // 过滤特定后缀的文件列表 fileList = util.FilterBySuffixes(fileList, config.MapBagFiltersuffixes...) //fmt.Println("fileList", fileList) objectKey := fileList[0] // 从OSS下载文件 reader, err := config.OssBucket.GetObject(objectKey) if err != nil { c.JSON(consts.StatusInternalServerError, entity.HttpResult{Status: false, Code: "", Message: "解析地图Bag下载失败。"}) return } defer reader.Close() // 设置响应头 c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(objectKey))) c.Response.Header.Set("Content-Type", "binary/octet-stream") // 将文件流式传输回客户端 data, err := io.ReadAll(reader) if err != nil { panic(err) } if _, err := c.Write(data); err != nil { c.JSON(consts.StatusInternalServerError, entity.HttpResult{Status: false, Code: "", Message: "解析地图Bag下载失败。"}) return } c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "解析地图Bag下载成功。"}) } // 根据id生成用于地图更新的压缩包 func generateMapZipById(ids []string, mapZipUrl string, mapPgmUrl string) (file string, tmpDir string, err error) { // 根据id获取对应的oss文件列表 allFileList, err := util.GetExactedMapFileByIds(ids) //fmt.Println("allFileList", allFileList) // 过滤特定后缀的文件列表 mapBufFileList := util.FilterBySuffixes(allFileList, config.MapBufFiltersuffixes...) //fmt.Println("Filtered Strings:", fileList) if err != nil { return } // 创建临时文件夹 tmpDir, err = os.MkdirTemp("", "temp-download-*") fmt.Println("tmpDir:", tmpDir) if err != nil { fmt.Println("Error creating temporary directory:", err) return "", "", err } c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir) // 创建根文件夹(文件打包的根目录) baseDir := filepath.Join(tmpDir, "data") if err := os.Mkdir(baseDir, 0755); err != nil { fmt.Println("Error creating subdirectory:", err) return "", "", err } c_log.GlobalLogger.Info("创建文件打包根目录:", baseDir) // 根目录创建子文件夹 bag_folder bagFolderDir := filepath.Join(baseDir, "bag_folder") if err := os.Mkdir(bagFolderDir, 0755); err != nil { fmt.Println("Error creating subdirectory:", err) return "", "", err } c_log.GlobalLogger.Info("根目录下创建bag_folder子文件夹:", bagFolderDir) // 根目录创建子文件夹 origin_map_folder originMapFolderDir := filepath.Join(baseDir, "origin_map_folder") if err := os.Mkdir(originMapFolderDir, 0755); err != nil { fmt.Println("Error creating subdirectory:", err) return "", "", err } c_log.GlobalLogger.Info("根目录下创建origin_map_folder子文件夹:", originMapFolderDir) // 子文件夹 bag_folder 创建子文件夹 mapBuf mapBufDir := filepath.Join(bagFolderDir, "mapBuf") if err := os.Mkdir(mapBufDir, 0755); err != nil { fmt.Println("Error creating subdirectory:", err) return "", "", err } c_log.GlobalLogger.Info("bag_folder创建mapBuf子文件夹 :", mapBufDir) // 从oss下载build_map.bag文件到 bag_folder 文件夹 // 过滤特定后缀的文件列表 buildMapBagFileList := util.FilterBySuffixes(allFileList, config.BuildMapBagFiltersuffixes...) fmt.Println("buildMapBagFileList", buildMapBagFileList) //buildMapBagFile := buildMapBagFileList[0] //err = config.OssBucket.GetObjectToFile(buildMapBagFile, filepath.Join(bagFolderDir, filepath.Base(buildMapBagFile))) for i, file := range buildMapBagFileList { fileName := "build_map" + "-" + strconv.Itoa(i) + ".bag" err = config.OssBucket.GetObjectToFile(file, filepath.Join(bagFolderDir, fileName)) if err != nil { fmt.Println("Error downloading build map file:", err) return "", "", err } } c_log.GlobalLogger.Info("下载文件到bag_folder文件夹 - 成功") // 从oss下载文件到 mapBuf 文件夹 for _, file := range mapBufFileList { err = config.OssBucket.GetObjectToFile(file, filepath.Join(mapBufDir, filepath.Base(file))) if err != nil { fmt.Println("Error downloading mapBuf file:", err) return "", "", err } } c_log.GlobalLogger.Info("下载文件到mapBuf子文件夹 - 成功") // 从oss下载bag文件到 origin_map_folder 文件夹 // 过滤特定后缀的文件列表 //originMapFileList := util.FilterBySuffixes(allFileList, config.OriginMapFiltersuffixes...) //for _, file := range originMapFileList { // err = config.OssBucket.GetObjectToFile(file, filepath.Join(originMapFolderDir, filepath.Base(file))) // if err != nil { // fmt.Println("Error downloading origin_map file:", err) // return "", "", err // } //} //err = config.OssBucket.GetObjectToFile(originUrl, filepath.Join(originMapFolderDir, filepath.Base(originUrl))) err = util.Downloadfile(mapZipUrl, filepath.Join(originMapFolderDir, filepath.Base(mapZipUrl))) if err != nil { fmt.Println("Error downloading origin_map file:", err) return "", "", err } err = config.OssBucket.GetObjectToFile(mapPgmUrl, filepath.Join(originMapFolderDir, filepath.Base(mapPgmUrl))) if err != nil { fmt.Println("Error downloading map.pgm file:", err) return "", "", err } c_log.GlobalLogger.Info("下载文件到origin_map_folder文件夹 - 成功") // 创建压缩文件 zipPath := filepath.Join(tmpDir, "mapFile-"+ids[0]+".zip") zipFile, err := os.Create(zipPath) if err != nil { fmt.Println("Error creating ZIP file:", err) return "", "", err } defer zipFile.Close() zipWriter := zip.NewWriter(zipFile) defer zipWriter.Close() // 压缩文件夹 if err := util.AddDirToZip(baseDir, zipWriter); err != nil { fmt.Println("Error adding directory to ZIP:", err) return "", "", err } fmt.Println("ZIP file created successfully.") c_log.GlobalLogger.Info("创建压缩文件 - 成功") return zipPath, tmpDir, nil } // UploadMapFile 将地图更新相关文件上传到oss // @router /map/upload/map [GET] func UploadMapFile(ctx context.Context, c *app.RequestContext) { equipmentNo := c.Query("equipmentNo") fmt.Println("equipmentNo", equipmentNo) mapId := c.Query("mapId") fmt.Println("mapId", mapId) timeStamp := c.Query("timeStamp") fmt.Println("timeStamp", timeStamp) header, err := c.FormFile("file") if err != nil { c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error())) return } fileName := header.Filename fmt.Println("filename", fileName) ossObjectKey := config.UpdateMapOssBasePrefix + "/" + equipmentNo + "/" + mapId + "/" + timeStamp + "/" + fileName fmt.Println("ossObjectKey", ossObjectKey) f, _ := header.Open() defer f.Close() config.OssMutex.Lock() err = config.OssBucket.PutObject(ossObjectKey, f) config.OssMutex.Unlock() if err != nil { c_log.GlobalLogger.Error("程序异常退出。上传文件", fileName, "->", ossObjectKey, "出错:", err) c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传文件失败", Details: ""}) return } c_log.GlobalLogger.Info("上传文件", fileName, "->", ossObjectKey, "成功。") c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传文件成功", Details: ossObjectKey}) } // AddMapUpdateRecord 添加仿真测试记录 // @router /map/add/record [GET] func AddMapUpdateRecord(ctx context.Context, c *app.RequestContext) { var record model.MapUpdate err := c.BindAndValidate(&record) record.ID = uuid.NewV1().String() fmt.Println("record", record) if err != nil { c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error())) return } err = mysql.AddMapUpdateOneRecord(ctx, record) if err != nil { c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "地图更新记录添加失败"}) return } c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "地图更新记录添加成功"}) } // QueryMapUpdateRecord 根据条件查询地图更新记录 // @router /map/query/update/record [GET] func QueryMapUpdateRecord(ctx context.Context, c *app.RequestContext) { var record model.MapUpdate err := c.BindAndValidate(&record) fmt.Println("record", record) var pageFlag bool if c.Query("page") != "" && c.Query("pageSize") != "" { pageFlag = true } else { pageFlag = false } page, _ := strconv.Atoi(c.Query("page")) pageSize, _ := strconv.Atoi(c.Query("pageSize")) records, count, err := mysql.QueryMapUpdateRecords(ctx, &record, pageFlag, page, pageSize) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图更新记录查询失败", Total: 0}) return } output, err := json.Marshal(records) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图更新记录查询失败", Total: 0}) return } c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "地图更新查询成功", Data: string(output), Total: int(count)}) } // TestUploadUpdateMap 测试朴津地图上传接口 // @router /map/upload/update/map [GET] func TestUploadUpdateMap(ctx context.Context, c *app.RequestContext) { header, err := c.FormFile("file") if err != nil { c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error())) return } fileName := header.Filename fmt.Println("filename", fileName) f, _ := header.Open() defer f.Close() paramMap := make(map[string]interface{}) // Map paramMap=new HashMap<>(); // paramMap.put("mapId","2247aeb31ad34wwddfd9c3ba24536e52859"); // paramMap.put("mapName","合肥演示"); // paramMap.put("buildId"," 7b4e8d01c08f472fabf7ccff2b2336s"); // paramMap.put("floorId"," 695abbcfdf99471sdfff3b9b1d0818164e7"); // paramMap.put("updateType"," 7b4e8d01c08f472fabf7ccff2b2336s"); // paramMap.put("floor","1"); // paramMap.put("buildName"," 2"); // paramMap.put("customAreaId"," 355"); // paramMap.put("snCode","P1YTYD1M233M00224"); // paramMap.put("fileName","1702284791385137152.zip"); // paramMap.put("mapType","0"); paramMap["snCode"] = "P1YNYD1M225000112" paramMap["mapId"] = "8068197000f84a5483dabf5a6ad3707c" paramMap["mapName"] = "智慧楼" paramMap["buildId"] = "8afb5463ad9140aa9e0e42b9ca93e075" paramMap["buildName"] = "智慧楼" paramMap["floorId"] = "fc86319a04824122b63719b062811106" paramMap["floor"] = "1" paramMap["customAreaId"] = "334" paramMap["updateType"] = "0" paramMap["mapType"] = "0" paramMap["filename"] = "" bytes, err := io.ReadAll(f) if err != nil { return } // 调用 朴津地图上传 接口 resp, err := pji_client.ApiClient.UploadRequest(c_pji.PjiApiBaseUrl+"mapUpload", paramMap, pji_client.MapSecretId, bytes) fmt.Println(string(resp.Body())) c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传地图测试"}) } // UpdateDeviceMapById 根据场景id维护设备地图表 // @router /map/update/deviceMap/record [GET] func UpdateDeviceMapById(ctx context.Context, c *app.RequestContext) { sceneId := c.Query("sceneId") fmt.Println("sceneId", sceneId) deviceNo := c.Query("deviceNo") fmt.Println("deviceNo", deviceNo) deviceName := c.Query("deviceName") fmt.Println("deviceName", deviceName) deviceType := c.Query("deviceType") fmt.Println("deviceType", deviceType) // 根据场景id查询地图id mapId, err := getMapIdById(sceneId) if err != nil || mapId == "" { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取mapId失败"}) return } fmt.Println("mapId", mapId) // 查询朴津地图列表 mapInfoList, err := getMapInfoList() if err != nil || mapInfoList == nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"}) return } fmt.Println("mapInfoList", mapInfoList) // 判断map id是否存在于朴津地图列表中 mapId = "fee84db56d254d79b85971cb4ab2e7f1" // 存在 //mapId = "553d9d47f8404734997e11175f0e63f5" // 不存在 exist, record := checkMapIdExist(mapId, mapInfoList) fmt.Println("exist", exist) fmt.Println("record", record) if !exist { // map id不存在于朴津地图列表中 // 查询地图设备表中是否存在对应map id且有效的记录 records, err := mysql.QueryValidDeviceMapByMapId(ctx, mapId) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"}) return } fmt.Println("records", records) if len(records) > 0 { // 存在, fmt.Println("设置无效") // 将对应的map id的记录设置为无效 update, err := mysql.UpdateDeviceMapValidFlagByMapId(ctx, mapId, config.MAP_NOT_VALID_FLAG) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"}) return } fmt.Println("update", update) } c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "1001", Message: "map id不存在地图列表中"}) } else { // map id存在于朴津地图列表中 snCodeList := record.SnCodeList for _, snCode := range snCodeList { // snCodeList 代表配置了对应map id地图的机器人sn码列表, 上限为机器人数量 fmt.Println("snCode", snCode) deviceMap := model.DeviceMap{DeviceNo: snCode, DeviceName: deviceName, DeviceType: deviceType, MapID: record.MapId, MapType: record.MapType, MapName: record.MapName, FloorID: record.FloorId, Floor: strconv.Itoa(record.Floor), BuildID: record.BuildId, BuildName: record.BuildName, MapCreateTime: record.CreateTime, MapUpdateTime: record.UpdateTime, MapVersion: strconv.Itoa(record.Version), MapValidFlag: config.MAP_VALID_FLAG, PjiMapZipURL: record.ZipUrl, CustomAreaID: int32(record.CustomAreaId)} fmt.Println("deviceMap", deviceMap) deviceMap.MapVersion = "123456" // 查询[对应设备]是否存在对应map id且有效的记录 records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapId, snCode) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"}) return } fmt.Println("records", records) if len(records) == 0 { // 对应设备不存在对应map id且有效的记录, 说明该设备部署了新的地图(重采或新的区域地图) // 添加记录 deviceMap.ID = uuid.NewV1().String() err = mysql.AddDeviceMapOneRecord(ctx, deviceMap) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"}) return } } else if len(records) == 1 { // 对应设备存在1个对应map id且有效的记录, 说明该设备之前部署过该地图, 需要检测版本号来判断更新记录(版本号一致)还是添加记录(版本号不一致) fmt.Println("record", record) if deviceMap.MapVersion == records[0].MapVersion { // 地图列表查询到的版本与设备地图表中版本一致, 则更新记录 // 更新记录 deviceMap.ID = records[0].ID update, err := mysql.UpdateDeviceMapOneRecord(ctx, deviceMap) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图记录失败"}) return } fmt.Println("update", update) } else { // 地图列表查询到的版本与设备地图表中版本不一致, 则先将之前的记录状态设置为无效, 然后添加记录 // 将对应设备存在的对应map id的记录设置为无效 update, err := mysql.UpdateDeviceMapValidFlagByMapIdAndDevice(ctx, mapId, snCode, config.MAP_NOT_VALID_FLAG) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"}) return } fmt.Println("update", update) // 添加记录 deviceMap.ID = uuid.NewV1().String() err = mysql.AddDeviceMapOneRecord(ctx, deviceMap) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"}) return } } } } c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "设备地图数据更新成功"}) } } // CheckDeviceMapStatus 根据设备号及map id查询地图状态 // @router /map/check/deviceMap/status [GET] func CheckDeviceMapStatus(ctx context.Context, c *app.RequestContext) { sceneId := c.Query("sceneId") fmt.Println("sceneId", sceneId) deviceNo := c.Query("deviceNo") fmt.Println("deviceNo", deviceNo) deviceName := c.Query("deviceName") fmt.Println("deviceName", deviceName) deviceType := c.Query("deviceType") fmt.Println("deviceType", deviceType) recordTime := c.Query("recordTime") fmt.Println("recordTime", recordTime) // 根据场景id查询地图id mapId, err := getMapIdById(sceneId) if err != nil || mapId == "" { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取mapId失败"}) return } fmt.Println("mapId", mapId) // 查询 设备sn码 是否存在于 设备表中 device, err := mysql.QueryDeviceByDeviceNo(ctx, deviceNo) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备记录失败"}) return } fmt.Println("device", device) if device == nil { // 设备sn码 不在 设备表中 record := model.Device{DeviceNo: deviceNo, DeviceName: deviceName, DeviceType: deviceType} err := mysql.AddDeviceOneRecord(ctx, record) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备记录失败"}) return } } // 查询[对应设备]是否存在对应map id且有效的记录 records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapId, deviceNo) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备地图失败"}) return } fmt.Println("records", records) if len(records) == 0 { // [对应设备]不存在对应map id且有效的记录 // 地图已重采/删除 c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1001", Message: "地图已重采/删除, 不符合地图更新条件"}) return } // 判断记录时间戳(多个记录取最晚时间戳)是否晚于地图版本时间戳,即判断记录采集后,地图是否发生更新 record := records[0] versionTimeVal, _ := strconv.Atoi(record.MapVersion) fmt.Println("versionTimeVal", versionTimeVal) recordTimeVal, _ := strconv.Atoi(recordTime) fmt.Println("recordTimeVal", recordTimeVal) if versionTimeVal > recordTimeVal { // 地图已更新 c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1003", Message: "数据采集后地图已更新版本, 不符合地图更新条件"}) return } c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "1000", Message: "记录存在,符合地图更新条件"}) } // CheckUpdateMapStatus 根据设备号及map id查询地图状态 // @router /map/check/updateMap/status [GET] func CheckUpdateMapStatus(ctx context.Context, c *app.RequestContext) { deviceNo := c.Query("deviceNo") fmt.Println("deviceNo", deviceNo) mapId := c.Query("mapId") fmt.Println("mapId", mapId) updateTime := c.Query("updateTime") fmt.Println("updateTime", updateTime) // 查询 设备sn码 是否存在于 设备表中 device, err := mysql.QueryDeviceByDeviceNo(ctx, deviceNo) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备记录失败"}) return } fmt.Println("device", device) // 查询[对应设备]是否存在对应map id且有效的记录 records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapId, deviceNo) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备地图失败"}) return } fmt.Println("records", records) if len(records) == 0 { // [对应设备]不存在对应map id且有效的记录 // 地图已重采/删除 c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1--001", Message: "地图已重采/删除, 不符合地图上传条件"}) return } // 判断记录时间戳(多个记录取最晚时间戳)是否晚于地图版本时间戳,即判断记录采集后,地图是否发生更新 record := records[0] versionTimeVal, _ := strconv.Atoi(record.MapVersion) fmt.Println("versionTimeVal", versionTimeVal) updateTimeVal, _ := strconv.Atoi(updateTime) fmt.Println("updateTimeVal", updateTimeVal) if versionTimeVal > updateTimeVal { // 地图已更新 c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1003", Message: "数据采集后地图已更新版本, 不符合地图上传条件"}) return } c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "1000", Message: "记录存在,符合地图上传条件"}) } // UpdateDeviceMap 根据地图列表及设备列表维护设备地图表 // @router /map/update/deviceMap/record [GET] func UpdateDeviceMap(ctx context.Context, c *app.RequestContext) { // 查询朴津地图列表 mapInfoList, err := getMapInfoList() if err != nil || mapInfoList == nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"}) return } //fmt.Println("mapInfoList", mapInfoList) snCodeMap := groupMapListByDevice(mapInfoList) fmt.Println("snCodeMap", snCodeMap) // 查询设备表 devices, err := mysql.QueryAllDevices(ctx) for _, device := range devices { //fmt.Println("device", device) deviceNo := device.DeviceNo fmt.Println("deviceNo", deviceNo) if _, exist := snCodeMap[deviceNo]; !exist { // 设备不存在于朴津地图列表中, 在地图接口列表区域权限配置正确的前提下, 说明该设备没有部署地图 // 查询设备地图表中是否存在有效的记录 deviceMaps, err := mysql.QueryValidDeviceMapByDeviceNo(ctx, deviceNo) if err != nil { return } if len(deviceMaps) > 0 { // 之前存在有效记录 // 将之前的记录有效标志设置为无效 updates, err := mysql.UpdateDeviceMapValidFlagByDeviceNo(ctx, deviceNo, config.MAP_NOT_VALID_FLAG) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"}) return } fmt.Println("updates", updates) } } else { // 设备存在于朴津地图列表中,说明该设备当前有部署地图 mapList := snCodeMap[deviceNo] fmt.Println("mapList", mapList) for _, mapInfo := range mapList { // 判断该设备对应map id是否在设备地图表中存在有效记录 records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapInfo.MapId, deviceNo) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"}) return } fmt.Println("records", records) deviceMap := model.DeviceMap{DeviceNo: device.DeviceNo, DeviceName: device.DeviceName, DeviceType: device.DeviceType, MapID: mapInfo.MapId, MapType: mapInfo.MapType, MapName: mapInfo.MapName, FloorID: mapInfo.FloorId, Floor: strconv.Itoa(mapInfo.Floor), BuildID: mapInfo.BuildId, BuildName: mapInfo.BuildName, MapCreateTime: mapInfo.CreateTime, MapUpdateTime: mapInfo.UpdateTime, MapVersion: strconv.Itoa(mapInfo.Version), MapValidFlag: config.MAP_VALID_FLAG, PjiMapZipURL: mapInfo.ZipUrl, CustomAreaID: int32(mapInfo.CustomAreaId)} if len(records) == 0 { // 该设备对应map id是否在设备地图表中不存在有效记录 // 添加记录 deviceMap.ID = uuid.NewV1().String() fmt.Println("deviceMap", deviceMap) err = mysql.AddDeviceMapOneRecord(ctx, deviceMap) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"}) return } } else { // 该设备对应map id是否在设备地图表中存在有效记录 // 判断版本号是否一致 record := records[0] fmt.Println("record", record) if record.MapVersion == strconv.Itoa(mapInfo.Version) { // 版本号一致 // 更新记录 deviceMap.ID = record.ID update, err := mysql.UpdateDeviceMapOneRecord(ctx, deviceMap) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图记录失败"}) return } fmt.Println("update", update) } else { // 版本号不一致 // 将之前的记录有效标志设置为无效 updates, err := mysql.UpdateDeviceMapValidFlagByDeviceNo(ctx, deviceNo, config.MAP_NOT_VALID_FLAG) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"}) return } fmt.Println("updates", updates) // 添加记录 deviceMap.ID = uuid.NewV1().String() fmt.Println("deviceMap", deviceMap) err = mysql.AddDeviceMapOneRecord(ctx, deviceMap) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"}) return } } } } } } c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "设备地图数据更新成功"}) } // UpdateOriginalMap 根据地图列表及设备列表维护原始地图表 // @router /map/update/originalMap/record [GET] func UpdateOriginalMap(ctx context.Context, c *app.RequestContext) { // 查询朴津地图列表 mapInfoList, err := getMapInfoList() if err != nil || mapInfoList == nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"}) return } //fmt.Println("mapInfoList", mapInfoList) snCodeMap := groupMapListByDevice(mapInfoList) fmt.Println("snCodeMap", snCodeMap) // 查询设备表 devices, err := mysql.QueryAllDevices(ctx) for _, device := range devices { //fmt.Println("device", device) deviceNo := device.DeviceNo fmt.Println("deviceNo", deviceNo) if _, exist := snCodeMap[deviceNo]; exist { // 设备存在于朴津地图列表中,说明该设备当前有部署地图 mapList := snCodeMap[deviceNo] fmt.Println("mapList", mapList) for _, mapInfo := range mapList { records, err := mysql.QueryOriginalMapByMapIdAndDeviceNo(ctx, mapInfo.MapId, deviceNo) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询原始地图失败"}) } fmt.Println("records", records) originalMap := model.OriginalMap{DeviceNo: device.DeviceNo, DeviceName: device.DeviceName, DeviceType: device.DeviceType, MapID: mapInfo.MapId, MapType: mapInfo.MapType, MapName: mapInfo.MapName, FloorID: mapInfo.FloorId, Floor: strconv.Itoa(mapInfo.Floor), BuildID: mapInfo.BuildId, BuildName: mapInfo.BuildName, MapCreateTime: mapInfo.CreateTime, MapUpdateTime: mapInfo.UpdateTime, MapVersion: strconv.Itoa(mapInfo.Version), PjiMapZipURL: mapInfo.ZipUrl, CustomAreaID: int32(mapInfo.CustomAreaId)} if len(records) == 0 { // 该设备对应map id是否在原始地图表中不存在记录 // 添加记录 originalMap.ID = uuid.NewV1().String() fmt.Println("originalMap", originalMap) err = mysql.AddOriginalMapOneRecord(ctx, originalMap) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加原始地图记录失败"}) return } } } } } c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "原始地图数据更新成功"}) } // UploadOriginalMapById 根据id,将原始地图朴津压缩包(及其中的map.pgm)上传到oss // @router /map/upload/originalMap/oneRecord [GET] func UploadOriginalMapById(ctx context.Context, c *app.RequestContext) { sceneId := c.Query("sceneId") fmt.Println("sceneId", sceneId) deviceNo := c.Query("deviceNo") fmt.Println("deviceNo", deviceNo) // 根据场景id查询地图id mapId, err := getMapIdById(sceneId) if err != nil || mapId == "" { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取mapId失败"}) return } fmt.Println("mapId", mapId) // 查询原始地图对应记录 records, err := mysql.QueryOriginalMapByMapIdAndDeviceNo(ctx, mapId, deviceNo) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询原始地图失败"}) return } fmt.Println("records", len(records)) for _, record := range records { fmt.Println("record", record) var zipName string var zipPath string var destPath string updateFlag := false if record.CicvMapZipURL == nil || *record.CicvMapZipURL == "" || record.CicvMapPgmURL == nil || *record.CicvMapPgmURL == "" { updateFlag = true // 创建临时文件夹 tmpDir, err := os.MkdirTemp("", "temp-download-*") fmt.Println("tmpDir:", tmpDir) if err != nil { fmt.Println("Error creating temporary directory:", err) c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "创建临时文件夹失败"}) return } // 下载朴津地图压缩包 zipName = filepath.Base(record.PjiMapZipURL) zipPath = filepath.Join(tmpDir, zipName) destPath = filepath.Join(tmpDir, "map") err = util.Downloadfile(record.PjiMapZipURL, zipPath) if err != nil { fmt.Println("Error downloading origin_map file:", err) c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "下载原始地图文件失败"}) return } c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir) // 检查文件是否存在 if _, err := os.Stat(zipPath); os.IsNotExist(err) { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "下载原始地图文件失败"}) return } } // 上传朴津地图压缩包到oss if record.CicvMapZipURL == nil || *record.CicvMapZipURL == "" { ossObjectKey := config.OriginalMapOssBasePrefix + "/" + record.DeviceNo + "/" + record.MapID + "/" + record.MapVersion + "/" + zipName fmt.Println("ossObjectKey", ossObjectKey) // 打开文件 f, err := os.Open(zipPath) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "打开原始地图文件失败"}) fmt.Println("Failed to open file") return } defer f.Close() config.OssMutex.Lock() err = config.OssBucket.PutObject(ossObjectKey, f) config.OssMutex.Unlock() if err != nil { c_log.GlobalLogger.Error("程序异常退出。上传文件", zipName, "->", ossObjectKey, "出错:", err) c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "上传原始地图文件失败"}) return } c_log.GlobalLogger.Info("上传文件", zipName, "->", ossObjectKey, "成功。") record.CicvMapZipURL = &ossObjectKey } // 上传朴津原始地图到oss if record.CicvMapPgmURL == nil || *record.CicvMapPgmURL == "" { ossObjectKey := config.OriginalMapOssBasePrefix + "/" + record.DeviceNo + "/" + record.MapID + "/" + record.MapVersion + "/" + "map.pgm" // 压缩包解压 err := Unzip(zipPath, destPath) if err != nil { fmt.Println("Error unzipping file:", err) c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "解压原始地图文件失败"}) return } // 文件 - map.pgm pgmPath := filepath.Join(destPath, "map.pgm") // 检查文件是否存在 if _, err := os.Stat(pgmPath); os.IsNotExist(err) { fmt.Println("解压目录中map.pgm不存在") c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "解压目录中map.pgm不存在"}) return } // 打开文件 f, err := os.Open(pgmPath) if err != nil { fmt.Println("Failed to open file") c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "打开map.pgm失败"}) return } defer f.Close() config.OssMutex.Lock() err = config.OssBucket.PutObject(ossObjectKey, f) config.OssMutex.Unlock() if err != nil { c_log.GlobalLogger.Error("程序异常退出。上传文件", "map.pgm", "->", ossObjectKey, "出错:", err) c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "上传map.pgm失败"}) return } c_log.GlobalLogger.Info("上传文件", "map.pgm", "->", ossObjectKey, "成功。") record.CicvMapPgmURL = &ossObjectKey } if updateFlag { _, err = mysql.UpdateOriginalMapOneRecord(ctx, *record) if err != nil { fmt.Println("更新原始地图失败") return } } } c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "原始地图文件上传成功"}) } // UploadOriginalMapWithoutHttp 将原始地图朴津压缩包(及其中的map.pgm)上传到oss func UploadOriginalMapWithoutHttp(ctx context.Context) bool { // 查询原始地图压缩包/pgm为空的记录 records, err := mysql.QueryOriginalMapWithoutCicvMap(ctx) if err != nil { fmt.Println("查询原始地图失败") return false } fmt.Println("records", len(records)) for _, record := range records { fmt.Println("record", record) var zipName string var zipPath string var destPath string updateFlag := false if record.CicvMapZipURL == nil || *record.CicvMapZipURL == "" || record.CicvMapPgmURL == nil || *record.CicvMapPgmURL == "" { updateFlag = true // 创建临时文件夹 tmpDir, err := os.MkdirTemp("", "temp-download-*") fmt.Println("tmpDir:", tmpDir) if err != nil { fmt.Println("Error creating temporary directory:", err) return false } // 下载朴津地图压缩包 zipName = filepath.Base(record.PjiMapZipURL) zipPath = filepath.Join(tmpDir, zipName) destPath = filepath.Join(tmpDir, "map") err = util.Downloadfile(record.PjiMapZipURL, zipPath) if err != nil { fmt.Println("Error downloading origin_map file:", err) return false } c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir) // 检查文件是否存在 if _, err := os.Stat(zipPath); os.IsNotExist(err) { return false } } // 上传朴津地图压缩包到oss if record.CicvMapZipURL == nil || *record.CicvMapZipURL == "" { ossObjectKey := config.OriginalMapOssBasePrefix + "/" + record.DeviceNo + "/" + record.MapID + "/" + record.MapVersion + "/" + zipName fmt.Println("ossObjectKey", ossObjectKey) // 打开文件 f, err := os.Open(zipPath) if err != nil { fmt.Println("Failed to open file") return false } defer f.Close() config.OssMutex.Lock() err = config.OssBucket.PutObject(ossObjectKey, f) config.OssMutex.Unlock() if err != nil { c_log.GlobalLogger.Error("程序异常退出。上传文件", zipName, "->", ossObjectKey, "出错:", err) return false } c_log.GlobalLogger.Info("上传文件", zipName, "->", ossObjectKey, "成功。") record.CicvMapZipURL = &ossObjectKey } // 上传朴津原始地图到oss if record.CicvMapPgmURL == nil || *record.CicvMapPgmURL == "" { ossObjectKey := config.OriginalMapOssBasePrefix + "/" + record.DeviceNo + "/" + record.MapID + "/" + record.MapVersion + "/" + "map.pgm" // 压缩包解压 err := Unzip(zipPath, destPath) if err != nil { fmt.Println("Error unzipping file:", err) return false } // 文件 - map.pgm pgmPath := filepath.Join(destPath, "map.pgm") // 检查文件是否存在 if _, err := os.Stat(pgmPath); os.IsNotExist(err) { fmt.Println("解压目录中map.pgm不存在") return false } // 打开文件 f, err := os.Open(pgmPath) if err != nil { fmt.Println("Failed to open file") return false } defer f.Close() config.OssMutex.Lock() err = config.OssBucket.PutObject(ossObjectKey, f) config.OssMutex.Unlock() if err != nil { c_log.GlobalLogger.Error("程序异常退出。上传文件", "map.pgm", "->", ossObjectKey, "出错:", err) return false } c_log.GlobalLogger.Info("上传文件", "map.pgm", "->", ossObjectKey, "成功。") record.CicvMapPgmURL = &ossObjectKey } if updateFlag { _, err = mysql.UpdateOriginalMapOneRecord(ctx, *record) if err != nil { fmt.Println("更新原始地图失败") return false } } } return true } // UploadPjiMapAndNotify 上传oss更新地图压缩包到朴津,并通知运维人员 // @router /map/update/pji/upload [GET] func UploadPjiMapAndNotify(ctx context.Context, c *app.RequestContext) { id := c.Query("id") fmt.Println("id", id) // 根据id查询更新地图 mapUpdate, err := mysql.QueryMapUpdateRecordById(ctx, id) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图更新列表失败"}) return } mapId := mapUpdate.MapID fmt.Println("mapId", mapId) deviceNo := mapUpdate.DeviceSn fmt.Println("deviceNo", deviceNo) UpdateMapUrl := mapUpdate.UpdateMapURL fmt.Println("UpdateMapUrl", UpdateMapUrl) // 查询朴津地图列表 mapInfoList, err := getMapInfoList() if err != nil || mapInfoList == nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"}) return } // 查询地图id是否存在于地图列表中 exist, record := checkMapIdExist(mapId, mapInfoList) fmt.Println("exist", exist) fmt.Println("record", record) if !exist { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1002", Message: "地图已重采/删除,不符合地图上传条件"}) return } // 拉取oss文件流 // 获取对象 body, err := config.OssBucket.GetObject(UpdateMapUrl) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新地图文件获取失败"}) return } // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。 defer body.Close() file, err := ioutil.ReadAll(body) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新地图文件获取失败"}) return } paramMap := make(map[string]interface{}) //paramMap["snCode"] = "P1YNYD1M225000112" //paramMap["mapId"] = "8068197000f84a5483dabf5a6ad3707c" //paramMap["mapName"] = "智慧楼" //paramMap["buildId"] = "8afb5463ad9140aa9e0e42b9ca93e075" //paramMap["buildName"] = "智慧楼" //paramMap["floorId"] = "fc86319a04824122b63719b062811106" //paramMap["floor"] = "1" //paramMap["customAreaId"] = "334" //paramMap["updateType"] = "0" //paramMap["mapType"] = "0" //paramMap["filename"] = "" paramMap["snCode"] = deviceNo paramMap["mapId"] = mapId paramMap["mapName"] = record.MapName paramMap["buildId"] = record.BuildId paramMap["buildName"] = record.BuildName paramMap["floorId"] = record.FloorId paramMap["floor"] = strconv.Itoa(record.Floor) paramMap["customAreaId"] = strconv.Itoa(record.CustomAreaId) paramMap["updateType"] = "0" paramMap["mapType"] = record.MapType paramMap["filename"] = "" // 调用 朴津地图上传 接口 resp, err := pji_client.ApiClient.UploadRequest(c_pji.PjiApiBaseUrl+"mapUpload", paramMap, pji_client.MapSecretId, file) fmt.Println(string(resp.Body())) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "上传朴津地图失败"}) return } // 解析结果 var mapUploadRes model.MapUploadRes // Json转换为map err = json.Unmarshal(resp.Body(), &mapUploadRes) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "解析响应数据失败"}) return } fmt.Println("mapUploadRes:", mapUploadRes) if mapUploadRes.Code != 200 { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "上传朴津地图失败"}) return } version := strconv.Itoa(mapUploadRes.Data.Version) mapUpdate.UploadVersion = &version mapUpdate.UpdateFlag = 1 info, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "修改地图更新数据失败"}) return } fmt.Println("info", info) //发送修图通知 paramMap = make(map[string]interface{}) paramMap["mapId"] = mapId paramMap["type"] = "0" // 0代表修图通知,1代表续扫通知 paramMap["customAreaId"] = record.CustomAreaId resp, err = pji_client.ApiClient.JsonPostRequest(c_pji.PjiApiBaseUrl+"sendMsg", paramMap, pji_client.MapSecretId) if err != nil { return } fmt.Println(string(resp.Body())) c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传朴津地图成功并已通知运维人员"}) } // AddMapRescanNotifyWithHttp 查询累积地图更新率大于等于续扫阈值的记录,并通知运维人员 // @router /map/rescan/notify/add [GET] func AddMapRescanNotifyWithHttp(ctx context.Context, c *app.RequestContext) { // 查询朴津地图列表 mapInfoList, err := getMapInfoList() if err != nil || mapInfoList == nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"}) return } // 查询地图续扫阈值 systemConfig, err := mysql.QuerySystemConfig(ctx) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取系统配置失败"}) return } threshold := systemConfig.RescanReminderThreshold fmt.Println("threshold", threshold) // 查询地图更新表中未发送续扫提醒通知的记录(续扫提醒标志为0) mapUpdates, err := mysql.QueryMapUpdateRecordsByRescanFlag(ctx, config.MAP_NOT_RESCAN_FLAG) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询地图更新列表失败"}) return } // 记录已发送通知的地图id // 对于同一地图id的多个符合续扫通知提醒的记录,只允许发送一次续扫通知,但相关记录均会被标记为已发送续扫提醒 mapIdList := make(map[string]bool) for _, mapUpdate := range mapUpdates { // 查询地图id是否存在于地图列表中 exist, record := checkMapIdExist(mapUpdate.MapID, mapInfoList) fmt.Println("exist", exist) fmt.Println("record", record) if !exist { fmt.Println("地图列表中不存在该地图id,跳过...") continue } //fmt.Println("mapUpdate", mapUpdate) updatePercentage, err := getUpdatePercentage(mapUpdate.CumulativeUpdateRate) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: err.Error()}) } fmt.Println("updatePercentage", updatePercentage) if updatePercentage >= threshold { mapId := mapUpdate.MapID if _, exits := mapIdList[mapId]; !exits { // 发送续扫通知 paramMap := make(map[string]interface{}) paramMap["mapId"] = mapId paramMap["type"] = "1" // 0代表修图通知,1代表续扫通知 paramMap["customAreaId"] = record.CustomAreaId resp, err := pji_client.ApiClient.JsonPostRequest(c_pji.PjiApiBaseUrl+"sendMsg", paramMap, pji_client.MapSecretId) if err != nil { continue } fmt.Println(string(resp.Body())) mapIdList[mapId] = true } // 修改续扫提醒标志 mapUpdate.RescanNotifyFlag = config.MAP_RESCAN_FLAG thresholdStr := strconv.FormatFloat(float64(threshold), 'f', 2, 32) + "%" fmt.Println("thresholdStr", thresholdStr) mapUpdate.RescanNotifyThreshold = &thresholdStr _, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate) if err != nil { continue } } } c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "更新地图续扫提醒成功"}) } // AddMapRescanNotifyWithoutHttp 查询累积地图更新率大于等于续扫阈值的记录,并通知运维人员 func AddMapRescanNotifyWithoutHttp(ctx context.Context) bool { // 查询朴津地图列表 mapInfoList, err := getMapInfoList() if err != nil || mapInfoList == nil { fmt.Println("获取地图列表数据失败") return false } // 查询地图续扫阈值 systemConfig, err := mysql.QuerySystemConfig(ctx) if err != nil { fmt.Println("获取系统配置失败") return false } threshold := systemConfig.RescanReminderThreshold fmt.Println("threshold", threshold) // 查询地图更新表中未发送续扫提醒通知的记录(续扫提醒标志为0) mapUpdates, err := mysql.QueryMapUpdateRecordsByRescanFlag(ctx, config.MAP_NOT_RESCAN_FLAG) if err != nil { fmt.Println("查询地图更新列表失败") return false } // 记录已发送通知的地图id // 对于同一地图id的多个符合续扫通知提醒的记录,只允许发送一次续扫通知,但相关记录均会被标记为已发送续扫提醒 mapIdList := make(map[string]bool) for _, mapUpdate := range mapUpdates { // 查询地图id是否存在于地图列表中 exist, record := checkMapIdExist(mapUpdate.MapID, mapInfoList) fmt.Println("exist", exist) fmt.Println("record", record) if !exist { fmt.Println("地图列表中不存在该地图id,跳过...") continue } //fmt.Println("mapUpdate", mapUpdate) updatePercentage, err := getUpdatePercentage(mapUpdate.CumulativeUpdateRate) if err != nil { fmt.Println("error:", err) } fmt.Println("updatePercentage", updatePercentage) if updatePercentage >= threshold { mapId := mapUpdate.MapID if _, exits := mapIdList[mapId]; !exits { // 发送续扫通知 paramMap := make(map[string]interface{}) paramMap["mapId"] = mapId paramMap["type"] = "1" // 0代表修图通知,1代表续扫通知 paramMap["customAreaId"] = record.CustomAreaId resp, err := pji_client.ApiClient.JsonPostRequest(c_pji.PjiApiBaseUrl+"sendMsg", paramMap, pji_client.MapSecretId) if err != nil { continue } fmt.Println(string(resp.Body())) mapIdList[mapId] = true } // 修改续扫提醒标志 mapUpdate.RescanNotifyFlag = config.MAP_RESCAN_FLAG // 记录发送通知时的续扫提醒阈值 thresholdStr := strconv.FormatFloat(float64(threshold), 'f', 2, 32) + "%" mapUpdate.RescanNotifyThreshold = &thresholdStr _, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate) if err != nil { continue } } } fmt.Println("更新地图续扫提醒成功") return true } // UpdateMapDeployStatusWithoutHttp 根据地图id和版本号查询朴津地图下发情况,并更新对应地图状态 func UpdateMapDeployStatusWithoutHttp(ctx context.Context) bool { // 查询 update_flag 为1的更新地图列表 mapUpdates, err := mysql.QueryMapUpdateRecordsByUpdateFlag(ctx, config.UPDATE_MAP_UPLOAD_FLAG) if err != nil { fmt.Println("获取更新地图列表失败") return false } for _, mapUpdate := range mapUpdates { //// 查询地图id是否存在于地图列表中 //exist, record := checkMapIdExist(mapUpdate.MapID, mapInfoList) //fmt.Println("exist", exist) //fmt.Println("record", record) //if !exist { // fmt.Println("地图列表中不存在该地图id,跳过...") // continue //} mapDeployRes, err := checkMapDeployStatus(mapUpdate.MapID, *mapUpdate.UploadVersion) if err != nil { return false } fmt.Println("mapDeployRes", mapDeployRes) changeFlag := false if mapDeployRes.Code == 500 { // 地图不存在,将更新地图状态标记为无效 mapUpdate.UpdateFlag = config.UPDATE_MAP_NOT_VALID_FLAG changeFlag = true } else if mapDeployRes.Code == 200 { if mapDeployRes.Data.Status == 1 { mapUpdate.UpdateFlag = config.UPDATE_MAP_DEPLOY_FLAG changeFlag = true } } if changeFlag { _, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate) if err != nil { fmt.Println("修改更新地图状态失败") return false } } time.Sleep(2 * time.Second) } return true } // QueryMapRescanNotifyList 查询地图续扫提醒列表 // @router /map/rescan/notify/list [GET] func QueryMapRescanNotifyList(ctx context.Context, c *app.RequestContext) { var record model.MapUpdate err := c.BindAndValidate(&record) fmt.Println("record", record) var pageFlag bool if c.Query("page") != "" && c.Query("pageSize") != "" { pageFlag = true } else { pageFlag = false } page, _ := strconv.Atoi(c.Query("page")) pageSize, _ := strconv.Atoi(c.Query("pageSize")) records, count, err := mysql.QueryMapRescanList(ctx, &record, pageFlag, page, pageSize) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图续扫提醒记录查询失败", Total: 0}) return } output, err := json.Marshal(records) if err != nil { c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图续扫提醒记录查询失败", Total: 0}) return } c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "地图续扫提醒记录查询成功", Data: string(output), Total: int(count)}) } func getUpdatePercentage(percentageStr string) (float32, error) { // 去掉末尾的% valueStr := percentageStr[:len(percentageStr)-1] //fmt.Println("valueStr", valueStr) // 将字符串转为float32 value, err := strconv.ParseFloat(valueStr, 32) if err != nil { fmt.Println("err", err) return 0, err } return float32(value), nil } // 计算oss中文件列表的总大小 func calculateTotalFileSize(fileList []string) int { var totalSize int for _, file := range fileList { size, err := util.GetOSSFileSize(config.OssBucket, file) // 获取oss中单个文件的大小 if err != nil { return 0 } totalSize += size } return totalSize } func getTime(timeStr string, layout string) (time.Time, error) { t, err := time.Parse(layout, timeStr) if err != nil { return time.Time{}, err } return t, nil } // 调用 朴津地图列表查询 接口 func getMapInfoList() ([]model.MapInfo, error) { paramMap := make(map[string]interface{}) paramMap["mapType"] = "0" resp, err := pji_client.ApiClient.GetRequest(c_pji.PjiApiBaseUrl+"mapList?mapType=0", paramMap, pji_client.MapSecretId) if err != nil { return nil, err } fmt.Println("resp", resp) // 解析json响应 body := resp.Body() //body := string("{\n\t\"msg\": \"操作成功\",\n\t\"code\": 200,\n\t\"data\": [\n\t\t{\n\t\t\t\"mapType\": \"1\",\n\t\t\t\"mapId\": \"fee84db56d254d79b85971cb4ab2e7f1\",\n\t\t\t\"mapName\": \"太和桥办公室20221115\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"c976c8c5cf5249f8adb89c478a56ea8a\",\n\t\t\t\"buildId\": \"70c8574ad80248f6a90cd6fc5f2e11bf\",\n\t\t\t\"buildName\": \"日新楼\",\n\t\t\t\"createTime\": \"2023-05-30 15:09:34\",\n\t\t\t\"updateTime\": \"2024-01-09 14:21:02\",\n\t\t\t\"version\": 1699948169228,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1669241329772306432.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": [\n\t\t\t\t\"P1YTXS1M22AM00001\"\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"1965f3dbd1924f32b1031a471ea9f63c\",\n\t\t\t\"mapName\": \"充电图\",\n\t\t\t\"floor\": 0,\n\t\t\t\"floorId\": \"52798b3b8c1f4b2d9dd33ccec27c499a\",\n\t\t\t\"buildId\": \"736fe900d6484370a868ce8af49e4280\",\n\t\t\t\"buildName\": \"1\",\n\t\t\t\"createTime\": \"2023-06-21 21:27:59\",\n\t\t\t\"updateTime\": \"2023-11-01 23:07:08\",\n\t\t\t\"version\": 1687354079376,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1671510275657474048.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"207c7618965d4875b0d6b10382929e21\",\n\t\t\t\"mapName\": \"温泉\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"ad2239dc409c418688575edbfac0cd01\",\n\t\t\t\"buildId\": \"01928c7726cd461b902625780b0c64a5\",\n\t\t\t\"buildName\": \"1324\",\n\t\t\t\"createTime\": \"2023-07-05 13:57:17\",\n\t\t\t\"updateTime\": \"2023-11-30 09:21:59\",\n\t\t\t\"version\": 1691545587368,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1676470285395206144.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": [\n\t\t\t\t\"P1YTXS1M22AM00001\"\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"611028da8815415c9f7c93caf4625707\",\n\t\t\t\"mapName\": \"大厅建图测试\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"f2eda7e4022e4dbcacba56ee206c07f9\",\n\t\t\t\"buildId\": \"8cf1bdfdde7f465cbb893dba8e9a3500\",\n\t\t\t\"buildName\": \"日小楼1\",\n\t\t\t\"createTime\": \"2023-08-07 14:05:17\",\n\t\t\t\"updateTime\": \"2024-04-15 13:45:01\",\n\t\t\t\"version\": 1697632909852,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1688431099182227456.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": [\n\t\t\t\t\"P1YTXS1M22AM00002\"\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"da516324d8fc4de7a85c6c90d731f0ba\",\n\t\t\t\"mapName\": \"北京鑫雷晟星途4S店\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"458bb51e42ce4b98bc518626bb6b7c43\",\n\t\t\t\"buildId\": \"50df97f353dd49c59779ddfabdd89b66\",\n\t\t\t\"buildName\": \"北京鑫雷晟星途4S店\",\n\t\t\t\"createTime\": \"2023-08-10 11:12:18\",\n\t\t\t\"updateTime\": \"2023-09-13 18:20:46\",\n\t\t\t\"version\": 1692175845494,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1691723397236170752.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"0db5f09b8afc4a69bb095de8fb7312d8\",\n\t\t\t\"mapName\": \"8.14采集地图\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"761e1d04fa5242338f5b4ae4044fe8f0\",\n\t\t\t\"buildId\": \"0db7b180bc9b4f7599d3fc8737c73698\",\n\t\t\t\"buildName\": \"星途店\",\n\t\t\t\"createTime\": \"2023-08-14 10:37:50\",\n\t\t\t\"updateTime\": \"2024-01-18 14:41:16\",\n\t\t\t\"version\": 1705560075636,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/mapPackage/1747445238371164160.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"4518d2ae484448749753f102794328cd\",\n\t\t\t\"mapName\": \"食堂二楼\",\n\t\t\t\"floor\": 2,\n\t\t\t\"floorId\": \"24ec39c051cd41748e03988aff264aea\",\n\t\t\t\"buildId\": \"017a3f3304f041158e28107fc3fe30a3\",\n\t\t\t\"buildName\": \"怡沁楼\",\n\t\t\t\"createTime\": \"2023-08-15 17:10:53\",\n\t\t\t\"updateTime\": \"2024-04-10 16:07:47\",\n\t\t\t\"version\": 1692090653814,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1691376917866520576.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"1349a648005b445798653f186b635b58\",\n\t\t\t\"mapName\": \"北京捷奥泰星途体验中心\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"eb6c129a9c3248198e8300abf2a001a9\",\n\t\t\t\"buildId\": \"e37daecac1e74d5f8f0138a6a187c735\",\n\t\t\t\"buildName\": \"北京捷奥泰星途体验中心\",\n\t\t\t\"createTime\": \"2023-09-12 09:17:29\",\n\t\t\t\"updateTime\": \"2023-10-26 17:26:48\",\n\t\t\t\"version\": 1694770614834,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1702556497806860288.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"25118252d6024a48b2df128100dd4416\",\n\t\t\t\"mapName\": \"新定位地图\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"a61842ae220d4f9792780a8767ff7bea\",\n\t\t\t\"buildId\": \"1931d525d5b04f399dd7554c7cc48c8c\",\n\t\t\t\"buildName\": \"新新楼\",\n\t\t\t\"createTime\": \"2023-09-13 11:42:26\",\n\t\t\t\"updateTime\": \"2023-09-13 11:42:26\",\n\t\t\t\"version\": 1694576546472,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1701803497835110400.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"4f8c3a790ba6488ebcbf94c4a621a111\",\n\t\t\t\"mapName\": \"系统集成专用安庆2楼\",\n\t\t\t\"floor\": 2,\n\t\t\t\"floorId\": \"abb15b8c7bc94c21a21ee8b3f09c0111\",\n\t\t\t\"buildId\": \"d700b0ebbcfc4345ae0553364114b111\",\n\t\t\t\"buildName\": \"系统集成专用安庆\",\n\t\t\t\"createTime\": \"2023-11-13 10:53:25\",\n\t\t\t\"updateTime\": \"2023-11-22 10:09:05\",\n\t\t\t\"version\": 1699844005614,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1723896825847455744.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"8068197000f84a5483dabf5a6ad3707c\",\n\t\t\t\"mapName\": \"智慧楼\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"fc86319a04824122b63719b062811106\",\n\t\t\t\"buildId\": \"8afb5463ad9140aa9e0e42b9ca93e075\",\n\t\t\t\"buildName\": \"智慧楼\",\n\t\t\t\"createTime\": \"2024-03-21 10:59:27\",\n\t\t\t\"updateTime\": \"2024-04-19 18:15:07\",\n\t\t\t\"version\": 1713521707438,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/mapPackage/20240411/1778341656103198720.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t}\n\t]\n}") //body := string("{\n\t\"msg\": \"操作成功\",\n\t\"code\": 200,\n\t\"data\": [\n\t\t{\n\t\t\t\"mapType\": \"1\",\n\t\t\t\"mapId\": \"2247aeb31ad34wwddfd9c3ba24536e52859\",\n\t\t\t\"mapName\": \"合肥演示\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"695abbcfdf99471sdfff3b9b1d0818164e7\",\n\t\t\t\"buildId\": \"7b4e8d01c08f472fabf7ccff2b2336s\",\n\t\t\t\"buildName\": \"2\",\n\t\t\t\"createTime\": \"2023-05-30 15:09:34\",\n\t\t\t\"updateTime\": \"2024-01-09 14:21:02\",\n\t\t\t\"version\": 1699948169228,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1669241329772306432.zip\",\n\t\t\t\"customAreaId\": 355,\n\t\t\t\"snCodeList\": [\n\t\t\t\t\"P1YTXS1M22AM00001\",\n\t\t\t\t\"P1YTYD1M233M00224\"\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"1965f3dbd1924f32b1031a471ea9f63c\",\n\t\t\t\"mapName\": \"充电图\",\n\t\t\t\"floor\": 0,\n\t\t\t\"floorId\": \"52798b3b8c1f4b2d9dd33ccec27c499a\",\n\t\t\t\"buildId\": \"736fe900d6484370a868ce8af49e4280\",\n\t\t\t\"buildName\": \"1\",\n\t\t\t\"createTime\": \"2023-06-21 21:27:59\",\n\t\t\t\"updateTime\": \"2023-11-01 23:07:08\",\n\t\t\t\"version\": 1687354079376,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1671510275657474048.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"207c7618965d4875b0d6b10382929e21\",\n\t\t\t\"mapName\": \"温泉\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"ad2239dc409c418688575edbfac0cd01\",\n\t\t\t\"buildId\": \"01928c7726cd461b902625780b0c64a5\",\n\t\t\t\"buildName\": \"1324\",\n\t\t\t\"createTime\": \"2023-07-05 13:57:17\",\n\t\t\t\"updateTime\": \"2023-11-30 09:21:59\",\n\t\t\t\"version\": 1691545587368,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1676470285395206144.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": [\n\t\t\t\t\"P1YTXS1M22AM00003\"\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"611028da8815415c9f7c93caf4625707\",\n\t\t\t\"mapName\": \"大厅建图测试\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"f2eda7e4022e4dbcacba56ee206c07f9\",\n\t\t\t\"buildId\": \"8cf1bdfdde7f465cbb893dba8e9a3500\",\n\t\t\t\"buildName\": \"日小楼1\",\n\t\t\t\"createTime\": \"2023-08-07 14:05:17\",\n\t\t\t\"updateTime\": \"2024-04-15 13:45:01\",\n\t\t\t\"version\": 1697632909852,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1688431099182227456.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": [\n\t\t\t\t\"P1YTXS1M22AM00002\"\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"da516324d8fc4de7a85c6c90d731f0ba\",\n\t\t\t\"mapName\": \"北京鑫雷晟星途4S店\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"458bb51e42ce4b98bc518626bb6b7c43\",\n\t\t\t\"buildId\": \"50df97f353dd49c59779ddfabdd89b66\",\n\t\t\t\"buildName\": \"北京鑫雷晟星途4S店\",\n\t\t\t\"createTime\": \"2023-08-10 11:12:18\",\n\t\t\t\"updateTime\": \"2023-09-13 18:20:46\",\n\t\t\t\"version\": 1692175845494,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1691723397236170752.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"0db5f09b8afc4a69bb095de8fb7312d8\",\n\t\t\t\"mapName\": \"8.14采集地图\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"761e1d04fa5242338f5b4ae4044fe8f0\",\n\t\t\t\"buildId\": \"0db7b180bc9b4f7599d3fc8737c73698\",\n\t\t\t\"buildName\": \"星途店\",\n\t\t\t\"createTime\": \"2023-08-14 10:37:50\",\n\t\t\t\"updateTime\": \"2024-01-18 14:41:16\",\n\t\t\t\"version\": 1705560075636,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/mapPackage/1747445238371164160.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"4518d2ae484448749753f102794328cd\",\n\t\t\t\"mapName\": \"食堂二楼\",\n\t\t\t\"floor\": 2,\n\t\t\t\"floorId\": \"24ec39c051cd41748e03988aff264aea\",\n\t\t\t\"buildId\": \"017a3f3304f041158e28107fc3fe30a3\",\n\t\t\t\"buildName\": \"怡沁楼\",\n\t\t\t\"createTime\": \"2023-08-15 17:10:53\",\n\t\t\t\"updateTime\": \"2024-04-10 16:07:47\",\n\t\t\t\"version\": 1692090653814,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1691376917866520576.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"1349a648005b445798653f186b635b58\",\n\t\t\t\"mapName\": \"北京捷奥泰星途体验中心\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"eb6c129a9c3248198e8300abf2a001a9\",\n\t\t\t\"buildId\": \"e37daecac1e74d5f8f0138a6a187c735\",\n\t\t\t\"buildName\": \"北京捷奥泰星途体验中心\",\n\t\t\t\"createTime\": \"2023-09-12 09:17:29\",\n\t\t\t\"updateTime\": \"2023-10-26 17:26:48\",\n\t\t\t\"version\": 1694770614834,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1702556497806860288.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"25118252d6024a48b2df128100dd4416\",\n\t\t\t\"mapName\": \"新定位地图\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"a61842ae220d4f9792780a8767ff7bea\",\n\t\t\t\"buildId\": \"1931d525d5b04f399dd7554c7cc48c8c\",\n\t\t\t\"buildName\": \"新新楼\",\n\t\t\t\"createTime\": \"2023-09-13 11:42:26\",\n\t\t\t\"updateTime\": \"2023-09-13 11:42:26\",\n\t\t\t\"version\": 1694576546472,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1701803497835110400.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"4f8c3a790ba6488ebcbf94c4a621a111\",\n\t\t\t\"mapName\": \"系统集成专用安庆2楼\",\n\t\t\t\"floor\": 2,\n\t\t\t\"floorId\": \"abb15b8c7bc94c21a21ee8b3f09c0111\",\n\t\t\t\"buildId\": \"d700b0ebbcfc4345ae0553364114b111\",\n\t\t\t\"buildName\": \"系统集成专用安庆\",\n\t\t\t\"createTime\": \"2023-11-13 10:53:25\",\n\t\t\t\"updateTime\": \"2023-11-22 10:09:05\",\n\t\t\t\"version\": 1699844005614,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/public/1723896825847455744.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": null\n\t\t},\n\t\t{\n\t\t\t\"mapType\": \"0\",\n\t\t\t\"mapId\": \"8068197000f84a5483dabf5a6ad3707c\",\n\t\t\t\"mapName\": \"智慧楼\",\n\t\t\t\"floor\": 1,\n\t\t\t\"floorId\": \"fc86319a04824122b63719b062811106\",\n\t\t\t\"buildId\": \"8afb5463ad9140aa9e0e42b9ca93e075\",\n\t\t\t\"buildName\": \"智慧楼\",\n\t\t\t\"createTime\": \"2024-03-21 10:59:27\",\n\t\t\t\"updateTime\": \"2024-04-19 18:15:07\",\n\t\t\t\"version\": 1713521707438,\n\t\t\t\"zipUrl\": \"https://sitfile.chinapji.com/pjicloud/mapPackage/20240411/1778341656103198720.zip\",\n\t\t\t\"customAreaId\": 334,\n\t\t\t\"snCodeList\": [\"P1YNYD1M225000112\"]\n\t\t}\n\t]\n}") fmt.Println("body", body) var mapRes model.MapInfoRes err = json.Unmarshal([]byte(body), &mapRes) if err != nil || mapRes.Code != 200 { return nil, err } //fmt.Println("mapRes", mapRes) return mapRes.Data, nil } // 调用 朴津地图下发状态查询 接口 func checkMapDeployStatus(mapId string, version string) (model.MapDeployRes, error) { paramMap := make(map[string]interface{}) paramMap["mapId"] = mapId paramMap["version"] = version fmt.Println("paramMap", paramMap) resp, err := pji_client.ApiClient.GetRequestWithForm(c_pji.PjiApiBaseUrl+"getMapVersionStatus?mapId="+mapId+"&version="+version, paramMap, pji_client.MapSecretId) if err != nil { return model.MapDeployRes{}, err } //fmt.Println("resp", resp) // 解析json响应 body := resp.Body() //fmt.Println("body", body) var mapRes model.MapDeployRes err = json.Unmarshal([]byte(body), &mapRes) fmt.Println("mapRes", mapRes) if err != nil { return mapRes, err } return mapRes, nil } // 查询地图id是否存在于地图列表中 func checkMapIdExist(mapId string, mapInfoList []model.MapInfo) (bool, model.MapInfo) { // 转为map IdMap := make(map[string]model.MapInfo) for _, info := range mapInfoList { IdMap[info.MapId] = info } record, exist := IdMap[mapId] return exist, record } func groupMapListByDevice(mapInfoList []model.MapInfo) map[string][]model.MapInfo { // 转为map snCodeMap := make(map[string][]model.MapInfo) for _, info := range mapInfoList { for _, snCode := range info.SnCodeList { if _, exist := snCodeMap[snCode]; !exist { snCodeMap[snCode] = make([]model.MapInfo, 0) snCodeMap[snCode] = append(snCodeMap[snCode], info) } else { // 判断buildId及floorId是否与snCodeMap[snCode]已记录的数据相同 for index, record := range snCodeMap[snCode] { if record.BuildId == info.BuildId && record.FloorId == info.FloorId { // 相同则说明为同一区域的地图有多个版本,只保留一个版本 // 取最近的版本号(当前版本号为时间戳,即取时间戳最大的作为最近的版本号) if record.Version < info.Version { snCodeMap[snCode][index] = info break } } } // snCodeMap[snCode]已记录的数据未找到相同区域的地图,则添加记录 snCodeMap[snCode] = append(snCodeMap[snCode], info) } } } return snCodeMap } // 根据场景id获取数据采集时的地图id func getMapIdById(id string) (string, error) { var mapId string // 下载map.json // 根据id获取对应的oss文件列表 allFileList, err := util.GetExactedMapFileById(id) //fmt.Println("allFileList", allFileList) // 过滤特定后缀的文件列表 fileList := util.FilterBySuffixes(allFileList, config.MapJsonFiltersuffixes...) fmt.Println("fileList", fileList) // 创建临时文件夹 tmpDir, err := os.MkdirTemp("", "temp-download-*") fmt.Println("tmpDir:", tmpDir) if err != nil { fmt.Println("Error creating temporary directory:", err) return "", err } c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir) for _, file := range fileList { path := filepath.Join(tmpDir, filepath.Base(file)) err = config.OssBucket.GetObjectToFile(file, path) if err != nil { fmt.Println("Error downloading file:", err) return "", err } c_log.GlobalLogger.Info("下载map.json文件 - 成功") // 读取json文件 - mapId // 打开文件 file, err := os.Open(path) if err != nil { fmt.Println("Error opening file:", err) return "", err } defer file.Close() // 读取文件内容 fileData, err := ioutil.ReadAll(file) if err != nil { fmt.Println("Error reading file:", err) return "", err } // Json转换为map var mapData map[string]interface{} err = json.Unmarshal(fileData, &mapData) data := mapData["map"].(map[string]interface{}) // 读取mapId mapId = data["mapId"].(string) fmt.Println("mapId:", mapId) } return mapId, err } func Unzip(zipPath, destPath string) error { r, err := zip.OpenReader(zipPath) if err != nil { fmt.Println("压缩包读取失败") return err } defer r.Close() // 遍历zip文件中的每个文件和目录 for _, f := range r.File { // 获取文件路径信息 fileDestPath := filepath.Join(destPath, f.Name) // 创建目标文件或目录的父目录(如果不存在的话) if err := os.MkdirAll(filepath.Dir(fileDestPath), 0755); err != nil { fmt.Println("创建目录失败:", err) } // 如果是目录,则创建对应的目录结构 if f.FileInfo().IsDir() { continue } // 打开要写入的目标文件 out, err := os.OpenFile(fileDestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) if err != nil { return err } defer out.Close() // 读取zip中的文件内容并写入到目标文件 in, err := f.Open() if err != nil { return err } defer in.Close() _, err = io.Copy(out, in) if err != nil { return err } } return nil }