winnertask.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. package main
  2. import (
  3. "context"
  4. "esindex/config"
  5. "fmt"
  6. "go.mongodb.org/mongo-driver/bson/primitive"
  7. "go.uber.org/zap"
  8. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  9. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  10. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  11. "sync"
  12. "time"
  13. )
  14. // winnerEsTaskOnce 中标单位每天新增数据
  15. func winnerEsTaskOnce() {
  16. defer util.Catch()
  17. arrEs := []map[string]interface{}{}
  18. winerEsLock := &sync.Mutex{}
  19. defer util.Catch()
  20. now := time.Now()
  21. tarTime := time.Date(now.Year(), now.Month(), now.Day()-1, 00, 00, 00, 00, time.Local)
  22. curTime := tarTime.Format("2006-01-02")
  23. pool := make(chan bool, 15) //控制线程数
  24. wg := &sync.WaitGroup{}
  25. rowsPerPage := 10000
  26. finalId := 0
  27. lastSql := fmt.Sprintf(`
  28. SELECT
  29. id
  30. FROM
  31. dws_f_ent_baseinfo
  32. WHERE ( createtime >= '%v' OR updatetime >= '%v') && (identity_type&(1<<1))>0 && company_id !=''
  33. ORDER BY id DESC LIMIT 1
  34. `, curTime, curTime)
  35. lastInfo := Mysql.SelectBySql(lastSql)
  36. if len(*lastInfo) > 0 {
  37. finalId = util.IntAll((*lastInfo)[0]["id"])
  38. }
  39. log.Info("winnerEsTaskOnce", zap.Int("finalId", finalId))
  40. lastid, total := 0, 0
  41. for {
  42. query := fmt.Sprintf(`
  43. SELECT
  44. b.name,
  45. b.name_id,
  46. b.id,
  47. b.company_id,
  48. b.seo_id,
  49. c.area,
  50. c.city,
  51. b.createtime,
  52. b.updatetime
  53. FROM
  54. dws_f_ent_baseinfo AS b
  55. LEFT JOIN code_area AS c ON b.city_code = c.code
  56. WHERE b.id > %d && (identity_type&(1<<1))>0 && b.company_id !='' && ( b.createtime >= '%v' OR b.updatetime >= '%v')
  57. ORDER BY b.id ASC
  58. LIMIT %d;
  59. `, lastid, curTime, curTime, rowsPerPage)
  60. ctx := context.Background()
  61. rows, err := Mysql.DB.QueryContext(ctx, query)
  62. if err != nil {
  63. log.Info("winnerEsTaskOnce", zap.Any("QueryContext err", err))
  64. }
  65. if finalId == lastid {
  66. log.Info("winnerEsTaskOnce over", zap.Any("total", total), zap.Any("lastid", lastid))
  67. break
  68. }
  69. columns, err := rows.Columns()
  70. if err != nil {
  71. log.Info("winnerEsTaskOnce", zap.Any("rows.Columns", err))
  72. }
  73. for rows.Next() {
  74. scanArgs := make([]interface{}, len(columns))
  75. values := make([]interface{}, len(columns))
  76. ret := make(map[string]interface{})
  77. for k := range values {
  78. scanArgs[k] = &values[k]
  79. }
  80. err = rows.Scan(scanArgs...)
  81. if err != nil {
  82. log.Info("winnerEsTaskOnce", zap.Any("rows.Scan", err))
  83. break
  84. }
  85. for i, col := range values {
  86. if v, ok := col.([]uint8); ok {
  87. ret[columns[i]] = string(v)
  88. } else {
  89. ret[columns[i]] = col
  90. }
  91. }
  92. total++
  93. if total%1000 == 0 {
  94. log.Info("winnerEsTaskOnce", zap.Int("current total", total), zap.Any("lastid", lastid))
  95. }
  96. lastid = util.IntAll(ret["id"])
  97. pool <- true
  98. wg.Add(1)
  99. go func(tmp map[string]interface{}) {
  100. defer func() {
  101. <-pool
  102. wg.Done()
  103. }()
  104. savetmp := map[string]interface{}{}
  105. tmp_id := tmp["name_id"]
  106. savetmp["_id"] = tmp_id
  107. savetmp["id"] = tmp_id
  108. savetmp["name"] = tmp["name"]
  109. savetmp["winner_name"] = tmp["name"]
  110. if tmp["seo_id"] != nil {
  111. savetmp["seo_id"] = tmp["seo_id"]
  112. }
  113. createtime := tmp["createtime"].(time.Time)
  114. savetmp["l_createtime"] = createtime.Unix()
  115. savetmp["pici"] = createtime.Unix()
  116. if tmp["updatetime"] != nil {
  117. updatetime := tmp["updatetime"].(time.Time)
  118. savetmp["pici"] = updatetime.Unix()
  119. }
  120. if province := util.ObjToString(tmp["area"]); province != "" {
  121. savetmp["province"] = province
  122. }
  123. if city := util.ObjToString(tmp["city"]); city != "" {
  124. savetmp["city"] = city
  125. }
  126. winerEsLock.Lock()
  127. arrEs = append(arrEs, savetmp)
  128. if len(arrEs) >= EsBulkSize {
  129. tmps := arrEs
  130. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  131. // 华为云新集群,存储标讯、项目、凭安数据
  132. if config.Conf.DB.Es.Addr3 != "" {
  133. Es3.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  134. }
  135. arrEs = []map[string]interface{}{}
  136. }
  137. winerEsLock.Unlock()
  138. }(ret)
  139. ret = make(map[string]interface{})
  140. }
  141. rows.Close()
  142. wg.Wait()
  143. if err := rows.Err(); err != nil {
  144. log.Info("winnerEsTaskOnce", zap.Any("err", err))
  145. }
  146. }
  147. if len(arrEs) > 0 {
  148. tmps := arrEs
  149. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  150. // 华为云新集群,存储标讯、项目、凭安数据
  151. if config.Conf.DB.Es.Addr3 != "" {
  152. Es3.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  153. }
  154. arrEs = []map[string]interface{}{}
  155. }
  156. log.Info("winnerEsTaskOnce", zap.Int("结束,total:", total))
  157. }
  158. // winnerEsAll 存量中标单位
  159. func winnerEsAll() {
  160. arrEs := []map[string]interface{}{}
  161. winerEsLock := &sync.Mutex{}
  162. defer util.Catch()
  163. winnerPool := make(chan bool, 15) //控制线程数
  164. wg := &sync.WaitGroup{}
  165. rowsPerPage := 10000
  166. finalId := 0
  167. lastSql := fmt.Sprintf(`
  168. SELECT
  169. id
  170. FROM
  171. dws_f_ent_baseinfo
  172. WHERE (identity_type&(1<<1))>0 && company_id !=''
  173. ORDER BY id DESC LIMIT 1
  174. `)
  175. lastInfo := Mysql.SelectBySql(lastSql)
  176. if len(*lastInfo) > 0 {
  177. finalId = util.IntAll((*lastInfo)[0]["id"])
  178. }
  179. log.Info("winnerEsAll", zap.Int("finalId", finalId))
  180. lastid, total := 0, 0
  181. for {
  182. query := fmt.Sprintf(`
  183. SELECT
  184. b.name,
  185. b.name_id,
  186. b.id,
  187. b.company_id,
  188. b.seo_id,
  189. c.area,
  190. c.city,
  191. b.createtime,
  192. b.updatetime
  193. FROM
  194. dws_f_ent_baseinfo AS b
  195. LEFT JOIN code_area AS c ON b.city_code = c.code
  196. WHERE b.id > %d && (identity_type&(1<<1))>0 && b.company_id !=''
  197. ORDER BY b.id ASC
  198. LIMIT %d;
  199. `, lastid, rowsPerPage)
  200. ctx := context.Background()
  201. rows, err := Mysql.DB.QueryContext(ctx, query)
  202. if err != nil {
  203. log.Info("winnerEsAll", zap.Any("QueryContext err", err))
  204. }
  205. if finalId == lastid {
  206. log.Info("winnerEsAll over", zap.Any("total", total), zap.Any("lastid", lastid))
  207. break
  208. }
  209. columns, err := rows.Columns()
  210. if err != nil {
  211. log.Info("winnerEsAll", zap.Any("rows.Columns", err))
  212. }
  213. for rows.Next() {
  214. scanArgs := make([]interface{}, len(columns))
  215. values := make([]interface{}, len(columns))
  216. ret := make(map[string]interface{})
  217. for k := range values {
  218. scanArgs[k] = &values[k]
  219. }
  220. err = rows.Scan(scanArgs...)
  221. if err != nil {
  222. log.Info("winnerEsAll", zap.Any("rows.Scan", err))
  223. break
  224. }
  225. for i, col := range values {
  226. if v, ok := col.([]uint8); ok {
  227. ret[columns[i]] = string(v)
  228. } else {
  229. ret[columns[i]] = col
  230. }
  231. }
  232. total++
  233. if total%1000 == 0 {
  234. log.Info("winnerEsAll", zap.Int("current total", total), zap.Any("lastid", lastid))
  235. }
  236. lastid = util.IntAll(ret["id"])
  237. winnerPool <- true
  238. wg.Add(1)
  239. go func(tmp map[string]interface{}) {
  240. defer func() {
  241. <-winnerPool
  242. wg.Done()
  243. }()
  244. savetmp := map[string]interface{}{}
  245. tmp_id := tmp["name_id"]
  246. savetmp["_id"] = tmp_id
  247. savetmp["id"] = tmp_id
  248. savetmp["name"] = tmp["name"]
  249. savetmp["winner_name"] = tmp["name"]
  250. if tmp["seo_id"] != nil {
  251. savetmp["seo_id"] = tmp["seo_id"]
  252. }
  253. createtime := tmp["createtime"].(time.Time)
  254. savetmp["l_createtime"] = createtime.Unix()
  255. savetmp["pici"] = createtime.Unix()
  256. if tmp["updatetime"] != nil {
  257. updatetime := tmp["updatetime"].(time.Time)
  258. savetmp["pici"] = updatetime.Unix()
  259. }
  260. if province := util.ObjToString(tmp["area"]); province != "" {
  261. savetmp["province"] = province
  262. }
  263. if city := util.ObjToString(tmp["city"]); city != "" {
  264. savetmp["city"] = city
  265. }
  266. winerEsLock.Lock()
  267. arrEs = append(arrEs, savetmp)
  268. if len(arrEs) >= EsBulkSize {
  269. tmps := arrEs
  270. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  271. arrEs = []map[string]interface{}{}
  272. }
  273. winerEsLock.Unlock()
  274. }(ret)
  275. ret = make(map[string]interface{})
  276. }
  277. rows.Close()
  278. wg.Wait()
  279. if err := rows.Err(); err != nil {
  280. log.Info("winnerEsAll", zap.Any("err", err))
  281. }
  282. }
  283. if len(arrEs) > 0 {
  284. tmps := arrEs
  285. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  286. arrEs = []map[string]interface{}{}
  287. }
  288. log.Info("winnerEsAll", zap.Int("结束,total:", total))
  289. }
  290. func winnerEsTaskOnceOld() {
  291. defer util.Catch()
  292. arrEs := []map[string]interface{}{}
  293. winerEsLock := &sync.Mutex{}
  294. pool := make(chan bool, 3)
  295. wg := &sync.WaitGroup{}
  296. now := time.Now()
  297. preTime := time.Date(now.Year(), now.Month(), now.Day()-1, now.Hour(), 0, 0, 0, time.Local)
  298. curTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.Local)
  299. task_sid := mongodb.BsonIdToSId(primitive.NewObjectIDFromTimestamp(preTime))
  300. task_eid := mongodb.BsonIdToSId(primitive.NewObjectIDFromTimestamp(curTime))
  301. log.Info("winner 区间id", zap.String("sid", task_sid), zap.String("eid", task_eid))
  302. //区间id
  303. q := map[string]interface{}{
  304. "_id": map[string]interface{}{
  305. "$gte": mongodb.StringTOBsonId(task_sid),
  306. "$lt": mongodb.StringTOBsonId(task_eid),
  307. },
  308. }
  309. //mongo
  310. sess := MgoQ.GetMgoConn()
  311. defer MgoQ.DestoryMongoConn(sess)
  312. it_1 := sess.DB(MgoQ.DbName).C("winner_enterprise").Find(&q).Sort("_id").Iter()
  313. num_1 := 0
  314. for tmp := make(map[string]interface{}); it_1.Next(&tmp); num_1++ {
  315. if num_1%2000 == 0 && num_1 > 0 {
  316. log.Info("winnerEsTaskOnce current", zap.Int("count", num_1))
  317. }
  318. pool <- true
  319. wg.Add(1)
  320. go func(tmp map[string]interface{}) {
  321. defer func() {
  322. <-pool
  323. wg.Done()
  324. }()
  325. savetmp := map[string]interface{}{}
  326. tmp_id := mongodb.BsonIdToSId(tmp["_id"])
  327. savetmp["_id"] = tmp_id
  328. savetmp["name"] = tmp["company_name"]
  329. savetmp["winner_name"] = tmp["company_name"]
  330. savetmp["pici"] = tmp["updatetime"]
  331. if province := util.ObjToString(tmp["province"]); province != "" {
  332. savetmp["province"] = province
  333. }
  334. if city := util.ObjToString(tmp["city"]); city != "" {
  335. savetmp["city"] = city
  336. }
  337. if text := util.ObjToString(tmp["tag_business"]); text != "" {
  338. savetmp["tag_business"] = text
  339. }
  340. winerEsLock.Lock()
  341. arrEs = append(arrEs, savetmp)
  342. if len(arrEs) >= EsBulkSize {
  343. tmps := arrEs
  344. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  345. arrEs = []map[string]interface{}{}
  346. }
  347. winerEsLock.Unlock()
  348. }(tmp)
  349. tmp = make(map[string]interface{})
  350. }
  351. wg.Wait()
  352. winerEsLock.Lock()
  353. if len(arrEs) > 0 {
  354. tmps := arrEs
  355. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  356. arrEs = []map[string]interface{}{}
  357. }
  358. winerEsLock.Unlock()
  359. log.Info("winner over!", zap.Int("总计", num_1))
  360. }