timedTaskAgency.go 21 KB

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