main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package main
  2. import (
  3. "github.com/tealeg/xlsx"
  4. "mongodb"
  5. "qfw/util"
  6. "time"
  7. )
  8. var (
  9. Sysconfig map[string]interface{}
  10. Mgo *mongodb.MongodbSim
  11. DbColl string
  12. FilePath string
  13. UpdateMap map[string]interface{}
  14. AddMap map[string]interface{}
  15. keyIndex map[int]interface{}
  16. updatePool chan []map[string]interface{}
  17. updateSp chan bool
  18. SE = util.SimpleEncrypt{Key: "topJYBX2019"}
  19. )
  20. type RowData struct {
  21. row int
  22. id string
  23. field map[string]interface{}
  24. }
  25. func init() {
  26. util.ReadConfig(&Sysconfig)
  27. Mgo = &mongodb.MongodbSim{
  28. MongodbAddr: Sysconfig["mongodb"].(string),
  29. Size: util.IntAllDef(Sysconfig["dbsize"], 5),
  30. DbName: Sysconfig["dbname"].(string),
  31. }
  32. Mgo.InitPool()
  33. DbColl = Sysconfig["dbcoll"].(string)
  34. FilePath = Sysconfig["filepath"].(string)
  35. UpdateMap = Sysconfig["update_field"].(map[string]interface{})
  36. AddMap = Sysconfig["add_field"].(map[string]interface{})
  37. keyIndex = make(map[int]interface{}) // 记录需要保存的字段/表格列索引
  38. updatePool = make(chan []map[string]interface{}, 5000)
  39. updateSp = make(chan bool, 5)
  40. }
  41. func main() {
  42. go updateMethod()
  43. xlFile, err := xlsx.OpenFile(FilePath)
  44. if err != nil {
  45. return
  46. }
  47. sheet := xlFile.Sheets[0]
  48. for rowIndex, row := range sheet.Rows {
  49. fieldMap := make(map[string]interface{})
  50. for cellIndex, cell := range row.Cells {
  51. util.Debug(rowIndex, cellIndex, cell.String())
  52. if rowIndex == 0 {
  53. //根据列标题获取列索引
  54. if cell.String() == "唯一标识" {
  55. keyIndex[cellIndex] = "id"
  56. } else {
  57. for k, v := range AddMap {
  58. if cell.String() == k {
  59. keyIndex[cellIndex] = v
  60. }
  61. }
  62. for k, v := range UpdateMap {
  63. if cell.String() == k {
  64. keyIndex[cellIndex] = v
  65. }
  66. }
  67. }
  68. } else {
  69. util.Debug(keyIndex)
  70. if field := util.ObjToString(keyIndex[cellIndex]); field != "" {
  71. if field == "id" {
  72. fieldMap[field] = SE.DecodeString(cell.String())
  73. } else if field == "budget" || field == "bidamount" {
  74. fieldMap[field], _ = cell.Float()
  75. } else {
  76. fieldMap[field] = cell.String()
  77. }
  78. }
  79. }
  80. }
  81. if rowIndex != 0 && len(fieldMap) > 0 {
  82. saveInfo := []map[string]interface{}{
  83. {
  84. "_id": mongodb.StringTOBsonId(util.ObjToString(fieldMap["id"])),
  85. },
  86. {"$set": fieldMap},
  87. }
  88. updatePool <- saveInfo
  89. }
  90. }
  91. c := make(chan bool, 1)
  92. <-c
  93. }
  94. func updateMethod() {
  95. arru := make([][]map[string]interface{}, 200)
  96. indexu := 0
  97. for {
  98. select {
  99. case v := <-updatePool:
  100. arru[indexu] = v
  101. indexu++
  102. if indexu == 200 {
  103. updateSp <- true
  104. go func(arru [][]map[string]interface{}) {
  105. defer func() {
  106. <-updateSp
  107. }()
  108. Mgo.UpdateBulk(DbColl, arru...)
  109. }(arru)
  110. arru = make([][]map[string]interface{}, 200)
  111. indexu = 0
  112. }
  113. case <-time.After(1000 * time.Millisecond):
  114. if indexu > 0 {
  115. updateSp <- true
  116. go func(arru [][]map[string]interface{}) {
  117. defer func() {
  118. <-updateSp
  119. }()
  120. Mgo.UpdateBulk(DbColl, arru...)
  121. }(arru[:indexu])
  122. arru = make([][]map[string]interface{}, 200)
  123. indexu = 0
  124. }
  125. }
  126. }
  127. }