timedTaskWinner.go 23 KB

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