map_service.go 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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. if err != nil {
  250. fmt.Println("Error downloading Bag file:", err)
  251. return "", "", err
  252. }
  253. c_log.GlobalLogger.Info("下载文件到bag_folder文件夹 - 成功")
  254. // 从oss下载文件到 mapBuf 文件夹
  255. for _, file := range mapBufFileList {
  256. err = config.OssBucket.GetObjectToFile(file, filepath.Join(mapBufDir, filepath.Base(file)))
  257. if err != nil {
  258. fmt.Println("Error downloading mapBuf file:", err)
  259. return "", "", err
  260. }
  261. }
  262. c_log.GlobalLogger.Info("下载文件到mapBuf子文件夹 - 成功")
  263. // 从oss下载bag文件到 origin_map_folder 文件夹
  264. // 过滤特定后缀的文件列表
  265. //originMapFileList := util.FilterBySuffixes(allFileList, config.OriginMapFiltersuffixes...)
  266. //for _, file := range originMapFileList {
  267. // err = config.OssBucket.GetObjectToFile(file, filepath.Join(originMapFolderDir, filepath.Base(file)))
  268. // if err != nil {
  269. // fmt.Println("Error downloading origin_map file:", err)
  270. // return "", "", err
  271. // }
  272. //}
  273. //err = config.OssBucket.GetObjectToFile(originUrl, filepath.Join(originMapFolderDir, filepath.Base(originUrl)))
  274. err = util.Downloadfile(originUrl, filepath.Join(originMapFolderDir, filepath.Base(originUrl)))
  275. if err != nil {
  276. fmt.Println("Error downloading origin_map file:", err)
  277. return "", "", err
  278. }
  279. c_log.GlobalLogger.Info("下载文件到origin_map_folder文件夹 - 成功")
  280. // 创建压缩文件
  281. zipPath := filepath.Join(tmpDir, "mapFile-"+ids[0]+".zip")
  282. zipFile, err := os.Create(zipPath)
  283. if err != nil {
  284. fmt.Println("Error creating ZIP file:", err)
  285. return "", "", err
  286. }
  287. defer zipFile.Close()
  288. zipWriter := zip.NewWriter(zipFile)
  289. defer zipWriter.Close()
  290. // 压缩文件夹
  291. if err := util.AddDirToZip(baseDir, zipWriter); err != nil {
  292. fmt.Println("Error adding directory to ZIP:", err)
  293. return "", "", err
  294. }
  295. fmt.Println("ZIP file created successfully.")
  296. c_log.GlobalLogger.Info("创建压缩文件 - 成功")
  297. return zipPath, tmpDir, nil
  298. }
  299. // UploadMapFile 将地图更新相关文件上传到oss
  300. // @router /map/upload/map [GET]
  301. func UploadMapFile(ctx context.Context, c *app.RequestContext) {
  302. equipmentNo := c.Query("equipmentNo")
  303. fmt.Println("equipmentNo", equipmentNo)
  304. mapId := c.Query("mapId")
  305. fmt.Println("mapId", mapId)
  306. timeStamp := c.Query("timeStamp")
  307. fmt.Println("timeStamp", timeStamp)
  308. header, err := c.FormFile("file")
  309. if err != nil {
  310. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  311. return
  312. }
  313. fileName := header.Filename
  314. fmt.Println("filename", fileName)
  315. ossObjectKey := config.UpdateMapOssBasePrefix + "/" + equipmentNo + "/" + mapId + "/" + timeStamp + "/" + fileName
  316. fmt.Println("ossObjectKey", ossObjectKey)
  317. f, _ := header.Open()
  318. defer f.Close()
  319. config.OssMutex.Lock()
  320. err = config.OssBucket.PutObject(ossObjectKey, f)
  321. config.OssMutex.Unlock()
  322. if err != nil {
  323. c_log.GlobalLogger.Error("程序异常退出。上传文件", fileName, "->", ossObjectKey, "出错:", err)
  324. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "上传文件失败", Details: ""})
  325. return
  326. }
  327. c_log.GlobalLogger.Info("上传文件", fileName, "->", ossObjectKey, "成功。")
  328. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传文件成功", Details: ossObjectKey})
  329. }
  330. // AddMapUpdateRecord 添加仿真测试记录
  331. // @router /map/add/record [GET]
  332. func AddMapUpdateRecord(ctx context.Context, c *app.RequestContext) {
  333. var record model.MapUpdate
  334. err := c.BindAndValidate(&record)
  335. record.ID = uuid.NewV1().String()
  336. fmt.Println("record", record)
  337. if err != nil {
  338. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  339. return
  340. }
  341. err = mysql.AddMapUpdateOneRecord(ctx, record)
  342. if err != nil {
  343. c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "地图更新记录添加失败"})
  344. return
  345. }
  346. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "地图更新记录添加成功"})
  347. }
  348. // QueryMapUpdateRecord 根据条件查询地图更新记录
  349. // @router /map/query/update/record [GET]
  350. func QueryMapUpdateRecord(ctx context.Context, c *app.RequestContext) {
  351. var record model.MapUpdate
  352. err := c.BindAndValidate(&record)
  353. fmt.Println("record", record)
  354. var pageFlag bool
  355. if c.Query("page") != "" && c.Query("pageSize") != "" {
  356. pageFlag = true
  357. } else {
  358. pageFlag = false
  359. }
  360. page, _ := strconv.Atoi(c.Query("page"))
  361. pageSize, _ := strconv.Atoi(c.Query("pageSize"))
  362. records, count, err := mysql.QueryMapUpdateRecords(ctx, &record, pageFlag, page, pageSize)
  363. if err != nil {
  364. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图更新记录查询失败", Total: 0})
  365. return
  366. }
  367. output, err := json.Marshal(records)
  368. if err != nil {
  369. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图更新记录查询失败", Total: 0})
  370. return
  371. }
  372. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "地图更新查询成功", Data: string(output), Total: int(count)})
  373. }
  374. // TestUploadUpdateMap 测试朴津地图上传接口
  375. // @router /map/upload/update/map [GET]
  376. func TestUploadUpdateMap(ctx context.Context, c *app.RequestContext) {
  377. header, err := c.FormFile("file")
  378. if err != nil {
  379. c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))
  380. return
  381. }
  382. fileName := header.Filename
  383. fmt.Println("filename", fileName)
  384. f, _ := header.Open()
  385. defer f.Close()
  386. paramMap := make(map[string]interface{})
  387. // Map<String, String> paramMap=new HashMap<>();
  388. // paramMap.put("mapId","2247aeb31ad34wwddfd9c3ba24536e52859");
  389. // paramMap.put("mapName","合肥演示");
  390. // paramMap.put("buildId"," 7b4e8d01c08f472fabf7ccff2b2336s");
  391. // paramMap.put("floorId"," 695abbcfdf99471sdfff3b9b1d0818164e7");
  392. // paramMap.put("updateType"," 7b4e8d01c08f472fabf7ccff2b2336s");
  393. // paramMap.put("floor","1");
  394. // paramMap.put("buildName"," 2");
  395. // paramMap.put("customAreaId"," 355");
  396. // paramMap.put("snCode","P1YTYD1M233M00224");
  397. // paramMap.put("fileName","1702284791385137152.zip");
  398. // paramMap.put("mapType","0");
  399. paramMap["snCode"] = "P1YNYD1M225000112"
  400. paramMap["mapId"] = "8068197000f84a5483dabf5a6ad3707c"
  401. paramMap["mapName"] = "智慧楼"
  402. paramMap["buildId"] = "8afb5463ad9140aa9e0e42b9ca93e075"
  403. paramMap["buildName"] = "智慧楼"
  404. paramMap["floorId"] = "fc86319a04824122b63719b062811106"
  405. paramMap["floor"] = "1"
  406. paramMap["customAreaId"] = "334"
  407. paramMap["updateType"] = "0"
  408. paramMap["mapType"] = "0"
  409. paramMap["filename"] = ""
  410. bytes, err := io.ReadAll(f)
  411. if err != nil {
  412. return
  413. }
  414. // 调用 朴津地图上传 接口
  415. resp, err := pji_client.ApiClient.UploadRequest(c_pji.PjiApiSitBaseUrl+"mapUpload", paramMap, pji_client.MapSecretId, bytes)
  416. fmt.Println(string(resp.Body()))
  417. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传地图测试"})
  418. }
  419. // UpdateDeviceMapById 根据场景id维护设备地图表
  420. // @router /map/update/deviceMap/record [GET]
  421. func UpdateDeviceMapById(ctx context.Context, c *app.RequestContext) {
  422. sceneId := c.Query("sceneId")
  423. fmt.Println("sceneId", sceneId)
  424. deviceNo := c.Query("deviceNo")
  425. fmt.Println("deviceNo", deviceNo)
  426. deviceName := c.Query("deviceName")
  427. fmt.Println("deviceName", deviceName)
  428. deviceType := c.Query("deviceType")
  429. fmt.Println("deviceType", deviceType)
  430. // 根据场景id查询地图id
  431. mapId, err := getMapIdById(sceneId)
  432. if err != nil || mapId == "" {
  433. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取mapId失败"})
  434. return
  435. }
  436. fmt.Println("mapId", mapId)
  437. // 查询朴津地图列表
  438. mapInfoList, err := getMapInfoList()
  439. if err != nil || mapInfoList == nil {
  440. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"})
  441. return
  442. }
  443. fmt.Println("mapInfoList", mapInfoList)
  444. // 判断map id是否存在于朴津地图列表中
  445. mapId = "fee84db56d254d79b85971cb4ab2e7f1" // 存在
  446. //mapId = "553d9d47f8404734997e11175f0e63f5" // 不存在
  447. exist, record := checkMapIdExist(mapId, mapInfoList)
  448. fmt.Println("exist", exist)
  449. fmt.Println("record", record)
  450. if !exist { // map id不存在于朴津地图列表中
  451. // 查询地图设备表中是否存在对应map id且有效的记录
  452. records, err := mysql.QueryValidDeviceMapByMapId(ctx, mapId)
  453. if err != nil {
  454. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"})
  455. return
  456. }
  457. fmt.Println("records", records)
  458. if len(records) > 0 { // 存在,
  459. fmt.Println("设置无效")
  460. // 将对应的map id的记录设置为无效
  461. update, err := mysql.UpdateDeviceMapValidFlagByMapId(ctx, mapId, config.MAP_NOT_VALID_FLAG)
  462. if err != nil {
  463. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
  464. return
  465. }
  466. fmt.Println("update", update)
  467. }
  468. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "1001", Message: "map id不存在地图列表中"})
  469. } else { // map id存在于朴津地图列表中
  470. snCodeList := record.SnCodeList
  471. for _, snCode := range snCodeList { // snCodeList 代表配置了对应map id地图的机器人sn码列表, 上限为机器人数量
  472. fmt.Println("snCode", snCode)
  473. deviceMap := model.DeviceMap{DeviceNo: snCode, DeviceName: deviceName,
  474. DeviceType: deviceType, MapID: record.MapId, MapType: record.MapType, MapName: record.MapName, FloorID: record.FloorId,
  475. Floor: strconv.Itoa(record.Floor), BuildID: record.BuildId, BuildName: record.BuildName, MapCreateTime: record.CreateTime, MapUpdateTime: record.UpdateTime,
  476. MapVersion: strconv.Itoa(record.Version), MapValidFlag: config.MAP_VALID_FLAG, PjiMapZipURL: record.ZipUrl, CustomAreaID: int32(record.CustomAreaId)}
  477. fmt.Println("deviceMap", deviceMap)
  478. deviceMap.MapVersion = "123456"
  479. // 查询[对应设备]是否存在对应map id且有效的记录
  480. records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapId, snCode)
  481. if err != nil {
  482. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"})
  483. return
  484. }
  485. fmt.Println("records", records)
  486. if len(records) == 0 { // 对应设备不存在对应map id且有效的记录, 说明该设备部署了新的地图(重采或新的区域地图)
  487. // 添加记录
  488. deviceMap.ID = uuid.NewV1().String()
  489. err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
  490. if err != nil {
  491. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
  492. return
  493. }
  494. } else if len(records) == 1 { // 对应设备存在1个对应map id且有效的记录, 说明该设备之前部署过该地图, 需要检测版本号来判断更新记录(版本号一致)还是添加记录(版本号不一致)
  495. fmt.Println("record", record)
  496. if deviceMap.MapVersion == records[0].MapVersion { // 地图列表查询到的版本与设备地图表中版本一致, 则更新记录
  497. // 更新记录
  498. deviceMap.ID = records[0].ID
  499. update, err := mysql.UpdateDeviceMapOneRecord(ctx, deviceMap)
  500. if err != nil {
  501. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图记录失败"})
  502. return
  503. }
  504. fmt.Println("update", update)
  505. } else { // 地图列表查询到的版本与设备地图表中版本不一致, 则先将之前的记录状态设置为无效, 然后添加记录
  506. // 将对应设备存在的对应map id的记录设置为无效
  507. update, err := mysql.UpdateDeviceMapValidFlagByMapIdAndDevice(ctx, mapId, snCode, config.MAP_NOT_VALID_FLAG)
  508. if err != nil {
  509. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
  510. return
  511. }
  512. fmt.Println("update", update)
  513. // 添加记录
  514. deviceMap.ID = uuid.NewV1().String()
  515. err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
  516. if err != nil {
  517. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
  518. return
  519. }
  520. }
  521. }
  522. }
  523. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "设备地图数据更新成功"})
  524. }
  525. }
  526. // CheckDeviceMapStatus 根据设备号及map id查询地图状态
  527. // @router /map/check/deviceMap/status [GET]
  528. func CheckDeviceMapStatus(ctx context.Context, c *app.RequestContext) {
  529. sceneId := c.Query("sceneId")
  530. fmt.Println("sceneId", sceneId)
  531. deviceNo := c.Query("deviceNo")
  532. fmt.Println("deviceNo", deviceNo)
  533. deviceName := c.Query("deviceName")
  534. fmt.Println("deviceName", deviceName)
  535. deviceType := c.Query("deviceType")
  536. fmt.Println("deviceType", deviceType)
  537. // 根据场景id查询地图id
  538. mapId, err := getMapIdById(sceneId)
  539. if err != nil || mapId == "" {
  540. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取mapId失败"})
  541. return
  542. }
  543. fmt.Println("mapId", mapId)
  544. // 查询 设备sn码 是否存在于 设备表中
  545. device, err := mysql.QueryDeviceByDeviceNo(ctx, deviceNo)
  546. if err != nil {
  547. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备记录失败"})
  548. return
  549. }
  550. fmt.Println("device", device)
  551. if device == nil { // 设备sn码 不在 设备表中
  552. record := model.Device{DeviceNo: deviceNo, DeviceName: deviceName, DeviceType: deviceType}
  553. err := mysql.AddDeviceOneRecord(ctx, record)
  554. if err != nil {
  555. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备记录失败"})
  556. return
  557. }
  558. }
  559. // 查询[对应设备]是否存在对应map id且有效的记录
  560. records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapId, deviceNo)
  561. if err != nil {
  562. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询设备地图失败"})
  563. return
  564. }
  565. fmt.Println("records", records)
  566. if len(records) == 0 { // [对应设备]不存在对应map id且有效的记录
  567. // 地图已重采/删除
  568. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1001", Message: "地图已重采/删除, 不符合地图更新条件"})
  569. return
  570. }
  571. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "1000", Message: "记录存在,符合地图更新条件"})
  572. }
  573. // UpdateDeviceMap 根据地图列表及设备列表维护设备地图表
  574. // @router /map/update/deviceMap/record [GET]
  575. func UpdateDeviceMap(ctx context.Context, c *app.RequestContext) {
  576. //sceneId := c.Query("sceneId")
  577. //fmt.Println("sceneId", sceneId)
  578. //deviceNo := c.Query("deviceNo")
  579. //fmt.Println("deviceNo", deviceNo)
  580. //deviceName := c.Query("deviceName")
  581. //fmt.Println("deviceName", deviceName)
  582. //deviceType := c.Query("deviceType")
  583. //fmt.Println("deviceType", deviceType)
  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. // 判断该设备对应地图的数量
  618. var mapInfo model.MapInfo
  619. if len(mapList) == 1 {
  620. mapInfo = mapList[0]
  621. } else {
  622. var latestMap *model.MapInfo
  623. layout := "2006-01-02 15:04:05"
  624. // 查找地图更新时间最近的记录
  625. for _, info := range mapList {
  626. if latestMap == nil {
  627. latestMap = &info
  628. } else {
  629. updateTime, err := getTime(info.UpdateTime, layout)
  630. if err != nil {
  631. fmt.Println(err)
  632. continue
  633. }
  634. latestTime, err := getTime(latestMap.UpdateTime, layout)
  635. if err != nil {
  636. fmt.Println(err)
  637. continue
  638. }
  639. if updateTime.After(latestTime) {
  640. latestMap = &info
  641. }
  642. }
  643. }
  644. mapInfo = *latestMap
  645. }
  646. // 判断该设备对应map id是否在设备地图表中存在有效记录
  647. records, err := mysql.QueryValidDeviceMapByMapIdAndDeviceNo(ctx, mapInfo.MapId, deviceNo)
  648. if err != nil {
  649. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: " 查询设备地图失败"})
  650. return
  651. }
  652. fmt.Println("records", records)
  653. deviceMap := model.DeviceMap{DeviceNo: device.DeviceNo, DeviceName: device.DeviceName,
  654. DeviceType: device.DeviceType, MapID: mapInfo.MapId, MapType: mapInfo.MapType, MapName: mapInfo.MapName, FloorID: mapInfo.FloorId,
  655. Floor: strconv.Itoa(mapInfo.Floor), BuildID: mapInfo.BuildId, BuildName: mapInfo.BuildName, MapCreateTime: mapInfo.CreateTime, MapUpdateTime: mapInfo.UpdateTime,
  656. MapVersion: strconv.Itoa(mapInfo.Version), MapValidFlag: config.MAP_VALID_FLAG, PjiMapZipURL: mapInfo.ZipUrl, CustomAreaID: int32(mapInfo.CustomAreaId)}
  657. if len(records) == 0 { // 该设备对应map id是否在设备地图表中不存在有效记录
  658. // 将之前的记录有效标志设置为无效
  659. updates, err := mysql.UpdateDeviceMapValidFlagByDeviceNo(ctx, deviceNo, config.MAP_NOT_VALID_FLAG)
  660. if err != nil {
  661. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
  662. return
  663. }
  664. fmt.Println("updates", updates)
  665. // 添加记录
  666. deviceMap.ID = uuid.NewV1().String()
  667. fmt.Println("deviceMap", deviceMap)
  668. err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
  669. if err != nil {
  670. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
  671. return
  672. }
  673. } else { // 该设备对应map id是否在设备地图表中存在有效记录
  674. // 判断版本号是否一致
  675. record := records[0]
  676. fmt.Println("record", record)
  677. if record.MapVersion == strconv.Itoa(mapInfo.Version) { // 版本号一致
  678. // 更新记录
  679. deviceMap.ID = records[0].ID
  680. update, err := mysql.UpdateDeviceMapOneRecord(ctx, deviceMap)
  681. if err != nil {
  682. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图记录失败"})
  683. return
  684. }
  685. fmt.Println("update", update)
  686. } else { // 版本号不一致
  687. // 将之前的记录有效标志设置为无效
  688. updates, err := mysql.UpdateDeviceMapValidFlagByDeviceNo(ctx, deviceNo, config.MAP_NOT_VALID_FLAG)
  689. if err != nil {
  690. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新设备地图状态失败"})
  691. return
  692. }
  693. fmt.Println("updates", updates)
  694. // 添加记录
  695. deviceMap.ID = uuid.NewV1().String()
  696. fmt.Println("deviceMap", deviceMap)
  697. err = mysql.AddDeviceMapOneRecord(ctx, deviceMap)
  698. if err != nil {
  699. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "添加设备地图记录失败"})
  700. return
  701. }
  702. }
  703. }
  704. }
  705. }
  706. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "设备地图数据更新成功"})
  707. }
  708. // UploadPjiMapAndNotify 上传oss更新地图压缩包到朴津,并通知运维人员
  709. // @router /map/update/pji/upload [GET]
  710. func UploadPjiMapAndNotify(ctx context.Context, c *app.RequestContext) {
  711. id := c.Query("id")
  712. fmt.Println("id", id)
  713. // 根据id查询更新地图
  714. mapUpdate, err := mysql.QueryMapUpdateRecordById(ctx, id)
  715. if err != nil {
  716. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图更新列表失败"})
  717. return
  718. }
  719. mapId := mapUpdate.MapID
  720. fmt.Println("mapId", mapId)
  721. deviceNo := mapUpdate.DeviceSn
  722. fmt.Println("deviceNo", deviceNo)
  723. UpdateMapUrl := mapUpdate.UpdateMapURL
  724. fmt.Println("UpdateMapUrl", UpdateMapUrl)
  725. // 查询朴津地图列表
  726. mapInfoList, err := getMapInfoList()
  727. if err != nil || mapInfoList == nil {
  728. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"})
  729. return
  730. }
  731. // 查询地图id是否存在于地图列表中
  732. exist, record := checkMapIdExist(mapId, mapInfoList)
  733. fmt.Println("exist", exist)
  734. fmt.Println("record", record)
  735. if !exist {
  736. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "1002", Message: "地图已重采/删除,不符合地图上传条件"})
  737. return
  738. }
  739. // 拉取oss文件流
  740. // 获取对象
  741. body, err := config.OssBucket.GetObject(UpdateMapUrl)
  742. if err != nil {
  743. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新地图文件获取失败"})
  744. return
  745. }
  746. // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
  747. defer body.Close()
  748. file, err := ioutil.ReadAll(body)
  749. if err != nil {
  750. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "更新地图文件获取失败"})
  751. return
  752. }
  753. paramMap := make(map[string]interface{})
  754. //paramMap["snCode"] = "P1YNYD1M225000112"
  755. //paramMap["mapId"] = "8068197000f84a5483dabf5a6ad3707c"
  756. //paramMap["mapName"] = "智慧楼"
  757. //paramMap["buildId"] = "8afb5463ad9140aa9e0e42b9ca93e075"
  758. //paramMap["buildName"] = "智慧楼"
  759. //paramMap["floorId"] = "fc86319a04824122b63719b062811106"
  760. //paramMap["floor"] = "1"
  761. //paramMap["customAreaId"] = "334"
  762. //paramMap["updateType"] = "0"
  763. //paramMap["mapType"] = "0"
  764. //paramMap["filename"] = ""
  765. paramMap["snCode"] = deviceNo
  766. paramMap["mapId"] = mapId
  767. paramMap["mapName"] = record.MapName
  768. paramMap["buildId"] = record.BuildId
  769. paramMap["buildName"] = record.BuildName
  770. paramMap["floorId"] = record.FloorId
  771. paramMap["floor"] = strconv.Itoa(record.Floor)
  772. paramMap["customAreaId"] = strconv.Itoa(record.CustomAreaId)
  773. paramMap["updateType"] = "0"
  774. paramMap["mapType"] = record.MapType
  775. paramMap["filename"] = ""
  776. // 调用 朴津地图上传 接口
  777. resp, err := pji_client.ApiClient.UploadRequest(c_pji.PjiApiSitBaseUrl+"mapUpload", paramMap, pji_client.MapSecretId, file)
  778. fmt.Println(string(resp.Body()))
  779. if err != nil {
  780. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "上传朴津地图失败"})
  781. return
  782. }
  783. // 解析结果
  784. var mapUploadRes model.MapUploadRes
  785. // Json转换为map
  786. err = json.Unmarshal(resp.Body(), &mapUploadRes)
  787. if err != nil {
  788. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "解析响应数据失败"})
  789. return
  790. }
  791. fmt.Println("mapUploadRes:", mapUploadRes)
  792. if mapUploadRes.Code != 200 {
  793. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "上传朴津地图失败"})
  794. return
  795. }
  796. version := strconv.Itoa(mapUploadRes.Data.Version)
  797. mapUpdate.UploadVersion = &version
  798. mapUpdate.UpdateFlag = 1
  799. info, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate)
  800. if err != nil {
  801. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "修改地图更新数据失败"})
  802. return
  803. }
  804. fmt.Println("info", info)
  805. //发送修图通知
  806. //paramMap = make(map[string]interface{})
  807. //paramMap["mapId"] = mapId
  808. //paramMap["type"] = "0" // 0代表修图通知,1代表续扫通知
  809. //paramMap["customAreaId"] = record.CustomAreaId
  810. //resp, err = pji_client.ApiClient.JsonPostRequest(c_pji.PjiApiSitBaseUrl+"sendMsg", paramMap, pji_client.MapSecretId)
  811. //if err != nil {
  812. // return
  813. //}
  814. //fmt.Println(string(resp.Body()))
  815. c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "上传朴津地图成功并已通知运维人员"})
  816. }
  817. // AddMapRescanNotifyWithHttp 查询累积地图更新率低于续扫阈值的记录,并通知运维人员
  818. // @router /map/rescan/notify/add [GET]
  819. func AddMapRescanNotifyWithHttp(ctx context.Context, c *app.RequestContext) {
  820. // 查询朴津地图列表
  821. mapInfoList, err := getMapInfoList()
  822. if err != nil || mapInfoList == nil {
  823. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取地图列表数据失败"})
  824. return
  825. }
  826. // 查询地图续扫阈值
  827. systemConfig, err := mysql.QuerySystemConfig(ctx)
  828. if err != nil {
  829. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "获取系统配置失败"})
  830. return
  831. }
  832. threshold := systemConfig.RescanReminderThreshold
  833. fmt.Println("threshold", threshold)
  834. // 查询地图更新表中未发送续扫提醒通知的记录(续扫提醒标志为0)
  835. mapUpdates, err := mysql.QueryMapUpdateRecordsByRescanFlag(ctx, config.MAP_NOT_RESCAN_FLAG)
  836. if err != nil {
  837. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "查询地图更新列表失败"})
  838. return
  839. }
  840. // 记录已发送通知的地图id
  841. // 对于同一地图id的多个符合续扫通知提醒的记录,只允许发送一次续扫通知,但相关记录均会被标记为已发送续扫提醒
  842. mapIdList := make(map[string]bool)
  843. for _, mapUpdate := range mapUpdates {
  844. // 查询地图id是否存在于地图列表中
  845. exist, record := checkMapIdExist(mapUpdate.MapID, mapInfoList)
  846. fmt.Println("exist", exist)
  847. fmt.Println("record", record)
  848. if !exist {
  849. fmt.Println("地图列表中不存在该地图id,跳过...")
  850. continue
  851. }
  852. //fmt.Println("mapUpdate", mapUpdate)
  853. updatePercentage, err := getUpdatePercentage(mapUpdate.CumulativeUpdateRate)
  854. if err != nil {
  855. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: err.Error()})
  856. }
  857. fmt.Println("updatePercentage", updatePercentage)
  858. if updatePercentage >= threshold {
  859. mapId := mapUpdate.MapID
  860. if _, exits := mapIdList[mapId]; !exits {
  861. // 发送续扫通知
  862. paramMap := make(map[string]interface{})
  863. paramMap["mapId"] = mapId
  864. paramMap["type"] = "1" // 0代表修图通知,1代表续扫通知
  865. paramMap["customAreaId"] = record.CustomAreaId
  866. resp, err := pji_client.ApiClient.JsonPostRequest(c_pji.PjiApiSitBaseUrl+"sendMsg", paramMap, pji_client.MapSecretId)
  867. if err != nil {
  868. continue
  869. }
  870. fmt.Println(string(resp.Body()))
  871. mapIdList[mapId] = true
  872. }
  873. // 修改续扫提醒标志
  874. mapUpdate.RescanNotifyFlag = config.MAP_RESCAN_FLAG
  875. thresholdStr := strconv.FormatFloat(float64(threshold), 'f', 2, 32) + "%"
  876. fmt.Println("thresholdStr", thresholdStr)
  877. mapUpdate.RescanNotifyThreshold = &thresholdStr
  878. _, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate)
  879. if err != nil {
  880. continue
  881. }
  882. }
  883. }
  884. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "更新地图续扫提醒成功"})
  885. }
  886. // AddMapRescanNotifyWithoutHttp 查询累积地图更新率低于续扫阈值的记录,并通知运维人员
  887. func AddMapRescanNotifyWithoutHttp(ctx context.Context) bool {
  888. // 查询朴津地图列表
  889. mapInfoList, err := getMapInfoList()
  890. if err != nil || mapInfoList == nil {
  891. fmt.Println("获取地图列表数据失败")
  892. return false
  893. }
  894. // 查询地图续扫阈值
  895. systemConfig, err := mysql.QuerySystemConfig(ctx)
  896. if err != nil {
  897. fmt.Println("获取系统配置失败")
  898. return false
  899. }
  900. threshold := systemConfig.RescanReminderThreshold
  901. fmt.Println("threshold", threshold)
  902. // 查询地图更新表中未发送续扫提醒通知的记录(续扫提醒标志为0)
  903. mapUpdates, err := mysql.QueryMapUpdateRecordsByRescanFlag(ctx, config.MAP_NOT_RESCAN_FLAG)
  904. if err != nil {
  905. fmt.Println("查询地图更新列表失败")
  906. return false
  907. }
  908. // 记录已发送通知的地图id
  909. // 对于同一地图id的多个符合续扫通知提醒的记录,只允许发送一次续扫通知,但相关记录均会被标记为已发送续扫提醒
  910. mapIdList := make(map[string]bool)
  911. for _, mapUpdate := range mapUpdates {
  912. // 查询地图id是否存在于地图列表中
  913. exist, record := checkMapIdExist(mapUpdate.MapID, mapInfoList)
  914. fmt.Println("exist", exist)
  915. fmt.Println("record", record)
  916. if !exist {
  917. fmt.Println("地图列表中不存在该地图id,跳过...")
  918. continue
  919. }
  920. //fmt.Println("mapUpdate", mapUpdate)
  921. updatePercentage, err := getUpdatePercentage(mapUpdate.CumulativeUpdateRate)
  922. if err != nil {
  923. fmt.Println("error:", err)
  924. }
  925. fmt.Println("updatePercentage", updatePercentage)
  926. if updatePercentage >= threshold {
  927. mapId := mapUpdate.MapID
  928. if _, exits := mapIdList[mapId]; !exits {
  929. // 发送续扫通知
  930. //paramMap := make(map[string]interface{})
  931. //paramMap["mapId"] = mapId
  932. //paramMap["type"] = "1" // 0代表修图通知,1代表续扫通知
  933. //paramMap["customAreaId"] = record.CustomAreaId
  934. //resp, err := pji_client.ApiClient.JsonPostRequest(c_pji.PjiApiSitBaseUrl+"sendMsg", paramMap, pji_client.MapSecretId)
  935. //if err != nil {
  936. // continue
  937. //}
  938. //fmt.Println(string(resp.Body()))
  939. mapIdList[mapId] = true
  940. }
  941. // 修改续扫提醒标志
  942. mapUpdate.RescanNotifyFlag = config.MAP_RESCAN_FLAG
  943. // 记录发送通知时的续扫提醒阈值
  944. thresholdStr := strconv.FormatFloat(float64(threshold), 'f', 2, 32) + "%"
  945. mapUpdate.RescanNotifyThreshold = &thresholdStr
  946. _, err := mysql.UpdateMapUpdateOneRecord(ctx, *mapUpdate)
  947. if err != nil {
  948. continue
  949. }
  950. }
  951. }
  952. fmt.Println("更新地图续扫提醒成功")
  953. return true
  954. }
  955. // QueryMapRescanNotifyList 查询地图续扫提醒列表
  956. // @router /map/rescan/notify/list [GET]
  957. func QueryMapRescanNotifyList(ctx context.Context, c *app.RequestContext) {
  958. var record model.MapUpdate
  959. err := c.BindAndValidate(&record)
  960. fmt.Println("record", record)
  961. var pageFlag bool
  962. if c.Query("page") != "" && c.Query("pageSize") != "" {
  963. pageFlag = true
  964. } else {
  965. pageFlag = false
  966. }
  967. page, _ := strconv.Atoi(c.Query("page"))
  968. pageSize, _ := strconv.Atoi(c.Query("pageSize"))
  969. records, count, err := mysql.QueryMapRescanList(ctx, &record, pageFlag, page, pageSize)
  970. if err != nil {
  971. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图续扫提醒记录查询失败", Total: 0})
  972. return
  973. }
  974. output, err := json.Marshal(records)
  975. if err != nil {
  976. c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "地图续扫提醒记录查询失败", Total: 0})
  977. return
  978. }
  979. c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "地图续扫提醒记录查询成功", Data: string(output), Total: int(count)})
  980. }
  981. func getUpdatePercentage(percentageStr string) (float32, error) {
  982. // 去掉末尾的%
  983. valueStr := percentageStr[:len(percentageStr)-1]
  984. //fmt.Println("valueStr", valueStr)
  985. // 将字符串转为float32
  986. value, err := strconv.ParseFloat(valueStr, 32)
  987. if err != nil {
  988. fmt.Println("err", err)
  989. return 0, err
  990. }
  991. return float32(value), nil
  992. }
  993. // 计算oss中文件列表的总大小
  994. func calculateTotalFileSize(fileList []string) int {
  995. var totalSize int
  996. for _, file := range fileList {
  997. size, err := util.GetOSSFileSize(config.OssBucket, file) // 获取oss中单个文件的大小
  998. if err != nil {
  999. return 0
  1000. }
  1001. totalSize += size
  1002. }
  1003. return totalSize
  1004. }
  1005. func getTime(timeStr string, layout string) (time.Time, error) {
  1006. t, err := time.Parse(layout, timeStr)
  1007. if err != nil {
  1008. return time.Time{}, err
  1009. }
  1010. return t, nil
  1011. }
  1012. // 调用 朴津地图列表查询 接口
  1013. func getMapInfoList() ([]model.MapInfo, error) {
  1014. paramMap := make(map[string]interface{})
  1015. paramMap["mapType"] = "0"
  1016. _, err := pji_client.ApiClient.GetRequest(c_pji.PjiApiSitBaseUrl+"mapList?mapType=0", paramMap, pji_client.MapSecretId)
  1017. if err != nil {
  1018. return nil, err
  1019. }
  1020. // 解析json响应
  1021. //body := resp.Body()
  1022. //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}")
  1023. 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}")
  1024. //fmt.Println("body", body)
  1025. var mapRes model.MapInfoRes
  1026. err = json.Unmarshal([]byte(body), &mapRes)
  1027. if err != nil || mapRes.Code != 200 {
  1028. return nil, err
  1029. }
  1030. //fmt.Println("mapRes", mapRes)
  1031. return mapRes.Data, nil
  1032. }
  1033. // 查询地图id是否存在于地图列表中
  1034. func checkMapIdExist(mapId string, mapInfoList []model.MapInfo) (bool, model.MapInfo) {
  1035. // 转为map
  1036. IdMap := make(map[string]model.MapInfo)
  1037. for _, info := range mapInfoList {
  1038. IdMap[info.MapId] = info
  1039. }
  1040. record, exist := IdMap[mapId]
  1041. return exist, record
  1042. }
  1043. func groupMapListByDevice(mapInfoList []model.MapInfo) map[string][]model.MapInfo {
  1044. // 转为map
  1045. snCodeMap := make(map[string][]model.MapInfo)
  1046. for _, info := range mapInfoList {
  1047. for _, snCode := range info.SnCodeList {
  1048. if _, exist := snCodeMap[snCode]; !exist {
  1049. snCodeMap[snCode] = make([]model.MapInfo, 0)
  1050. snCodeMap[snCode] = append(snCodeMap[snCode], info)
  1051. } else {
  1052. snCodeMap[snCode] = append(snCodeMap[snCode], info)
  1053. }
  1054. }
  1055. }
  1056. return snCodeMap
  1057. }
  1058. // 根据场景id获取数据采集时的地图id
  1059. func getMapIdById(id string) (string, error) {
  1060. var mapId string
  1061. // 下载map.json
  1062. // 根据id获取对应的oss文件列表
  1063. allFileList, err := util.GetExactedMapFileById(id)
  1064. //fmt.Println("allFileList", allFileList)
  1065. // 过滤特定后缀的文件列表
  1066. fileList := util.FilterBySuffixes(allFileList, config.MapJsonFiltersuffixes...)
  1067. fmt.Println("fileList", fileList)
  1068. // 创建临时文件夹
  1069. tmpDir, err := os.MkdirTemp("", "temp-download-*")
  1070. fmt.Println("tmpDir:", tmpDir)
  1071. if err != nil {
  1072. fmt.Println("Error creating temporary directory:", err)
  1073. return "", err
  1074. }
  1075. c_log.GlobalLogger.Info("创建下载-临时文件夹:", tmpDir)
  1076. for _, file := range fileList {
  1077. path := filepath.Join(tmpDir, filepath.Base(file))
  1078. err = config.OssBucket.GetObjectToFile(file, path)
  1079. if err != nil {
  1080. fmt.Println("Error downloading file:", err)
  1081. return "", err
  1082. }
  1083. c_log.GlobalLogger.Info("下载map.json文件 - 成功")
  1084. // 读取json文件 - mapId
  1085. // 打开文件
  1086. file, err := os.Open(path)
  1087. if err != nil {
  1088. fmt.Println("Error opening file:", err)
  1089. return "", err
  1090. }
  1091. defer file.Close()
  1092. // 读取文件内容
  1093. fileData, err := ioutil.ReadAll(file)
  1094. if err != nil {
  1095. fmt.Println("Error reading file:", err)
  1096. return "", err
  1097. }
  1098. // Json转换为map
  1099. var mapData map[string]interface{}
  1100. err = json.Unmarshal(fileData, &mapData)
  1101. data := mapData["map"].(map[string]interface{})
  1102. // 读取mapId
  1103. mapId = data["mapId"].(string)
  1104. fmt.Println("mapId:", mapId)
  1105. }
  1106. return mapId, err
  1107. }