exportBidding.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // exportBidding 导出bidding 数据 excel
  14. func exportBidding() {
  15. //Mgo := &mongodb.MongodbSim{
  16. // MongodbAddr: "192.168.3.206:27002",
  17. // DbName: "qfw_data",
  18. // Size: 10,
  19. // UserName: "root",
  20. // Password: "root",
  21. // Direct: true,
  22. //}
  23. //Mgo.InitPool()
  24. Mgo := &mongodb.MongodbSim{
  25. //MongodbAddr: "172.17.189.140:27080",
  26. MongodbAddr: "127.0.0.1:27083",
  27. Size: 10,
  28. DbName: "qfw",
  29. UserName: "SJZY_RWbid_ES",
  30. Password: "SJZY@B4i4D5e6S",
  31. Direct: true,
  32. }
  33. Mgo.InitPool()
  34. sess := Mgo.GetMgoConn()
  35. defer Mgo.DestoryMongoConn(sess)
  36. //start, _ := time.Parse("2006-01-02 15:04:05", "2023-09-06 21:00:00")
  37. //end, _ := time.Parse("2006-01-02 15:04:05", "2023-09-07 09:00:00")
  38. //////
  39. q := map[string]interface{}{
  40. "publish": map[string]interface{}{
  41. "$gte": 1701360000,
  42. "$lte": 1706716800,
  43. },
  44. //"modifyinfo.toptype": map[string]interface{}{
  45. // "$exists": 1,
  46. //},
  47. "toptype": "拟建",
  48. //"title": map[string]interface{}{
  49. // "$regex": "充电",
  50. //},
  51. }
  52. query := sess.DB("qfw").C("bidding").Find(q).Select(nil).Iter()
  53. count := 0
  54. file := time.Now().Format("20060102") + "导出数据.xlsx"
  55. currentPwd, _ := os.Getwd()
  56. exportFile := fmt.Sprintf("%s/%s", currentPwd, file)
  57. xlsx := excelize.NewFile(excelize.Options{ShortDatePattern: "yyyy/m/dd"})
  58. styleOne, _ := xlsx.NewStyle(
  59. &excelize.Style{
  60. Alignment: &excelize.Alignment{
  61. Horizontal: "left",
  62. Vertical: "left",
  63. },
  64. },
  65. )
  66. line := 0
  67. sheet := "Sheet1"
  68. //xlsx.NewSheet(sheet)
  69. //xlsx.DeleteSheet("Sheet1")
  70. //_ = xlsx.SetColWidth(sheet, "A", "A", 20)
  71. //_ = xlsx.SetColWidth(sheet, "B", "B", 25)
  72. //_ = xlsx.SetColWidth(sheet, "C", "C", 30)
  73. //_ = xlsx.SetColWidth(sheet, "D", "D", 35)
  74. //_ = xlsx.SetColWidth(sheet, "E", "E", 45)
  75. //_ = xlsx.SetColWidth(sheet, "F", "F", 20)
  76. subtitles := []string{"title", "projectname", "href", "jyhref", "toptype", "subtype"}
  77. //subtitles := []string{"bidding_id", "title", "detail", "href", "jyhref", "toptype", "new_toptype", "new_subtype"}
  78. line++
  79. //设置第一行title
  80. _ = xlsx.SetSheetRow(sheet, fmt.Sprintf("%s%d", "A", line), &subtitles)
  81. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  82. if count%1000 == 0 {
  83. log.Println("current --- ", count)
  84. }
  85. line++
  86. title := util.ObjToString(tmp["title"])
  87. if !strings.Contains(title, "充电") {
  88. continue
  89. }
  90. id := mongodb.BsonIdToSId(tmp["_id"])
  91. val := []interface{}{}
  92. for _, v := range subtitles {
  93. if v == "jyhref" {
  94. val = append(val, GetJyURLByID(id))
  95. } else if v == "publishtime" {
  96. timeObj := time.Unix(util.Int64All(tmp[v]), 0)
  97. formattedTime := timeObj.Format("2006-01-02 15:04:05")
  98. val = append(val, formattedTime)
  99. } else {
  100. val = append(val, tmp[v])
  101. }
  102. }
  103. err := xlsx.SetSheetRow(sheet, fmt.Sprintf("%s%d", "A", line), &val)
  104. if err != nil {
  105. log.Println(err)
  106. return
  107. }
  108. _ = xlsx.SetCellStyle(sheet, fmt.Sprintf("%s%d", "A", line), "BA"+strconv.Itoa(line), styleOne)
  109. tmp = make(map[string]interface{})
  110. }
  111. xlsx.Path = exportFile
  112. xlsx.Save()
  113. log.Println("dealTmp over ", count)
  114. }
  115. // GetJyURLByID 获取剑鱼地址
  116. func GetJyURLByID(id string) string {
  117. var Url = "https://www.jianyu360.com/article/content/%s.html"
  118. url := fmt.Sprintf(Url, util.CommonEncodeArticle("content", id))
  119. return url
  120. }