asset.go 3.5 KB

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