timedTaskBuyer.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. Fcconn := FClient.GetMgoConn(86400)
  352. tmpLast := map[string]interface{}{}
  353. 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 {
  354. if !iter.Next(&tmpLast) {
  355. //临时表无数据
  356. log.Println("临时表无数据:")
  357. t2.Reset(time.Minute * 1)
  358. FClient.DestoryMongoConn(Fcconn)
  359. continue
  360. } else {
  361. log.Println("临时表有数据:", tmpLast["_id"])
  362. fconn := FClient.GetMgoConn(86400)
  363. cursor := fconn.DB(Config["mgodb_extract_kf"]).C(Config["mgo_qyk_c_b_new"]).Find(bson.M{
  364. "_id": bson.M{
  365. "$lte": tmpLast["_id"],
  366. },
  367. }).Sort("_id").Iter()
  368. if cursor == nil {
  369. log.Println("查询失败")
  370. t2.Reset(time.Second * 5)
  371. FClient.DestoryMongoConn(fconn)
  372. continue
  373. }
  374. //遍历临时表数据,匹配不到原始库存入异常表
  375. tmp := make(map[string]interface{})
  376. for cursor.Next(&tmp) {
  377. tmpId := tmp["_id"].(bson.ObjectId).Hex()
  378. errbuyer, ok := tmp["buyer"].(string)
  379. if !ok || errbuyer == "" {
  380. continue
  381. }
  382. //再重新查找redis,存在发udp处理,不存在走新增合并
  383. rdb := RedisPool.Get()
  384. rdb.Do("SELECT", redis_buyer_db)
  385. if _, err := redis.String(rdb.Do("GET", errbuyer)); err == nil {
  386. //增量合并
  387. AddBuyer(tmpId, tmp)
  388. //存在的话删除tmp mongo表
  389. if DeletedCount := FClient.Del(Config["mgo_qyk_c_b_new"], bson.M{"_id": bson.ObjectIdHex(tmpId)}); !DeletedCount {
  390. log.Println("删除临时表err:", DeletedCount)
  391. }
  392. if err := rdb.Close(); err != nil {
  393. log.Println(err)
  394. }
  395. continue
  396. } else {
  397. if err = rdb.Close(); err != nil {
  398. log.Println(err)
  399. }
  400. }
  401. //查询redis不存在新增
  402. sessionfinone := FClient.GetMgoConn()
  403. resulttmp := make(map[string]interface{})
  404. err := sessionfinone.DB(Config["mgodb_enterprise"]).C(Config["mgodb_enterprise_c"]).Find(bson.M{"company_name": errbuyer}).One(&resulttmp)
  405. FClient.DestoryMongoConn(sessionfinone)
  406. if err != nil || resulttmp["_id"] == nil {
  407. //log.Println(r)
  408. //人工审核正则
  409. var isok bool
  410. //先遍历ok
  411. for _, v := range BuyerRegOk {
  412. isok = v.MatchString(errbuyer)
  413. if isok {
  414. //匹配ok完,匹配err
  415. for _, vRegErr := range BuyerRegErr {
  416. isok = vRegErr.MatchString(errbuyer)
  417. //匹配到ok 也匹配到err 按err算
  418. if isok {
  419. tmp["buyer_err"] = 1
  420. break
  421. }
  422. }
  423. //匹配ok,没匹配err 按ok算
  424. if tmp["buyer_err"] == nil {
  425. tmp["buyer_ok"] = 1
  426. break
  427. }
  428. }
  429. }
  430. //都没匹配
  431. if tmp["buyer_ok"] == nil && tmp["bnuyer_err"] == nil {
  432. tmp["buyer_err"] = 1
  433. }
  434. //匹配不到原始库,存入异常表删除临时表
  435. if errb := FClient.SaveByOriID(Config["mgo_qyk_c_b_err"], tmp); !errb{
  436. log.Println("存入异常表错误", errb, tmp)
  437. }
  438. if deleteNum := FClient.Del(Config["mgo_qyk_c_b_new"], bson.M{"_id": bson.ObjectIdHex(tmpId)}); !deleteNum {
  439. log.Println("删除临时表错误", deleteNum)
  440. }
  441. continue
  442. } else {
  443. //log.Println(123)
  444. //匹配到原始库,新增 resulttmp buyer
  445. if resulttmp["credit_no"] != nil {
  446. if credit_no, ok := resulttmp["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
  447. len(strings.TrimSpace(credit_no)) > 8 {
  448. dataNo := strings.TrimSpace(credit_no)[2:8]
  449. if Addrs[dataNo] != nil {
  450. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  451. if resulttmp["province"] == nil || resulttmp["province"] == "" {
  452. resulttmp["province"] = v["province"]
  453. }
  454. resulttmp["city"] = v["city"]
  455. resulttmp["district"] = v["district"]
  456. }
  457. }
  458. }
  459. }
  460. //行业类型
  461. tmpclass := make([]string, 0)
  462. if tclasss, ok := tmp["topscopeclass"].([]interface{}); ok {
  463. for _, vv := range tclasss {
  464. if vvv, ok := vv.(string); ok {
  465. if len(vvv) > 1 {
  466. tmpclass = append(tmpclass, vvv[:len(vvv)-1])
  467. }
  468. }
  469. }
  470. }
  471. contacts := make([]map[string]interface{}, 0)
  472. if legal_person, ok := resulttmp["legal_person"].(string); ok && legal_person != "" && !Reg_xing.MatchString(legal_person) && Reg_person.MatchString(legal_person) {
  473. contact := make(map[string]interface{}, 0)
  474. contact["contact_person"] = legal_person //联系人
  475. contact["contact_type"] = "法定代表人" //法定代表人
  476. if resulttmp["annual_reports"] != nil {
  477. bytes, err := json.Marshal(resulttmp["annual_reports"])
  478. if err != nil {
  479. log.Println("annual_reports err:", err)
  480. }
  481. phonetmp := make([]map[string]interface{}, 0)
  482. err = json.Unmarshal(bytes, &phonetmp)
  483. if err != nil {
  484. log.Println("Unmarshal err:", err)
  485. }
  486. for _, vv := range phonetmp {
  487. if vv["company_phone"] != nil {
  488. if vv["company_phone"] == "" {
  489. continue
  490. } else {
  491. contact["phone"] = vv["company_phone"] //联系电话
  492. break
  493. }
  494. } else {
  495. contact["phone"] = "" //联系电话
  496. }
  497. }
  498. }
  499. //log.Println(k, contact["phone"], resulttmp["_id"])
  500. //time.Sleep(10 * time.Second)
  501. if phone, ok := contact["phone"].(string); ok && phone != "" {
  502. if Reg_xing.MatchString(phone) || !Reg_tel.MatchString(phone) {
  503. contact["phone"] = "" //联系电话
  504. }
  505. } else {
  506. contact["phone"] = "" //联系电话
  507. }
  508. contact["topscopeclass"] = "企业公示" //项目类型
  509. contact["updatetime"] = time.Now().Unix() //更新时间
  510. contact["infoid"] = "" //招标信息id
  511. contacts = append(contacts, contact)
  512. }
  513. //添加临时表匹配到的联系人
  514. if buyerperson, ok := tmp["buyerperson"].(string); ok && buyerperson != "" &&
  515. !Reg_xing.MatchString(buyerperson) && Reg_person.MatchString(buyerperson) {
  516. vvv := make(map[string]interface{})
  517. vvv["infoid"] = tmp["_id"].(bson.ObjectId).Hex()
  518. vvv["contact_person"] = buyerperson
  519. vvv["contact_type"] = "项目联系人"
  520. if buyertel, ok := tmp["buyertel"].(string); ok && !Reg_xing.MatchString(buyertel) && Reg_tel.MatchString(buyertel) {
  521. vvv["phone"] = buyertel
  522. } else {
  523. vvv["phone"] = ""
  524. }
  525. vvv["topscopeclass"] = strings.Join(tmpclass, ";")
  526. vvv["updatetime"] = time.Now().Unix()
  527. contacts = append(contacts, vvv)
  528. }
  529. savetmp := make(map[string]interface{}, 0)
  530. for _, sk := range BuyerFields {
  531. if sk == "_id" {
  532. savetmp["tmp"+sk] = resulttmp[sk]
  533. continue
  534. } else if sk == "area_code" {
  535. //行政区划代码
  536. savetmp[sk] = fmt.Sprint(resulttmp[sk])
  537. continue
  538. } else if sk == "report_websites" {
  539. //网址
  540. if resulttmp["report_websites"] == nil {
  541. savetmp["website"] = ""
  542. } else {
  543. report_websitesArr := []string{}
  544. if ppms, ok := resulttmp[sk].([]interface{}); ok {
  545. for _, v := range ppms {
  546. if vvv, ok := v.(map[string]interface{}); ok {
  547. if rv, ok := vvv["website_url"].(string); ok {
  548. report_websitesArr = append(report_websitesArr, rv)
  549. }
  550. }
  551. }
  552. }
  553. sort.Strings(report_websitesArr)
  554. savetmp["website"] = strings.Join(report_websitesArr, ";")
  555. }
  556. continue
  557. } else if sk == "wechat_accounts" {
  558. savetmp[sk] = []interface{}{}
  559. continue
  560. } else if sk == "buyer_name" {
  561. if resulttmp["company_name"] == nil {
  562. savetmp[sk] = ""
  563. } else {
  564. savetmp[sk] = resulttmp["company_name"]
  565. }
  566. continue
  567. } else if sk == "address" {
  568. if resulttmp["company_address"] == nil {
  569. savetmp[sk] = ""
  570. } else {
  571. savetmp[sk] = resulttmp["company_address"]
  572. }
  573. continue
  574. }else if sk == "buyerclass" {
  575. if tmp["buyerclass"]==nil {
  576. savetmp[sk] = ""
  577. }else {
  578. savetmp[sk] = tmp["buyerclass"]
  579. }
  580. continue
  581. }
  582. if resulttmp[sk] == nil && sk != "history_name" && sk != "wechat_accounts" &&
  583. sk != "buyer_name" && sk != "address" && sk != "buyerclass"&&
  584. sk != "contact" && sk != "report_websites" {
  585. savetmp[sk] = ""
  586. } else {
  587. savetmp[sk] = resulttmp[sk]
  588. }
  589. }
  590. //判断分包
  591. if tmp["package"] != nil {
  592. PackageDealWithBuyer(&savetmp, tmp, errbuyer)
  593. }
  594. //tmps = append(tmps, savetmp)
  595. savetmp["comeintime"] = time.Now().Unix()
  596. savetmp["updatatime"] = time.Now().Unix()
  597. //保存mongo
  598. saveid := FClient.Save(Config["mgo_qyk_buyer"], savetmp)
  599. if saveid != "" {
  600. //保存redis
  601. rc := RedisPool.Get()
  602. rc.Do("SELECT", redis_buyer_db)
  603. if _, err := rc.Do("SET", savetmp["buyer_name"], saveid); err != nil {
  604. log.Println("save redis err:", tmp["_id"], savetmp["_id"], savetmp["buyer_name"], err)
  605. } else {
  606. //删除临时表
  607. if deleteNum := FClient.Del(Config["mgo_qyk_c_b_new"], bson.M{"_id": bson.ObjectIdHex(tmpId)}); !deleteNum {
  608. log.Println("删除临时表失败", deleteNum)
  609. }
  610. }
  611. if err := rc.Close(); err != nil {
  612. log.Println(err)
  613. }
  614. } else {
  615. log.Println("save mongo err:", saveid, tmp["_id"])
  616. }
  617. }
  618. }
  619. FClient.DestoryMongoConn(fconn)
  620. log.Println("buyer_new,遍历完成")
  621. }
  622. }
  623. FClient.DestoryMongoConn(Fcconn)
  624. t2.Reset(time.Minute)
  625. }
  626. }
  627. //分包处理
  628. func PackageDealWithBuyer(contactMap *map[string]interface{}, tmp map[string]interface{}, comName string) []interface{} {
  629. util.Catch()
  630. //if v, ok := tmp["package"].(map[string]interface{}); ok {
  631. //for i, pv := range v {
  632. // log.Println(i, pv)
  633. //}
  634. //}
  635. return nil
  636. }