timedTaskBuyer.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/garyburd/redigo/redis"
  6. "gopkg.in/mgo.v2/bson"
  7. "log"
  8. util2 "mfw/util"
  9. "net"
  10. "qfw/util"
  11. "sort"
  12. "strings"
  13. "time"
  14. "unicode/utf8"
  15. )
  16. //之前main方法,只更新
  17. func TaskBuyer(mapinfo *map[string]interface{}) {
  18. defer util.Catch()
  19. gtid, lteid := util.ObjToString((*mapinfo)["gtid"]), util.ObjToString((*mapinfo)["lteid"])
  20. if gtid == "" || lteid == "" {
  21. log.Println(gtid, lteid, "参数错误")
  22. return
  23. }
  24. var GId, LtId bson.ObjectId
  25. if bson.IsObjectIdHex(gtid) && bson.IsObjectIdHex(lteid) {
  26. GId = bson.ObjectIdHex(gtid)
  27. LtId = bson.ObjectIdHex(lteid)
  28. } else {
  29. log.Println(gtid, lteid, "不是Objectid,转换_id错误", gtid, lteid)
  30. return
  31. }
  32. //timenow:=time.Now().Unix()
  33. //udp的id区间查询bidding 中标人 中标联系人 中标联系电话
  34. // topscopeclass项目类型-industry行业类型&&topscopeclass联系人项目类型
  35. // (area地区-province省份 city城市-city城市 district区县-district区县)
  36. // buyeraddr-company_address企业地址
  37. SourceClientcc := SourceClient.GetMgoConn(8640000)
  38. cursor := SourceClientcc.DB(Config["mgodb_bidding"]).C(Config["mgodb_mgoinit_c"]).Find(bson.M{
  39. "_id": bson.M{
  40. "$gte": GId,
  41. "$lte": LtId,
  42. },
  43. }).Select(bson.M{"buyer": 1, "buyertel": 1, "buyerperson": 1, "topscopeclass": 1,
  44. "buyeraddr": 1, "buyerclass": 1}).Iter()
  45. if cursor.Err() != nil {
  46. SourceClient.DestoryMongoConn(SourceClientcc)
  47. log.Println(cursor.Err())
  48. return
  49. }
  50. //判断是否是存量,是存量走Redis遍历
  51. if v, ok := (*mapinfo)["data_info"].(string); ok && v == "save" {
  52. //存量处理
  53. conn := HisRedisPool.Conn()
  54. defer conn.Close()
  55. //选择redis db
  56. conn.Select(redis_buyer_db)
  57. //遍历bidding表保存到redis
  58. //key:企业名 value:json结构体{"buyer": 1, "buyertel": 1, "buyerperson": 1,"topscopeclass": 1, "buyeraddr": 1,"_id":1}
  59. tmp := make(map[string]interface{})
  60. var num int
  61. var tmpRangeId string
  62. for cursor.Next(&tmp) {
  63. num++
  64. mgoId := tmp["_id"].(bson.ObjectId).Hex()
  65. tmpRangeId = mgoId
  66. buyer, ok := tmp["buyer"].(string)
  67. if !ok || utf8.RuneCountInString(buyer) < 4 {
  68. continue
  69. }
  70. //判断redis key是否存在
  71. e_num := conn.Exists(buyer).Val()
  72. //获取字符串_id
  73. //替换_id
  74. tmp["_id"] = mgoId
  75. //创建value数组
  76. tmps := make([]map[string]interface{}, 0)
  77. if e_num > 0 {
  78. //存量redis的key存在,累加更新
  79. bytes, _ := conn.Get(buyer).Bytes()
  80. json.Unmarshal(bytes, &tmps)
  81. }
  82. tmps = append(tmps, tmp)
  83. bytes, _ := json.Marshal(tmps)
  84. //存量redis的key不存在,新增 key :企业名 val :[]map
  85. if err := conn.Set(buyer, string(bytes), 0).Err(); err != nil {
  86. log.Println(err)
  87. }
  88. }
  89. log.Println("存量buyer mongo遍历完成:",num)
  90. if tmpRangeId != lteid{
  91. by, _ := json.Marshal(map[string]interface{}{
  92. "gtid": tmpRangeId,
  93. "lteid": lteid,
  94. "data_info":"save",
  95. "stype": "buyer",
  96. })
  97. if e := udpclient.WriteUdp(by, util2.OP_TYPE_DATA, &net.UDPAddr{
  98. IP: net.ParseIP("127.0.0.1"),
  99. Port: Updport,
  100. }); e != nil {
  101. log.Println(e)
  102. }
  103. SourceClient.DestoryMongoConn(SourceClientcc)
  104. return
  105. }
  106. SourceClient.DestoryMongoConn(SourceClientcc)
  107. //遍历redis
  108. if scan := conn.Scan(0, "", 100); scan.Err() != nil {
  109. log.Println(scan.Err())
  110. return
  111. } else {
  112. iterator := scan.Iterator()
  113. for iterator.Next() {
  114. redisCName := iterator.Val() //redis key 企业名
  115. redisvalueBytes, _ := conn.Get(redisCName).Bytes() //redis val []数组
  116. rValuesMaps := make([]map[string]interface{}, 0)
  117. json.Unmarshal(redisvalueBytes, &rValuesMaps)
  118. //redis查询是否存在
  119. rdb := RedisPool.Get()
  120. rdb.Do("SELECT", redis_buyer_db)
  121. if reply, err := redis.String(rdb.Do("GET", redisCName)); err != nil {
  122. //redis不存在,存到临时表,定时任务处理
  123. for _, vmap := range rValuesMaps {
  124. vmap["_id"] = bson.ObjectIdHex(vmap["_id"].(string))
  125. if errb := FClient.SaveByOriID(Config["mgo_qyk_c_b_new"], vmap); !errb {
  126. log.Println("存量 FClient.Save err", errb, vmap)
  127. }
  128. }
  129. //log.Println("get redis id err:定时任务处理", err, tmp)
  130. if err := rdb.Close(); err != nil {
  131. log.Println("存量", err)
  132. }
  133. continue
  134. } else {
  135. //redis存在更新合并
  136. if err := rdb.Close(); err != nil {
  137. log.Println(err)
  138. }
  139. //拿到合并后的qyk
  140. FClient.DbName = Config["mgodb_extract_kf"]
  141. oldTmp, b := FClient.FindById(Config["mgo_qyk_buyer"], reply, nil)
  142. if !b || (*oldTmp) == nil|| reply==""||(*oldTmp)["_id"]==nil{
  143. log.Println(redisCName, "存量 redis id 不存在", reply,"数据:",oldTmp)
  144. continue
  145. }
  146. tmpTopscopeclass := []string{}
  147. tmpTopscopeclassMap := make(map[string]bool)
  148. for _, rvaluemaps := range rValuesMaps {
  149. if tclasss, ok := rvaluemaps["topscopeclass"].([]string); ok {
  150. for _, vv := range tclasss {
  151. if len(vv) > 1 {
  152. tmpTopscopeclassMap[vv[:len(vv)-1]] = true
  153. }
  154. }
  155. }
  156. }
  157. for k := range tmpTopscopeclassMap {
  158. tmpTopscopeclass = append(tmpTopscopeclass, k)
  159. }
  160. sort.Strings(tmpTopscopeclass)
  161. esId := (*oldTmp)["_id"].(bson.ObjectId).Hex()
  162. //更新buyerclass合并
  163. if tmp["buyerclass"] == nil || tmp["buyerclass"] == "" {
  164. //无值,不更新
  165. } else {
  166. var buyerclass_new, buyerclass_old string
  167. buyerclass_new = tmp["buyerclass"].(string)
  168. buyerclass_old = (*oldTmp)["buyerclass"].(string)
  169. if buyerclass_old == "" {
  170. (*oldTmp)["buyerclass"] = buyerclass_new
  171. } else {
  172. if buyerclass_new != buyerclass_old {
  173. if !strings.Contains(buyerclass_old, buyerclass_new) {
  174. (*oldTmp)["buyerclass"] = buyerclass_old + "," + buyerclass_new //采购单位类型
  175. }
  176. }
  177. }
  178. }
  179. //联系方式合并
  180. contactMaps := make([]interface{}, 0)
  181. if (*oldTmp)["contact"] != nil {
  182. //直接添加联系人,不再判断
  183. if v, ok := (*oldTmp)["contact"].([]interface{}); ok {
  184. contactMaps = append(contactMaps, v...)
  185. }
  186. }
  187. //遍历redis value联系人
  188. for _, rvmap := range rValuesMaps {
  189. var tmpperson, buyertel string
  190. if rvmapperson, ok := rvmap["buyerperson"].(string); ok && utf8.RuneCountInString(rvmapperson)>=2 && rvmapperson != "" {
  191. tmpperson = rvmapperson
  192. } else {
  193. continue
  194. }
  195. if rvmapwintel, ok := rvmap["buyertel"].(string); ok {
  196. buyertel = rvmapwintel
  197. } else {
  198. buyertel = ""
  199. }
  200. if Reg_xing.MatchString(buyertel) || !Reg_tel.MatchString(buyertel) {
  201. buyertel = ""
  202. }
  203. tmpContact := make(map[string]interface{})
  204. tmpContact["infoid"] = rvmap["_id"]
  205. tmpContact["contact_person"] = tmpperson
  206. tmpContact["contact_type"] = "项目联系人"
  207. tmpContact["phone"] = buyertel
  208. tmpclass := make([]string, 0)
  209. if tclasss, ok := rvmap["topscopeclass"].([]string); ok {
  210. for _, vv := range tclasss {
  211. if len(vv) > 1 {
  212. tmpclass = append(tmpclass, vv[:len(vv)-1])
  213. }
  214. }
  215. }
  216. tmpContact["topscopeclass"] = strings.Join(tmpclass, ";")
  217. tmpContact["updatetime"] = time.Now().Unix()
  218. contactMaps = append(contactMaps, tmpContact)
  219. }
  220. (*oldTmp)["contact"] = contactMaps
  221. //mongo更新
  222. (*oldTmp)["updatatime"] = time.Now().Unix()
  223. if !FClient.UpdateById(Config["mgo_qyk_buyer"], esId, bson.M{"$set": oldTmp}) {
  224. log.Println("存量 mongo更新 err", esId, oldTmp)
  225. }
  226. //es更新
  227. delete((*oldTmp), "_id")
  228. }
  229. }
  230. }
  231. log.Println("存量历史合并执行完成 ok", gtid, lteid)
  232. } else {
  233. //增量处理
  234. overid := gtid
  235. tmp := map[string]interface{}{}
  236. for cursor.Next(&tmp) {
  237. overid = AddBuyer(overid, tmp)
  238. }
  239. SourceClient.DestoryMongoConn(SourceClientcc)
  240. log.Println("增量合并执行完成 ok", gtid, lteid, overid)
  241. //发送udp 更新es段
  242. //nextNode("buyerent",timenow)
  243. }
  244. }
  245. //增量
  246. func AddBuyer(overid string, tmp map[string]interface{}) string {
  247. overid = tmp["_id"].(bson.ObjectId).Hex()
  248. buyer, ok := tmp["buyer"].(string)
  249. if !ok || utf8.RuneCountInString(buyer) < 4 {
  250. return overid
  251. }
  252. //redis查询是否存在
  253. rdb := RedisPool.Get()
  254. rdb.Do("SELECT", redis_buyer_db)
  255. if reply, err := redis.String(rdb.Do("GET", buyer)); err != nil {
  256. //redis不存在存到临时表,定时任务处理
  257. if errb := FClient.SaveByOriID(Config["mgo_qyk_c_b_new"], tmp); !errb {
  258. log.Println("FClient.Save err", errb, tmp)
  259. }
  260. //log.Println("get redis id err:定时任务处理", err, tmp)
  261. if err := rdb.Close(); err != nil {
  262. log.Println(err)
  263. }
  264. return overid
  265. } else {
  266. if err := rdb.Close(); err != nil {
  267. log.Println(err)
  268. }
  269. //拿到合并后的qyk
  270. oldTmp, b := FClient.FindById(Config["mgo_qyk_buyer"], reply, bson.M{})
  271. if !b || (*oldTmp) == nil || reply == "" || (*oldTmp)["_id"] == nil {
  272. log.Println("redis id 不存在", reply)
  273. return overid
  274. }
  275. //比较合并 行业类型
  276. tmpTopscopeclass := []string{}
  277. tmpConTopscopeclass := []string{}
  278. tmpTopscopeclassMap := make(map[string]bool)
  279. if v, ok := tmp["topscopeclass"].([]interface{}); ok {
  280. for _, vv := range v {
  281. if vvv, ok := vv.(string); ok && len(vvv) > 1 {
  282. tmpTopscopeclassMap[vvv[:len(vvv)-1]] = true
  283. tmpConTopscopeclass = append(tmpConTopscopeclass, vvv[:len(vvv)-1])
  284. }
  285. }
  286. }
  287. for k := range tmpTopscopeclassMap {
  288. tmpTopscopeclass = append(tmpTopscopeclass, k)
  289. }
  290. sort.Strings(tmpTopscopeclass)
  291. esId := (*oldTmp)["_id"].(bson.ObjectId).Hex()
  292. //更新buyerclass合并
  293. if tmp["buyerclass"] == nil || tmp["buyerclass"] == "" {
  294. //无值,不更新
  295. } else {
  296. var buyerclass_new, buyerclass_old string
  297. buyerclass_new = tmp["buyerclass"].(string)
  298. buyerclass_old = (*oldTmp)["buyerclass"].(string)
  299. if buyerclass_old == "" {
  300. (*oldTmp)["buyerclass"] = buyerclass_new
  301. } else {
  302. if buyerclass_new != buyerclass_old {
  303. if !strings.Contains(buyerclass_old, buyerclass_new) {
  304. (*oldTmp)["buyerclass"] = buyerclass_old + "," + buyerclass_new //采购单位类型
  305. }
  306. }
  307. }
  308. }
  309. //更新行业类型
  310. if tmp["buyerperson"] == nil || tmp["buyerperson"] == "" || Reg_xing.MatchString(util.ObjToString(tmp["buyerperson"])) {
  311. (*oldTmp)["updatatime"] = time.Now().Unix()
  312. //mongo更新
  313. if !FClient.UpdateById(Config["mgo_qyk_buyer"], esId, bson.M{"$set": oldTmp}) {
  314. log.Println("mongo更新err", esId)
  315. }
  316. //es更新
  317. delete((*oldTmp), "_id")
  318. return overid
  319. }
  320. //联系方式合并
  321. contactMaps := make([]map[string]interface{}, 0)
  322. if (*oldTmp)["contact"] != nil {
  323. //直接添加联系人,不再判断
  324. if v, ok := (*oldTmp)["contact"].([]interface{}); ok {
  325. for _, vv := range v {
  326. contactMaps = append(contactMaps, vv.(map[string]interface{}))
  327. }
  328. }
  329. }
  330. var tmpperson, buyertel string
  331. if tmppersona, ok := tmp["buyerperson"].(string); ok && utf8.RuneCountInString(tmppersona)>=2 && tmppersona != "" && Reg_person.MatchString(tmppersona) && !Reg_xing.MatchString(tmppersona) {
  332. tmpperson = tmppersona
  333. }
  334. if tmpperson != "" {
  335. if buyerteltmp, ok := tmp["buyertel"].(string); ok {
  336. buyertel = buyerteltmp
  337. }
  338. if Reg_xing.MatchString(buyertel) || !Reg_tel.MatchString(buyertel) {
  339. buyertel = ""
  340. } else {
  341. buyertel = buyertel
  342. }
  343. vvv := make(map[string]interface{})
  344. vvv["infoid"] = overid
  345. vvv["contact_person"] = tmpperson
  346. vvv["contact_type"] = "项目联系人"
  347. vvv["phone"] = buyertel
  348. vvv["topscopeclass"] = strings.Join(tmpConTopscopeclass, ";")
  349. vvv["updatetime"] = time.Now().Unix()
  350. contactMaps = append(contactMaps, vvv)
  351. }
  352. //分包处理
  353. if tmp["package"] != nil {
  354. PackageDealWithBuyer(oldTmp, tmp, buyer)
  355. }
  356. (*oldTmp)["contact"] = contactMaps
  357. //mongo更新
  358. (*oldTmp)["updatatime"] = time.Now().Unix()
  359. if !FClient.UpdateById(Config["mgo_qyk_buyer"], esId, bson.M{"$set": oldTmp}) {
  360. log.Println("mongo更新 err", esId, oldTmp)
  361. }
  362. //es更新
  363. delete((*oldTmp), "_id")
  364. }
  365. return overid
  366. }
  367. //定时任务 新增
  368. //1.存异常表
  369. //2.合并原始库新增
  370. func TimedTaskBuyer() {
  371. //time.Sleep(time.Hour*70)
  372. t2 := time.NewTimer(time.Second * 5)
  373. for range t2.C {
  374. //timenow := time.Now().Unix()
  375. Fcconn := FClient.GetMgoConn(86400)
  376. tmpLast := map[string]interface{}{}
  377. if iter := Fcconn.DB(Config["mgodb_extract_kf"]).C(Config["mgo_qyk_c_b_new"]).Find(bson.M{}).Sort("-_id").Limit(1).Iter(); iter != nil {
  378. if !iter.Next(&tmpLast) {
  379. //临时表无数据
  380. log.Println("临时表无数据:")
  381. t2.Reset(time.Minute * 5)
  382. FClient.DestoryMongoConn(Fcconn)
  383. continue
  384. } else {
  385. log.Println("临时表有数据:", tmpLast["_id"])
  386. fconn := FClient.GetMgoConn(86400)
  387. cursor := fconn.DB(Config["mgodb_extract_kf"]).C(Config["mgo_qyk_c_b_new"]).Find(bson.M{
  388. "_id": bson.M{
  389. "$lte": tmpLast["_id"],
  390. },
  391. }).Sort("_id").Iter()
  392. if cursor == nil {
  393. log.Println("查询失败")
  394. t2.Reset(time.Second * 5)
  395. FClient.DestoryMongoConn(fconn)
  396. continue
  397. }
  398. //遍历临时表数据,匹配不到原始库存入异常表
  399. tmp := make(map[string]interface{})
  400. for cursor.Next(&tmp) {
  401. tmpId := tmp["_id"].(bson.ObjectId).Hex()
  402. errbuyer, ok := tmp["buyer"].(string)
  403. if !ok || errbuyer == "" {
  404. continue
  405. }
  406. //再重新查找redis,存在发udp处理,不存在走新增合并
  407. rdb := RedisPool.Get()
  408. rdb.Do("SELECT", redis_buyer_db)
  409. if _, err := redis.String(rdb.Do("GET", errbuyer)); err == nil {
  410. //增量合并
  411. AddBuyer(tmpId, tmp)
  412. //存在的话删除tmp mongo表
  413. if DeletedCount := FClient.Del(Config["mgo_qyk_c_b_new"], bson.M{"_id": bson.ObjectIdHex(tmpId)}); !DeletedCount {
  414. log.Println("删除临时表err:", DeletedCount)
  415. }
  416. if err := rdb.Close(); err != nil {
  417. log.Println(err)
  418. }
  419. continue
  420. } else {
  421. if err = rdb.Close(); err != nil {
  422. log.Println(err)
  423. }
  424. }
  425. //查询redis不存在新增
  426. sessionfinone := FClient.GetMgoConn()
  427. resulttmp := make(map[string]interface{})
  428. err := sessionfinone.DB(Config["mgodb_enterprise"]).C(Config["mgodb_enterprise_c"]).Find(bson.M{"company_name": errbuyer}).One(&resulttmp)
  429. FClient.DestoryMongoConn(sessionfinone)
  430. if err != nil || resulttmp["_id"] == nil {
  431. //log.Println(r)
  432. //人工审核正则
  433. var isok bool
  434. //先遍历ok
  435. for _, v := range BuyerRegOk {
  436. isok = v.MatchString(errbuyer)
  437. if isok {
  438. //匹配ok完,匹配err
  439. for _, vRegErr := range BuyerRegErr {
  440. isok = vRegErr.MatchString(errbuyer)
  441. //匹配到ok 也匹配到err 按err算
  442. if isok {
  443. tmp["buyer_err"] = 1
  444. break
  445. }
  446. }
  447. //匹配ok,没匹配err 按ok算
  448. if tmp["buyer_err"] == nil {
  449. tmp["buyer_ok"] = 1
  450. break
  451. }
  452. }
  453. }
  454. //都没匹配
  455. if tmp["buyer_ok"] == nil && tmp["bnuyer_err"] == nil {
  456. tmp["buyer_err"] = 1
  457. }
  458. //匹配不到原始库,存入异常表删除临时表
  459. if errb := FClient.SaveByOriID(Config["mgo_qyk_c_b_err"], tmp); !errb{
  460. log.Println("存入异常表错误", errb, tmp)
  461. }
  462. if deleteNum := FClient.Del(Config["mgo_qyk_c_b_new"], bson.M{"_id": bson.ObjectIdHex(tmpId)}); !deleteNum {
  463. log.Println("删除临时表错误", deleteNum)
  464. }
  465. continue
  466. } else {
  467. //log.Println(123)
  468. //匹配到原始库,新增 resulttmp buyer
  469. if resulttmp["credit_no"] != nil {
  470. if credit_no, ok := resulttmp["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
  471. len(strings.TrimSpace(credit_no)) > 8 {
  472. dataNo := strings.TrimSpace(credit_no)[2:8]
  473. if Addrs[dataNo] != nil {
  474. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  475. if resulttmp["province"] == nil || resulttmp["province"] == "" {
  476. resulttmp["province"] = v["province"]
  477. }
  478. resulttmp["city"] = v["city"]
  479. resulttmp["district"] = v["district"]
  480. }
  481. }
  482. }
  483. }
  484. //行业类型
  485. tmpclass := make([]string, 0)
  486. if tclasss, ok := tmp["topscopeclass"].([]interface{}); ok {
  487. for _, vv := range tclasss {
  488. if vvv, ok := vv.(string); ok {
  489. if len(vvv) > 1 {
  490. tmpclass = append(tmpclass, vvv[:len(vvv)-1])
  491. }
  492. }
  493. }
  494. }
  495. contacts := make([]map[string]interface{}, 0)
  496. if legal_person, ok := resulttmp["legal_person"].(string); ok && utf8.RuneCountInString(legal_person)>=2 &&legal_person != "" && !Reg_xing.MatchString(legal_person) && Reg_person.MatchString(legal_person) {
  497. contact := make(map[string]interface{}, 0)
  498. contact["contact_person"] = legal_person //联系人
  499. contact["contact_type"] = "法定代表人" //法定代表人
  500. if resulttmp["annual_reports"] != nil {
  501. bytes, err := json.Marshal(resulttmp["annual_reports"])
  502. if err != nil {
  503. log.Println("annual_reports err:", err)
  504. }
  505. phonetmp := make([]map[string]interface{}, 0)
  506. err = json.Unmarshal(bytes, &phonetmp)
  507. if err != nil {
  508. log.Println("Unmarshal err:", err)
  509. }
  510. for _, vv := range phonetmp {
  511. if vv["company_phone"] != nil {
  512. if vv["company_phone"] == "" {
  513. continue
  514. } else {
  515. contact["phone"] = vv["company_phone"] //联系电话
  516. break
  517. }
  518. } else {
  519. contact["phone"] = "" //联系电话
  520. }
  521. }
  522. }
  523. //log.Println(k, contact["phone"], resulttmp["_id"])
  524. //time.Sleep(10 * time.Second)
  525. if phone, ok := contact["phone"].(string); ok && phone != "" {
  526. if Reg_xing.MatchString(phone) || !Reg_tel.MatchString(phone) {
  527. contact["phone"] = "" //联系电话
  528. }
  529. } else {
  530. contact["phone"] = "" //联系电话
  531. }
  532. contact["topscopeclass"] = "企业公示" //项目类型
  533. contact["updatetime"] = time.Now().Unix() //更新时间
  534. contact["infoid"] = "" //招标信息id
  535. contacts = append(contacts, contact)
  536. }
  537. //添加临时表匹配到的联系人
  538. if buyerperson, ok := tmp["buyerperson"].(string); ok &&utf8.RuneCountInString(buyerperson)>=2 && buyerperson != "" &&
  539. !Reg_xing.MatchString(buyerperson) && Reg_person.MatchString(buyerperson) {
  540. vvv := make(map[string]interface{})
  541. vvv["infoid"] = tmp["_id"].(bson.ObjectId).Hex()
  542. vvv["contact_person"] = buyerperson
  543. vvv["contact_type"] = "项目联系人"
  544. if buyertel, ok := tmp["buyertel"].(string); ok && !Reg_xing.MatchString(buyertel) && Reg_tel.MatchString(buyertel) {
  545. vvv["phone"] = buyertel
  546. } else {
  547. vvv["phone"] = ""
  548. }
  549. vvv["topscopeclass"] = strings.Join(tmpclass, ";")
  550. vvv["updatetime"] = time.Now().Unix()
  551. contacts = append(contacts, vvv)
  552. }
  553. savetmp := make(map[string]interface{}, 0)
  554. for _, sk := range BuyerFields {
  555. if sk == "_id" {
  556. savetmp["tmp"+sk] = resulttmp[sk]
  557. continue
  558. } else if sk == "area_code" {
  559. //行政区划代码
  560. savetmp[sk] = fmt.Sprint(resulttmp[sk])
  561. continue
  562. } else if sk == "report_websites" {
  563. //网址
  564. if resulttmp["report_websites"] == nil {
  565. savetmp["website"] = ""
  566. } else {
  567. report_websitesArr := []string{}
  568. if ppms, ok := resulttmp[sk].([]interface{}); ok {
  569. for _, v := range ppms {
  570. if vvv, ok := v.(map[string]interface{}); ok {
  571. if rv, ok := vvv["website_url"].(string); ok {
  572. report_websitesArr = append(report_websitesArr, rv)
  573. }
  574. }
  575. }
  576. }
  577. sort.Strings(report_websitesArr)
  578. savetmp["website"] = strings.Join(report_websitesArr, ";")
  579. }
  580. continue
  581. } else if sk == "wechat_accounts" {
  582. savetmp[sk] = []interface{}{}
  583. continue
  584. } else if sk == "buyer_name" {
  585. if resulttmp["company_name"] == nil {
  586. savetmp[sk] = ""
  587. } else {
  588. savetmp[sk] = resulttmp["company_name"]
  589. }
  590. continue
  591. } else if sk == "address" {
  592. if resulttmp["company_address"] == nil {
  593. savetmp[sk] = ""
  594. } else {
  595. savetmp[sk] = resulttmp["company_address"]
  596. }
  597. continue
  598. }else if sk == "buyerclass" {
  599. if tmp["buyerclass"]==nil {
  600. savetmp[sk] = ""
  601. }else {
  602. savetmp[sk] = tmp["buyerclass"]
  603. }
  604. continue
  605. }
  606. if resulttmp[sk] == nil && sk != "history_name" && sk != "wechat_accounts" &&
  607. sk != "buyer_name" && sk != "address" && sk != "buyerclass"&&
  608. sk != "contact" && sk != "report_websites" {
  609. savetmp[sk] = ""
  610. } else {
  611. savetmp[sk] = resulttmp[sk]
  612. }
  613. }
  614. //判断分包
  615. if tmp["package"] != nil {
  616. PackageDealWithBuyer(&savetmp, tmp, errbuyer)
  617. }
  618. //tmps = append(tmps, savetmp)
  619. savetmp["comeintime"] = time.Now().Unix()
  620. savetmp["updatatime"] = time.Now().Unix()
  621. //保存mongo
  622. saveid := FClient.Save(Config["mgo_qyk_buyer"], savetmp)
  623. if saveid != "" {
  624. //保存redis
  625. rc := RedisPool.Get()
  626. rc.Do("SELECT", redis_buyer_db)
  627. if _, err := rc.Do("SET", savetmp["buyer_name"], saveid); err != nil {
  628. log.Println("save redis err:", tmp["_id"], savetmp["_id"], savetmp["buyer_name"], err)
  629. } else {
  630. //删除临时表
  631. if deleteNum := FClient.Del(Config["mgo_qyk_c_b_new"], bson.M{"_id": bson.ObjectIdHex(tmpId)}); !deleteNum {
  632. log.Println("删除临时表失败", deleteNum)
  633. }
  634. }
  635. if err := rc.Close(); err != nil {
  636. log.Println(err)
  637. }
  638. } else {
  639. log.Println("save mongo err:", saveid, tmp["_id"])
  640. }
  641. }
  642. }
  643. FClient.DestoryMongoConn(fconn)
  644. log.Println("buyer_new,遍历完成")
  645. }
  646. }
  647. FClient.DestoryMongoConn(Fcconn)
  648. t2.Reset(time.Minute)
  649. //nextNode("buyerent",timenow)
  650. }
  651. }
  652. //分包处理
  653. func PackageDealWithBuyer(contactMap *map[string]interface{}, tmp map[string]interface{}, comName string) []interface{} {
  654. util.Catch()
  655. //if v, ok := tmp["package"].(map[string]interface{}); ok {
  656. //for i, pv := range v {
  657. // log.Println(i, pv)
  658. //}
  659. //}
  660. return nil
  661. }