main.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package main
  2. import (
  3. "fieldproject_common/config"
  4. "fmt"
  5. "github.com/spf13/cobra"
  6. "go.mongodb.org/mongo-driver/bson"
  7. "go.uber.org/zap"
  8. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  9. "jygit.jydev.jianyu360.cn/data_processing/common_utils/elastic"
  10. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  12. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mysqldb"
  13. "sync"
  14. "time"
  15. )
  16. var (
  17. MongoTool, MongoTool1, MongoTool2 *mongodb.MongodbSim
  18. MysqlB, MysqlM *mysqldb.Mysql
  19. Es *elastic.Elastic
  20. ChEs chan bool
  21. saveSize int
  22. savePool chan map[string]interface{}
  23. saveSp chan bool
  24. updatePool chan []map[string]interface{}
  25. updateSp chan bool
  26. EsSaveCache chan map[string]interface{}
  27. SP chan bool
  28. updateEsPool chan []map[string]interface{}
  29. updateEsSp chan bool
  30. saveBasePool chan map[string]interface{}
  31. saveBaseSp chan bool
  32. saveBasePool1 chan map[string]interface{}
  33. saveBaseSp1 chan bool
  34. saveRcPool chan map[string]interface{}
  35. saveRcSp chan bool
  36. )
  37. func init() {
  38. config.Init("./common.toml")
  39. InitLog()
  40. InitMgo()
  41. InitMysql()
  42. InitEs()
  43. ChEs = make(chan bool, 10)
  44. saveSize = 200
  45. savePool = make(chan map[string]interface{}, 5000)
  46. saveSp = make(chan bool, 2)
  47. updatePool = make(chan []map[string]interface{}, 5000)
  48. updateSp = make(chan bool, 2)
  49. EsSaveCache = make(chan map[string]interface{}, 1000)
  50. SP = make(chan bool, 5)
  51. updateEsPool = make(chan []map[string]interface{}, 5000)
  52. updateEsSp = make(chan bool, 1)
  53. saveBasePool = make(chan map[string]interface{}, 5000)
  54. saveBaseSp = make(chan bool, 1)
  55. saveBasePool1 = make(chan map[string]interface{}, 5000)
  56. saveBaseSp1 = make(chan bool, 1)
  57. saveRcPool = make(chan map[string]interface{}, 5000)
  58. saveRcSp = make(chan bool, 1)
  59. InitField()
  60. log.Info("init success")
  61. }
  62. func main() {
  63. //go UpdateMethod()
  64. //task()
  65. //taskBiddingData()
  66. //taskCompanyData()
  67. //taskMedicalData()
  68. rootCmd := &cobra.Command{Use: "my cmd"}
  69. rootCmd.AddCommand(institution())
  70. rootCmd.AddCommand(bidding())
  71. rootCmd.AddCommand(dealer()) // 经销商
  72. rootCmd.AddCommand(dealerEs())
  73. rootCmd.AddCommand(ent()) // 法人
  74. rootCmd.AddCommand(register()) // 许可备案
  75. rootCmd.AddCommand(product()) // 产品信息
  76. rootCmd.AddCommand(project()) // 标的物信息宽表、中标信息
  77. if err := rootCmd.Execute(); err != nil {
  78. fmt.Println("rootCmd.Execute failed", err.Error())
  79. }
  80. c := make(chan bool, 1)
  81. <-c
  82. }
  83. func task() {
  84. sess := MongoTool2.GetMgoConn()
  85. defer MongoTool2.DestoryMongoConn(sess)
  86. ch := make(chan bool, 2)
  87. wg := &sync.WaitGroup{}
  88. //log.Info(fmt.Sprintf("%d", MongoTool.Count("qyxy_0824", nil)))
  89. field := map[string]interface{}{"company_name": 1, "company_id": 1}
  90. query := sess.DB(config.Conf.DB.Mongo2.Dbname).C("zktest_mysql_company_info").Find(nil).Select(field).Iter()
  91. count := 0
  92. for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
  93. if count%20000 == 0 {
  94. log.Info(fmt.Sprintf("current --- %d", count))
  95. }
  96. ch <- true
  97. wg.Add(1)
  98. go func(tmp map[string]interface{}) {
  99. defer func() {
  100. <-ch
  101. wg.Done()
  102. }()
  103. update := make(map[string]interface{})
  104. name := util.ObjToString(tmp["company_name"])
  105. info := MysqlB.FindOne("dws_f_ent_baseinfo", bson.M{"name": name}, "name_id", "")
  106. if info != nil && len(*info) > 0 {
  107. update["name_id"] = (*info)["name_id"]
  108. } else {
  109. info = MysqlM.FindOne("dws_d_name_id_record", bson.M{"name": name}, "name_id", "")
  110. if info != nil && len(*info) > 0 {
  111. update["name_id"] = (*info)["name_id"]
  112. }
  113. }
  114. if len(update) > 0 {
  115. updatePool <- []map[string]interface{}{
  116. {"_id": tmp["_id"]},
  117. {"$set": update},
  118. }
  119. }
  120. }(tmp)
  121. tmp = make(map[string]interface{})
  122. }
  123. wg.Wait()
  124. log.Info(fmt.Sprintf("over --- %d", count))
  125. }
  126. // @Description mysql迭代
  127. // @Author J 2022/8/9 17:32
  128. func taskMedicalData() {
  129. pool := make(chan bool, 10) //控制线程数
  130. wg := &sync.WaitGroup{}
  131. finalId := 0
  132. lastInfo := MysqlM.SelectBySql(fmt.Sprintf("SELECT * FROM %s where mark_id = 4 ORDER BY id DESC LIMIT 1", "institution_baseinfo"))
  133. //lastInfo := MysqlM.SelectBySql(fmt.Sprintf("SELECT * FROM %s where id=58830 ORDER BY id DESC LIMIT 1", "institution_baseinfo"))
  134. if len(*lastInfo) > 0 {
  135. finalId = util.IntAll((*lastInfo)[0]["id"])
  136. }
  137. log.Info("查询最后id---", zap.Int("finally id: ", finalId))
  138. lastid, count := 0, 0
  139. for {
  140. log.Info("重新查询,lastid---", zap.Int("lastid: ", lastid))
  141. q := fmt.Sprintf("SELECT id, company_id FROM %s WHERE id > %d AND mark_id = 4 ORDER BY id ASC limit 1000000", "institution_baseinfo", lastid)
  142. //q := fmt.Sprintf("SELECT id, company_id FROM %s WHERE id=58830 ORDER BY id ASC limit 1000000", "institution_baseinfo")
  143. rows, err := MysqlM.DB.Query(q)
  144. if err != nil {
  145. log.Error("mysql query err ", zap.Error(err))
  146. }
  147. columns, err := rows.Columns()
  148. if finalId == lastid {
  149. log.Info("----finish-----", zap.Int("count: ", count))
  150. break
  151. }
  152. for rows.Next() {
  153. scanArgs := make([]interface{}, len(columns))
  154. values := make([]interface{}, len(columns))
  155. ret := make(map[string]interface{})
  156. for k := range values {
  157. scanArgs[k] = &values[k]
  158. }
  159. err = rows.Scan(scanArgs...)
  160. if err != nil {
  161. log.Error("mysql scan err ", zap.Error(err))
  162. break
  163. }
  164. for i, col := range values {
  165. if v, ok := col.([]uint8); ok {
  166. ret[columns[i]] = string(v)
  167. } else {
  168. ret[columns[i]] = col
  169. }
  170. }
  171. lastid = util.IntAll(ret["id"])
  172. count++
  173. if count%2000 == 0 {
  174. log.Info("current----", zap.Int("count: ", count), zap.Int("lastid: ", lastid))
  175. }
  176. pool <- true
  177. wg.Add(1)
  178. go func(tmp map[string]interface{}) {
  179. defer func() {
  180. <-pool
  181. wg.Done()
  182. }()
  183. // mark_id = 1
  184. //taskB(util.ObjToString(tmp["company_id"]))
  185. // mark_id = 4
  186. taskB_1(util.ObjToString(tmp["company_id"]))
  187. }(ret)
  188. ret = make(map[string]interface{})
  189. }
  190. _ = rows.Close()
  191. wg.Wait()
  192. }
  193. }
  194. func SaveMethod() {
  195. arru := make([]map[string]interface{}, saveSize)
  196. indexu := 0
  197. for {
  198. select {
  199. case v := <-savePool:
  200. arru[indexu] = v
  201. indexu++
  202. if indexu == saveSize {
  203. saveSp <- true
  204. go func(arru []map[string]interface{}) {
  205. defer func() {
  206. <-saveSp
  207. }()
  208. MongoTool.SaveBulk("bidding_p_list_0907", arru...)
  209. }(arru)
  210. arru = make([]map[string]interface{}, saveSize)
  211. indexu = 0
  212. }
  213. case <-time.After(1000 * time.Millisecond):
  214. if indexu > 0 {
  215. saveSp <- true
  216. go func(arru []map[string]interface{}) {
  217. defer func() {
  218. <-saveSp
  219. }()
  220. MongoTool.SaveBulk("bidding_p_list_0907", arru...)
  221. }(arru[:indexu])
  222. arru = make([]map[string]interface{}, saveSize)
  223. indexu = 0
  224. }
  225. }
  226. }
  227. }
  228. func SaveFunc(table string, arr []string) {
  229. arru := make([]map[string]interface{}, saveSize)
  230. indexu := 0
  231. for {
  232. select {
  233. case v := <-saveBasePool:
  234. arru[indexu] = v
  235. indexu++
  236. if indexu == saveSize {
  237. saveBaseSp <- true
  238. go func(arru []map[string]interface{}) {
  239. defer func() {
  240. <-saveBaseSp
  241. }()
  242. MysqlM.InsertBulk(table, arr, arru...)
  243. }(arru)
  244. arru = make([]map[string]interface{}, saveSize)
  245. indexu = 0
  246. }
  247. case <-time.After(1000 * time.Millisecond):
  248. if indexu > 0 {
  249. saveBaseSp <- true
  250. go func(arru []map[string]interface{}) {
  251. defer func() {
  252. <-saveBaseSp
  253. }()
  254. MysqlM.InsertBulk(table, arr, arru...)
  255. }(arru[:indexu])
  256. arru = make([]map[string]interface{}, saveSize)
  257. indexu = 0
  258. }
  259. }
  260. }
  261. }
  262. func SaveFunc1(table string, arr []string) {
  263. arru := make([]map[string]interface{}, saveSize)
  264. indexu := 0
  265. for {
  266. select {
  267. case v := <-saveBasePool1:
  268. arru[indexu] = v
  269. indexu++
  270. if indexu == saveSize {
  271. saveBaseSp1 <- true
  272. go func(arru []map[string]interface{}) {
  273. defer func() {
  274. <-saveBaseSp1
  275. }()
  276. MysqlM.InsertBulk(table, arr, arru...)
  277. }(arru)
  278. arru = make([]map[string]interface{}, saveSize)
  279. indexu = 0
  280. }
  281. case <-time.After(1000 * time.Millisecond):
  282. if indexu > 0 {
  283. saveBaseSp1 <- true
  284. go func(arru []map[string]interface{}) {
  285. defer func() {
  286. <-saveBaseSp1
  287. }()
  288. MysqlM.InsertBulk(table, arr, arru...)
  289. }(arru[:indexu])
  290. arru = make([]map[string]interface{}, saveSize)
  291. indexu = 0
  292. }
  293. }
  294. }
  295. }
  296. func SaveFuncRc() {
  297. arru := make([]map[string]interface{}, saveSize)
  298. indexu := 0
  299. for {
  300. select {
  301. case v := <-saveRcPool:
  302. arru[indexu] = v
  303. indexu++
  304. if indexu == saveSize {
  305. saveRcSp <- true
  306. go func(arru []map[string]interface{}) {
  307. defer func() {
  308. <-saveRcSp
  309. }()
  310. MysqlM.InsertBulk("dws_d_name_id_record", RecordField, arru...)
  311. }(arru)
  312. arru = make([]map[string]interface{}, saveSize)
  313. indexu = 0
  314. }
  315. case <-time.After(1000 * time.Millisecond):
  316. if indexu > 0 {
  317. saveRcSp <- true
  318. go func(arru []map[string]interface{}) {
  319. defer func() {
  320. <-saveRcSp
  321. }()
  322. MysqlM.InsertBulk("dws_d_name_id_record", RecordField, arru...)
  323. }(arru[:indexu])
  324. arru = make([]map[string]interface{}, saveSize)
  325. indexu = 0
  326. }
  327. }
  328. }
  329. }
  330. func UpdateMethod() {
  331. arru := make([][]map[string]interface{}, saveSize)
  332. indexu := 0
  333. for {
  334. select {
  335. case v := <-updatePool:
  336. arru[indexu] = v
  337. indexu++
  338. if indexu == saveSize {
  339. updateSp <- true
  340. go func(arru [][]map[string]interface{}) {
  341. defer func() {
  342. <-updateSp
  343. }()
  344. MongoTool2.UpdateBulk("zktest_mysql_company_info", arru...)
  345. }(arru)
  346. arru = make([][]map[string]interface{}, saveSize)
  347. indexu = 0
  348. }
  349. case <-time.After(1000 * time.Millisecond):
  350. if indexu > 0 {
  351. updateSp <- true
  352. go func(arru [][]map[string]interface{}) {
  353. defer func() {
  354. <-updateSp
  355. }()
  356. MongoTool2.UpdateBulk("zktest_mysql_company_info", arru...)
  357. }(arru[:indexu])
  358. arru = make([][]map[string]interface{}, saveSize)
  359. indexu = 0
  360. }
  361. }
  362. }
  363. }