main.go 15 KB

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