asset.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package service
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/k0kubun/pp/v3"
  6. "github.com/pkg/errors"
  7. _ "github.com/go-sql-driver/mysql"
  8. log "github.com/sirupsen/logrus"
  9. "kpt.xdmy/apiserver/model"
  10. "kpt.xdmy/apiserver/model/http"
  11. )
  12. var assetChan chan error
  13. func (s *Service) SapAsset(t time.Time, code string) {
  14. assetChan = make(chan error, 10)
  15. r := new(http.AssetReq)
  16. rp := new(http.AssetResp)
  17. var e error
  18. r.Dest.DestID = "EQMAN"
  19. r.Dest.BussTp = "FI004"
  20. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/EAM/FI004/QueryAsset"
  21. if t.IsZero() {
  22. t = time.Now()
  23. }
  24. r.Data.BudatB = t.Format("20060102")
  25. r.Data.BudatE = t.Format("20060102")
  26. r.Data.CompanyCode = []http.Company{
  27. // {Code: "9099"}, {Code: "1006"}
  28. {Code: "1002"},
  29. }
  30. if code != "" {
  31. r.Data.Codes = []http.TANLN1{{Code: code}}
  32. }
  33. if e = s.SyncSap(r, rp); e == nil {
  34. if rp.Dest.Status == "S" {
  35. log.Infof("sap asset success sum=%d", len(rp.Data.Master))
  36. } else {
  37. log.Infof("sap asset fail", rp.Dest.MessText)
  38. pp.Print(r)
  39. }
  40. } else {
  41. log.Error(e)
  42. }
  43. var update error
  44. var count int
  45. for _, v := range rp.Data.Master {
  46. update = nil
  47. go updateEQ(&v)
  48. update = <-assetChan
  49. if update != nil {
  50. log.Error(update)
  51. count++
  52. l := model.SapDetailLog{Name: "asset", Code: fmt.Sprintf("%s-%s", v.AssetCode, v.SonCode), ErrorText: update.Error()}
  53. if e := s.d.DB.Create(&l); e != nil {
  54. log.Errorf("add sapdetail log: %v", e)
  55. }
  56. }
  57. }
  58. if e != nil {
  59. log.Error(e)
  60. } else if update != nil {
  61. e = errors.Errorf("asset update fail sum:%d", count)
  62. } else {
  63. log.Infof("asset update success sum:%d", len(rp.Data.Master)-count)
  64. }
  65. s.AddSapLog(r, e)
  66. return
  67. }
  68. func updateEQ(p *http.Asset) {
  69. eq := new(model.Equipment)
  70. pa := new(model.Pasture)
  71. pv := new(model.Provider)
  72. if e := s.d.DB.First(pa, model.Pasture{CompanyCode: p.CompanyCode}).Error; e != nil {
  73. assetChan <- errors.Wrapf(e, "pasture companycode:%s", p.CompanyCode)
  74. }
  75. if e := s.d.DB.First(pa, model.Provider{ProviderNumber: p.Supplier}).Error; e != nil {
  76. assetChan <- errors.Wrapf(e, "Provider ProviderNumber:%s", p.Supplier)
  77. }
  78. if len(p.Udate) == 8 {
  79. p.Udate = p.Udate[:4] + "-" + p.Udate[4:6] + "-" + p.Udate[6:]
  80. }
  81. if len(p.Utime) == 6 {
  82. p.Utime = p.Utime[:2] + ":" + p.Utime[2:4] + ":" + p.Utime[4:]
  83. }
  84. if p.Utime != "" {
  85. p.Udate = p.Udate + " " + p.Utime
  86. }
  87. if t, e := time.Parse("2006-01-02 15:04:05", p.Udate); e != nil {
  88. assetChan <- errors.Errorf("util ParseDateAndTime : %s, %s", p.Udate)
  89. } else {
  90. eq.ModifyTime = t
  91. }
  92. eq.PastureId = pa.ID
  93. eq.SHStatus = 1
  94. eq.Status = 17
  95. eq.EqName = p.Name
  96. eq.AssetCode = p.AssetCode
  97. eq.FinanceCode = p.AssetCode
  98. eq.SonCode = p.SonCode
  99. eq.EqCode = fmt.Sprintf("%s-%s", p.AssetCode, p.SonCode)
  100. eq.EqClassName = p.Type
  101. eq.Specification = p.Model
  102. eq.EntranceDate = p.BuyDate
  103. eq.Yuanzhi = p.BuyValue
  104. eq.CompanyCode = p.CompanyCode
  105. eq.Quantity = p.Quantity
  106. eq.CostCenter = p.CostCenter
  107. eq.Unit = p.Unit
  108. eq.ProfitCenter = p.ProfitCenter
  109. eq.EqCode = p.AssetCode
  110. eq.ProId = pv.ID
  111. if p.Dflag == "" && p.TransferFlag != "X" {
  112. eq.Enable = 1
  113. }
  114. // pp.Print(eq)
  115. if e := s.d.DB.Where(model.Equipment{EqCode: eq.EqCode}).Assign(eq).FirstOrCreate(&eq).Error; e != nil {
  116. assetChan <- errors.Wrapf(e, "equipment update : %s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
  117. } else {
  118. log.Infof("equipment update sucess :%s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
  119. assetChan <- nil
  120. }
  121. }