timedTaskBuyer.go 21 KB

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