loadData.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "gorm.io/gorm"
  8. "kpt.xdmy/apiserver/model"
  9. "kpt.xdmy/pkg/util"
  10. log "github.com/sirupsen/logrus"
  11. "github.com/xuri/excelize/v2"
  12. )
  13. func PartBatch() (count int, e error) {
  14. nch = make(chan error, 10)
  15. r, e := s.d.DB.Raw(`select id FROM parts WHERE issync = 0 and statue = 1 `).Rows()
  16. defer r.Close()
  17. if e != nil {
  18. fmt.Print(e)
  19. return 0, e
  20. }
  21. var id string
  22. for r.Next() {
  23. r.Scan(&id)
  24. easBillSync(id)
  25. if e = <-nch; e != nil {
  26. fmt.Print(e)
  27. } else {
  28. count++
  29. }
  30. }
  31. return count, nil
  32. }
  33. func easBillSync(id string) {
  34. e := util.EASBillSync(5, id, "1")
  35. nch <- e
  36. }
  37. var parchan chan *gorm.DB
  38. func (s *Service) PartUpdate() {
  39. parchan = make(chan *gorm.DB, 10)
  40. // f, e := excelize.OpenFile("/Users/desire/Documents/partcode.xlsx")
  41. f, e := excelize.OpenFile("partcode.xlsx")
  42. if e != nil {
  43. log.Error("excle open :%d", e)
  44. }
  45. defer func() {
  46. // Close the spreadsheet.
  47. if err := f.Close(); err != nil {
  48. fmt.Println(err)
  49. }
  50. }()
  51. sheet := []string{"Sheet1", "Sheet2"}
  52. // var s []string
  53. for _, name := range sheet {
  54. rows, err := f.GetRows(name)
  55. if err != nil {
  56. fmt.Println(err)
  57. return
  58. }
  59. fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
  60. value := ""
  61. for i, row := range rows {
  62. if i == 0 {
  63. value += fmt.Sprintf("('%s', '%s','%s',%s)", row[0], row[1], row[2], row[3])
  64. } else {
  65. value += fmt.Sprintf(",('%s', '%s','%s',%s)", row[0], row[1], row[2], row[3])
  66. }
  67. }
  68. iusql := fmt.Sprintf(`insert into parts (partcode ,new_partcode,unit,issave) values %s
  69. on DUPLICATE KEY UPDATE new_partcode=values(new_partcode) ,unit=values(unit),issave=values(issave) `, value)
  70. if r := s.d.DB.Exec(iusql); r.Error != nil {
  71. fmt.Printf("insert part_Data: %v\n", r.Error)
  72. return
  73. } else {
  74. fmt.Printf("insert part_Data success!:%d\n", r.RowsAffected)
  75. }
  76. fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
  77. }
  78. }
  79. func (s *Service) PartInsert() {
  80. // f, e := excelize.OpenFile("/Users/desire/Downloads/part_insert.xlsx")
  81. f, e := excelize.OpenFile("part_insert.xlsx")
  82. if e != nil {
  83. log.Error("excle open :%d", e)
  84. }
  85. defer func() {
  86. if err := f.Close(); err != nil {
  87. fmt.Println(err)
  88. }
  89. }()
  90. slist := f.GetSheetList()
  91. log.Infof("%v", slist)
  92. // sheet := "Sheet1"
  93. rows, err := f.GetRows(slist[0])
  94. if err != nil {
  95. fmt.Println(err)
  96. return
  97. }
  98. value := ""
  99. for i, row := range rows {
  100. // if i > 3550 {
  101. // continue
  102. // }
  103. if i == 0 {
  104. value += fmt.Sprintf("('%s', '%s','%s','%s')", row[0], row[1], row[2], row[3])
  105. } else {
  106. value += fmt.Sprintf(",('%s', '%s','%s','%s')", row[0], row[1], row[2], row[3])
  107. }
  108. }
  109. iusql := fmt.Sprintf(`insert into parts (partcode ,name,specification,unit) values %s
  110. on DUPLICATE KEY UPDATE partcode=values(partcode) ,name=values(name),specification=values(specification),
  111. unit=values(unit)`, value)
  112. if r := s.d.DB.Exec(iusql); r.Error != nil {
  113. fmt.Printf("insert parts: %v\n", r.Error)
  114. return
  115. } else {
  116. fmt.Printf("insert parts success!:%d\n", r.RowsAffected)
  117. }
  118. }
  119. func (s *Service) RepertoryUpdate() {
  120. f, e := excelize.OpenFile("repertory.xlsx")
  121. // f, e := excelize.OpenFile("/Users/desire/Documents/repertory.xlsx")
  122. if e != nil {
  123. log.Errorf("excle open :%d", e)
  124. }
  125. defer func() {
  126. if err := f.Close(); err != nil {
  127. fmt.Println(err)
  128. }
  129. }()
  130. rows, e := f.GetRows("SheetJS")
  131. if e != nil {
  132. fmt.Println(e)
  133. return
  134. }
  135. sql := "call insertRepertory(?,?,?,?,?,?,?,?,?,?,?)"
  136. var sum, count, suc int
  137. for _, row := range rows[1:2] {
  138. sum++
  139. var e error
  140. // var msg, msg2 string
  141. // scan := new(sqlScan)
  142. var res interface{}
  143. var storage, price float64
  144. if storage, e = strconv.ParseFloat(row[6], 64); e != nil {
  145. log.Errorf("failed to parse storage:%s , e:%v", row[6], e)
  146. continue
  147. }
  148. if price, e = strconv.ParseFloat(row[7], 64); e != nil {
  149. log.Errorf("failed to parse price:%s , e:%v", row[7], e)
  150. continue
  151. }
  152. if r := s.d.DB.Raw(sql, row[0], row[1], row[2], row[3], row[4], storage, price, row[7], row[8], row[9], "00611"); r.Error != nil {
  153. log.Errorf("exec :%v", r.Error)
  154. } else {
  155. count++
  156. // r.Row().Scan(&msg, &msg2)
  157. r.Row().Scan(&res)
  158. // r.Row().Scan(scan)
  159. log.Infof("res:%s", res)
  160. // if msg == "success" {
  161. // suc++
  162. // } else {
  163. // log.Infoln(msg)
  164. // }
  165. }
  166. }
  167. log.Infoln(suc, count, sum)
  168. }
  169. type sqlScan struct {
  170. msg string
  171. id string
  172. contractid string
  173. }
  174. func (s *Service) EqInsert() {
  175. // f, e := excelize.OpenFile("/Users/desire/Downloads/eq.xlsx")
  176. f, e := excelize.OpenFile("eq.xlsx")
  177. if e != nil {
  178. log.Error("excle open :%d", e)
  179. }
  180. defer func() {
  181. if err := f.Close(); err != nil {
  182. fmt.Println(err)
  183. }
  184. }()
  185. slist := f.GetSheetList()
  186. rows, err := f.GetRows(slist[0])
  187. if err != nil {
  188. fmt.Println(err)
  189. return
  190. }
  191. names := []string{"pastureName", "eqClassName", "eqName", "eqCode", "specification",
  192. "brand", "proName", "purpose", "status", "purchaseDate",
  193. "entranceDate", "depreciation", "financeCode", "yuanzhi", "salvage",
  194. "departmentName", "employeName", "inputUser"}
  195. esl := []model.Equipment{}
  196. for i, row := range rows {
  197. if i < 2 {
  198. continue
  199. }
  200. m := make(map[string]interface{})
  201. eq := new(model.Equipment)
  202. for j, v := range row {
  203. m[names[j]] = v
  204. }
  205. // if status, e := strconv.ParseInt(m["status"].(string), 10, 32); e != nil {
  206. // log.Error(e)
  207. // } else {
  208. // m["status"] = status
  209. // }
  210. if m["status"] == "正常" {
  211. m["status"] = 17
  212. }
  213. if b, e := json.Marshal(m); e != nil {
  214. log.Error(e)
  215. } else {
  216. if e := json.Unmarshal(b, eq); e != nil {
  217. log.Error(e)
  218. } else {
  219. esl = append(esl, *eq)
  220. }
  221. }
  222. }
  223. log.Infof("len==%v", len(esl))
  224. // s.d.DB.CreateInBatches(esl, 100)
  225. r := s.d.DB.Create(&esl)
  226. log.Infof("rows==%v", r.RowsAffected)
  227. }