map_service.go 69 KB

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