winnertask.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. arrEs = []map[string]interface{}{}
  132. }
  133. winerEsLock.Unlock()
  134. }(ret)
  135. ret = make(map[string]interface{})
  136. }
  137. rows.Close()
  138. wg.Wait()
  139. if err := rows.Err(); err != nil {
  140. log.Info("winnerEsTaskOnce", zap.Any("err", err))
  141. }
  142. }
  143. if len(arrEs) > 0 {
  144. tmps := arrEs
  145. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  146. arrEs = []map[string]interface{}{}
  147. }
  148. log.Info("winnerEsTaskOnce", zap.Int("结束,total:", total))
  149. }
  150. // winnerEsAll 存量中标单位
  151. func winnerEsAll() {
  152. arrEs := []map[string]interface{}{}
  153. winerEsLock := &sync.Mutex{}
  154. defer util.Catch()
  155. winnerPool := make(chan bool, 15) //控制线程数
  156. wg := &sync.WaitGroup{}
  157. rowsPerPage := 10000
  158. finalId := 0
  159. lastSql := fmt.Sprintf(`
  160. SELECT
  161. id
  162. FROM
  163. dws_f_ent_baseinfo
  164. WHERE (identity_type&(1<<1))>0 && company_id !=''
  165. ORDER BY id DESC LIMIT 1
  166. `)
  167. lastInfo := Mysql.SelectBySql(lastSql)
  168. if len(*lastInfo) > 0 {
  169. finalId = util.IntAll((*lastInfo)[0]["id"])
  170. }
  171. log.Info("winnerEsAll", zap.Int("finalId", finalId))
  172. lastid, total := 0, 0
  173. for {
  174. query := fmt.Sprintf(`
  175. SELECT
  176. b.name,
  177. b.name_id,
  178. b.id,
  179. b.company_id,
  180. b.seo_id,
  181. c.area,
  182. c.city,
  183. b.createtime,
  184. b.updatetime
  185. FROM
  186. dws_f_ent_baseinfo AS b
  187. LEFT JOIN code_area AS c ON b.city_code = c.code
  188. WHERE b.id > %d && (identity_type&(1<<1))>0 && b.company_id !=''
  189. ORDER BY b.id ASC
  190. LIMIT %d;
  191. `, lastid, rowsPerPage)
  192. ctx := context.Background()
  193. rows, err := Mysql.DB.QueryContext(ctx, query)
  194. if err != nil {
  195. log.Info("winnerEsAll", zap.Any("QueryContext err", err))
  196. }
  197. if finalId == lastid {
  198. log.Info("winnerEsAll over", zap.Any("total", total), zap.Any("lastid", lastid))
  199. break
  200. }
  201. columns, err := rows.Columns()
  202. if err != nil {
  203. log.Info("winnerEsAll", zap.Any("rows.Columns", err))
  204. }
  205. for rows.Next() {
  206. scanArgs := make([]interface{}, len(columns))
  207. values := make([]interface{}, len(columns))
  208. ret := make(map[string]interface{})
  209. for k := range values {
  210. scanArgs[k] = &values[k]
  211. }
  212. err = rows.Scan(scanArgs...)
  213. if err != nil {
  214. log.Info("winnerEsAll", zap.Any("rows.Scan", err))
  215. break
  216. }
  217. for i, col := range values {
  218. if v, ok := col.([]uint8); ok {
  219. ret[columns[i]] = string(v)
  220. } else {
  221. ret[columns[i]] = col
  222. }
  223. }
  224. total++
  225. if total%1000 == 0 {
  226. log.Info("winnerEsAll", zap.Int("current total", total), zap.Any("lastid", lastid))
  227. }
  228. lastid = util.IntAll(ret["id"])
  229. winnerPool <- true
  230. wg.Add(1)
  231. go func(tmp map[string]interface{}) {
  232. defer func() {
  233. <-winnerPool
  234. wg.Done()
  235. }()
  236. savetmp := map[string]interface{}{}
  237. tmp_id := tmp["name_id"]
  238. savetmp["_id"] = tmp_id
  239. savetmp["id"] = tmp_id
  240. savetmp["name"] = tmp["name"]
  241. savetmp["winner_name"] = tmp["name"]
  242. if tmp["seo_id"] != nil {
  243. savetmp["seo_id"] = tmp["seo_id"]
  244. }
  245. createtime := tmp["createtime"].(time.Time)
  246. savetmp["l_createtime"] = createtime.Unix()
  247. savetmp["pici"] = createtime.Unix()
  248. if tmp["updatetime"] != nil {
  249. updatetime := tmp["updatetime"].(time.Time)
  250. savetmp["pici"] = updatetime.Unix()
  251. }
  252. if province := util.ObjToString(tmp["area"]); province != "" {
  253. savetmp["province"] = province
  254. }
  255. if city := util.ObjToString(tmp["city"]); city != "" {
  256. savetmp["city"] = city
  257. }
  258. winerEsLock.Lock()
  259. arrEs = append(arrEs, savetmp)
  260. if len(arrEs) >= EsBulkSize {
  261. tmps := arrEs
  262. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  263. arrEs = []map[string]interface{}{}
  264. }
  265. winerEsLock.Unlock()
  266. }(ret)
  267. ret = make(map[string]interface{})
  268. }
  269. rows.Close()
  270. wg.Wait()
  271. if err := rows.Err(); err != nil {
  272. log.Info("winnerEsAll", zap.Any("err", err))
  273. }
  274. }
  275. if len(arrEs) > 0 {
  276. tmps := arrEs
  277. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  278. arrEs = []map[string]interface{}{}
  279. }
  280. log.Info("winnerEsAll", zap.Int("结束,total:", total))
  281. }
  282. func winnerEsTaskOnceOld() {
  283. defer util.Catch()
  284. arrEs := []map[string]interface{}{}
  285. winerEsLock := &sync.Mutex{}
  286. pool := make(chan bool, 3)
  287. wg := &sync.WaitGroup{}
  288. now := time.Now()
  289. preTime := time.Date(now.Year(), now.Month(), now.Day()-1, now.Hour(), 0, 0, 0, time.Local)
  290. curTime := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, time.Local)
  291. task_sid := mongodb.BsonIdToSId(primitive.NewObjectIDFromTimestamp(preTime))
  292. task_eid := mongodb.BsonIdToSId(primitive.NewObjectIDFromTimestamp(curTime))
  293. log.Info("winner 区间id", zap.String("sid", task_sid), zap.String("eid", task_eid))
  294. //区间id
  295. q := map[string]interface{}{
  296. "_id": map[string]interface{}{
  297. "$gte": mongodb.StringTOBsonId(task_sid),
  298. "$lt": mongodb.StringTOBsonId(task_eid),
  299. },
  300. }
  301. //mongo
  302. sess := MgoQ.GetMgoConn()
  303. defer MgoQ.DestoryMongoConn(sess)
  304. it_1 := sess.DB(MgoQ.DbName).C("winner_enterprise").Find(&q).Sort("_id").Iter()
  305. num_1 := 0
  306. for tmp := make(map[string]interface{}); it_1.Next(&tmp); num_1++ {
  307. if num_1%2000 == 0 && num_1 > 0 {
  308. log.Info("winnerEsTaskOnce current", zap.Int("count", num_1))
  309. }
  310. pool <- true
  311. wg.Add(1)
  312. go func(tmp map[string]interface{}) {
  313. defer func() {
  314. <-pool
  315. wg.Done()
  316. }()
  317. savetmp := map[string]interface{}{}
  318. tmp_id := mongodb.BsonIdToSId(tmp["_id"])
  319. savetmp["_id"] = tmp_id
  320. savetmp["name"] = tmp["company_name"]
  321. savetmp["winner_name"] = tmp["company_name"]
  322. savetmp["pici"] = tmp["updatetime"]
  323. if province := util.ObjToString(tmp["province"]); province != "" {
  324. savetmp["province"] = province
  325. }
  326. if city := util.ObjToString(tmp["city"]); city != "" {
  327. savetmp["city"] = city
  328. }
  329. if text := util.ObjToString(tmp["tag_business"]); text != "" {
  330. savetmp["tag_business"] = text
  331. }
  332. winerEsLock.Lock()
  333. arrEs = append(arrEs, savetmp)
  334. if len(arrEs) >= EsBulkSize {
  335. tmps := arrEs
  336. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  337. arrEs = []map[string]interface{}{}
  338. }
  339. winerEsLock.Unlock()
  340. }(tmp)
  341. tmp = make(map[string]interface{})
  342. }
  343. wg.Wait()
  344. winerEsLock.Lock()
  345. if len(arrEs) > 0 {
  346. tmps := arrEs
  347. Es.BulkSave(config.Conf.DB.Es.IndexWinner, tmps)
  348. arrEs = []map[string]interface{}{}
  349. }
  350. winerEsLock.Unlock()
  351. log.Info("winner over!", zap.Int("总计", num_1))
  352. }