timedTaskBuyer.go 21 KB

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