part.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package service
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "gorm.io/gorm"
  7. "kpt.xdmy/pkg/util"
  8. log "github.com/sirupsen/logrus"
  9. "github.com/xuri/excelize/v2"
  10. )
  11. func PartBatch() (count int, e error) {
  12. nch = make(chan error, 10)
  13. r, e := s.d.DB.Raw(`select id FROM parts WHERE issync = 0 and statue = 1 `).Rows()
  14. defer r.Close()
  15. if e != nil {
  16. fmt.Print(e)
  17. return 0, e
  18. }
  19. var id string
  20. for r.Next() {
  21. r.Scan(&id)
  22. easBillSync(id)
  23. if e = <-nch; e != nil {
  24. fmt.Print(e)
  25. } else {
  26. count++
  27. }
  28. }
  29. return count, nil
  30. }
  31. func easBillSync(id string) {
  32. e := util.EASBillSync(5, id, "1")
  33. nch <- e
  34. }
  35. var parchan chan *gorm.DB
  36. func (s *Service) PartUpdate() {
  37. parchan = make(chan *gorm.DB, 10)
  38. // f, e := excelize.OpenFile("/Users/desire/Documents/partcode.xlsx")
  39. f, e := excelize.OpenFile("partcode.xlsx")
  40. if e != nil {
  41. log.Error("excle open :%d", e)
  42. }
  43. defer func() {
  44. // Close the spreadsheet.
  45. if err := f.Close(); err != nil {
  46. fmt.Println(err)
  47. }
  48. }()
  49. sheet := []string{"Sheet1", "Sheet2"}
  50. // var s []string
  51. for _, name := range sheet {
  52. rows, err := f.GetRows(name)
  53. if err != nil {
  54. fmt.Println(err)
  55. return
  56. }
  57. fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
  58. value := ""
  59. for i, row := range rows {
  60. if i == 0 {
  61. value += fmt.Sprintf("('%s', '%s','%s',%s)", row[0], row[1], row[2], row[3])
  62. } else {
  63. value += fmt.Sprintf(",('%s', '%s','%s',%s)", row[0], row[1], row[2], row[3])
  64. }
  65. }
  66. iusql := fmt.Sprintf(`insert into parts (partcode ,new_partcode,unit,issave) values %s
  67. on DUPLICATE KEY UPDATE new_partcode=values(new_partcode) ,unit=values(unit),issave=values(issave) `, value)
  68. if r := s.d.DB.Exec(iusql); r.Error != nil {
  69. fmt.Printf("insert part_Data: %v\n", r.Error)
  70. return
  71. } else {
  72. fmt.Printf("insert part_Data success!:%d\n", r.RowsAffected)
  73. }
  74. fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
  75. }
  76. }
  77. func (s *Service) PartInsert() {
  78. // f, e := excelize.OpenFile("/Users/desire/Downloads/part_insert.xlsx")
  79. f, e := excelize.OpenFile("part_insert.xlsx")
  80. if e != nil {
  81. log.Error("excle open :%d", e)
  82. }
  83. defer func() {
  84. if err := f.Close(); err != nil {
  85. fmt.Println(err)
  86. }
  87. }()
  88. slist := f.GetSheetList()
  89. log.Infof("%v", slist)
  90. // sheet := "Sheet1"
  91. rows, err := f.GetRows(slist[0])
  92. if err != nil {
  93. fmt.Println(err)
  94. return
  95. }
  96. value := ""
  97. for i, row := range rows {
  98. // if i > 3550 {
  99. // continue
  100. // }
  101. if i == 0 {
  102. value += fmt.Sprintf("('%s', '%s','%s','%s')", row[0], row[1], row[2], row[3])
  103. } else {
  104. value += fmt.Sprintf(",('%s', '%s','%s','%s')", row[0], row[1], row[2], row[3])
  105. }
  106. }
  107. iusql := fmt.Sprintf(`insert into parts (partcode ,name,specification,unit) values %s
  108. on DUPLICATE KEY UPDATE partcode=values(partcode) ,name=values(name),specification=values(specification),
  109. unit=values(unit)`, value)
  110. if r := s.d.DB.Exec(iusql); r.Error != nil {
  111. fmt.Printf("insert parts: %v\n", r.Error)
  112. return
  113. } else {
  114. fmt.Printf("insert parts success!:%d\n", r.RowsAffected)
  115. }
  116. }
  117. func (s *Service) RepertoryUpdate() {
  118. f, e := excelize.OpenFile("repertory.xlsx")
  119. // f, e := excelize.OpenFile("/Users/desire/Documents/repertory.xlsx")
  120. if e != nil {
  121. log.Errorf("excle open :%d", e)
  122. }
  123. defer func() {
  124. if err := f.Close(); err != nil {
  125. fmt.Println(err)
  126. }
  127. }()
  128. rows, e := f.GetRows("SheetJS")
  129. if e != nil {
  130. fmt.Println(e)
  131. return
  132. }
  133. sql := "call insertRepertory(?,?,?,?,?,?,?,?,?,?,?)"
  134. var sum, count, suc int
  135. for _, row := range rows[1:2] {
  136. sum++
  137. var e error
  138. // var msg, msg2 string
  139. // scan := new(sqlScan)
  140. var res interface{}
  141. var storage, price float64
  142. if storage, e = strconv.ParseFloat(row[6], 64); e != nil {
  143. log.Errorf("failed to parse storage:%s , e:%v", row[6], e)
  144. continue
  145. }
  146. if price, e = strconv.ParseFloat(row[7], 64); e != nil {
  147. log.Errorf("failed to parse price:%s , e:%v", row[7], e)
  148. continue
  149. }
  150. 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 {
  151. log.Errorf("exec :%v", r.Error)
  152. } else {
  153. count++
  154. // r.Row().Scan(&msg, &msg2)
  155. r.Row().Scan(&res)
  156. // r.Row().Scan(scan)
  157. log.Infof("res:%s", res)
  158. // if msg == "success" {
  159. // suc++
  160. // } else {
  161. // log.Infoln(msg)
  162. // }
  163. }
  164. }
  165. log.Infoln(suc, count, sum)
  166. }
  167. type sqlScan struct {
  168. msg string
  169. id string
  170. contractid string
  171. }