timedTask.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. elastic "qfw/common/src/qfw/util/elastic"
  7. "go.mongodb.org/mongo-driver/bson/primitive"
  8. "go.mongodb.org/mongo-driver/mongo/options"
  9. "gopkg.in/mgo.v2/bson"
  10. "log"
  11. "sort"
  12. "strings"
  13. "time"
  14. )
  15. //定时任务
  16. //1.存异常表
  17. //2.合并原始库新增
  18. func TimedTask() {
  19. t2 := time.NewTimer(time.Second * 5)
  20. for range t2.C {
  21. tmpLast := map[string]interface{}{}
  22. if err := FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_new").FindOne(context.TODO(), bson.M{}, options.FindOne().SetSort(bson.M{"_id": -1})).Decode(&tmpLast); err != nil {
  23. //临时表无数据
  24. log.Println("临时表无数据:", err)
  25. t2.Reset(time.Minute * 5)
  26. continue
  27. } else {
  28. //临时表有数据
  29. log.Println("临时表有数据:", tmpLast)
  30. cursor, err := FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_new").Find(context.TODO(), bson.M{
  31. "_id": bson.M{
  32. "$lte": tmpLast["_id"],
  33. },
  34. }, options.Find().SetSort(bson.M{"_id": 1}))
  35. if err != nil {
  36. log.Println(err)
  37. t2.Reset(time.Second * 5)
  38. continue
  39. }
  40. //遍历临时表数据,匹配不到原始库存入异常表
  41. for cursor.Next(context.TODO()) {
  42. tmp := make(map[string]interface{})
  43. if err := cursor.Decode(&tmp); err == nil {
  44. resulttmp := make(map[string]interface{})
  45. r := FClient.Database(Config["mgodb_enterprise"]).Collection(Config["mgodb_enterprise_c"]).FindOne(context.TODO(), bson.M{"company_name": tmp["winner"]}).Decode(&resulttmp)
  46. if r != nil {
  47. //log.Println(r)
  48. //匹配不到原始库,存入异常表删除临时表
  49. FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_err").InsertOne(context.TODO(), tmp)
  50. FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_new").DeleteOne(context.TODO(), tmp)
  51. continue
  52. } else {
  53. //log.Println(123)
  54. //匹配到原始库,新增 resulttmp
  55. if resulttmp["credit_no"] != nil {
  56. if credit_no, ok := resulttmp["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
  57. len(strings.TrimSpace(credit_no)) > 8 {
  58. dataNo := strings.TrimSpace(credit_no)[2:8]
  59. if Addrs[dataNo] != nil {
  60. if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
  61. if resulttmp["province"] == nil || resulttmp["province"] == "" {
  62. resulttmp["province"] = v["province"]
  63. }
  64. resulttmp["city"] = v["city"]
  65. resulttmp["district"] = v["district"]
  66. }
  67. }
  68. }
  69. }
  70. contacts := make([]map[string]interface{}, 0)
  71. contact := make(map[string]interface{}, 0)
  72. if resulttmp["legal_person"] != nil {
  73. contact["contact_person"] = resulttmp["legal_person"] //联系人
  74. } else {
  75. contact["contact_person"] = "" //联系人
  76. }
  77. contact["contact_type"] = "法定代表人" //法定代表人
  78. //log.Println(1)
  79. if resulttmp["annual_reports"] != nil {
  80. bytes, err := json.Marshal(resulttmp["annual_reports"])
  81. if err != nil {
  82. log.Println("annual_reports err:", err)
  83. }
  84. phonetmp := make([]map[string]interface{}, 0)
  85. err = json.Unmarshal(bytes, &phonetmp)
  86. if err != nil {
  87. log.Println("Unmarshal err:", err)
  88. }
  89. for _, vv := range phonetmp {
  90. if vv["company_phone"] != nil {
  91. if vv["company_phone"] == "" {
  92. continue
  93. } else {
  94. contact["phone"] = vv["company_phone"] //联系电话
  95. break
  96. }
  97. } else {
  98. contact["phone"] = "" //联系电话
  99. }
  100. }
  101. }
  102. //log.Println(k, contact["phone"], resulttmp["_id"])
  103. //time.Sleep(10 * time.Second)
  104. if contact["phone"] == nil {
  105. contact["phone"] = "" //联系电话
  106. }
  107. contact["topscopeclass"] = "企业公示" //项目类型
  108. contact["updatetime"] = time.Now().Unix() //更新时间
  109. contacts = append(contacts, contact)
  110. resulttmp["contact"] = contacts
  111. savetmp := make(map[string]interface{}, 0)
  112. for _, sk := range Fields {
  113. if sk == "establish_date" {
  114. if resulttmp[sk] != nil {
  115. savetmp[sk] = resulttmp[sk].(primitive.DateTime).Time().UTC().Unix()
  116. continue
  117. }
  118. } else if sk == "capital" {
  119. //log.Println(sk, resulttmp[sk])
  120. savetmp[sk] = ObjToMoney([]interface{}{resulttmp[sk], ""})[0]
  121. continue
  122. } else if sk == "partners" {
  123. //log.Println(sk, resulttmp[sk], )
  124. //fmt.Println(reflect.TypeOf(resulttmp[sk]))
  125. if resulttmp[sk] != nil {
  126. if ppms, ok := resulttmp[sk].(primitive.A); ok {
  127. for i, _ := range ppms {
  128. if ppms[i].(map[string]interface{})["stock_type"] != nil {
  129. ppms[i].(map[string]interface{})["stock_type"] = "企业公示"
  130. }
  131. delete(ppms[i].(map[string]interface{}), "identify_type")
  132. }
  133. savetmp[sk] = ppms
  134. }
  135. }else {
  136. savetmp[sk] = []interface{}{}
  137. }
  138. continue
  139. } else if sk == "_id" {
  140. savetmp["tmp"+sk] = resulttmp[sk]
  141. continue
  142. } else if sk == "area_code" {
  143. //行政区划代码
  144. savetmp[sk] = fmt.Sprint(resulttmp[sk])
  145. continue
  146. } else if sk == "report_websites" {
  147. //网址
  148. if resulttmp["report_websites"] == nil {
  149. savetmp["website"] = ""
  150. } else {
  151. report_websitesArr := []string{}
  152. if ppms, ok := resulttmp[sk].(primitive.A); ok {
  153. for _, v := range ppms {
  154. if vvv, ok := v.(map[string]interface{}); ok {
  155. if rv, ok := vvv["website_url"].(string); ok {
  156. report_websitesArr = append(report_websitesArr, rv)
  157. }
  158. }
  159. }
  160. }
  161. sort.Strings(report_websitesArr)
  162. savetmp["website"] = strings.Join(report_websitesArr, ";")
  163. }
  164. continue
  165. } else if sk == "wechat_accounts" {
  166. savetmp[sk] = []interface{}{}
  167. continue
  168. }
  169. if resulttmp[sk] == nil && sk != "history_name" &&sk != "wechat_accounts" && sk != "establish_date" && sk != "capital" && sk != "partners" && sk != "contact" && sk != "report_websites" {
  170. savetmp[sk] = ""
  171. } else {
  172. savetmp[sk] = resulttmp[sk]
  173. }
  174. }
  175. //tmps = append(tmps, savetmp)
  176. savetmp["updatatime"] =time.Now().Unix()
  177. //保存mongo
  178. result, err := FClient.Database(Config["mgodb_extract_kf"]).Collection(Config["mgo_qyk_c"]).
  179. InsertOne(context.TODO(), savetmp)
  180. if err == nil {
  181. //保存redis
  182. rc := RedisPool.Get()
  183. defer rc.Close()
  184. var _id string
  185. if v, ok := result.InsertedID.(primitive.ObjectID); ok {
  186. _id = v.Hex()
  187. }
  188. if _, err := rc.Do("SET", savetmp["company_name"], result.InsertedID.(primitive.ObjectID).Hex()); err != nil {
  189. log.Println("save redis err:", tmp["_id"], savetmp["_id"], savetmp["company_name"], err)
  190. } else {
  191. //保存es
  192. delete(savetmp, "_id")
  193. esConn := elastic.GetEsConn()
  194. defer elastic.DestoryEsConn(esConn)
  195. if _, err := esConn.Index().Index(Config["elasticsearch_index"]).Type(Config["elasticsearch_type"]).Id(_id).BodyJson(savetmp).Refresh(true).Do(); err != nil {
  196. log.Println("save es err :", tmp["_id"], savetmp["_id"], err)
  197. } else {
  198. //删除临时表
  199. FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_new").DeleteOne(context.TODO(), tmp)
  200. }
  201. }
  202. } else {
  203. log.Println("save mongo err:", err, tmp["_id"])
  204. }
  205. }
  206. }
  207. }
  208. defer cursor.Close(context.TODO())
  209. }
  210. t2.Reset(time.Minute)
  211. }
  212. }