xlsx.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  6. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  7. "log"
  8. "time"
  9. )
  10. // dealXlsxData 处理xlsx 给的样例数据,匹配在建项目数据,然后存储MongoDB
  11. func dealXlsxData() {
  12. //url := "http://172.17.4.184:19908"
  13. ////url := "http://127.0.0.1:19908"
  14. //username := "jybid"
  15. //password := "Top2023_JEB01i@31"
  16. ////index := "bidding" //索引名称
  17. //// 创建 Elasticsearch 客户端
  18. //client, err := elastic.NewClient(
  19. // elastic.SetURL(url),
  20. // elastic.SetBasicAuth(username, password),
  21. // elastic.SetSniff(false),
  22. //)
  23. //if err != nil {
  24. // log.Fatalf("创建 Elasticsearch 客户端失败:%s", err)
  25. //}
  26. // 2. 读取 Excel
  27. f, err := excelize.OpenFile("投资入库项目_202504.xlsx")
  28. if err != nil {
  29. log.Fatal("❌ 无法打开 Excel 文件:", err)
  30. }
  31. defer func() {
  32. f.Save()
  33. if err := f.Close(); err != nil {
  34. fmt.Println(err)
  35. }
  36. }()
  37. // 3. 读取 B 列(项目名称)
  38. rows, err := f.GetRows("Sheet1") // 替换为正确的 Sheet 名称
  39. if err != nil {
  40. log.Fatal("❌ 无法读取 Excel:", err)
  41. }
  42. log.Println("开始处理数据--------")
  43. for i := 1; i < len(rows); i++ {
  44. p1_project_code := rows[i][0]
  45. p1_project_name := rows[i][1]
  46. p1_project_owner := rows[i][2]
  47. p1_project_owner_code := rows[i][3]
  48. p1_project_owner_area := rows[i][4]
  49. p1_project_total := rows[i][5]
  50. //
  51. log.Println("项目名称--------", p1_project_name)
  52. insert := map[string]interface{}{
  53. "p1_project_code": p1_project_code,
  54. "p1_project_name": p1_project_name,
  55. "p1_project_owner": p1_project_owner,
  56. "p1_project_owner_code": p1_project_owner_code,
  57. "p1_project_owner_area": p1_project_owner_area,
  58. "p1_project_total": p1_project_total,
  59. }
  60. start := 8
  61. row := rows[i]
  62. p1_biddings := make([]map[string]interface{}, 0)
  63. p1_bidding_ids := make([]string, 0)
  64. biddingIdMap := make(map[string]map[string]interface{}, 0)
  65. //projectIdMap := make(map[string]map[string]interface{}, 0) //project_id -> project map
  66. for i := start; i+3 < len(row); i += 4 {
  67. href := row[i+2]
  68. score := row[i+3]
  69. bid := GetIdByURL(href)
  70. p1_bidding_ids = append(p1_bidding_ids, bid)
  71. bidding, _ := MgoB.FindById("bidding", bid, nil)
  72. da := map[string]interface{}{
  73. "id": bid,
  74. "title": (*bidding)["title"],
  75. "area": (*bidding)["area"],
  76. "city": (*bidding)["city"],
  77. "projectname": (*bidding)["projectname"],
  78. "score": score,
  79. "toptype": (*bidding)["toptype"],
  80. "subtype": (*bidding)["subtype"],
  81. "buyer": (*bidding)["buyer"],
  82. "budget": (*bidding)["budget"],
  83. "buyerperson": (*bidding)["buyerperson"],
  84. "buyertel": (*bidding)["buyertel"],
  85. "s_winner": (*bidding)["s_winner"],
  86. "bidamount": (*bidding)["bidamount"],
  87. "winnertel": (*bidding)["winnertel"],
  88. "agency": (*bidding)["agency"],
  89. "publishtime": (*bidding)["publishtime"],
  90. }
  91. biddingIdMap[bid] = da
  92. //log.Println("aa", insert, amount, title, href, score)
  93. p1_biddings = append(p1_biddings, da)
  94. }
  95. //insert["biddings"] = p1_biddings
  96. p1_bidding_ids = removeDuplicates(p1_bidding_ids)
  97. for _, vv := range p1_bidding_ids {
  98. where2 := map[string]interface{}{
  99. "ids": vv,
  100. }
  101. projectset, _ := MgoP.FindOne("projectset_20230904", where2)
  102. if projectset != nil && len(*projectset) > 0 {
  103. data := map[string]interface{}{
  104. "project_id": mongodb.BsonIdToSId((*projectset)["_id"]),
  105. "projectname": util.ObjToString((*projectset)["projectname"]),
  106. "bidamount": (*projectset)["bidamount"],
  107. "sortprice": (*projectset)["sortprice"],
  108. "area": (*projectset)["area"],
  109. "city": (*projectset)["city"],
  110. "district": (*projectset)["district"],
  111. "bidstatus": (*projectset)["bidstatus"],
  112. "buyer": (*projectset)["buyer"],
  113. "firsttime": (*projectset)["firsttime"],
  114. "bidtype": (*projectset)["bidtype"],
  115. }
  116. biddData := biddingIdMap[vv]
  117. biddData["project"] = data
  118. biddingIdMap[vv] = biddData
  119. }
  120. }
  121. var insertBidding = make([]map[string]interface{}, 0)
  122. for _, v := range biddingIdMap {
  123. insertBidding = append(insertBidding, v)
  124. }
  125. insert["bidding"] = insertBidding
  126. MgoP.InsertOrUpdate("qfw", "wcc_dealXlsxData_0524", insert)
  127. }
  128. log.Println("数据处理完毕--------")
  129. }
  130. // exportTestData 导出拓扑统计局 样例数据
  131. func exportTestData() {
  132. // 打开已有模板
  133. f, err := excelize.OpenFile("统计局入库项目数据目录树.xlsx")
  134. if err != nil {
  135. log.Fatal("打开模板失败:", err)
  136. }
  137. sheet := f.GetSheetName(0) // 使用第一个 Sheet,也可以改成具体 Sheet 名
  138. startRow := 2 // 假设第 1 行是表头,数据从第 2 行开始
  139. rowIndex := startRow
  140. sess := MgoP.GetMgoConn()
  141. defer MgoP.DestoryMongoConn(sess)
  142. queryMgo := sess.DB("qfw").C("wcc_dealXlsxData_0524").Find(nil).Select(nil).Iter()
  143. count := 0
  144. for tmp := make(map[string]interface{}); queryMgo.Next(tmp); count++ {
  145. if count%1000 == 0 {
  146. log.Println("current", count)
  147. }
  148. //
  149. projectname := util.ObjToString(tmp["p1_project_name"])
  150. where := map[string]interface{}{
  151. "projectname": projectname,
  152. }
  153. pro_project, _ := MgoP.FindOne("projectset_proposed", where)
  154. ppid := ""
  155. if pro_project != nil {
  156. ppid = mongodb.BsonIdToSId((*pro_project)["_id"])
  157. }
  158. //1.拟建原始数据
  159. v1 := []interface{}{
  160. ppid,
  161. tmp["p1_project_code"],
  162. tmp["p1_project_name"],
  163. tmp["p1_project_owner"],
  164. tmp["p1_project_owner_code"],
  165. tmp["p1_project_owner_area"],
  166. }
  167. // 遍历招采信息
  168. if biddingList, ok := tmp["bidding"].([]interface{}); ok {
  169. for _, bidItem := range biddingList {
  170. bid := bidItem.(map[string]interface{})
  171. project := bid["project"].(map[string]interface{})
  172. // 在建招采项目信息
  173. v2 := []interface{}{
  174. project["project_id"],
  175. project["projectname"],
  176. project["bidamount"],
  177. project["area"],
  178. project["city"],
  179. project["bidstatus"],
  180. project["buyer"],
  181. time.Unix(util.Int64All(project["firsttime"]), 0).In(time.FixedZone("CST", 8*3600)).Format("2006-01-02 15:04:05"),
  182. bid["score"],
  183. }
  184. // 标讯信息
  185. v3 := []interface{}{
  186. bid["id"],
  187. bid["title"],
  188. bid["subtype"],
  189. bid["area"],
  190. bid["city"],
  191. bid["buyer"],
  192. bid["budget"],
  193. bid["buyerperson"],
  194. bid["buyertel"],
  195. bid["s_winner"],
  196. bid["bidamount"],
  197. bid["winnertel"],
  198. bid["agency"],
  199. time.Unix(util.Int64All(bid["publishtime"]), 0).In(time.FixedZone("CST", 8*3600)).Format("2006-01-02 15:04:05"),
  200. //bid["publishtime"],
  201. bid["score"],
  202. }
  203. // 拼接成一行
  204. fullRow := append(append(v1, v2...), v3...)
  205. cell, _ := excelize.CoordinatesToCellName(1, rowIndex)
  206. f.SetSheetRow(sheet, cell, &fullRow)
  207. rowIndex++
  208. // 写入表格,如写入 Excel
  209. //excelWriter.WriteRow(fullRow) // 你需要实现 WriteRow 方法
  210. }
  211. }
  212. }
  213. // 保存为另一个文件,防止覆盖模板
  214. outputFile := "统计局入库项目数据导出.xlsx"
  215. if err := f.SaveAs(outputFile); err != nil {
  216. log.Fatal("保存 Excel 失败:", err)
  217. }
  218. log.Println("✅ 数据写入完成,共写入行数:", rowIndex-startRow)
  219. }
  220. // exportTestData1 导出统计局 拟建数据
  221. func exportTestData1() {
  222. // 1. 连接 MongoDB
  223. sess := MgoP.GetMgoConn()
  224. defer MgoP.DestoryMongoConn(sess)
  225. // 2. 打开 Excel 模板
  226. f, err := excelize.OpenFile("统计局入库项目数据目录树.xlsx")
  227. if err != nil {
  228. log.Fatalf("无法打开Excel模板: %v", err)
  229. }
  230. sheet := f.GetSheetName(0)
  231. rowIndex := 3 // 假设第一行是表头
  232. where := map[string]interface{}{
  233. "city": "陇南市",
  234. }
  235. // 3. 查询 Mongo 数据
  236. queryMgo := sess.DB("qfw").C("wcc_dealProposed22_0524").Find(where).Iter()
  237. count := 0
  238. for tmp := make(map[string]interface{}); queryMgo.Next(tmp); count++ {
  239. if count%1000 == 0 {
  240. log.Println("current", count)
  241. }
  242. //projectname := util.ObjToString(tmp["p1_project_name"])
  243. //where := map[string]interface{}{"projectname": projectname}
  244. //pro_project, _ := MgoP.FindOne("projectset_proposed", where)
  245. //ppid := ""
  246. //if pro_project != nil {
  247. // ppid = mongodb.BsonIdToSId((*pro_project)["_id"])
  248. //}
  249. v1 := []interface{}{
  250. //ppid,
  251. tmp["proposed_id"],
  252. tmp["approvecode"],
  253. tmp["projectname"],
  254. tmp["buyer"],
  255. tmp["credit_no"],
  256. fmt.Sprintf("%s-%s-%s", tmp["area"], tmp["city"], tmp["district"]),
  257. }
  258. // ----------- 核心容错处理逻辑 ------------
  259. if biddingListRaw, ok := tmp["bidding"]; ok {
  260. biddingList, ok := biddingListRaw.([]interface{})
  261. if !ok || len(biddingList) == 0 {
  262. writeEmptyRow(f, sheet, rowIndex, v1)
  263. rowIndex++
  264. } else {
  265. for _, bidItem := range biddingList {
  266. bidMap, ok := bidItem.(map[string]interface{})
  267. if !ok {
  268. continue
  269. }
  270. if util.ObjToString(bidMap["toptype"]) == "拟建" {
  271. continue
  272. }
  273. // v2
  274. v2 := make([]interface{}, 9)
  275. for i := range v2 {
  276. v2[i] = ""
  277. }
  278. if projRaw, ok := bidMap["project"]; ok {
  279. if project, ok := projRaw.(map[string]interface{}); ok {
  280. firstTimeStr := ""
  281. if val, ok := project["firsttime"]; ok {
  282. if t := util.Int64All(val); t > 0 {
  283. firstTimeStr = time.Unix(t, 0).In(time.FixedZone("CST", 8*3600)).Format("2006-01-02 15:04:05")
  284. }
  285. }
  286. v2 = []interface{}{
  287. project["project_id"],
  288. project["projectname"],
  289. project["bidamount"],
  290. project["area"],
  291. project["city"],
  292. project["bidstatus"],
  293. project["buyer"],
  294. firstTimeStr,
  295. bidMap["score"],
  296. }
  297. }
  298. }
  299. publishTimeStr := ""
  300. if val, ok := bidMap["publishtime"]; ok {
  301. if t := util.Int64All(val); t > 0 {
  302. publishTimeStr = time.Unix(t, 0).In(time.FixedZone("CST", 8*3600)).Format("2006-01-02 15:04:05")
  303. }
  304. }
  305. v3 := []interface{}{
  306. bidMap["id"],
  307. bidMap["title"],
  308. bidMap["subtype"],
  309. bidMap["area"],
  310. bidMap["city"],
  311. bidMap["buyer"],
  312. bidMap["budget"],
  313. bidMap["buyerperson"],
  314. bidMap["buyertel"],
  315. bidMap["s_winner"],
  316. bidMap["bidamount"],
  317. bidMap["winnertel"],
  318. bidMap["agency"],
  319. publishTimeStr,
  320. bidMap["score"],
  321. }
  322. fullRow := append(append(v1, v2...), v3...)
  323. cell, _ := excelize.CoordinatesToCellName(1, rowIndex)
  324. f.SetSheetRow(sheet, cell, &fullRow)
  325. rowIndex++
  326. }
  327. }
  328. } else {
  329. // bidding 字段不存在
  330. writeEmptyRow(f, sheet, rowIndex, v1)
  331. rowIndex++
  332. }
  333. }
  334. // 4. 保存结果
  335. if err := f.SaveAs("输出结果.xlsx"); err != nil {
  336. log.Fatalf("保存文件失败: %v", err)
  337. }
  338. log.Println("导出完成,保存为 输出结果.xlsx")
  339. }
  340. // exportTestData2 导出 陇南市 拟建数据
  341. func exportTestData2() {
  342. // 1. 连接 MongoDB
  343. sess := MgoP.GetMgoConn()
  344. defer MgoP.DestoryMongoConn(sess)
  345. // 2. 打开 Excel 模板
  346. f, err := excelize.OpenFile("统计局入库项目数据目录树.xlsx")
  347. if err != nil {
  348. log.Fatalf("无法打开Excel模板: %v", err)
  349. }
  350. sheet := f.GetSheetName(0)
  351. rowIndex := 3 // 假设第一行是表头
  352. where := map[string]interface{}{
  353. "city": "陇南市",
  354. }
  355. // 3. 查询 Mongo 数据
  356. queryMgo := sess.DB("qfw").C("wcc_dealProposed22_0524").Find(where).Iter()
  357. count := 0
  358. for tmp := make(map[string]interface{}); queryMgo.Next(tmp); count++ {
  359. if count%1000 == 0 {
  360. log.Println("current", count)
  361. }
  362. //projectname := util.ObjToString(tmp["p1_project_name"])
  363. //where := map[string]interface{}{"projectname": projectname}
  364. //pro_project, _ := MgoP.FindOne("projectset_proposed", where)
  365. //ppid := ""
  366. //if pro_project != nil {
  367. // ppid = mongodb.BsonIdToSId((*pro_project)["_id"])
  368. //}
  369. v1 := []interface{}{
  370. //ppid,
  371. tmp["proposed_id"],
  372. tmp["approvecode"],
  373. tmp["projectname"],
  374. tmp["buyer"],
  375. tmp["credit_no"],
  376. }
  377. //fmt.Sprintf("%s-%s-%s", tmp["area"], tmp["city"], tmp["district"]),
  378. acd := ""
  379. if util.ObjToString(tmp["area"]) != "" {
  380. acd = util.ObjToString(tmp["area"])
  381. }
  382. if util.ObjToString(tmp["city"]) != "" {
  383. acd = acd + "-" + util.ObjToString(tmp["city"])
  384. }
  385. if util.ObjToString(tmp["district"]) != "" {
  386. acd = acd + "-" + util.ObjToString(tmp["district"])
  387. }
  388. v1 = append(v1, acd)
  389. // ----------- 核心容错处理逻辑 ------------
  390. if biddingListRaw, ok := tmp["bidding"]; ok {
  391. biddingList, ok := biddingListRaw.([]interface{})
  392. if !ok || len(biddingList) == 0 {
  393. writeEmptyRow(f, sheet, rowIndex, v1)
  394. rowIndex++
  395. } else {
  396. for _, bidItem := range biddingList {
  397. bidMap, ok := bidItem.(map[string]interface{})
  398. if !ok {
  399. continue
  400. }
  401. if util.ObjToString(bidMap["toptype"]) == "拟建" {
  402. continue
  403. }
  404. // v2
  405. v2 := make([]interface{}, 9)
  406. for i := range v2 {
  407. v2[i] = ""
  408. }
  409. if projRaw, ok := bidMap["project"]; ok {
  410. if project, ok := projRaw.(map[string]interface{}); ok {
  411. firstTimeStr := ""
  412. if val, ok := project["firsttime"]; ok {
  413. if t := util.Int64All(val); t > 0 {
  414. firstTimeStr = time.Unix(t, 0).In(time.FixedZone("CST", 8*3600)).Format("2006-01-02 15:04:05")
  415. }
  416. }
  417. v2 = []interface{}{
  418. project["project_id"],
  419. project["projectname"],
  420. project["bidamount"],
  421. project["area"],
  422. project["city"],
  423. project["bidstatus"],
  424. project["buyer"],
  425. firstTimeStr,
  426. bidMap["score"],
  427. }
  428. }
  429. }
  430. publishTimeStr := ""
  431. if val, ok := bidMap["publishtime"]; ok {
  432. if t := util.Int64All(val); t > 0 {
  433. publishTimeStr = time.Unix(t, 0).In(time.FixedZone("CST", 8*3600)).Format("2006-01-02 15:04:05")
  434. }
  435. }
  436. v3 := []interface{}{
  437. bidMap["id"],
  438. bidMap["title"],
  439. bidMap["subtype"],
  440. bidMap["area"],
  441. bidMap["city"],
  442. bidMap["buyer"],
  443. bidMap["budget"],
  444. bidMap["buyerperson"],
  445. bidMap["buyertel"],
  446. bidMap["s_winner"],
  447. bidMap["bidamount"],
  448. bidMap["winnertel"],
  449. bidMap["agency"],
  450. publishTimeStr,
  451. bidMap["score"],
  452. }
  453. fullRow := append(append(v1, v2...), v3...)
  454. cell, _ := excelize.CoordinatesToCellName(1, rowIndex)
  455. f.SetSheetRow(sheet, cell, &fullRow)
  456. rowIndex++
  457. }
  458. }
  459. } else {
  460. // bidding 字段不存在
  461. writeEmptyRow(f, sheet, rowIndex, v1)
  462. rowIndex++
  463. }
  464. }
  465. // 4. 保存结果
  466. if err := f.SaveAs("输出结果.xlsx"); err != nil {
  467. log.Fatalf("保存文件失败: %v", err)
  468. }
  469. log.Println("导出完成,保存为 输出结果.xlsx")
  470. }
  471. func writeEmptyRow(f *excelize.File, sheet string, rowIndex int, v1 []interface{}) {
  472. emptyV2 := make([]interface{}, 9)
  473. emptyV3 := make([]interface{}, 15)
  474. for i := range emptyV2 {
  475. emptyV2[i] = ""
  476. }
  477. for i := range emptyV3 {
  478. emptyV3[i] = ""
  479. }
  480. fullRow := append(append(v1, emptyV2...), emptyV3...)
  481. cell, _ := excelize.CoordinatesToCellName(1, rowIndex)
  482. f.SetSheetRow(sheet, cell, &fullRow)
  483. }
  484. // 测试,更新approvecode 字段
  485. func ddd() {
  486. sess := MgoP.GetMgoConn()
  487. defer MgoP.DestoryMongoConn(sess)
  488. // 3. 查询 Mongo 数据
  489. queryMgo := sess.DB("qfw").C("wcc_dealProposed22_0524").Find(nil).Iter()
  490. count := 0
  491. for tmp := make(map[string]interface{}); queryMgo.Next(tmp); count++ {
  492. if count%1000 == 0 {
  493. log.Println("current", count)
  494. }
  495. pro_id := util.ObjToString(tmp["proposed_id"])
  496. pro, _ := MgoP.FindById("projectset_proposed", pro_id, nil)
  497. if util.ObjToString((*pro)["approvecode"]) != "" {
  498. update := map[string]interface{}{
  499. "approvecode": (*pro)["approvecode"],
  500. }
  501. id := mongodb.BsonIdToSId(tmp["_id"])
  502. MgoP.UpdateById("wcc_dealProposed22_0524", id, map[string]interface{}{"$set": update})
  503. }
  504. }
  505. }