main.go 15 KB

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