main.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/tealeg/xlsx"
  6. "github.com/xuri/excelize/v2"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/mongo"
  9. "go.mongodb.org/mongo-driver/mongo/options"
  10. "gorm.io/driver/mysql"
  11. "gorm.io/gorm"
  12. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  13. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  14. "log"
  15. "net/url"
  16. "os"
  17. "sort"
  18. "strings"
  19. )
  20. type WinnerData struct {
  21. Winner string `bson:"winner"`
  22. CreditNo string `bson:"credit_no"`
  23. Num int `bson:"num"`
  24. }
  25. func main() {
  26. //导出数据到Excel
  27. //exportBidding()
  28. //fmt.Println(11111)
  29. //HighBidding()
  30. //exportQy()
  31. exportPhone2() // 导出联系电话
  32. //clickhouseData()
  33. //getName()
  34. log.Println("over")
  35. }
  36. // exportWinner 导出
  37. func exportWinner() {
  38. username := "SJZY_RWbid_ES"
  39. password := "SJZY@B4i4D5e6S"
  40. //addr := "172.17.189.140:27080"
  41. addr := "127.0.0.1:27083"
  42. direct := true
  43. if !strings.Contains(addr, "127") {
  44. direct = false
  45. }
  46. escapedUsername := url.QueryEscape(username)
  47. escapedPassword := url.QueryEscape(password)
  48. urls := fmt.Sprintf("mongodb://%s:%s@%s", escapedUsername, escapedPassword, addr)
  49. clientOptions := options.Client().ApplyURI(urls).SetDirect(direct)
  50. // 连接到MongoDB
  51. client, err := mongo.Connect(context.TODO(), clientOptions)
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. defer func() {
  56. if err := client.Disconnect(context.TODO()); err != nil {
  57. log.Fatal(err)
  58. }
  59. }()
  60. // 检查连接
  61. err = client.Ping(context.Background(), nil)
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. log.Println(1111)
  66. // 选择数据库和集合
  67. collection := client.Database("qfw").Collection("wcc_20240103")
  68. // 查询数据并排序
  69. // 设置查询条件
  70. filter := bson.D{
  71. //{"comeintime", bson.M{"$gte": 1640966400, "$lte": 1703952000}},
  72. //{"subtype", bson.M{"$in": []string{"中标", "单一", "成交", "合同"}}},
  73. }
  74. // 设置投影,排除 contenthtml 字段
  75. projection := bson.D{
  76. {"title", 1}, // 0表示不返回该字段
  77. {"detail", 1}, // 0表示不返回该字段
  78. {"href", 1}, // 0表示不返回该字段
  79. {"bidding_id", 1}, // 0表示不返回该字段
  80. {"subtype", 1}, // 0表示不返回该字段
  81. {"jyhref", 1}, // 0表示不返回该字段
  82. {"data_type", 1}, // 0表示不返回该字段
  83. {"toptype", 1}, // 0表示不返回该字段
  84. }
  85. //findOptions := options.Find().SetProjection(projection)
  86. findOptions := options.Find().SetSort(map[string]int{"num": -1}).SetLimit(100000).SetProjection(projection)
  87. cursor, err := collection.Find(context.Background(), filter, findOptions)
  88. if err != nil {
  89. log.Fatal(err)
  90. }
  91. defer cursor.Close(context.Background())
  92. // 创建 Excel 文件
  93. file := xlsx.NewFile()
  94. sheet, err := file.AddSheet("Sheet1")
  95. if err != nil {
  96. log.Fatal(err)
  97. }
  98. // 添加表头
  99. row := sheet.AddRow()
  100. row.AddCell().SetValue("Winner")
  101. row.AddCell().SetValue("Credit No")
  102. row.AddCell().SetValue("Num")
  103. // 遍历结果集并写入 Excel 文件
  104. for cursor.Next(context.Background()) {
  105. var winnerData WinnerData
  106. if err := cursor.Decode(&winnerData); err != nil {
  107. log.Fatal(err)
  108. }
  109. if !strings.Contains(winnerData.Winner, "公司") {
  110. continue
  111. }
  112. row = sheet.AddRow()
  113. row.AddCell().SetValue(winnerData.Winner)
  114. row.AddCell().SetValue(winnerData.CreditNo)
  115. row.AddCell().SetInt(winnerData.Num)
  116. }
  117. // 保存 Excel 文件
  118. outputFile, err := os.Create("exported_data.xlsx")
  119. if err != nil {
  120. log.Fatal(err)
  121. }
  122. defer outputFile.Close()
  123. err = file.Write(outputFile)
  124. if err != nil {
  125. log.Fatal(err)
  126. }
  127. fmt.Println("数据已成功导出到 exported_data.xlsx")
  128. }
  129. // exportPhone 根据企业名单,导出联系人电话
  130. func exportPhone() {
  131. Mgo := &mongodb.MongodbSim{
  132. //MongodbAddr: "172.17.189.140:27080",
  133. MongodbAddr: "127.0.0.1:27083",
  134. Size: 10,
  135. DbName: "mixdata",
  136. UserName: "SJZY_RWbid_ES",
  137. Password: "SJZY@B4i4D5e6S",
  138. Direct: true,
  139. }
  140. Mgo.InitPool()
  141. //
  142. username := "datascbi"
  143. password := "Da#Bi20221111SC"
  144. host := "127.0.0.1:4001"
  145. //host := "172.17.4.242:4000"
  146. database := "global_common_data"
  147. dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", username, password, host, database)
  148. // 连接到数据库
  149. db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
  150. if err != nil {
  151. log.Println("Failed to connect to database:", err)
  152. return
  153. }
  154. f, err := excelize.OpenFile("./河南省物业企业名单.xlsx")
  155. if err != nil {
  156. fmt.Println(err)
  157. return
  158. }
  159. defer func() {
  160. if err := f.Close(); err != nil {
  161. fmt.Println(err)
  162. }
  163. }()
  164. rows, err := f.GetRows("Sheet1")
  165. if err != nil {
  166. fmt.Println(err)
  167. return
  168. }
  169. type BaseInfo struct {
  170. Name string `json:"name"`
  171. NameId string `json:"name_id"`
  172. }
  173. type Contact struct {
  174. ContactName string `json:"contact_name"`
  175. ContactTel string `json:"contact_tel"`
  176. }
  177. for i := 1; i < len(rows); i++ {
  178. name := rows[i][1]
  179. if !strings.Contains(name, "公司") {
  180. continue
  181. }
  182. log.Println(i, "----", name)
  183. stds, _ := Mgo.FindOne("qyxy_std", map[string]interface{}{"company_name": name})
  184. var reportsMap = make([]string, 0)
  185. contactsMap := make([]string, 0)
  186. if reports, ok := (*stds)["annual_reports"]; ok {
  187. if rs, ok := reports.([]interface{}); ok {
  188. for _, v := range rs {
  189. if da, ok := v.(map[string]interface{}); ok {
  190. if util.ObjToString(da["operator_name"]) != "" && util.ObjToString(da["company_phone"]) != "" {
  191. tm := util.ObjToString(da["operator_name"]) + "_" + util.ObjToString(da["company_phone"])
  192. if !IsInStringArray(tm, reportsMap) {
  193. reportsMap = append(reportsMap, tm)
  194. }
  195. } else if util.ObjToString(da["company_phone"]) != "" {
  196. if !IsInStringArray(util.ObjToString(da["company_phone"]), reportsMap) {
  197. reportsMap = append(reportsMap, util.ObjToString(da["company_phone"]))
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. if len(reportsMap) > 0 {
  205. res := strings.Join(reportsMap, ",")
  206. f.SetCellValue("Sheet1", fmt.Sprintf("E%v", i+1), res)
  207. }
  208. baseinfo := BaseInfo{}
  209. db.Table("dws_f_ent_baseinfo").Select("name", "name_id").Where("name = ? ", name).Scan(&baseinfo)
  210. if baseinfo.NameId != "" {
  211. contacts := []Contact{}
  212. db.Table("dws_f_ent_contact").Select("contact_name", "contact_tel").Where("name_id = ? ", baseinfo.NameId).Scan(&contacts)
  213. if len(contacts) > 0 {
  214. for _, v := range contacts {
  215. if strings.Contains(v.ContactTel, ">") {
  216. continue
  217. }
  218. if v.ContactName != "" && v.ContactTel != "" {
  219. s := v.ContactName + "_" + v.ContactTel
  220. if !IsInStringArray(s, contactsMap) {
  221. contactsMap = append(contactsMap, s)
  222. }
  223. } else {
  224. if !IsInStringArray(v.ContactTel, contactsMap) {
  225. contactsMap = append(contactsMap, v.ContactTel)
  226. }
  227. }
  228. }
  229. }
  230. }
  231. if len(contactsMap) > 0 {
  232. res := strings.Join(contactsMap, ",")
  233. f.SetCellValue("Sheet1", fmt.Sprintf("F%v", i+1), res)
  234. }
  235. }
  236. f.Save()
  237. }
  238. // exportPhone 根据企业名单,导出联系人电话,只要抽取到的数据,联系人,联系电话 只要一个
  239. func exportPhone2() {
  240. Mgo := &mongodb.MongodbSim{
  241. //MongodbAddr: "172.17.189.140:27080",
  242. MongodbAddr: "127.0.0.1:27083",
  243. Size: 10,
  244. DbName: "mixdata",
  245. UserName: "SJZY_RWbid_ES",
  246. Password: "SJZY@B4i4D5e6S",
  247. Direct: true,
  248. }
  249. Mgo.InitPool()
  250. //
  251. username := "datascbi"
  252. password := "Da#Bi20221111SC"
  253. host := "127.0.0.1:4001"
  254. //host := "172.17.4.242:4000"
  255. database := "global_common_data"
  256. dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", username, password, host, database)
  257. // 连接到数据库
  258. db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
  259. if err != nil {
  260. log.Println("Failed to connect to database:", err)
  261. return
  262. }
  263. f, err := excelize.OpenFile("./河南省物业企业名单.xlsx")
  264. if err != nil {
  265. fmt.Println(err)
  266. return
  267. }
  268. defer func() {
  269. if err := f.Close(); err != nil {
  270. fmt.Println(err)
  271. }
  272. }()
  273. rows, err := f.GetRows("Sheet1")
  274. if err != nil {
  275. fmt.Println(err)
  276. return
  277. }
  278. type BaseInfo struct {
  279. Name string `json:"name"`
  280. NameId string `json:"name_id"`
  281. }
  282. type Contact struct {
  283. ContactName string `json:"contact_name"`
  284. ContactTel string `json:"contact_tel"`
  285. }
  286. for i := 1; i < len(rows); i++ {
  287. name := rows[i][1]
  288. if !strings.Contains(name, "公司") {
  289. continue
  290. }
  291. log.Println(i, "----", name)
  292. //contactsMap := make([]string, 0)
  293. //stds, _ := Mgo.FindOne("qyxy_std", map[string]interface{}{"company_name": name})
  294. //var reportsMap = make([]string, 0)
  295. //if reports, ok := (*stds)["annual_reports"]; ok {
  296. // if rs, ok := reports.([]interface{}); ok {
  297. // for _, v := range rs {
  298. // if da, ok := v.(map[string]interface{}); ok {
  299. // if util.ObjToString(da["operator_name"]) != "" && util.ObjToString(da["company_phone"]) != "" {
  300. // tm := util.ObjToString(da["operator_name"]) + "_" + util.ObjToString(da["company_phone"])
  301. // if !IsInStringArray(tm, reportsMap) {
  302. // reportsMap = append(reportsMap, tm)
  303. // }
  304. // } else if util.ObjToString(da["company_phone"]) != "" {
  305. // if !IsInStringArray(util.ObjToString(da["company_phone"]), reportsMap) {
  306. // reportsMap = append(reportsMap, util.ObjToString(da["company_phone"]))
  307. // }
  308. // }
  309. // }
  310. //
  311. // }
  312. // }
  313. //}
  314. //if len(reportsMap) > 0 {
  315. // res := strings.Join(reportsMap, ",")
  316. // f.SetCellValue("Sheet1", fmt.Sprintf("E%v", i+1), res)
  317. //}
  318. baseinfo := BaseInfo{}
  319. db.Table("dws_f_ent_baseinfo").Select("name", "name_id").Where("name = ? ", name).Scan(&baseinfo)
  320. if baseinfo.NameId != "" {
  321. contacts := []Contact{}
  322. db.Table("dws_f_ent_contact").Select("contact_name", "contact_tel").Where("name_id = ? ", baseinfo.NameId).Scan(&contacts)
  323. if len(contacts) > 0 {
  324. for _, v := range contacts {
  325. if strings.Contains(v.ContactTel, ">") {
  326. continue
  327. }
  328. if v.ContactName != "" && v.ContactTel != "" {
  329. f.SetCellValue("Sheet1", fmt.Sprintf("D%v", i+1), v.ContactName)
  330. f.SetCellValue("Sheet1", fmt.Sprintf("E%v", i+1), v.ContactTel)
  331. break
  332. }
  333. }
  334. }
  335. }
  336. }
  337. f.Save()
  338. }
  339. func test() {
  340. //Mgo := &mongodb.MongodbSim{
  341. // //MongodbAddr: "172.17.189.140:27080",
  342. // MongodbAddr: "192.168.3.166:27082",
  343. // Size: 10,
  344. // DbName: "majiajia",
  345. // //UserName: "SJZY_RWbid_ES",
  346. // //Password: "SJZY@B4i4D5e6S",
  347. // //Direct: true,
  348. //}
  349. //Mgo.InitPool()
  350. MgoP := &mongodb.MongodbSim{
  351. //MongodbAddr: "172.17.189.140:27080",
  352. MongodbAddr: "127.0.0.1:27080",
  353. Size: 10,
  354. DbName: "qfw",
  355. Direct: true,
  356. //UserName: "SJZY_RWbid_ES",
  357. //Password: "SJZY@B4i4D5e6S",
  358. }
  359. MgoP.InitPool()
  360. f, err := excelize.OpenFile("./河南物业.xlsx")
  361. if err != nil {
  362. fmt.Println(err)
  363. return
  364. }
  365. defer func() {
  366. if err := f.Close(); err != nil {
  367. fmt.Println(err)
  368. }
  369. }()
  370. rows, err := f.GetRows("Sheet1")
  371. if err != nil {
  372. fmt.Println(err)
  373. return
  374. }
  375. for i := 1; i < len(rows); i++ {
  376. id := rows[i][0]
  377. rs, _ := MgoP.FindById("projectset_20230904", id, nil)
  378. if rs == nil {
  379. continue
  380. }
  381. if phone, ok := (*rs)["buyer"]; ok {
  382. if util.ObjToString(phone) != "" {
  383. f.SetCellValue("Sheet1", fmt.Sprintf("K%v", i+1), phone)
  384. }
  385. }
  386. }
  387. f.Save()
  388. }
  389. // IsInStringArray 判断数组中是否存在字符串
  390. func IsInStringArray(str string, arr []string) bool {
  391. // 先对字符串数组进行排序
  392. sort.Strings(arr)
  393. // 使用二分查找算法查找字符串
  394. pos := sort.SearchStrings(arr, str)
  395. // 如果找到了则返回 true,否则返回 false
  396. return pos < len(arr) && arr[pos] == str
  397. }