map_service.go 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. package map_service
  2. import (
  3. "archive/zip"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/cloudwego/hertz/pkg/app"
  8. "github.com/cloudwego/hertz/pkg/protocol/consts"
  9. uuid "github.com/satori/go.uuid"
  10. "io"
  11. "io/ioutil"
  12. "net/http"
  13. "os"
  14. "path/filepath"
  15. "pji_desktop_http/biz/dal/mysql"
  16. "pji_desktop_http/biz/model"
  17. "pji_desktop_http/common/config"
  18. "pji_desktop_http/common/config/c_log"
  19. "pji_desktop_http/common/config/c_pji"
  20. "pji_desktop_http/common/entity"
  21. "pji_desktop_http/common/util"
  22. "pji_desktop_http/pji_client"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. // CheckMapBufConsistency 检查请求id对应的mapBuf文件夹的一致性
  28. // @router /map/checkmapbuf [POST]
  29. func CheckMapBufConsistency(ctx context.Context, c *app.RequestContext) {
  30. var req []string
  31. err := c.BindAndValidate(&req)
  32. if err != nil {
  33. c.String(consts.StatusBadRequest, err.Error())
  34. return
  35. }
  36. fmt.Println(req)
  37. if len(req) == 0 { // 请求数据为空
  38. c.JSON(consts.StatusBadRequest, entity.HttpResult{Status: false, Code: "", Message: "请求数据为空。"})
  39. } else if len(req) == 1 { // 只有一条数据则直接返回
  40. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "mapBuf文件夹数据一致。"})
  41. } else {
  42. var firstValue int
  43. for i, id := range req {
  44. // 根据id获取对应的oss文件列表
  45. fileList, err := util.GetExactedMapFileById(id)
  46. // 过滤特定后缀的文件列表
  47. fileList = util.FilterBySuffixes(fileList, config.MapBufFiltersuffixes...)
  48. //fmt.Println("Filtered Strings:", fileList)
  49. if err != nil {
  50. c.String(consts.StatusBadRequest, err.Error())
  51. return
  52. }
  53. // 获取文件列表的总大小
  54. totalSize := calculateTotalFileSize(fileList)
  55. fmt.Println("Total Size:", totalSize)
  56. // 判断不同文件列表(mapBuf)中文件的总大小是否一致
  57. if i == 0 {
  58. firstValue = totalSize
  59. } else {
  60. if totalSize != firstValue {
  61. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "mapBuf文件夹数据不一致。"})
  62. return
  63. }
  64. }
  65. }
  66. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "mapBuf文件夹数据一致。"})
  67. }
  68. }
  69. // DownloadOSSFile 根据objectKey下载指定的oss文件
  70. // @router /map/downloadossfile [GET]
  71. func DownloadOSSFile(ctx context.Context, c *app.RequestContext) {
  72. objectKey := c.Query("objectKey")
  73. // 从OSS下载文件
  74. reader, err := config.OssBucket.GetObject(objectKey)
  75. if err != nil {
  76. c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
  77. return
  78. }
  79. defer reader.Close()
  80. // 设置响应头
  81. c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(objectKey)))
  82. c.Response.Header.Set("Content-Type", "binary/octet-stream")
  83. // 将文件流式传输回客户端
  84. data, err := io.ReadAll(reader)
  85. if err != nil {
  86. panic(err)
  87. }
  88. if _, err := c.Write(data); err != nil {
  89. c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
  90. return
  91. }
  92. }
  93. // DownloadMapZipFile 根据请求id从oss拉取并打包下载建图所需要的文件
  94. // @router /map/download/map/zip [GET]
  95. func DownloadMapZipFile(ctx context.Context, c *app.RequestContext) {
  96. id := c.Query("id")
  97. fmt.Println("id", id)
  98. deviceNo := c.Query("deviceNo")
  99. fmt.Println("deviceNo", deviceNo)
  100. ids := strings.Split(id, ",")
  101. fmt.Println("ids", ids)
  102. sceneId := ids[0]
  103. // 根据场景id查询地图id
  104. mapId, err := getMapIdById(sceneId)
  105. if err != nil || mapId == "" {
  106. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取mapId失败"})
  107. return
  108. }
  109. fmt.Println("mapId", mapId)
  110. // 获取朴津地图压缩包url
  111. //originUrl
  112. deviceMaps, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapId, deviceNo)
  113. if err != nil {
  114. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备地图失败"})
  115. return
  116. }
  117. if len(deviceMaps) == 0 {
  118. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "未查询到对应设备地图"})
  119. return
  120. }
  121. deviceMap := deviceMaps[0]
  122. originUrl := deviceMap.PjiMapZipURL
  123. fmt.Println("originUrl", originUrl)
  124. // 根据id生成用于地图更新的压缩包
  125. filePath, tmpDir, err := generateMapZipById(ids, originUrl)
  126. if err != nil {
  127. c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
  128. }
  129. fmt.Println("filePath", filePath)
  130. // 检查文件是否存在
  131. if _, err := os.Stat(filePath); os.IsNotExist(err) {
  132. c.JSON(http.StatusNotFound, map[string]string{"error": "File not found"})
  133. return
  134. }
  135. // 打开文件
  136. f, err := os.Open(filePath)
  137. if err != nil {
  138. c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to open file"})
  139. return
  140. }
  141. defer f.Close()
  142. // 设置响应头
  143. c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(filePath)))
  144. c.Response.Header.Set("Content-Type", "binary/octet-stream")
  145. // 将文件流式传输回客户端
  146. data, err := io.ReadAll(f)
  147. if err != nil {
  148. panic(err)
  149. }
  150. if _, err := c.Write(data); err != nil {
  151. c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
  152. return
  153. }
  154. defer os.RemoveAll(tmpDir)
  155. }
  156. // DownloadMapBagFile 根据请求id从oss拉取并下载解析后的map.bag文件
  157. // @router /map/downloadmapbagfile [GET]
  158. func DownloadMapBagFile(ctx context.Context, c *app.RequestContext) {
  159. id := c.Query("id")
  160. fmt.Println("id: ", id)
  161. // 根据id获取对应的oss文件列表
  162. fileList, err := util.GetExactedMapFileById(id)
  163. // 过滤特定后缀的文件列表
  164. fileList = util.FilterBySuffixes(fileList, config.MapBagFiltersuffixes...)
  165. //fmt.Println("fileList", fileList)
  166. objectKey := fileList[0]
  167. // 从OSS下载文件
  168. reader, err := config.OssBucket.GetObject(objectKey)
  169. if err != nil {
  170. c.JSON(consts.StatusInternalServerError, entity.HttpResult{Status: false, Code: "", Message: "解析地图Bag下载失败。"})
  171. return
  172. }
  173. defer reader.Close()
  174. // 设置响应头
  175. c.Response.Header.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filepath.Base(objectKey)))
  176. c.Response.Header.Set("Content-Type", "binary/octet-stream")
  177. // 将文件流式传输回客户端
  178. data, err := io.ReadAll(reader)
  179. if err != nil {
  180. panic(err)
  181. }
  182. if _, err := c.Write(data); err != nil {
  183. c.JSON(consts.StatusInternalServerError, entity.HttpResult{Status: false, Code: "", Message: "解析地图Bag下载失败。"})
  184. return
  185. }
  186. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "解析地图Bag下载成功。"})
  187. }
  188. // 根据id生成用于地图更新的压缩包
  189. func generateMapZipById(ids []string, originUrl string) (file string, tmpDir string, err error) {
  190. // 根据id获取对应的oss文件列表
  191. allFileList, err := util.GetExactedMapFileByIds(ids)
  192. //fmt.Println("allFileList", allFileList)
  193. // 过滤特定后缀的文件列表
  194. mapBufFileList := util.FilterBySuffixes(allFileList, config.MapBufFiltersuffixes...)
  195. //fmt.Println("Filtered Strings:", fileList)
  196. if err != nil {
  197. return
  198. }
  199. // 创建临时文件夹
  200. tmpDir, err = os.MkdirTemp("", "temp-download-*")
  201. fmt.Println("tmpDir:", tmpDir)
  202. if err != nil {
  203. fmt.Println("Error creating temporary directory:", err)
  204. return "", "", err
  205. }
  206. c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir)
  207. // 创建根文件夹(文件打包的根目录)
  208. baseDir := filepath.Join(tmpDir, "data")
  209. if err := os.Mkdir(baseDir, 0755); err != nil {
  210. fmt.Println("Error creating subdirectory:", err)
  211. return "", "", err
  212. }
  213. c_log.GlobalLogger.Info("创建文件打包根目录:", baseDir)
  214. // 根目录创建子文件夹 bag_folder
  215. bagFolderDir := filepath.Join(baseDir, "bag_folder")
  216. if err := os.Mkdir(bagFolderDir, 0755); err != nil {
  217. fmt.Println("Error creating subdirectory:", err)
  218. return "", "", err
  219. }
  220. c_log.GlobalLogger.Info("根目录下创建bag_folder子文件夹:", bagFolderDir)
  221. // 根目录创建子文件夹 origin_map_folder
  222. originMapFolderDir := filepath.Join(baseDir, "origin_map_folder")
  223. if err := os.Mkdir(originMapFolderDir, 0755); err != nil {
  224. fmt.Println("Error creating subdirectory:", err)
  225. return "", "", err
  226. }
  227. c_log.GlobalLogger.Info("根目录下创建origin_map_folder子文件夹:", originMapFolderDir)
  228. // 子文件夹 bag_folder 创建子文件夹 mapBuf
  229. mapBufDir := filepath.Join(bagFolderDir, "mapBuf")
  230. if err := os.Mkdir(mapBufDir, 0755); err != nil {
  231. fmt.Println("Error creating subdirectory:", err)
  232. return "", "", err
  233. }
  234. c_log.GlobalLogger.Info("bag_folder创建mapBuf子文件夹 :", mapBufDir)
  235. // 从oss下载build_map.bag文件到 bag_folder 文件夹
  236. // 过滤特定后缀的文件列表
  237. buildMapBagFileList := util.FilterBySuffixes(allFileList, config.BuildMapBagFiltersuffixes...)
  238. fmt.Println("buildMapBagFileList", buildMapBagFileList)
  239. //buildMapBagFile := buildMapBagFileList[0]
  240. //err = config.OssBucket.GetObjectToFile(buildMapBagFile, filepath.Join(bagFolderDir, filepath.Base(buildMapBagFile)))
  241. for i, file := range buildMapBagFileList {
  242. fileName := "build_map" + "-" + strconv.Itoa(i) + ".bag"
  243. err = config.OssBucket.GetObjectToFile(file, filepath.Join(bagFolderDir, fileName))
  244. if err != nil {
  245. fmt.Println("Error downloading build map file:", err)
  246. return "", "", err
  247. }
  248. }
  249. c_log.GlobalLogger.Info("下载文件到bag_folder文件夹 - 成功")
  250. // 从oss下载文件到 mapBuf 文件夹
  251. for _, file := range mapBufFileList {
  252. err = config.OssBucket.GetObjectToFile(file, filepath.Join(mapBufDir, filepath.Base(file)))
  253. if err != nil {
  254. fmt.Println("Error downloading mapBuf file:", err)
  255. return "", "", err
  256. }
  257. }
  258. c_log.GlobalLogger.Info("下载文件到mapBuf子文件夹 - 成功")
  259. // 从oss下载bag文件到 origin_map_folder 文件夹
  260. // 过滤特定后缀的文件列表
  261. //originMapFileList := util.FilterBySuffixes(allFileList, config.OriginMapFiltersuffixes...)
  262. //for _, file := range originMapFileList {
  263. // err = config.OssBucket.GetObjectToFile(file, filepath.Join(originMapFolderDir, filepath.Base(file)))
  264. // if err != nil {
  265. // fmt.Println("Error downloading origin_map file:", err)
  266. // return "", "", err
  267. // }
  268. //}
  269. //err = config.OssBucket.GetObjectToFile(originUrl, filepath.Join(originMapFolderDir, filepath.Base(originUrl)))
  270. err = util.Downloadfile(originUrl, filepath.Join(originMapFolderDir, filepath.Base(originUrl)))
  271. if err != nil {
  272. fmt.Println("Error downloading origin_map file:", err)
  273. return "", "", err
  274. }
  275. c_log.GlobalLogger.Info("下载文件到origin_map_folder文件夹 - 成功")
  276. // 创建压缩文件
  277. zipPath := filepath.Join(tmpDir, "mapFile-"+ids[0]+".zip")
  278. zipFile, err := os.Create(zipPath)
  279. if err != nil {
  280. fmt.Println("Error creating ZIP file:", err)
  281. return "", "", err
  282. }
  283. defer zipFile.Close()
  284. zipWriter := zip.NewWriter(zipFile)
  285. defer zipWriter.Close()
  286. // 压缩文件夹
  287. if err := util.AddDirToZip(baseDir, zipWriter); err != nil {
  288. fmt.Println("Error adding directory to ZIP:", err)
  289. return "", "", err
  290. }
  291. fmt.Println("ZIP file created successfully.")
  292. c_log.GlobalLogger.Info("创建压缩文件 - 成功")
  293. return zipPath, tmpDir, nil
  294. }
  295. // UploadMapFile 将地图更新相关文件上传到oss
  296. // @router /map/upload/map [GET]
  297. func UploadMapFile(ctx context.Context, c *app.RequestContext) {
  298. equipmentNo := c.Query("equipmentNo")
  299. fmt.Println("equipmentNo", equipmentNo)
  300. mapId := c.Query("mapId")
  301. fmt.Println("mapId", mapId)
  302. timeStamp := c.Query("timeStamp")
  303. fmt.Println("timeStamp", timeStamp)
  304. header, err := c.FormFile("file")
  305. if err != nil {
  306. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  307. return
  308. }
  309. fileName := header.Filename
  310. fmt.Println("filename", fileName)
  311. ossObjectKey := config.UpdateMapOssBasePrefix + "/" + equipmentNo + "/" + mapId + "/" + timeStamp + "/" + fileName
  312. fmt.Println("ossObjectKey", ossObjectKey)
  313. f, _ := header.Open()
  314. defer f.Close()
  315. config.OssMutex.Lock()
  316. err = config.OssBucket.PutObject(ossObjectKey, f)
  317. config.OssMutex.Unlock()
  318. if err != nil {
  319. c_log.GlobalLogger.Error("程序异常退出。上传文件", fileName, "->", ossObjectKey, "出错:", err)
  320. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传文件失败", Details: ""})
  321. return
  322. }
  323. c_log.GlobalLogger.Info("上传文件", fileName, "->", ossObjectKey, "成功。")
  324. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传文件成功", Details: ossObjectKey})
  325. }
  326. // AddMapUpdateRecord 添加仿真测试记录
  327. // @router /map/add/record [GET]
  328. func AddMapUpdateRecord(ctx context.Context, c *app.RequestContext) {
  329. var record model.MapUpdate
  330. err := c.BindAndValidate(&record)
  331. record.ID = uuid.NewV1().String()
  332. fmt.Println("record", record)
  333. if err != nil {
  334. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  335. return
  336. }
  337. err = mysql.AddMapUpdateOneRecord(ctx, record)
  338. if err != nil {
  339. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "地图更新记录添加失败"})
  340. return
  341. }
  342. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "地图更新记录添加成功"})
  343. }
  344. // QueryMapUpdateRecord 根据条件查询地图更新记录
  345. // @router /map/query/update/record [GET]
  346. func QueryMapUpdateRecord(ctx context.Context, c *app.RequestContext) {
  347. var record model.MapUpdate
  348. err := c.BindAndValidate(&record)
  349. fmt.Println("record", record)
  350. var pageFlag bool
  351. if c.Query("page") != "" && c.Query("pageSize") != "" {
  352. pageFlag = true
  353. } else {
  354. pageFlag = false
  355. }
  356. page, _ := strconv.Atoi(c.Query("page"))
  357. pageSize, _ := strconv.Atoi(c.Query("pageSize"))
  358. records, count, err := mysql.QueryMapUpdateRecords(ctx, &record, pageFlag, page, pageSize)
  359. if err != nil {
  360. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图更新记录查询失败", Total: 0})
  361. return
  362. }
  363. output, err := json.Marshal(records)
  364. if err != nil {
  365. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图更新记录查询失败", Total: 0})
  366. return
  367. }
  368. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "地图更新查询成功", Data: string(output), Total: int(count)})
  369. }
  370. // TestUploadUpdateMap 测试朴津地图上传接口
  371. // @router /map/upload/update/map [GET]
  372. func TestUploadUpdateMap(ctx context.Context, c *app.RequestContext) {
  373. header, err := c.FormFile("file")
  374. if err != nil {
  375. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  376. return
  377. }
  378. fileName := header.Filename
  379. fmt.Println("filename", fileName)
  380. f, _ := header.Open()
  381. defer f.Close()
  382. paramMap := make(map[string]interface{})
  383. // Map<String, String> paramMap=new HashMap<>();
  384. // paramMap.put("mapId","2247aeb31ad34wwddfd9c3ba24536e52859");
  385. // paramMap.put("mapName","合肥演示");
  386. // paramMap.put("buildId"," 7b4e8d01c08f472fabf7ccff2b2336s");
  387. // paramMap.put("floorId"," 695abbcfdf99471sdfff3b9b1d0818164e7");
  388. // paramMap.put("updateType"," 7b4e8d01c08f472fabf7ccff2b2336s");
  389. // paramMap.put("floor","1");
  390. // paramMap.put("buildName"," 2");
  391. // paramMap.put("customAreaId"," 355");
  392. // paramMap.put("snCode","P1YTYD1M233M00224");
  393. // paramMap.put("fileName","1702284791385137152.zip");
  394. // paramMap.put("mapType","0");
  395. paramMap["snCode"] = "P1YNYD1M225000112"
  396. paramMap["mapId"] = "8068197000f84a5483dabf5a6ad3707c"
  397. paramMap["mapName"] = "智慧楼"
  398. paramMap["buildId"] = "8afb5463ad9140aa9e0e42b9ca93e075"
  399. paramMap["buildName"] = "智慧楼"
  400. paramMap["floorId"] = "fc86319a04824122b63719b062811106"
  401. paramMap["floor"] = "1"
  402. paramMap["customAreaId"] = "334"
  403. paramMap["updateType"] = "0"
  404. paramMap["mapType"] = "0"
  405. paramMap["filename"] = ""
  406. bytes, err := io.ReadAll(f)
  407. if err != nil {
  408. return
  409. }
  410. // 调用 朴津地图上传 接口
  411. resp, err := pji_client.ApiClient.UploadRequest(c_pji.PjiApiBaseUrl+"mapUpload", paramMap, pji_client.MapSecretId, bytes)
  412. fmt.Println(string(resp.Body()))
  413. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传地图测试"})
  414. }
  415. // UpdateDeviceMapById 根据场景id维护设备地图表
  416. // @router /map/update/deviceMap/record [GET]
  417. func UpdateDeviceMapById(ctx context.Context, c *app.RequestContext) {
  418. sceneId := c.Query("sceneId")
  419. fmt.Println("sceneId", sceneId)
  420. deviceNo := c.Query("deviceNo")
  421. fmt.Println("deviceNo", deviceNo)
  422. deviceName := c.Query("deviceName")
  423. fmt.Println("deviceName", deviceName)
  424. deviceType := c.Query("deviceType")
  425. fmt.Println("deviceType", deviceType)
  426. // 根据场景id查询地图id
  427. mapId, err := getMapIdById(sceneId)
  428. if err != nil || mapId == "" {
  429. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取mapId失败"})
  430. return
  431. }
  432. fmt.Println("mapId", mapId)
  433. // 查询朴津地图列表
  434. mapInfoList, err := getMapInfoList()
  435. if err != nil || mapInfoList == nil {
  436. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"})
  437. return
  438. }
  439. fmt.Println("mapInfoList", mapInfoList)
  440. // 判断map id是否存在于朴津地图列表中
  441. mapId = "fee84db56d254d79b85971cb4ab2e7f1" // 存在
  442. //mapId = "553d9d47f8404734997e11175f0e63f5" // 不存在
  443. exist, record := checkMapIdExist(mapId, mapInfoList)
  444. fmt.Println("exist", exist)
  445. fmt.Println("record", record)
  446. if !exist { // map id不存在于朴津地图列表中
  447. // 查询地图设备表中是否存在对应map id且有效的记录
  448. records, err := mysql.QueryValidDeviceMapByMapId(ctx, mapId)
  449. if err != nil {
  450. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"})
  451. return
  452. }
  453. fmt.Println("records", records)
  454. if len(records) > 0 { // 存在,
  455. fmt.Println("设置无效")
  456. // 将对应的map id的记录设置为无效
  457. update, err := mysql.UpdateDeviceMapValidFlagByMapId(ctx, mapId, config.MAP_NOT_VALID_FLAG)
  458. if err != nil {
  459. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
  460. return
  461. }
  462. fmt.Println("update", update)
  463. }
  464. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "1001", Message: "map id不存在地图列表中"})
  465. } else { // map id存在于朴津地图列表中
  466. snCodeList := record.SnCodeList
  467. for _, snCode := range snCodeList { // snCodeList 代表配置了对应map id地图的机器人sn码列表, 上限为机器人数量
  468. fmt.Println("snCode", snCode)
  469. deviceMap := model.DeviceMap{DeviceNo: snCode, DeviceName: deviceName,
  470. DeviceType: deviceType, MapID: record.MapId, MapType: record.MapType, MapName: record.MapName, FloorID: record.FloorId,
  471. Floor: strconv.Itoa(record.Floor), BuildID: record.BuildId, BuildName: record.BuildName, MapCreateTime: record.CreateTime, MapUpdateTime: record.UpdateTime,
  472. MapVersion: strconv.Itoa(record.Version), MapValidFlag: config.MAP_VALID_FLAG, PjiMapZipURL: record.ZipUrl, CustomAreaID: int32(record.CustomAreaId)}
  473. fmt.Println("deviceMap", deviceMap)
  474. deviceMap.MapVersion = "123456"
  475. // 查询[对应设备]是否存在对应map id且有效的记录
  476. records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapId, snCode)
  477. if err != nil {
  478. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"})
  479. return
  480. }
  481. fmt.Println("records", records)
  482. if len(records) == 0 { // 对应设备不存在对应map id且有效的记录, 说明该设备部署了新的地图(重采或新的区域地图)
  483. // 添加记录
  484. deviceMap.ID = uuid.NewV1().String()
  485. err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
  486. if err != nil {
  487. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
  488. return
  489. }
  490. } else if len(records) == 1 { // 对应设备存在1个对应map id且有效的记录, 说明该设备之前部署过该地图, 需要检测版本号来判断更新记录(版本号一致)还是添加记录(版本号不一致)
  491. fmt.Println("record", record)
  492. if deviceMap.MapVersion == records[0].MapVersion { // 地图列表查询到的版本与设备地图表中版本一致, 则更新记录
  493. // 更新记录
  494. deviceMap.ID = records[0].ID
  495. update, err := mysql.UpdateDeviceMapOneRecord(ctx, deviceMap)
  496. if err != nil {
  497. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图记录失败"})
  498. return
  499. }
  500. fmt.Println("update", update)
  501. } else { // 地图列表查询到的版本与设备地图表中版本不一致, 则先将之前的记录状态设置为无效, 然后添加记录
  502. // 将对应设备存在的对应map id的记录设置为无效
  503. update, err := mysql.UpdateDeviceMapValidFlagByMapIdAndDevice(ctx, mapId, snCode, config.MAP_NOT_VALID_FLAG)
  504. if err != nil {
  505. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
  506. return
  507. }
  508. fmt.Println("update", update)
  509. // 添加记录
  510. deviceMap.ID = uuid.NewV1().String()
  511. err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
  512. if err != nil {
  513. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
  514. return
  515. }
  516. }
  517. }
  518. }
  519. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "设备地图数据更新成功"})
  520. }
  521. }
  522. // CheckDeviceMapStatus 根据设备号及map id查询地图状态
  523. // @router /map/check/deviceMap/status [GET]
  524. func CheckDeviceMapStatus(ctx context.Context, c *app.RequestContext) {
  525. sceneId := c.Query("sceneId")
  526. fmt.Println("sceneId", sceneId)
  527. deviceNo := c.Query("deviceNo")
  528. fmt.Println("deviceNo", deviceNo)
  529. deviceName := c.Query("deviceName")
  530. fmt.Println("deviceName", deviceName)
  531. deviceType := c.Query("deviceType")
  532. fmt.Println("deviceType", deviceType)
  533. recordTime := c.Query("recordTime")
  534. fmt.Println("recordTime", recordTime)
  535. // 根据场景id查询地图id
  536. mapId, err := getMapIdById(sceneId)
  537. if err != nil || mapId == "" {
  538. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取mapId失败"})
  539. return
  540. }
  541. fmt.Println("mapId", mapId)
  542. // 查询 设备sn码 是否存在于 设备表中
  543. device, err := mysql.QueryDeviceByDeviceNo(ctx, deviceNo)
  544. if err != nil {
  545. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备记录失败"})
  546. return
  547. }
  548. fmt.Println("device", device)
  549. if device == nil { // 设备sn码 不在 设备表中
  550. record := model.Device{DeviceNo: deviceNo, DeviceName: deviceName, DeviceType: deviceType}
  551. err := mysql.AddDeviceOneRecord(ctx, record)
  552. if err != nil {
  553. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备记录失败"})
  554. return
  555. }
  556. }
  557. // 查询[对应设备]是否存在对应map id且有效的记录
  558. records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapId, deviceNo)
  559. if err != nil {
  560. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备地图失败"})
  561. return
  562. }
  563. fmt.Println("records", records)
  564. if len(records) == 0 { // [对应设备]不存在对应map id且有效的记录
  565. // 地图已重采/删除
  566. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1001", Message: "地图已重采/删除, 不符合地图更新条件"})
  567. return
  568. }
  569. // 判断记录时间戳(多个记录取最晚时间戳)是否晚于地图版本时间戳,即判断记录采集后,地图是否发生更新
  570. record := records[0]
  571. versionTimeVal, _ := strconv.Atoi(record.MapVersion)
  572. fmt.Println("versionTimeVal", versionTimeVal)
  573. recordTimeVal, _ := strconv.Atoi(recordTime)
  574. fmt.Println("recordTimeVal", recordTimeVal)
  575. if versionTimeVal > recordTimeVal { // 地图已更新
  576. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1003", Message: "数据采集后地图已更新版本, 不符合地图更新条件"})
  577. return
  578. }
  579. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "1000", Message: "记录存在,符合地图更新条件"})
  580. }
  581. // UpdateDeviceMap 根据地图列表及设备列表维护设备地图表
  582. // @router /map/update/deviceMap/record [GET]
  583. func UpdateDeviceMap(ctx context.Context, c *app.RequestContext) {
  584. // 查询朴津地图列表
  585. mapInfoList, err := getMapInfoList()
  586. if err != nil || mapInfoList == nil {
  587. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"})
  588. return
  589. }
  590. //fmt.Println("mapInfoList", mapInfoList)
  591. snCodeMap := groupMapListByDevice(mapInfoList)
  592. fmt.Println("snCodeMap", snCodeMap)
  593. // 查询设备表
  594. devices, err := mysql.QueryAllDevices(ctx)
  595. for _, device := range devices {
  596. //fmt.Println("device", device)
  597. deviceNo := device.DeviceNo
  598. fmt.Println("deviceNo", deviceNo)
  599. if _, exist := snCodeMap[deviceNo]; !exist { // 设备不存在于朴津地图列表中, 在地图接口列表区域权限配置正确的前提下, 说明该设备没有部署地图
  600. // 查询设备地图表中是否存在有效的记录
  601. deviceMaps, err := mysql.QueryValidDeviceMapByDeviceNo(ctx, deviceNo)
  602. if err != nil {
  603. return
  604. }
  605. if len(deviceMaps) > 0 { // 之前存在有效记录
  606. // 将之前的记录有效标志设置为无效
  607. updates, err := mysql.UpdateDeviceMapValidFlagByDeviceNo(ctx, deviceNo, config.MAP_NOT_VALID_FLAG)
  608. if err != nil {
  609. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
  610. return
  611. }
  612. fmt.Println("updates", updates)
  613. }
  614. } else { // 设备存在于朴津地图列表中,说明该设备当前有部署地图
  615. mapList := snCodeMap[deviceNo]
  616. fmt.Println("mapList", mapList)
  617. for _, mapInfo := range mapList {
  618. // 判断该设备对应map id是否在设备地图表中存在有效记录
  619. records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapInfo.MapId, deviceNo)
  620. if err != nil {
  621. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"})
  622. return
  623. }
  624. fmt.Println("records", records)
  625. deviceMap := model.DeviceMap{DeviceNo: device.DeviceNo, DeviceName: device.DeviceName,
  626. DeviceType: device.DeviceType, MapID: mapInfo.MapId, MapType: mapInfo.MapType, MapName: mapInfo.MapName, FloorID: mapInfo.FloorId,
  627. Floor: strconv.Itoa(mapInfo.Floor), BuildID: mapInfo.BuildId, BuildName: mapInfo.BuildName, MapCreateTime: mapInfo.CreateTime, MapUpdateTime: mapInfo.UpdateTime,
  628. MapVersion: strconv.Itoa(mapInfo.Version), MapValidFlag: config.MAP_VALID_FLAG, PjiMapZipURL: mapInfo.ZipUrl, CustomAreaID: int32(mapInfo.CustomAreaId)}
  629. if len(records) == 0 { // 该设备对应map id是否在设备地图表中不存在有效记录
  630. // 添加记录
  631. deviceMap.ID = uuid.NewV1().String()
  632. fmt.Println("deviceMap", deviceMap)
  633. err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
  634. if err != nil {
  635. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
  636. return
  637. }
  638. } else { // 该设备对应map id是否在设备地图表中存在有效记录
  639. // 判断版本号是否一致
  640. record := records[0]
  641. fmt.Println("record", record)
  642. if record.MapVersion == strconv.Itoa(mapInfo.Version) { // 版本号一致
  643. // 更新记录
  644. deviceMap.ID = record.ID
  645. update, err := mysql.UpdateDeviceMapOneRecord(ctx, deviceMap)
  646. if err != nil {
  647. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图记录失败"})
  648. return
  649. }
  650. fmt.Println("update", update)
  651. } else { // 版本号不一致
  652. // 将之前的记录有效标志设置为无效
  653. updates, err := mysql.UpdateDeviceMapValidFlagByDeviceNo(ctx, deviceNo, config.MAP_NOT_VALID_FLAG)
  654. if err != nil {
  655. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
  656. return
  657. }
  658. fmt.Println("updates", updates)
  659. // 添加记录
  660. deviceMap.ID = uuid.NewV1().String()
  661. fmt.Println("deviceMap", deviceMap)
  662. err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
  663. if err != nil {
  664. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
  665. return
  666. }
  667. }
  668. }
  669. }
  670. }
  671. }
  672. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "设备地图数据更新成功"})
  673. }
  674. // UpdateOriginalMap 根据地图列表及设备列表维护原始地图表
  675. // @router /map/update/originalMap/record [GET]
  676. func UpdateOriginalMap(ctx context.Context, c *app.RequestContext) {
  677. // 查询朴津地图列表
  678. mapInfoList, err := getMapInfoList()
  679. if err != nil || mapInfoList == nil {
  680. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"})
  681. return
  682. }
  683. //fmt.Println("mapInfoList", mapInfoList)
  684. snCodeMap := groupMapListByDevice(mapInfoList)
  685. fmt.Println("snCodeMap", snCodeMap)
  686. // 查询设备表
  687. devices, err := mysql.QueryAllDevices(ctx)
  688. for _, device := range devices {
  689. //fmt.Println("device", device)
  690. deviceNo := device.DeviceNo
  691. fmt.Println("deviceNo", deviceNo)
  692. if _, exist := snCodeMap[deviceNo]; exist { // 设备存在于朴津地图列表中,说明该设备当前有部署地图
  693. mapList := snCodeMap[deviceNo]
  694. fmt.Println("mapList", mapList)
  695. for _, mapInfo := range mapList {
  696. records, err := mysql.QueryOriginalMapByMapIdAndDeviceNo(ctx, mapInfo.MapId, deviceNo)
  697. if err != nil {
  698. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询原始地图失败"})
  699. }
  700. fmt.Println("records", records)
  701. originalMap := model.OriginalMap{DeviceNo: device.DeviceNo, DeviceName: device.DeviceName,
  702. DeviceType: device.DeviceType, MapID: mapInfo.MapId, MapType: mapInfo.MapType, MapName: mapInfo.MapName, FloorID: mapInfo.FloorId,
  703. Floor: strconv.Itoa(mapInfo.Floor), BuildID: mapInfo.BuildId, BuildName: mapInfo.BuildName, MapCreateTime: mapInfo.CreateTime, MapUpdateTime: mapInfo.UpdateTime,
  704. MapVersion: strconv.Itoa(mapInfo.Version), PjiMapZipURL: mapInfo.ZipUrl, CustomAreaID: int32(mapInfo.CustomAreaId)}
  705. if len(records) == 0 { // 该设备对应map id是否在原始地图表中不存在记录
  706. // 添加记录
  707. originalMap.ID = uuid.NewV1().String()
  708. fmt.Println("originalMap", originalMap)
  709. err = mysql.AddOriginalMapOneRecord(ctx, originalMap)
  710. if err != nil {
  711. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加原始地图记录失败"})
  712. return
  713. }
  714. }
  715. }
  716. }
  717. }
  718. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "原始地图数据更新成功"})
  719. }
  720. // UploadOriginalMapWithoutHttp 将原始地图朴津压缩包(及其中的map.pgm)上传到oss
  721. func UploadOriginalMapWithoutHttp(ctx context.Context) bool {
  722. // 查询原始地图压缩包/pgm为空的记录
  723. records, err := mysql.QueryOriginalMapWithoutCicvMap(ctx)
  724. if err != nil {
  725. fmt.Println("查询原始地图失败")
  726. return false
  727. }
  728. fmt.Println("records", len(records))
  729. for _, record := range records {
  730. fmt.Println("record", record)
  731. var zipName string
  732. var zipPath string
  733. var destPath string
  734. if record.CicvMapZipURL == nil || *record.CicvMapZipURL == "" || record.CicvMapPgmURL == nil || *record.CicvMapPgmURL == "" {
  735. // 创建临时文件夹
  736. tmpDir, err := os.MkdirTemp("", "temp-download-*")
  737. fmt.Println("tmpDir:", tmpDir)
  738. if err != nil {
  739. fmt.Println("Error creating temporary directory:", err)
  740. return false
  741. }
  742. // 下载朴津地图压缩包
  743. zipName = filepath.Base(record.PjiMapZipURL)
  744. zipPath = filepath.Join(tmpDir, zipName)
  745. destPath = filepath.Join(tmpDir, "map")
  746. err = util.Downloadfile(record.PjiMapZipURL, zipPath)
  747. if err != nil {
  748. fmt.Println("Error downloading origin_map file:", err)
  749. return false
  750. }
  751. c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir)
  752. // 检查文件是否存在
  753. if _, err := os.Stat(zipPath); os.IsNotExist(err) {
  754. return false
  755. }
  756. }
  757. // 上传朴津地图压缩包到oss
  758. if record.CicvMapZipURL == nil || *record.CicvMapZipURL == "" {
  759. ossObjectKey := config.OriginalMapOssBasePrefix + "/" + record.DeviceNo + "/" + record.MapID + "/" + record.MapVersion + "/" + zipName
  760. fmt.Println("ossObjectKey", ossObjectKey)
  761. // 打开文件
  762. f, err := os.Open(zipPath)
  763. if err != nil {
  764. fmt.Println("Failed to open file")
  765. return false
  766. }
  767. defer f.Close()
  768. config.OssMutex.Lock()
  769. err = config.OssBucket.PutObject(ossObjectKey, f)
  770. config.OssMutex.Unlock()
  771. if err != nil {
  772. c_log.GlobalLogger.Error("程序异常退出。上传文件", zipName, "->", ossObjectKey, "出错:", err)
  773. return false
  774. }
  775. c_log.GlobalLogger.Info("上传文件", zipName, "->", ossObjectKey, "成功。")
  776. record.CicvMapZipURL = &ossObjectKey
  777. }
  778. // 上传朴津原始地图到oss
  779. if record.CicvMapPgmURL == nil || *record.CicvMapPgmURL == "" {
  780. ossObjectKey := config.OriginalMapOssBasePrefix + "/" + record.DeviceNo + "/" + record.MapID + "/" + record.MapVersion + "/" + "map.pgm"
  781. // 压缩包解压
  782. err := Unzip(zipPath, destPath)
  783. if err != nil {
  784. fmt.Println("Error unzipping file:", err)
  785. return false
  786. }
  787. // 文件 - map.pgm
  788. pgmPath := filepath.Join(destPath, "map.pgm")
  789. // 检查文件是否存在
  790. if _, err := os.Stat(pgmPath); os.IsNotExist(err) {
  791. fmt.Println("解压目录中map.pgm不存在")
  792. return false
  793. }
  794. // 打开文件
  795. f, err := os.Open(pgmPath)
  796. if err != nil {
  797. fmt.Println("Failed to open file")
  798. return false
  799. }
  800. defer f.Close()
  801. config.OssMutex.Lock()
  802. err = config.OssBucket.PutObject(ossObjectKey, f)
  803. config.OssMutex.Unlock()
  804. if err != nil {
  805. c_log.GlobalLogger.Error("程序异常退出。上传文件", "map.pgm", "->", ossObjectKey, "出错:", err)
  806. return false
  807. }
  808. c_log.GlobalLogger.Info("上传文件", "map.pgm", "->", ossObjectKey, "成功。")
  809. record.CicvMapPgmURL = &ossObjectKey
  810. }
  811. _, err = mysql.UpdateOriginalMapOneRecord(ctx, *record)
  812. if err != nil {
  813. fmt.Println("更新原始地图失败")
  814. return false
  815. }
  816. }
  817. return true
  818. }
  819. // UploadPjiMapAndNotify 上传oss更新地图压缩包到朴津,并通知运维人员
  820. // @router /map/update/pji/upload [GET]
  821. func UploadPjiMapAndNotify(ctx context.Context, c *app.RequestContext) {
  822. id := c.Query("id")
  823. fmt.Println("id", id)
  824. // 根据id查询更新地图
  825. mapUpdate, err := mysql.QueryMapUpdateRecordById(ctx, id)
  826. if err != nil {
  827. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图更新列表失败"})
  828. return
  829. }
  830. mapId := mapUpdate.MapID
  831. fmt.Println("mapId", mapId)
  832. deviceNo := mapUpdate.DeviceSn
  833. fmt.Println("deviceNo", deviceNo)
  834. UpdateMapUrl := mapUpdate.UpdateMapURL
  835. fmt.Println("UpdateMapUrl", UpdateMapUrl)
  836. // 查询朴津地图列表
  837. mapInfoList, err := getMapInfoList()
  838. if err != nil || mapInfoList == nil {
  839. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"})
  840. return
  841. }
  842. // 查询地图id是否存在于地图列表中
  843. exist, record := checkMapIdExist(mapId, mapInfoList)
  844. fmt.Println("exist", exist)
  845. fmt.Println("record", record)
  846. if !exist {
  847. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1002", Message: "地图已重采/删除,不符合地图上传条件"})
  848. return
  849. }
  850. // 拉取oss文件流
  851. // 获取对象
  852. body, err := config.OssBucket.GetObject(UpdateMapUrl)
  853. if err != nil {
  854. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新地图文件获取失败"})
  855. return
  856. }
  857. // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
  858. defer body.Close()
  859. file, err := ioutil.ReadAll(body)
  860. if err != nil {
  861. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新地图文件获取失败"})
  862. return
  863. }
  864. paramMap := make(map[string]interface{})
  865. //paramMap["snCode"] = "P1YNYD1M225000112"
  866. //paramMap["mapId"] = "8068197000f84a5483dabf5a6ad3707c"
  867. //paramMap["mapName"] = "智慧楼"
  868. //paramMap["buildId"] = "8afb5463ad9140aa9e0e42b9ca93e075"
  869. //paramMap["buildName"] = "智慧楼"
  870. //paramMap["floorId"] = "fc86319a04824122b63719b062811106"
  871. //paramMap["floor"] = "1"
  872. //paramMap["customAreaId"] = "334"
  873. //paramMap["updateType"] = "0"
  874. //paramMap["mapType"] = "0"
  875. //paramMap["filename"] = ""
  876. paramMap["snCode"] = deviceNo
  877. paramMap["mapId"] = mapId
  878. paramMap["mapName"] = record.MapName
  879. paramMap["buildId"] = record.BuildId
  880. paramMap["buildName"] = record.BuildName
  881. paramMap["floorId"] = record.FloorId
  882. paramMap["floor"] = strconv.Itoa(record.Floor)
  883. paramMap["customAreaId"] = strconv.Itoa(record.CustomAreaId)
  884. paramMap["updateType"] = "0"
  885. paramMap["mapType"] = record.MapType
  886. paramMap["filename"] = ""
  887. // 调用 朴津地图上传 接口
  888. resp, err := pji_client.ApiClient.UploadRequest(c_pji.PjiApiBaseUrl+"mapUpload", paramMap, pji_client.MapSecretId, file)
  889. fmt.Println(string(resp.Body()))
  890. if err != nil {
  891. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "上传朴津地图失败"})
  892. return
  893. }
  894. // 解析结果
  895. var mapUploadRes model.MapUploadRes
  896. // Json转换为map
  897. err = json.Unmarshal(resp.Body(), &mapUploadRes)
  898. if err != nil {
  899. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "解析响应数据失败"})
  900. return
  901. }
  902. fmt.Println("mapUploadRes:", mapUploadRes)
  903. if mapUploadRes.Code != 200 {
  904. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "上传朴津地图失败"})
  905. return
  906. }
  907. version := strconv.Itoa(mapUploadRes.Data.Version)
  908. mapUpdate.UploadVersion = &version
  909. mapUpdate.UpdateFlag = 1
  910. info, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate)
  911. if err != nil {
  912. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "修改地图更新数据失败"})
  913. return
  914. }
  915. fmt.Println("info", info)
  916. //发送修图通知
  917. paramMap = make(map[string]interface{})
  918. paramMap["mapId"] = mapId
  919. paramMap["type"] = "0" // 0代表修图通知,1代表续扫通知
  920. paramMap["customAreaId"] = record.CustomAreaId
  921. resp, err = pji_client.ApiClient.JsonPostRequest(c_pji.PjiApiBaseUrl+"sendMsg", paramMap, pji_client.MapSecretId)
  922. if err != nil {
  923. return
  924. }
  925. fmt.Println(string(resp.Body()))
  926. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传朴津地图成功并已通知运维人员"})
  927. }
  928. // AddMapRescanNotifyWithHttp 查询累积地图更新率低于续扫阈值的记录,并通知运维人员
  929. // @router /map/rescan/notify/add [GET]
  930. func AddMapRescanNotifyWithHttp(ctx context.Context, c *app.RequestContext) {
  931. // 查询朴津地图列表
  932. mapInfoList, err := getMapInfoList()
  933. if err != nil || mapInfoList == nil {
  934. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"})
  935. return
  936. }
  937. // 查询地图续扫阈值
  938. systemConfig, err := mysql.QuerySystemConfig(ctx)
  939. if err != nil {
  940. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取系统配置失败"})
  941. return
  942. }
  943. threshold := systemConfig.RescanReminderThreshold
  944. fmt.Println("threshold", threshold)
  945. // 查询地图更新表中未发送续扫提醒通知的记录(续扫提醒标志为0)
  946. mapUpdates, err := mysql.QueryMapUpdateRecordsByRescanFlag(ctx, config.MAP_NOT_RESCAN_FLAG)
  947. if err != nil {
  948. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询地图更新列表失败"})
  949. return
  950. }
  951. // 记录已发送通知的地图id
  952. // 对于同一地图id的多个符合续扫通知提醒的记录,只允许发送一次续扫通知,但相关记录均会被标记为已发送续扫提醒
  953. mapIdList := make(map[string]bool)
  954. for _, mapUpdate := range mapUpdates {
  955. // 查询地图id是否存在于地图列表中
  956. exist, record := checkMapIdExist(mapUpdate.MapID, mapInfoList)
  957. fmt.Println("exist", exist)
  958. fmt.Println("record", record)
  959. if !exist {
  960. fmt.Println("地图列表中不存在该地图id,跳过...")
  961. continue
  962. }
  963. //fmt.Println("mapUpdate", mapUpdate)
  964. updatePercentage, err := getUpdatePercentage(mapUpdate.CumulativeUpdateRate)
  965. if err != nil {
  966. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: err.Error()})
  967. }
  968. fmt.Println("updatePercentage", updatePercentage)
  969. if updatePercentage >= threshold {
  970. mapId := mapUpdate.MapID
  971. if _, exits := mapIdList[mapId]; !exits {
  972. // 发送续扫通知
  973. paramMap := make(map[string]interface{})
  974. paramMap["mapId"] = mapId
  975. paramMap["type"] = "1" // 0代表修图通知,1代表续扫通知
  976. paramMap["customAreaId"] = record.CustomAreaId
  977. resp, err := pji_client.ApiClient.JsonPostRequest(c_pji.PjiApiBaseUrl+"sendMsg", paramMap, pji_client.MapSecretId)
  978. if err != nil {
  979. continue
  980. }
  981. fmt.Println(string(resp.Body()))
  982. mapIdList[mapId] = true
  983. }
  984. // 修改续扫提醒标志
  985. mapUpdate.RescanNotifyFlag = config.MAP_RESCAN_FLAG
  986. thresholdStr := strconv.FormatFloat(float64(threshold), 'f', 2, 32) + "%"
  987. fmt.Println("thresholdStr", thresholdStr)
  988. mapUpdate.RescanNotifyThreshold = &thresholdStr
  989. _, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate)
  990. if err != nil {
  991. continue
  992. }
  993. }
  994. }
  995. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "更新地图续扫提醒成功"})
  996. }
  997. // AddMapRescanNotifyWithoutHttp 查询累积地图更新率低于续扫阈值的记录,并通知运维人员
  998. func AddMapRescanNotifyWithoutHttp(ctx context.Context) bool {
  999. // 查询朴津地图列表
  1000. mapInfoList, err := getMapInfoList()
  1001. if err != nil || mapInfoList == nil {
  1002. fmt.Println("获取地图列表数据失败")
  1003. return false
  1004. }
  1005. // 查询地图续扫阈值
  1006. systemConfig, err := mysql.QuerySystemConfig(ctx)
  1007. if err != nil {
  1008. fmt.Println("获取系统配置失败")
  1009. return false
  1010. }
  1011. threshold := systemConfig.RescanReminderThreshold
  1012. fmt.Println("threshold", threshold)
  1013. // 查询地图更新表中未发送续扫提醒通知的记录(续扫提醒标志为0)
  1014. mapUpdates, err := mysql.QueryMapUpdateRecordsByRescanFlag(ctx, config.MAP_NOT_RESCAN_FLAG)
  1015. if err != nil {
  1016. fmt.Println("查询地图更新列表失败")
  1017. return false
  1018. }
  1019. // 记录已发送通知的地图id
  1020. // 对于同一地图id的多个符合续扫通知提醒的记录,只允许发送一次续扫通知,但相关记录均会被标记为已发送续扫提醒
  1021. mapIdList := make(map[string]bool)
  1022. for _, mapUpdate := range mapUpdates {
  1023. // 查询地图id是否存在于地图列表中
  1024. exist, record := checkMapIdExist(mapUpdate.MapID, mapInfoList)
  1025. fmt.Println("exist", exist)
  1026. fmt.Println("record", record)
  1027. if !exist {
  1028. fmt.Println("地图列表中不存在该地图id,跳过...")
  1029. continue
  1030. }
  1031. //fmt.Println("mapUpdate", mapUpdate)
  1032. updatePercentage, err := getUpdatePercentage(mapUpdate.CumulativeUpdateRate)
  1033. if err != nil {
  1034. fmt.Println("error:", err)
  1035. }
  1036. fmt.Println("updatePercentage", updatePercentage)
  1037. if updatePercentage >= threshold {
  1038. mapId := mapUpdate.MapID
  1039. if _, exits := mapIdList[mapId]; !exits {
  1040. // 发送续扫通知
  1041. paramMap := make(map[string]interface{})
  1042. paramMap["mapId"] = mapId
  1043. paramMap["type"] = "1" // 0代表修图通知,1代表续扫通知
  1044. paramMap["customAreaId"] = record.CustomAreaId
  1045. resp, err := pji_client.ApiClient.JsonPostRequest(c_pji.PjiApiBaseUrl+"sendMsg", paramMap, pji_client.MapSecretId)
  1046. if err != nil {
  1047. continue
  1048. }
  1049. fmt.Println(string(resp.Body()))
  1050. mapIdList[mapId] = true
  1051. }
  1052. // 修改续扫提醒标志
  1053. mapUpdate.RescanNotifyFlag = config.MAP_RESCAN_FLAG
  1054. // 记录发送通知时的续扫提醒阈值
  1055. thresholdStr := strconv.FormatFloat(float64(threshold), 'f', 2, 32) + "%"
  1056. mapUpdate.RescanNotifyThreshold = &thresholdStr
  1057. _, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate)
  1058. if err != nil {
  1059. continue
  1060. }
  1061. }
  1062. }
  1063. fmt.Println("更新地图续扫提醒成功")
  1064. return true
  1065. }
  1066. // QueryMapRescanNotifyList 查询地图续扫提醒列表
  1067. // @router /map/rescan/notify/list [GET]
  1068. func QueryMapRescanNotifyList(ctx context.Context, c *app.RequestContext) {
  1069. var record model.MapUpdate
  1070. err := c.BindAndValidate(&record)
  1071. fmt.Println("record", record)
  1072. var pageFlag bool
  1073. if c.Query("page") != "" && c.Query("pageSize") != "" {
  1074. pageFlag = true
  1075. } else {
  1076. pageFlag = false
  1077. }
  1078. page, _ := strconv.Atoi(c.Query("page"))
  1079. pageSize, _ := strconv.Atoi(c.Query("pageSize"))
  1080. records, count, err := mysql.QueryMapRescanList(ctx, &record, pageFlag, page, pageSize)
  1081. if err != nil {
  1082. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图续扫提醒记录查询失败", Total: 0})
  1083. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图续扫提醒记录查询失败", Total: 0})
  1084. return
  1085. }
  1086. output, err := json.Marshal(records)
  1087. if err != nil {
  1088. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图续扫提醒记录查询失败", Total: 0})
  1089. return
  1090. }
  1091. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "地图续扫提醒记录查询成功", Data: string(output), Total: int(count)})
  1092. }
  1093. func getUpdatePercentage(percentageStr string) (float32, error) {
  1094. // 去掉末尾的%
  1095. valueStr := percentageStr[:len(percentageStr)-1]
  1096. //fmt.Println("valueStr", valueStr)
  1097. // 将字符串转为float32
  1098. value, err := strconv.ParseFloat(valueStr, 32)
  1099. if err != nil {
  1100. fmt.Println("err", err)
  1101. return 0, err
  1102. }
  1103. return float32(value), nil
  1104. }
  1105. // 计算oss中文件列表的总大小
  1106. func calculateTotalFileSize(fileList []string) int {
  1107. var totalSize int
  1108. for _, file := range fileList {
  1109. size, err := util.GetOSSFileSize(config.OssBucket, file) // 获取oss中单个文件的大小
  1110. if err != nil {
  1111. return 0
  1112. }
  1113. totalSize += size
  1114. }
  1115. return totalSize
  1116. }
  1117. func getTime(timeStr string, layout string) (time.Time, error) {
  1118. t, err := time.Parse(layout, timeStr)
  1119. if err != nil {
  1120. return time.Time{}, err
  1121. }
  1122. return t, nil
  1123. }
  1124. // 调用 朴津地图列表查询 接口
  1125. func getMapInfoList() ([]model.MapInfo, error) {
  1126. paramMap := make(map[string]interface{})
  1127. paramMap["mapType"] = "0"
  1128. resp, err := pji_client.ApiClient.GetRequest(c_pji.PjiApiBaseUrl+"mapList?mapType=0", paramMap, pji_client.MapSecretId)
  1129. if err != nil {
  1130. return nil, err
  1131. }
  1132. fmt.Println("resp", resp)
  1133. // 解析json响应
  1134. body := resp.Body()
  1135. //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}")
  1136. //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}")
  1137. fmt.Println("body", body)
  1138. var mapRes model.MapInfoRes
  1139. err = json.Unmarshal([]byte(body), &mapRes)
  1140. if err != nil || mapRes.Code != 200 {
  1141. return nil, err
  1142. }
  1143. //fmt.Println("mapRes", mapRes)
  1144. return mapRes.Data, nil
  1145. }
  1146. // 查询地图id是否存在于地图列表中
  1147. func checkMapIdExist(mapId string, mapInfoList []model.MapInfo) (bool, model.MapInfo) {
  1148. // 转为map
  1149. IdMap := make(map[string]model.MapInfo)
  1150. for _, info := range mapInfoList {
  1151. IdMap[info.MapId] = info
  1152. }
  1153. record, exist := IdMap[mapId]
  1154. return exist, record
  1155. }
  1156. func groupMapListByDevice(mapInfoList []model.MapInfo) map[string][]model.MapInfo {
  1157. // 转为map
  1158. snCodeMap := make(map[string][]model.MapInfo)
  1159. for _, info := range mapInfoList {
  1160. for _, snCode := range info.SnCodeList {
  1161. if _, exist := snCodeMap[snCode]; !exist {
  1162. snCodeMap[snCode] = make([]model.MapInfo, 0)
  1163. snCodeMap[snCode] = append(snCodeMap[snCode], info)
  1164. } else {
  1165. // 判断buildId及floorId是否与snCodeMap[snCode]已记录的数据相同
  1166. for index, record := range snCodeMap[snCode] {
  1167. if record.BuildId == info.BuildId && record.FloorId == info.FloorId { // 相同则说明为同一区域的地图有多个版本,只保留一个版本
  1168. // 取最近的版本号(当前版本号为时间戳,即取时间戳最大的作为最近的版本号)
  1169. if record.Version < info.Version {
  1170. snCodeMap[snCode][index] = info
  1171. break
  1172. }
  1173. }
  1174. }
  1175. // snCodeMap[snCode]已记录的数据未找到相同区域的地图,则添加记录
  1176. snCodeMap[snCode] = append(snCodeMap[snCode], info)
  1177. }
  1178. }
  1179. }
  1180. return snCodeMap
  1181. }
  1182. // 根据场景id获取数据采集时的地图id
  1183. func getMapIdById(id string) (string, error) {
  1184. var mapId string
  1185. // 下载map.json
  1186. // 根据id获取对应的oss文件列表
  1187. allFileList, err := util.GetExactedMapFileById(id)
  1188. //fmt.Println("allFileList", allFileList)
  1189. // 过滤特定后缀的文件列表
  1190. fileList := util.FilterBySuffixes(allFileList, config.MapJsonFiltersuffixes...)
  1191. fmt.Println("fileList", fileList)
  1192. // 创建临时文件夹
  1193. tmpDir, err := os.MkdirTemp("", "temp-download-*")
  1194. fmt.Println("tmpDir:", tmpDir)
  1195. if err != nil {
  1196. fmt.Println("Error creating temporary directory:", err)
  1197. return "", err
  1198. }
  1199. c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir)
  1200. for _, file := range fileList {
  1201. path := filepath.Join(tmpDir, filepath.Base(file))
  1202. err = config.OssBucket.GetObjectToFile(file, path)
  1203. if err != nil {
  1204. fmt.Println("Error downloading file:", err)
  1205. return "", err
  1206. }
  1207. c_log.GlobalLogger.Info("下载map.json文件 - 成功")
  1208. // 读取json文件 - mapId
  1209. // 打开文件
  1210. file, err := os.Open(path)
  1211. if err != nil {
  1212. fmt.Println("Error opening file:", err)
  1213. return "", err
  1214. }
  1215. defer file.Close()
  1216. // 读取文件内容
  1217. fileData, err := ioutil.ReadAll(file)
  1218. if err != nil {
  1219. fmt.Println("Error reading file:", err)
  1220. return "", err
  1221. }
  1222. // Json转换为map
  1223. var mapData map[string]interface{}
  1224. err = json.Unmarshal(fileData, &mapData)
  1225. data := mapData["map"].(map[string]interface{})
  1226. // 读取mapId
  1227. mapId = data["mapId"].(string)
  1228. fmt.Println("mapId:", mapId)
  1229. }
  1230. return mapId, err
  1231. }
  1232. func Unzip(zipPath, destPath string) error {
  1233. r, err := zip.OpenReader(zipPath)
  1234. if err != nil {
  1235. fmt.Println("压缩包读取失败")
  1236. return err
  1237. }
  1238. defer r.Close()
  1239. // 遍历zip文件中的每个文件和目录
  1240. for _, f := range r.File {
  1241. // 获取文件路径信息
  1242. fileDestPath := filepath.Join(destPath, f.Name)
  1243. // 创建目标文件或目录的父目录(如果不存在的话)
  1244. if err := os.MkdirAll(filepath.Dir(fileDestPath), 0755); err != nil {
  1245. fmt.Println("创建目录失败:", err)
  1246. }
  1247. // 如果是目录,则创建对应的目录结构
  1248. if f.FileInfo().IsDir() {
  1249. continue
  1250. }
  1251. // 打开要写入的目标文件
  1252. out, err := os.OpenFile(fileDestPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
  1253. if err != nil {
  1254. return err
  1255. }
  1256. defer out.Close()
  1257. // 读取zip中的文件内容并写入到目标文件
  1258. in, err := f.Open()
  1259. if err != nil {
  1260. return err
  1261. }
  1262. defer in.Close()
  1263. _, err = io.Copy(out, in)
  1264. if err != nil {
  1265. return err
  1266. }
  1267. }
  1268. return nil
  1269. }