timedTaskAgency.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 TaskAgency(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. // agencyaddr-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{"agency": 1, "agencytel": 1, "agencyperson": 1, "topscopeclass": 1,
  44. "agencyaddr": 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_agency_db)
  57. //遍历bidding表保存到redis
  58. //key:企业名 value:json结构体{"agency": 1, "agencytel": 1, "agencyperson": 1,"topscopeclass": 1, "agencyaddr": 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. agency, ok := tmp["agency"].(string)
  67. if !ok || utf8.RuneCountInString(agency) < 4 {
  68. continue
  69. }
  70. //判断redis key是否存在
  71. e_num := conn.Exists(agency).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(agency).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(agency, string(bytes), 0).Err(); err != nil {
  86. log.Println(err)
  87. }
  88. }
  89. log.Println("存量 agency 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": "agency",
  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_agency_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_a_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_agency"], reply, nil)
  142. if !b || (*oldTmp) == nil|| reply==""||(*oldTmp)["_id"]==nil{
  143. log.Println(redisCName, "存量 redis id 不存在", reply)
  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. //联系方式合并
  163. contactMaps := make([]interface{}, 0)
  164. if (*oldTmp)["contact"] != nil {
  165. //直接添加联系人,不再判断
  166. if v, ok := (*oldTmp)["contact"].([]interface{}); ok {
  167. contactMaps = append(contactMaps, v...)
  168. }
  169. }
  170. //遍历redis value联系人
  171. for _, rvmap := range rValuesMaps {
  172. var tmpperson, agencytel string
  173. if rvmapperson, ok := rvmap["agencyperson"].(string); ok && utf8.RuneCountInString(rvmapperson)>=2 && rvmapperson != "" {
  174. tmpperson = rvmapperson
  175. } else {
  176. continue
  177. }
  178. if rvmapwintel, ok := rvmap["agencytel"].(string); ok {
  179. agencytel = rvmapwintel
  180. } else {
  181. agencytel = ""
  182. }
  183. if Reg_xing.MatchString(agencytel) || !Reg_tel.MatchString(agencytel) {
  184. agencytel = ""
  185. }
  186. tmpContact := make(map[string]interface{})
  187. tmpContact["infoid"] = rvmap["_id"]
  188. tmpContact["contact_person"] = tmpperson
  189. tmpContact["contact_type"] = "项目联系人"
  190. tmpContact["phone"] = agencytel
  191. tmpclass := make([]string, 0)
  192. if tclasss, ok := rvmap["topscopeclass"].([]string); ok {
  193. for _, vv := range tclasss {
  194. if len(vv) > 1 {
  195. tmpclass = append(tmpclass, vv[:len(vv)-1])
  196. }
  197. }
  198. }
  199. tmpContact["topscopeclass"] = strings.Join(tmpclass, ";")
  200. tmpContact["updatetime"] = time.Now().Unix()
  201. contactMaps = append(contactMaps, tmpContact)
  202. }
  203. (*oldTmp)["contact"] = contactMaps
  204. //mongo更新
  205. (*oldTmp)["updatatime"] = time.Now().Unix()
  206. if !FClient.UpdateById(Config["mgo_qyk_agency"], esId, bson.M{"$set": oldTmp}) {
  207. log.Println("存量 mongo更新 err", esId, oldTmp)
  208. }
  209. //es更新
  210. delete((*oldTmp), "_id")
  211. }
  212. }
  213. }
  214. log.Println("存量历史合并执行完成 ok", gtid, lteid)
  215. //发送udp 更新es段
  216. } else {
  217. overid := gtid
  218. tmp := map[string]interface{}{}
  219. for cursor.Next(&tmp) {
  220. overid = AddAgency(overid, tmp)
  221. }
  222. SourceClient.DestoryMongoConn(SourceClientcc)
  223. log.Println("增量合并执行完成 ok", gtid, lteid, overid)
  224. //发送udp 更新es段
  225. //nextNode("agencyent",timenow)
  226. }
  227. }
  228. //增量
  229. func AddAgency(overid string, tmp map[string]interface{}) string {
  230. overid = tmp["_id"].(bson.ObjectId).Hex()
  231. agency, ok := tmp["agency"].(string)
  232. if !ok || utf8.RuneCountInString(agency) < 4 {
  233. return overid
  234. }
  235. //redis查询是否存在
  236. rdb := RedisPool.Get()
  237. rdb.Do("SELECT", redis_agency_db)
  238. if reply, err := redis.String(rdb.Do("GET", agency)); err != nil {
  239. //redis不存在存到临时表,定时任务处理
  240. if errb := FClient.SaveByOriID(Config["mgo_qyk_c_a_new"], tmp); !errb {
  241. log.Println("FClient.Save err", errb, tmp)
  242. }
  243. //log.Println("get redis id err:定时任务处理", err, tmp)
  244. if err := rdb.Close(); err != nil {
  245. log.Println(err)
  246. }
  247. return overid
  248. } else {
  249. if err := rdb.Close(); err != nil {
  250. log.Println(err)
  251. }
  252. //拿到合并后的qyk
  253. oldTmp, b := FClient.FindById(Config["mgo_qyk_agency"], reply, bson.M{})
  254. if !b || (*oldTmp) == nil || reply == "" || (*oldTmp)["_id"] == nil {
  255. log.Println("redis id 不存在", reply)
  256. return overid
  257. }
  258. //比较合并 行业类型
  259. tmpTopscopeclass := []string{}
  260. tmpConTopscopeclass := []string{}
  261. tmpTopscopeclassMap := make(map[string]bool)
  262. if v, ok := tmp["topscopeclass"].([]interface{}); ok {
  263. for _, vv := range v {
  264. if vvv, ok := vv.(string); ok && len(vvv) > 1 {
  265. tmpTopscopeclassMap[vvv[:len(vvv)-1]] = true
  266. tmpConTopscopeclass = append(tmpConTopscopeclass, vvv[:len(vvv)-1])
  267. }
  268. }
  269. }
  270. for k := range tmpTopscopeclassMap {
  271. tmpTopscopeclass = append(tmpTopscopeclass, k)
  272. }
  273. sort.Strings(tmpTopscopeclass)
  274. esId := (*oldTmp)["_id"].(bson.ObjectId).Hex()
  275. //更新行业类型
  276. if tmp["agencyperson"] == nil || tmp["agencyperson"] == "" || Reg_xing.MatchString(util.ObjToString(tmp["agencyperson"])) {
  277. (*oldTmp)["updatatime"] = time.Now().Unix()
  278. //mongo更新
  279. if !FClient.UpdateById(Config["mgo_qyk_agency"], esId, bson.M{"$set": oldTmp}) {
  280. log.Println("mongo更新err", esId)
  281. }
  282. //es更新
  283. delete((*oldTmp), "_id")
  284. return overid
  285. }
  286. //联系方式合并
  287. contactMaps := make([]map[string]interface{}, 0)
  288. if (*oldTmp)["contact"] != nil {
  289. //直接添加联系人,不再判断
  290. if v, ok := (*oldTmp)["contact"].([]interface{}); ok {
  291. for _, vv := range v {
  292. contactMaps = append(contactMaps, vv.(map[string]interface{}))
  293. }
  294. }
  295. }
  296. var tmpperson, agencytel string
  297. if tmppersona, ok := tmp["agencyperson"].(string); ok && utf8.RuneCountInString(tmppersona)>=2 && tmppersona != "" && Reg_person.MatchString(tmppersona) && !Reg_xing.MatchString(tmppersona) {
  298. tmpperson = tmppersona
  299. }
  300. if tmpperson != "" {
  301. if agencyteltmp, ok := tmp["agencytel"].(string); ok {
  302. agencytel = agencyteltmp
  303. }
  304. if Reg_xing.MatchString(agencytel) || !Reg_tel.MatchString(agencytel) {
  305. agencytel = ""
  306. } else {
  307. agencytel = agencytel
  308. }
  309. vvv := make(map[string]interface{})
  310. vvv["infoid"] = overid
  311. vvv["contact_person"] = tmpperson
  312. vvv["contact_type"] = "项目联系人"
  313. vvv["phone"] = agencytel
  314. vvv["topscopeclass"] = strings.Join(tmpConTopscopeclass, ";")
  315. vvv["updatetime"] = time.Now().Unix()
  316. contactMaps = append(contactMaps, vvv)
  317. }
  318. //分包处理
  319. if tmp["package"] != nil {
  320. PackageDealWithAgency(oldTmp, tmp, agency)
  321. }
  322. (*oldTmp)["contact"] = contactMaps
  323. //mongo更新
  324. (*oldTmp)["updatatime"] = time.Now().Unix()
  325. if !FClient.UpdateById(Config["mgo_qyk_agency"], esId, bson.M{"$set": oldTmp}) {
  326. log.Println("mongo更新 err", esId, oldTmp)
  327. }
  328. //es更新
  329. delete((*oldTmp), "_id")
  330. }
  331. return overid
  332. }
  333. //定时任务 新增
  334. //1.存异常表
  335. //2.合并原始库新增
  336. func TimedTaskAgency() {
  337. //time.Sleep(time.Hour*70)
  338. t2 := time.NewTimer(time.Second * 5)
  339. for range t2.C {
  340. //timenow:=time.Now().Unix()
  341. Fcconn := FClient.GetMgoConn(86400)
  342. tmpLast := map[string]interface{}{}
  343. if iter := Fcconn.DB(Config["mgodb_extract_kf"]).C(Config["mgo_qyk_c_a_new"]).Find(bson.M{}).Sort("-_id").Limit(1).Iter(); iter != nil {
  344. if !iter.Next(&tmpLast) {
  345. //临时表无数据
  346. log.Println("临时表无数据:")
  347. t2.Reset(time.Minute * 5)
  348. FClient.DestoryMongoConn(Fcconn)
  349. continue
  350. } else {
  351. log.Println("临时表有数据:", tmpLast["_id"])
  352. fconn := FClient.GetMgoConn(86400)
  353. cursor := fconn.DB(Config["mgodb_extract_kf"]).C(Config["mgo_qyk_c_a_new"]).Find(bson.M{
  354. "_id": bson.M{
  355. "$lte": tmpLast["_id"],
  356. },
  357. }).Sort("_id").Iter()
  358. if cursor == nil {
  359. log.Println("查询失败")
  360. t2.Reset(time.Second * 5)
  361. FClient.DestoryMongoConn(fconn)
  362. continue
  363. }
  364. //遍历临时表数据,匹配不到原始库存入异常表
  365. tmp := make(map[string]interface{})
  366. for cursor.Next(&tmp) {
  367. tmpId := tmp["_id"].(bson.ObjectId).Hex()
  368. erragency, ok := tmp["agency"].(string)
  369. if !ok || erragency == "" {
  370. continue
  371. }
  372. //再重新查找redis,存在发udp处理,不存在走新增合并
  373. rdb := RedisPool.Get()
  374. rdb.Do("SELECT", redis_agency_db)
  375. if _, err := redis.String(rdb.Do("GET", erragency)); err == nil {
  376. //增量合并
  377. AddAgency(tmpId, tmp)
  378. //存在的话删除tmp mongo表
  379. if DeletedCount := FClient.Del(Config["mgo_qyk_c_a_new"], bson.M{"_id": bson.ObjectIdHex(tmpId)}); !DeletedCount {
  380. log.Println("删除临时表err:", DeletedCount)
  381. }
  382. if err := rdb.Close(); err != nil {
  383. log.Println(err)
  384. }
  385. continue
  386. } else {
  387. if err = rdb.Close(); err != nil {
  388. log.Println(err)
  389. }
  390. }
  391. //查询redis不存在新增
  392. sessionfinone := FClient.GetMgoConn()
  393. resulttmp := make(map[string]interface{})
  394. err := sessionfinone.DB(Config["mgodb_enterprise"]).C(Config["mgodb_enterprise_c"]).Find(bson.M{"company_name": erragency}).One(&resulttmp)
  395. FClient.DestoryMongoConn(sessionfinone)
  396. if err != nil || resulttmp["_id"] == nil {
  397. //log.Println(r)
  398. //人工审核正则
  399. var isok bool
  400. //先遍历ok
  401. for _, v := range AgencyRegOk {
  402. isok = v.MatchString(erragency)
  403. if isok {
  404. //匹配ok完,匹配err
  405. for _, vRegErr := range AgencyRegErr {
  406. isok = vRegErr.MatchString(erragency)
  407. //匹配到ok 也匹配到err 按err算
  408. if isok {
  409. tmp["agency_err"] = 1
  410. break
  411. }
  412. }
  413. //匹配ok,没匹配err 按ok算
  414. if tmp["agency_err"] == nil {
  415. tmp["agency_ok"] = 1
  416. break
  417. }
  418. }
  419. }
  420. //都没匹配
  421. if tmp["agency_ok"] == nil && tmp["agency_err"] == nil {
  422. tmp["agency_err"] = 1
  423. }
  424. //匹配不到原始库,存入异常表删除临时表
  425. if errb := FClient.SaveByOriID(Config["mgo_qyk_c_a_err"], tmp); !errb {
  426. log.Println("存入异常表错误", errb, tmp)
  427. }
  428. if deleteNum := FClient.Del(Config["mgo_qyk_c_a_new"], bson.M{"_id": bson.ObjectIdHex(tmpId)}); !deleteNum {
  429. log.Println("删除临时表错误", deleteNum)
  430. }
  431. continue
  432. } else {
  433. //log.Println(123)
  434. //匹配到原始库,新增 resulttmp angency
  435. if resulttmp["credit_no"] != nil {
  436. if credit_no, ok := resulttmp["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
  437. len(strings.TrimSpace(credit_no)) > 8 {
  438. dataNo := strings.TrimSpace(credit_no)[2:8]
  439. if Addrs[dataNo] != nil {
  440. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  441. if resulttmp["province"] == nil || resulttmp["province"] == "" {
  442. resulttmp["province"] = v["province"]
  443. }
  444. resulttmp["city"] = v["city"]
  445. resulttmp["district"] = v["district"]
  446. }
  447. }
  448. }
  449. }
  450. //行业类型
  451. tmpclass := make([]string, 0)
  452. if tclasss, ok := tmp["topscopeclass"].([]interface{}); ok {
  453. for _, vv := range tclasss {
  454. if vvv, ok := vv.(string); ok {
  455. if len(vvv) > 1 {
  456. tmpclass = append(tmpclass, vvv[:len(vvv)-1])
  457. }
  458. }
  459. }
  460. }
  461. contacts := make([]map[string]interface{}, 0)
  462. 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) {
  463. contact := make(map[string]interface{}, 0)
  464. contact["contact_person"] = legal_person //联系人
  465. contact["contact_type"] = "法定代表人" //法定代表人
  466. if resulttmp["annual_reports"] != nil {
  467. bytes, err := json.Marshal(resulttmp["annual_reports"])
  468. if err != nil {
  469. log.Println("annual_reports err:", err)
  470. }
  471. phonetmp := make([]map[string]interface{}, 0)
  472. err = json.Unmarshal(bytes, &phonetmp)
  473. if err != nil {
  474. log.Println("Unmarshal err:", err)
  475. }
  476. for _, vv := range phonetmp {
  477. if vv["company_phone"] != nil {
  478. if vv["company_phone"] == "" {
  479. continue
  480. } else {
  481. contact["phone"] = vv["company_phone"] //联系电话
  482. break
  483. }
  484. } else {
  485. contact["phone"] = "" //联系电话
  486. }
  487. }
  488. }
  489. //log.Println(k, contact["phone"], resulttmp["_id"])
  490. //time.Sleep(10 * time.Second)
  491. if phone, ok := contact["phone"].(string); ok && phone != "" {
  492. if Reg_xing.MatchString(phone) || !Reg_tel.MatchString(phone) {
  493. contact["phone"] = "" //联系电话
  494. }
  495. } else {
  496. contact["phone"] = "" //联系电话
  497. }
  498. contact["topscopeclass"] = "企业公示" //项目类型
  499. contact["updatetime"] = time.Now().Unix() //更新时间
  500. contact["infoid"] = "" //招标信息id
  501. contacts = append(contacts, contact)
  502. }
  503. //添加临时表匹配到的联系人
  504. if agencyperson, ok := tmp["agencyperson"].(string); ok && utf8.RuneCountInString(agencyperson)>=2 && agencyperson != "" &&
  505. !Reg_xing.MatchString(agencyperson) && Reg_person.MatchString(agencyperson) {
  506. vvv := make(map[string]interface{})
  507. vvv["infoid"] = tmp["_id"].(bson.ObjectId).Hex()
  508. vvv["contact_person"] = agencyperson
  509. vvv["contact_type"] = "项目联系人"
  510. if agencytel, ok := tmp["agencytel"].(string); ok && !Reg_xing.MatchString(agencytel) && Reg_tel.MatchString(agencytel) {
  511. vvv["phone"] = agencytel
  512. } else {
  513. vvv["phone"] = ""
  514. }
  515. vvv["topscopeclass"] = strings.Join(tmpclass, ";")
  516. vvv["updatetime"] = time.Now().Unix()
  517. contacts = append(contacts, vvv)
  518. }
  519. savetmp := make(map[string]interface{}, 0)
  520. for _, sk := range AgencyFields {
  521. if sk == "_id" {
  522. savetmp["tmp"+sk] = resulttmp[sk]
  523. continue
  524. } else if sk == "area_code" {
  525. //行政区划代码
  526. savetmp[sk] = fmt.Sprint(resulttmp[sk])
  527. continue
  528. } else if sk == "report_websites" {
  529. //网址
  530. if resulttmp["report_websites"] == nil {
  531. savetmp["website"] = ""
  532. } else {
  533. report_websitesArr := []string{}
  534. if ppms, ok := resulttmp[sk].([]interface{}); ok {
  535. for _, v := range ppms {
  536. if vvv, ok := v.(map[string]interface{}); ok {
  537. if rv, ok := vvv["website_url"].(string); ok {
  538. report_websitesArr = append(report_websitesArr, rv)
  539. }
  540. }
  541. }
  542. }
  543. sort.Strings(report_websitesArr)
  544. savetmp["website"] = strings.Join(report_websitesArr, ";")
  545. }
  546. continue
  547. } else if sk == "wechat_accounts" {
  548. savetmp[sk] = []interface{}{}
  549. continue
  550. } else if sk == "agency_name" {
  551. if resulttmp["company_name"] == nil {
  552. savetmp[sk] = ""
  553. } else {
  554. savetmp[sk] = resulttmp["company_name"]
  555. }
  556. continue
  557. } else if sk == "address" {
  558. if resulttmp["company_address"] == nil {
  559. savetmp[sk] = ""
  560. } else {
  561. savetmp[sk] = resulttmp["company_address"]
  562. }
  563. continue
  564. }
  565. if resulttmp[sk] == nil && sk != "history_name" && sk != "wechat_accounts" &&
  566. sk != "agency_name" && sk != "address" &&
  567. sk != "contact" && sk != "report_websites" {
  568. savetmp[sk] = ""
  569. } else {
  570. savetmp[sk] = resulttmp[sk]
  571. }
  572. }
  573. //tmps = append(tmps, savetmp)
  574. savetmp["comeintime"] = time.Now().Unix()
  575. savetmp["updatatime"] = time.Now().Unix()
  576. //保存mongo
  577. saveid := FClient.Save(Config["mgo_qyk_agency"], savetmp)
  578. if saveid != "" {
  579. //保存redis
  580. rc := RedisPool.Get()
  581. rc.Do("SELECT", redis_agency_db)
  582. if _, err := rc.Do("SET", savetmp["agency_name"], saveid); err != nil {
  583. log.Println("save redis err:", tmp["_id"], savetmp["_id"], savetmp["agency_name"], err)
  584. } else {
  585. //删除临时表
  586. if deleteNum := FClient.Del(Config["mgo_qyk_c_a_new"], bson.M{"_id": bson.ObjectIdHex(tmpId)}); !deleteNum {
  587. log.Println("删除临时表失败", deleteNum)
  588. }
  589. }
  590. if err := rc.Close(); err != nil {
  591. log.Println(err)
  592. }
  593. } else {
  594. log.Println("save mongo err:", saveid, tmp["_id"])
  595. }
  596. }
  597. }
  598. FClient.DestoryMongoConn(fconn)
  599. log.Println("agency_new,遍历完成")
  600. }
  601. }
  602. FClient.DestoryMongoConn(Fcconn)
  603. t2.Reset(time.Minute)
  604. //nextNode("agencyent",timenow)
  605. }
  606. }
  607. //分包处理
  608. func PackageDealWithAgency(contactMap *map[string]interface{}, tmp map[string]interface{}, comName string) []interface{} {
  609. util.Catch()
  610. //if v, ok := tmp["package"].(map[string]interface{}); ok {
  611. //for i, pv := range v {
  612. // log.Println(i, pv)
  613. //}
  614. //}
  615. return nil
  616. }