timedTaskAgency.go 21 KB

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