main.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/garyburd/redigo/redis"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/bson/primitive"
  9. "go.mongodb.org/mongo-driver/mongo"
  10. "go.mongodb.org/mongo-driver/mongo/options"
  11. "log"
  12. mu "mfw/util"
  13. "net"
  14. elastic "qfw/common/src/qfw/util/elastic"
  15. "qfw/util"
  16. "regexp"
  17. "sort"
  18. "strings"
  19. "time"
  20. )
  21. var
  22. (
  23. Config = make(map[string]string)
  24. Fields []string
  25. SourceClient, FClient *mongo.Client
  26. RedisPool redis.Pool
  27. Addrs = make(map[string]interface{}, 0) //省市县
  28. udpclient mu.UdpClient //udp对象
  29. ElasticClientIndex, ElasticClientType string
  30. Reg_xing = regexp.MustCompile(`\*{1,}`)
  31. Reg_person = regexp.MustCompile("[\u4E00-\u9FA5\\s]+")
  32. Reg_tel = regexp.MustCompile(`^[0-9\-\s]*$`)
  33. )
  34. /**
  35. 新增
  36. 初始化
  37. */
  38. func init() {
  39. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  40. util.ReadConfig(&Config)
  41. log.Println(Config)
  42. Fields = []string{"_id", "contact", "partners", "business_scope", "company_address", "capital",
  43. "establish_date", "legal_person", "company_type", "district", "city", "province", "area_code", "credit_no",
  44. "company_name", "history_name", "topscopeclass", "wechat_accounts", "alias", "website", "report_websites"}
  45. var err error
  46. //mongo init
  47. SourceClient, err = mongo.NewClient(options.Client().ApplyURI("mongodb://" + Config["mgoinit"]).SetMaxPoolSize(20))
  48. if err != nil {
  49. log.Fatalln(err)
  50. }
  51. c1 := context.Background()
  52. err = SourceClient.Connect(c1)
  53. //defer SourceClient.Disconnect(c1)
  54. if err != nil {
  55. log.Fatalln(err)
  56. }
  57. FClient, err = mongo.NewClient(options.Client().ApplyURI("mongodb://" + Config["mgourl"]).SetMaxPoolSize(20))
  58. if err != nil {
  59. log.Fatalln(err)
  60. }
  61. cc := context.Background()
  62. err = FClient.Connect(cc)
  63. //defer FClient.Disconnect(cc)
  64. if err != nil {
  65. log.Fatalln(err)
  66. }
  67. //加载省市县代码
  68. cursor2, err := FClient.Database(Config["mgodb_extract_kf"]).Collection("address").Find(cc, bson.M{},
  69. options.Find().SetProjection(bson.M{"province": 1, "code": 1, "city": 1, "district": 1}))
  70. defer cursor2.Close(cc)
  71. defer FClient.Connect(cc)
  72. if err != nil {
  73. log.Fatalln(err)
  74. }
  75. for cursor2.Next(cc) {
  76. tmp := make(map[string]interface{})
  77. if err := cursor2.Decode(&tmp); err != nil {
  78. log.Println(err)
  79. continue
  80. } else {
  81. code := tmp["code"]
  82. if code != nil && strings.TrimSpace(code.(string)) != "" {
  83. Addrs[fmt.Sprint(code)] = tmp
  84. }
  85. }
  86. }
  87. log.Println(len(Addrs))
  88. //es.NewClient(es.SetURL(addrs...), es.SetMaxRetries(2), es.SetSniff(false))
  89. //es init
  90. elastic.InitElasticSize(Config["elasticsearch"], 10)
  91. //esConn := elastic.GetEsConn()
  92. //defer elastic.DestoryEsConn(esConn)
  93. //log.Println(esConn.Index().Index(Config["elasticsearch_index"]).Type(Config["elasticsearch_type"]).Id("123").BodyJson(map[string]interface{}{"testname":"六盘水市钟山开发区亿农科贸有限公司"}).Refresh(true).Do())
  94. //log.Println(esConn.Delete().Index(Config["elasticsearch_index"]).Type(Config["elasticsearch_type"]).Id("123").Refresh(true).Do())
  95. //if ESclient, err = elastic.NewClient(elastic.SetURL(Config["elasticsearch"]), elastic.SetHealthcheckTimeout(time.Minute)); err != nil {
  96. // log.Println(Config["elasticsearch"])
  97. // log.Fatalln("ElasticClient err:", err)
  98. //} else {
  99. // ElasticClientIndex = Config["elasticsearch_index"]
  100. // ElasticClientType = Config["elasticsearch_type"]
  101. //}
  102. //redis
  103. RedisPool = redis.Pool{
  104. MaxIdle: 10,
  105. IdleTimeout: 240 * time.Second,
  106. Dial: func() (redis.Conn, error) {
  107. conn, e := redis.Dial("tcp", Config["redis"])
  108. if e != nil {
  109. return conn, e
  110. }
  111. _, err = conn.Do("SELECT", "1")
  112. if err != nil {
  113. return nil, err
  114. }
  115. return conn, nil
  116. },}
  117. c := RedisPool.Get()
  118. if _, err := c.Do("PING"); err != nil {
  119. log.Fatalln(err)
  120. }
  121. defer c.Close()
  122. }
  123. func main() {
  124. //udp
  125. updport := Config["udpport"]
  126. udpclient = mu.UdpClient{Local: updport, BufSize: 1024}
  127. udpclient.Listen(processUdpMsg)
  128. log.Println("Udp服务监听", updport)
  129. go TimedTask() //定时任务
  130. c := make(chan int, 1)
  131. <-c
  132. }
  133. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  134. log.Println(act, string(data), ra)
  135. switch act {
  136. case mu.OP_TYPE_DATA: //上个节点的数据
  137. //从表中开始处理生成企业数据
  138. tmp := new(map[string]interface{})
  139. err := json.Unmarshal(data, &tmp)
  140. if err != nil {
  141. log.Println("err:", err)
  142. udpclient.WriteUdp([]byte("err:"+err.Error()), mu.OP_NOOP, ra)
  143. return
  144. } else if tmp != nil {
  145. udpclient.WriteUdp([]byte("ok,run"), mu.OP_NOOP, ra)
  146. go task(tmp)
  147. }
  148. case mu.OP_NOOP: //下个节点回应
  149. log.Println("发送成功", string(data))
  150. }
  151. }
  152. func task(mapinfo *map[string]interface{}) {
  153. defer util.Catch()
  154. gtid, lteid := util.ObjToString((*mapinfo)["gtid"]), util.ObjToString((*mapinfo)["lteid"])
  155. if gtid == "" || lteid == "" {
  156. log.Println(gtid, lteid, "参数错误")
  157. return
  158. }
  159. GId, err := primitive.ObjectIDFromHex(gtid)
  160. LtId, err2 := primitive.ObjectIDFromHex(lteid)
  161. if err != nil || err2 != nil {
  162. log.Println(gtid, lteid, "转换_id错误")
  163. return
  164. }
  165. //udp的id区间查询bidding 中标人 中标联系人 中标联系电话
  166. // topscopeclass项目类型-industry行业类型&&topscopeclass联系人项目类型
  167. // (area地区-province省份 city城市-city城市 district区县-district区县)
  168. // winneraddr-company_address企业地址
  169. cursor, err := SourceClient.Database(Config["mgodb_bidding"]).Collection(Config["mgodb_mgoinit_c"]).Find(context.TODO(), bson.M{
  170. "_id": bson.M{
  171. "$gte": GId,
  172. "$lte": LtId,
  173. },
  174. }, options.Find().SetProjection(bson.M{"winner": 1, "winnertel": 1, "winnerperson": 1,
  175. "topscopeclass": 1, "winneraddr": 1}))
  176. if err != nil {
  177. log.Println(err)
  178. return
  179. }
  180. for cursor.Next(context.TODO()) {
  181. tmp := map[string]interface{}{}
  182. if err := cursor.Decode(&tmp); err == nil {
  183. if tmp["winner"] == nil || tmp["winner"] == "" {
  184. continue
  185. }
  186. //redis查询是否存在
  187. rdb := RedisPool.Get()
  188. defer rdb.Close()
  189. if reply, err := redis.String(rdb.Do("GET", tmp["winner"])); err != nil {
  190. //redis不存在存到临时表,定时任务处理
  191. FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_new").InsertOne(context.TODO(), tmp)
  192. //log.Println(tmp, err)
  193. continue
  194. } else {
  195. //redis存在
  196. //log.Println(reply)
  197. //reply = "5e0316b998a9abaf6535df3d"
  198. id, err := primitive.ObjectIDFromHex(reply)
  199. if err != nil {
  200. log.Println("get redis id err:", err, tmp)
  201. continue
  202. }
  203. //拿到合并后的qyk
  204. oldTmp := make(map[string]interface{})
  205. err = FClient.Database(Config["mgodb_extract_kf"]).Collection(Config["mgo_qyk_c"]).
  206. FindOne(context.TODO(), bson.M{"_id": id}).Decode(&oldTmp)
  207. if err != nil {
  208. log.Println("qyk id err:", err, id)
  209. continue
  210. }
  211. //比较合并
  212. //行业类型
  213. tmpTopscopeclass := []string{}
  214. tmpTopscopeclassMap := make(map[string]bool)
  215. log.Println(tmp["_id"])
  216. if oldTmp["industry"] == nil {
  217. //log.Println(reflect.ValueOf(tmp["topscopeclass"]))
  218. if v, ok := tmp["topscopeclass"].(primitive.A); ok {
  219. for _, vv := range v {
  220. if vvv, ok := vv.(string); ok && len(vvv) > 1 {
  221. tmpTopscopeclassMap[vvv[:len(vvv)-1]] = true
  222. }
  223. }
  224. for k := range tmpTopscopeclassMap {
  225. tmpTopscopeclass = append(tmpTopscopeclass, k)
  226. }
  227. }
  228. } else {
  229. if v, ok := oldTmp["industry"].(primitive.A); ok {
  230. for _, vv := range v {
  231. if vvv, ok := vv.(string); ok {
  232. tmpTopscopeclassMap[vvv] = true
  233. }
  234. }
  235. }
  236. if v, ok := tmp["topscopeclass"].(primitive.A); ok {
  237. for _, vv := range v {
  238. if vvv, ok := vv.(string); ok && len(vvv) > 1 {
  239. tmpTopscopeclassMap[vvv[:len(vvv)-1]] = true
  240. }
  241. }
  242. for k := range tmpTopscopeclassMap {
  243. tmpTopscopeclass = append(tmpTopscopeclass, k)
  244. }
  245. }
  246. }
  247. sort.Strings(tmpTopscopeclass)
  248. oldTmp["industry"] = tmpTopscopeclass
  249. esId := oldTmp["_id"].(primitive.ObjectID).Hex()
  250. //更新行业类型
  251. if tmp["winnerperson"] == nil || tmp["winnerperson"] == "" || Reg_xing.MatchString(util.ObjToString(tmp["winnerperson"])) {
  252. oldTmp["updatatime"] = time.Now().Unix()
  253. //mongo更新
  254. FClient.Database(Config["mgodb_extract_kf"]).Collection(Config["mgo_qyk_c"]).
  255. UpdateOne(context.TODO(), bson.M{"_id": oldTmp["_id"]}, bson.M{"$set": oldTmp})
  256. //es更新
  257. delete(oldTmp, "_id")
  258. esConn := elastic.GetEsConn()
  259. defer elastic.DestoryEsConn(esConn)
  260. esConn.Update().Index(Config["elasticsearch_index"]).Type(Config["elasticsearch_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do()
  261. //log.Println( err2,err3)
  262. continue
  263. }
  264. //联系方式合并
  265. var tmpperson, winnertel string
  266. tmpperson = tmp["winnerperson"].(string)
  267. if tmp["winnertel"] == nil || tmp["winnertel"]==""{
  268. winnertel = ""
  269. }else {
  270. if Reg_xing.MatchString(util.ObjToString(tmp["winnertel"]))||!Reg_tel.MatchString(util.ObjToString(tmp["winnertel"])){
  271. winnertel = ""
  272. }else {
  273. winnertel = util.ObjToString(tmp["winnertel"])
  274. }
  275. }
  276. contactMaps := make([]interface{}, 0)
  277. if oldTmp["contact"] == nil {
  278. tmpContact := make(map[string]interface{})
  279. tmpContact["contact_person"] = tmpperson
  280. tmpContact["contact_type"] = "项目联系人"
  281. tmpContact["phone"] = winnertel
  282. tmpContact["topscopeclass"] = strings.Join(tmpTopscopeclass, ";")
  283. tmpContact["updatetime"] = time.Now().Unix()
  284. contactMaps = append(contactMaps, tmpContact)
  285. } else {
  286. //对比前四项,相等丢弃
  287. if v, ok := oldTmp["contact"].(primitive.A); ok {
  288. var isNotUpdate bool
  289. for _, vv := range v {
  290. if vvv, ok := vv.(map[string]interface{}); ok {
  291. if vvv["contact_person"] == tmpperson && vvv["contact_type"] == "项目联系人" &&
  292. vvv["phone"] == winnertel && vvv["topscopeclass"] == strings.Join(tmpTopscopeclass, ";") {
  293. isNotUpdate = true
  294. vvv["updatetime"] = time.Now().Unix()
  295. }
  296. contactMaps = append(contactMaps, vvv)
  297. }
  298. }
  299. if !isNotUpdate {
  300. vvv := make(map[string]interface{})
  301. vvv["contact_person"] = tmp["winnerperson"]
  302. vvv["contact_type"] = "项目联系人"
  303. vvv["phone"] = winnertel
  304. vvv["topscopeclass"] = strings.Join(tmpTopscopeclass, ";")
  305. vvv["updatetime"] = time.Now().Unix()
  306. contactMaps = append(contactMaps, vvv)
  307. }
  308. }
  309. }
  310. oldTmp["contact"] = contactMaps
  311. //mongo更新
  312. oldTmp["updatatime"] = time.Now().Unix()
  313. FClient.Database(Config["mgodb_extract_kf"]).Collection(Config["mgo_qyk_c"]).
  314. UpdateOne(context.TODO(), bson.M{"_id": oldTmp["_id"]}, bson.M{"$set": oldTmp})
  315. //es更新
  316. delete(oldTmp, "_id")
  317. esConn := elastic.GetEsConn()
  318. defer elastic.DestoryEsConn(esConn)
  319. esConn.Update().Index(Config["elasticsearch_index"]).Type(Config["elasticsearch_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do()
  320. //log.Println( err2,err3)
  321. }
  322. } else {
  323. log.Println(tmp)
  324. continue
  325. }
  326. }
  327. defer cursor.Close(context.TODO())
  328. //tmps := make([]interface{}, 0)
  329. //num, snum := 0, 0
  330. //for k := range keys {
  331. // //if num == 6 {
  332. // // return
  333. // //}
  334. // tmp := make(map[string]interface{})
  335. // err := Client.Database("enterprise").Collection("qyxy").FindOne(context.TODO(), bson.M{"company_name": k}).Decode(&tmp)
  336. // if err != nil {
  337. // //log.Println(k, err)
  338. // continue
  339. // }
  340. // if tmp["credit_no"] != nil {
  341. // if credit_no, ok := tmp["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
  342. // len(strings.TrimSpace(credit_no)) > 8 {
  343. // dataNo := strings.TrimSpace(credit_no)[2:8]
  344. // if Addrs[dataNo] != nil {
  345. // if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  346. // if tmp["province"] == nil || tmp["province"] == "" {
  347. // tmp["province"] = v["province"]
  348. // }
  349. // tmp["city"] = v["city"]
  350. // tmp["district"] = v["district"]
  351. //
  352. // }
  353. // }
  354. // }
  355. // }
  356. // contacts := make([]map[string]interface{}, 0)
  357. // contact := make(map[string]interface{}, 0)
  358. // if tmp["legal_person"] != nil {
  359. // contact["contact_person"] = tmp["legal_person"] //联系人
  360. // } else {
  361. // contact["contact_person"] = "" //联系人
  362. // }
  363. // contact["contact_type"] = "法定代表人" //法定代表人
  364. // //log.Println(1)
  365. // if tmp["annual_reports"] != nil {
  366. // bytes, err := json.Marshal(tmp["annual_reports"])
  367. // if err != nil {
  368. // log.Println("annual_reports err:", err)
  369. // }
  370. // //log.Println(2, string(bytes))
  371. // phonetmp := make([]map[string]interface{}, 0)
  372. // err = json.Unmarshal(bytes, &phonetmp)
  373. // if err != nil {
  374. // log.Println("Unmarshal err:", err)
  375. // }
  376. // //log.Println(44, err)
  377. // for _, vv := range phonetmp {
  378. // if vv["company_phone"] != nil {
  379. // if vv["company_phone"] == "" {
  380. // continue
  381. // } else {
  382. // contact["phone"] = vv["company_phone"] //联系电话
  383. // break
  384. // }
  385. // } else {
  386. // contact["phone"] = "" //联系电话
  387. // }
  388. //
  389. // }
  390. // }
  391. // //log.Println(k, contact["phone"], tmp["_id"])
  392. // //time.Sleep(10 * time.Second)
  393. // if contact["phone"] == nil {
  394. // contact["phone"] = "" //联系电话
  395. // }
  396. // contact["topscopeclass"] = "企业公示" //项目类型
  397. // contact["updatetime"] = time.Now().Unix() //更新时间
  398. // contacts = append(contacts, contact)
  399. // tmp["contact"] = contacts
  400. //
  401. // savetmp := make(map[string]interface{}, 0)
  402. // //字段处理
  403. // for _, sk := range Fields {
  404. // if sk == "establish_date" { //成立日期
  405. // if tmp[sk] != nil {
  406. // savetmp[sk] = tmp[sk].(primitive.DateTime).Time().UTC().Unix()
  407. // continue
  408. // }
  409. // } else if sk == "capital" { //注册资本
  410. // //log.Println(sk, tmp[sk])
  411. // savetmp[sk] = ObjToMoney([]interface{}{tmp[sk], ""})[0]
  412. // continue
  413. // } else if sk == "partners" { //股东及出资信息
  414. // //log.Println(sk, tmp[sk], )
  415. // //fmt.Println(reflect.TypeOf(tmp[sk]))
  416. // if tmp[sk] != nil {
  417. // if ppms, ok := tmp[sk].(primitive.A); ok {
  418. // for i, _ := range ppms {
  419. // if ppms[i].(map[string]interface{})["stock_type"] != nil {
  420. // ppms[i].(map[string]interface{})["stock_type"] = "企业公示"
  421. // }
  422. // delete(ppms[i].(map[string]interface{}), "identify_type")
  423. // }
  424. // savetmp[sk] = ppms
  425. // continue
  426. // }
  427. // }
  428. // } else if sk == "_id" { //_id备份企业库
  429. // savetmp["tmp"+sk] = tmp[sk]
  430. // continue
  431. // }
  432. // if tmp[sk] == nil && sk != "history_name" && sk != "establish_date" && sk != "capital" && sk != "partners" && sk != "contact" && sk != "wechat_accounts" {
  433. // savetmp[sk] = ""
  434. // } else {
  435. // if sk == "wechat_accounts" { //微信公众号
  436. // if savetmp[sk] == nil {
  437. // //TODO 微信公众号取值未确认
  438. // savetmp[sk] = []string{}
  439. // }
  440. // continue
  441. // } else if sk == "website" { //网址
  442. // //TODO 网址取值未确认
  443. // continue
  444. // }
  445. // savetmp[sk] = tmp[sk]
  446. // }
  447. // }
  448. // savetmp["alias"] = "" //别名
  449. // tmps = append(tmps, savetmp)
  450. // num++
  451. // snum++
  452. // if snum >= 300 {
  453. // _, err := Client.Database("extract_v3").Collection("enterprise_qyxy").InsertMany(context.TODO(), tmps)
  454. // if err != nil {
  455. // log.Println("save:", err)
  456. // continue
  457. // } else {
  458. // log.Println(num)
  459. // tmps = []interface{}{}
  460. // snum = 0
  461. // }
  462. // }
  463. //}
  464. //if len(tmps) > 0 {
  465. // result, err := Client.Database("extract_v3").Collection("enterprise_qyxy").InsertMany(context.TODO(), tmps)
  466. // if err != nil {
  467. // log.Println("save over:", err)
  468. // } else {
  469. // log.Println("last save num:", len(result.InsertedIDs))
  470. // }
  471. //}
  472. }