exportBidding.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. func exportBidding2() {
  14. //Mgo := &mongodb.MongodbSim{
  15. // MongodbAddr: "192.168.3.206:27002",
  16. // DbName: "qfw_data",
  17. // Size: 10,
  18. // UserName: "root",
  19. // Password: "root",
  20. // Direct: true,
  21. //}
  22. //Mgo.InitPool()
  23. Mgo := &mongodb.MongodbSim{
  24. //MongodbAddr: "172.17.189.140:27080",
  25. MongodbAddr: "127.0.0.1:27083",
  26. Size: 10,
  27. DbName: "qfw",
  28. UserName: "SJZY_RWbid_ES",
  29. Password: "SJZY@B4i4D5e6S",
  30. Direct: true,
  31. }
  32. Mgo.InitPool()
  33. sess := Mgo.GetMgoConn()
  34. defer Mgo.DestoryMongoConn(sess)
  35. //start, _ := time.Parse("2006-01-02 15:04:05", "2023-09-06 21:00:00")
  36. //end, _ := time.Parse("2006-01-02 15:04:05", "2023-09-07 09:00:00")
  37. //////
  38. //q := map[string]interface{}{
  39. // "publish": map[string]interface{}{
  40. // "$gte": 1701360000,
  41. // "$lte": 1706716800,
  42. // },
  43. // //"modifyinfo.toptype": map[string]interface{}{
  44. // // "$exists": 1,
  45. // //},
  46. // "toptype": "拟建",
  47. // //"title": map[string]interface{}{
  48. // // "$regex": "充电",
  49. // //},
  50. //}
  51. query := sess.DB("qfw").C("wcc_20250312_bidding").Find(nil).Select(nil).Iter()
  52. count := 0
  53. file := time.Now().Format("20060102") + "导出数据.xlsx"
  54. currentPwd, _ := os.Getwd()
  55. exportFile := fmt.Sprintf("%s/%s", currentPwd, file)
  56. xlsx := excelize.NewFile(excelize.Options{ShortDatePattern: "yyyy/m/dd"})
  57. styleOne, _ := xlsx.NewStyle(
  58. &excelize.Style{
  59. Alignment: &excelize.Alignment{
  60. Horizontal: "left",
  61. Vertical: "left",
  62. },
  63. },
  64. )
  65. line := 0
  66. sheet := "Sheet1"
  67. //xlsx.NewSheet(sheet)
  68. //xlsx.DeleteSheet("Sheet1")
  69. //_ = xlsx.SetColWidth(sheet, "A", "A", 20)
  70. //_ = xlsx.SetColWidth(sheet, "B", "B", 25)
  71. //_ = xlsx.SetColWidth(sheet, "C", "C", 30)
  72. //_ = xlsx.SetColWidth(sheet, "D", "D", 35)
  73. //_ = xlsx.SetColWidth(sheet, "E", "E", 45)
  74. //_ = xlsx.SetColWidth(sheet, "F", "F", 20)
  75. subtitles := []string{"title", "area", "buyeraddr", "channel", "projectname", "site", "spidercode", "toptype", "subtype", "detail", "district", "publishtime", "s_topscopeclass", "s_subscopeclass", "jyhref", "agencyaddr", "projectperiod", "project_duration", "bidendtime", "bidendtime", "city", "budget", "buyerclass", "winner", "agencytel", "bidamount", "bidamount", "project_timeunit", "purchasing_tag", "projectcode"}
  76. //subtitles := []string{"bidding_id", "title", "detail", "href", "jyhref", "toptype", "new_toptype", "new_subtype"}
  77. line++
  78. //设置第一行title
  79. _ = xlsx.SetSheetRow(sheet, fmt.Sprintf("%s%d", "A", line), &subtitles)
  80. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  81. if count%1000 == 0 {
  82. log.Println("current --- ", count)
  83. }
  84. line++
  85. val := []interface{}{}
  86. for _, v := range subtitles {
  87. if v == "publishtime" {
  88. timeObj := time.Unix(util.Int64All(tmp[v]), 0)
  89. formattedTime := timeObj.Format("2006-01-02 15:04:05")
  90. val = append(val, formattedTime)
  91. } else {
  92. val = append(val, tmp[v])
  93. }
  94. }
  95. err := xlsx.SetSheetRow(sheet, fmt.Sprintf("%s%d", "A", line), &val)
  96. if err != nil {
  97. log.Println(err)
  98. return
  99. }
  100. _ = xlsx.SetCellStyle(sheet, fmt.Sprintf("%s%d", "A", line), "BA"+strconv.Itoa(line), styleOne)
  101. tmp = make(map[string]interface{})
  102. }
  103. xlsx.Path = exportFile
  104. xlsx.Save()
  105. log.Println("dealTmp over ", count)
  106. }
  107. // exportBidding 导出bidding 数据 excel
  108. func exportBidding() {
  109. //Mgo := &mongodb.MongodbSim{
  110. // MongodbAddr: "192.168.3.206:27002",
  111. // DbName: "qfw_data",
  112. // Size: 10,
  113. // UserName: "root",
  114. // Password: "root",
  115. // Direct: true,
  116. //}
  117. //Mgo.InitPool()
  118. Mgo := &mongodb.MongodbSim{
  119. //MongodbAddr: "172.17.189.140:27080",
  120. MongodbAddr: "127.0.0.1:27083",
  121. Size: 10,
  122. DbName: "qfw",
  123. UserName: "SJZY_RWbid_ES",
  124. Password: "SJZY@B4i4D5e6S",
  125. Direct: true,
  126. }
  127. Mgo.InitPool()
  128. sess := Mgo.GetMgoConn()
  129. defer Mgo.DestoryMongoConn(sess)
  130. //start, _ := time.Parse("2006-01-02 15:04:05", "2023-09-06 21:00:00")
  131. //end, _ := time.Parse("2006-01-02 15:04:05", "2023-09-07 09:00:00")
  132. //////
  133. q := map[string]interface{}{
  134. "publish": map[string]interface{}{
  135. "$gte": 1701360000,
  136. "$lte": 1706716800,
  137. },
  138. //"modifyinfo.toptype": map[string]interface{}{
  139. // "$exists": 1,
  140. //},
  141. "toptype": "拟建",
  142. //"title": map[string]interface{}{
  143. // "$regex": "充电",
  144. //},
  145. }
  146. query := sess.DB("qfw").C("bidding").Find(q).Select(nil).Iter()
  147. count := 0
  148. file := time.Now().Format("20060102") + "导出数据.xlsx"
  149. currentPwd, _ := os.Getwd()
  150. exportFile := fmt.Sprintf("%s/%s", currentPwd, file)
  151. xlsx := excelize.NewFile(excelize.Options{ShortDatePattern: "yyyy/m/dd"})
  152. styleOne, _ := xlsx.NewStyle(
  153. &excelize.Style{
  154. Alignment: &excelize.Alignment{
  155. Horizontal: "left",
  156. Vertical: "left",
  157. },
  158. },
  159. )
  160. line := 0
  161. sheet := "Sheet1"
  162. //xlsx.NewSheet(sheet)
  163. //xlsx.DeleteSheet("Sheet1")
  164. //_ = xlsx.SetColWidth(sheet, "A", "A", 20)
  165. //_ = xlsx.SetColWidth(sheet, "B", "B", 25)
  166. //_ = xlsx.SetColWidth(sheet, "C", "C", 30)
  167. //_ = xlsx.SetColWidth(sheet, "D", "D", 35)
  168. //_ = xlsx.SetColWidth(sheet, "E", "E", 45)
  169. //_ = xlsx.SetColWidth(sheet, "F", "F", 20)
  170. subtitles := []string{"title", "projectname", "href", "jyhref", "toptype", "subtype"}
  171. //subtitles := []string{"bidding_id", "title", "detail", "href", "jyhref", "toptype", "new_toptype", "new_subtype"}
  172. line++
  173. //设置第一行title
  174. _ = xlsx.SetSheetRow(sheet, fmt.Sprintf("%s%d", "A", line), &subtitles)
  175. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  176. if count%1000 == 0 {
  177. log.Println("current --- ", count)
  178. }
  179. line++
  180. title := util.ObjToString(tmp["title"])
  181. if !strings.Contains(title, "充电") {
  182. continue
  183. }
  184. id := mongodb.BsonIdToSId(tmp["_id"])
  185. val := []interface{}{}
  186. for _, v := range subtitles {
  187. if v == "jyhref" {
  188. val = append(val, GetJyURLByID(id))
  189. } else if v == "publishtime" {
  190. timeObj := time.Unix(util.Int64All(tmp[v]), 0)
  191. formattedTime := timeObj.Format("2006-01-02 15:04:05")
  192. val = append(val, formattedTime)
  193. } else {
  194. val = append(val, tmp[v])
  195. }
  196. }
  197. err := xlsx.SetSheetRow(sheet, fmt.Sprintf("%s%d", "A", line), &val)
  198. if err != nil {
  199. log.Println(err)
  200. return
  201. }
  202. _ = xlsx.SetCellStyle(sheet, fmt.Sprintf("%s%d", "A", line), "BA"+strconv.Itoa(line), styleOne)
  203. tmp = make(map[string]interface{})
  204. }
  205. xlsx.Path = exportFile
  206. xlsx.Save()
  207. log.Println("dealTmp over ", count)
  208. }
  209. // GetJyURLByID 获取剑鱼地址
  210. func GetJyURLByID(id string) string {
  211. var Url = "https://www.jianyu360.com/article/content/%s.html"
  212. url := fmt.Sprintf(Url, util.CommonEncodeArticle("content", id))
  213. return url
  214. }
  215. // GetIdByURL 解密url,获取bidding ID
  216. func GetIdByURL(url string) string {
  217. if strings.Contains(url, "work-bench") {
  218. return ""
  219. }
  220. if strings.Contains(url, "/article/content") {
  221. urls := strings.Split(url, "content/")
  222. res := strings.Split(urls[1], ".html")
  223. ids := util.CommonDecodeArticle("content", res[0])
  224. return ids[0]
  225. }
  226. if strings.HasSuffix(url, "appid") {
  227. urls := strings.Split(url, "entservice/")
  228. res := strings.Split(urls[1], ".html")
  229. se := util.SimpleEncrypt{Key: "entservice"}
  230. id := se.DecodeString(res[0])
  231. return id
  232. }
  233. return ""
  234. }