main.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. es "gopkg.in/olivere/elastic.v1"
  12. "log"
  13. mu "mfw/util"
  14. "net"
  15. "qfw/common/src/qfw/util/elastic"
  16. "qfw/util"
  17. "regexp"
  18. "strconv"
  19. "sort"
  20. "strings"
  21. "time"
  22. )
  23. var
  24. (
  25. Config = make(map[string]string)
  26. Fields []string
  27. SourceClient, FClient *mongo.Client
  28. RedisPool redis.Pool
  29. Addrs = make(map[string]interface{}, 0) //省市县
  30. udpclient mu.UdpClient //udp对象
  31. ElasticClientIndex, ElasticClientType string
  32. Reg_xing = regexp.MustCompile(`\*{1,}`)
  33. Reg_person = regexp.MustCompile("[\u4E00-\u9FA5\\s]+")
  34. Reg_tel = regexp.MustCompile(`^[0-9\-\s]*$`)
  35. EsConn *es.Client
  36. Updport int
  37. )
  38. /**
  39. 新增
  40. 初始化
  41. */
  42. func init() {
  43. log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
  44. util.ReadConfig(&Config)
  45. log.Println(Config)
  46. Fields = []string{"_id", "contact", "partners", "business_scope", "company_address", "capital",
  47. "establish_date", "legal_person", "company_type", "district", "city", "province", "area_code", "credit_no",
  48. "company_name", "history_name", "topscopeclass", "wechat_accounts", "alias", "website", "report_websites"}
  49. var err error
  50. pool_size, _ := strconv.Atoi(Config["pool_size"])
  51. //mongo init
  52. SourceClient, err = mongo.NewClient(options.Client().ApplyURI("mongodb://" + Config["mgoinit"]).SetMaxPoolSize(uint16(pool_size)).SetMaxConnIdleTime(time.Hour * 24))
  53. if err != nil {
  54. log.Fatalln(err)
  55. }
  56. c1, _ := context.WithTimeout(context.Background(), 9999*time.Hour)
  57. err = SourceClient.Connect(c1)
  58. //defer SourceClient.Disconnect(c1)
  59. if err != nil {
  60. log.Fatalln(err)
  61. }
  62. FClient, err = mongo.NewClient(options.Client().ApplyURI("mongodb://" + Config["mgourl"]).SetMaxPoolSize(uint16(pool_size)).SetMaxConnIdleTime(time.Hour * 999999))
  63. if err != nil {
  64. log.Fatalln(err)
  65. }
  66. err = FClient.Connect(c1)
  67. //defer FClient.Disconnect(cc)
  68. if err != nil {
  69. log.Fatalln(err)
  70. }
  71. //加载省市县代码
  72. cursor2, err := FClient.Database(Config["mgodb_extract_kf"]).Collection("address").Find(c1, bson.M{},
  73. options.Find().SetProjection(bson.M{"province": 1, "code": 1, "city": 1, "district": 1}))
  74. //defer FClient.Connect(cc)
  75. if err != nil {
  76. log.Fatalln(err)
  77. }
  78. for cursor2.Next(context.TODO()) {
  79. if err := cursor2.Err(); err != nil {
  80. log.Println("cursor.Err();", err)
  81. }
  82. tmp := make(map[string]interface{})
  83. if err := cursor2.Decode(&tmp); err != nil {
  84. log.Println(err)
  85. continue
  86. } else {
  87. code := tmp["code"]
  88. if code != nil && strings.TrimSpace(code.(string)) != "" {
  89. Addrs[fmt.Sprint(code)] = tmp
  90. }
  91. }
  92. }
  93. defer cursor2.Close(context.TODO())
  94. log.Println(len(Addrs))
  95. //es.NewClient(es.SetURL(addrs...), es.SetMaxRetries(2), es.SetSniff(false))
  96. //es init
  97. elastic.InitElasticSize(Config["elasticsearch"], 50)
  98. EsConn = elastic.GetEsConn()
  99. defer elastic.DestoryEsConn(EsConn)
  100. //redis
  101. RedisPool = redis.Pool{
  102. MaxIdle: 50,
  103. IdleTimeout: 10 * time.Second,
  104. Dial: func() (redis.Conn, error) {
  105. conn, e := redis.Dial("tcp", Config["redis"])
  106. if e != nil {
  107. return conn, e
  108. }
  109. _, err = conn.Do("SELECT", "1")
  110. if err != nil {
  111. return nil, err
  112. }
  113. return conn, nil
  114. },}
  115. c := RedisPool.Get()
  116. if _, err := c.Do("PING"); err != nil {
  117. log.Fatalln("redis err:", err)
  118. }
  119. c.Close()
  120. }
  121. func main() {
  122. //udp
  123. updport := Config["udpport"]
  124. Updport, _ = strconv.Atoi(Config["port"])
  125. udpclient = mu.UdpClient{Local: updport, BufSize: 1024}
  126. udpclient.Listen(processUdpMsg)
  127. log.Println("Udp服务监听", updport)
  128. log.Println("发送端口port:",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.Background(), 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}).SetSort(bson.M{"_id": 1}).SetBatchSize(60000000).SetMaxTime(time.Hour*24))
  176. if err != nil {
  177. log.Println(err)
  178. return
  179. }
  180. overid := ""
  181. c2, _ := context.WithTimeout(context.Background(), 9999*time.Hour)
  182. for cursor.Next(c2) {
  183. if err := cursor.Err(); err != nil {
  184. log.Println("cursor.Err();", err)
  185. }
  186. tmp := map[string]interface{}{}
  187. if err := cursor.Decode(&tmp); err == nil {
  188. if tmp["winner"] == nil || tmp["winner"] == "" {
  189. continue
  190. }
  191. //redis查询是否存在
  192. rdb := RedisPool.Get()
  193. if reply, err := redis.String(rdb.Do("GET", tmp["winner"])); err != nil {
  194. //redis不存在存到临时表,定时任务处理
  195. if _, err := FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_new").InsertOne(context.TODO(), tmp); err != nil {
  196. log.Println(err, tmp)
  197. }
  198. //log.Println("get redis id err:定时任务处理", err, tmp)
  199. if err := rdb.Close(); err != nil {
  200. log.Println(err)
  201. }
  202. continue
  203. } else {
  204. //log.Println("redis get :", reply)
  205. //redis存在
  206. //log.Println(reply)
  207. //reply = "5e0316b998a9abaf6535df3d"
  208. id, err := primitive.ObjectIDFromHex(reply)
  209. if err != nil {
  210. log.Println("get redis id Hex err:", err, tmp)
  211. if err := rdb.Close(); err != nil {
  212. log.Println(err)
  213. }
  214. continue
  215. }
  216. if err := rdb.Close(); err != nil {
  217. log.Println(err)
  218. }
  219. //拿到合并后的qyk
  220. oldTmp := make(map[string]interface{})
  221. err = FClient.Database(Config["mgodb_extract_kf"]).Collection(Config["mgo_qyk_c"]).
  222. FindOne(context.TODO(), bson.M{"_id": id}).Decode(&oldTmp)
  223. if err != nil {
  224. log.Println("qyk id err:", err, id)
  225. continue
  226. }
  227. //比较合并
  228. //行业类型
  229. tmpTopscopeclass := []string{}
  230. tmpTopscopeclassMap := make(map[string]bool)
  231. log.Println(tmp["_id"])
  232. overid = tmp["_id"].(primitive.ObjectID).Hex()
  233. if oldTmp["industry"] == nil {
  234. //log.Println(reflect.ValueOf(tmp["topscopeclass"]))
  235. if v, ok := tmp["topscopeclass"].(primitive.A); ok {
  236. for _, vv := range v {
  237. if vvv, ok := vv.(string); ok && len(vvv) > 1 {
  238. tmpTopscopeclassMap[vvv[:len(vvv)-1]] = true
  239. }
  240. }
  241. for k := range tmpTopscopeclassMap {
  242. tmpTopscopeclass = append(tmpTopscopeclass, k)
  243. }
  244. }
  245. } else {
  246. if v, ok := oldTmp["industry"].(primitive.A); ok {
  247. for _, vv := range v {
  248. if vvv, ok := vv.(string); ok {
  249. tmpTopscopeclassMap[vvv] = true
  250. }
  251. }
  252. }
  253. if v, ok := tmp["topscopeclass"].(primitive.A); ok {
  254. for _, vv := range v {
  255. if vvv, ok := vv.(string); ok && len(vvv) > 1 {
  256. tmpTopscopeclassMap[vvv[:len(vvv)-1]] = true
  257. }
  258. }
  259. for k := range tmpTopscopeclassMap {
  260. tmpTopscopeclass = append(tmpTopscopeclass, k)
  261. }
  262. }
  263. }
  264. sort.Strings(tmpTopscopeclass)
  265. oldTmp["industry"] = tmpTopscopeclass
  266. esId := oldTmp["_id"].(primitive.ObjectID).Hex()
  267. //更新行业类型
  268. if tmp["winnerperson"] == nil || tmp["winnerperson"] == "" || Reg_xing.MatchString(util.ObjToString(tmp["winnerperson"])) {
  269. oldTmp["updatatime"] = time.Now().Unix()
  270. //mongo更新
  271. if _, err := FClient.Database(Config["mgodb_extract_kf"]).Collection(Config["mgo_qyk_c"]).
  272. UpdateOne(context.TODO(), bson.M{"_id": oldTmp["_id"]}, bson.M{"$set": oldTmp}); err != nil {
  273. log.Println("mongo更新err:", err)
  274. }
  275. //es更新
  276. delete(oldTmp, "_id")
  277. //esConn := elastic.GetEsConn()
  278. //defer elastic.DestoryEsConn(esConn)
  279. if _, err := EsConn.Update().Index(Config["elasticsearch_index"]).Type(Config["elasticsearch_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do(); err != nil {
  280. log.Println("update es err:", err)
  281. }
  282. //log.Println( err2,err3)
  283. continue
  284. }
  285. //联系方式合并
  286. var tmpperson, winnertel string
  287. tmpperson = tmp["winnerperson"].(string)
  288. if tmp["winnertel"] == nil || tmp["winnertel"] == "" {
  289. winnertel = ""
  290. } else {
  291. if Reg_xing.MatchString(util.ObjToString(tmp["winnertel"])) || !Reg_tel.MatchString(util.ObjToString(tmp["winnertel"])) {
  292. winnertel = ""
  293. } else {
  294. winnertel = util.ObjToString(tmp["winnertel"])
  295. }
  296. }
  297. contactMaps := make([]interface{}, 0)
  298. if oldTmp["contact"] == nil {
  299. tmpContact := make(map[string]interface{})
  300. tmpContact["contact_person"] = tmpperson
  301. tmpContact["contact_type"] = "项目联系人"
  302. tmpContact["phone"] = winnertel
  303. tmpContact["topscopeclass"] = strings.Join(tmpTopscopeclass, ";")
  304. tmpContact["updatetime"] = time.Now().Unix()
  305. contactMaps = append(contactMaps, tmpContact)
  306. } else {
  307. //对比前四项,相等丢弃
  308. if v, ok := oldTmp["contact"].(primitive.A); ok {
  309. var isNotUpdate bool
  310. for _, vv := range v {
  311. if vvv, ok := vv.(map[string]interface{}); ok {
  312. if vvv["contact_person"] == tmpperson && vvv["contact_type"] == "项目联系人" &&
  313. vvv["phone"] == winnertel && vvv["topscopeclass"] == strings.Join(tmpTopscopeclass, ";") {
  314. isNotUpdate = true
  315. vvv["updatetime"] = time.Now().Unix()
  316. }
  317. contactMaps = append(contactMaps, vvv)
  318. }
  319. }
  320. if !isNotUpdate {
  321. vvv := make(map[string]interface{})
  322. vvv["contact_person"] = tmp["winnerperson"]
  323. vvv["contact_type"] = "项目联系人"
  324. vvv["phone"] = winnertel
  325. vvv["topscopeclass"] = strings.Join(tmpTopscopeclass, ";")
  326. vvv["updatetime"] = time.Now().Unix()
  327. contactMaps = append(contactMaps, vvv)
  328. }
  329. }
  330. }
  331. oldTmp["contact"] = contactMaps
  332. //mongo更新
  333. oldTmp["updatatime"] = time.Now().Unix()
  334. if _, err := FClient.Database(Config["mgodb_extract_kf"]).Collection(Config["mgo_qyk_c"]).
  335. UpdateOne(context.TODO(), bson.M{"_id": oldTmp["_id"]}, bson.M{"$set": oldTmp}); err != nil {
  336. log.Println("mongo更新 err :", err)
  337. }
  338. //es更新
  339. delete(oldTmp, "_id")
  340. //esConn := elastic.GetEsConn()
  341. //defer elastic.DestoryEsConn(esConn)
  342. if _, err := EsConn.Update().Index(Config["elasticsearch_index"]).Type(Config["elasticsearch_type"]).Id(esId).Doc(oldTmp).Refresh(true).Do(); err != nil {
  343. log.Println("EsConn err :", err)
  344. }
  345. //log.Println( err2,err3)
  346. }
  347. } else {
  348. log.Println(tmp)
  349. continue
  350. }
  351. }
  352. defer cursor.Close(c2)
  353. log.Println("合并执行完成", gtid, lteid ,overid)
  354. if gtid != lteid{
  355. by, _ := json.Marshal(map[string]interface{}{
  356. "gtid": overid,
  357. "lteid": lteid,
  358. "stype": "",
  359. })
  360. if e := udpclient.WriteUdp(by, mu.OP_TYPE_DATA, &net.UDPAddr{
  361. IP: net.ParseIP("127.0.0.1"),
  362. Port: Updport,
  363. });e != nil{
  364. log.Println(e)
  365. }
  366. log.Println("重新发送udp:",string(by))
  367. }
  368. log.Println("合并执行完成 ok", gtid, lteid,overid)
  369. }