123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package service
- import (
- "fmt"
- "time"
- "github.com/k0kubun/pp/v3"
- "github.com/pkg/errors"
- _ "github.com/go-sql-driver/mysql"
- log "github.com/sirupsen/logrus"
- "kpt.xdmy/apiserver/model"
- "kpt.xdmy/apiserver/model/http"
- )
- var assetChan chan error
- // sap 设备资产数据拉取
- // 定时拉取,参数 为空;手动拉取可以传入参数
- // 参数:时间,设备编码
- func (s *Service) SapAsset(t time.Time, code string) {
- assetChan = make(chan error, 10)
- r := new(http.AssetReq)
- rp := new(http.AssetResp)
- var e error
- r.Dest.DestID = "EQMAN"
- r.Dest.BussTp = "FI004"
- r.Dest.Url = "https://app.modernfarming.cn:7443/sap/EAM/FI004/QueryAsset"
- if t.IsZero() {
- t = time.Now()
- }
- r.Data.BudatB = t.Format("20060102")
- r.Data.BudatE = t.Format("20060102")
- r.Data.CompanyCode = []http.Company{
- // {Code: "9099"}, {Code: "1006"}
- {Code: "1002"},
- }
- if code != "" {
- r.Data.Codes = []http.TANLN1{{Code: code}}
- }
- if e = s.SyncSap(r, rp); e == nil {
- if rp.Dest.Status == "S" {
- log.Infof("sap asset success sum=%d", len(rp.Data.Master))
- } else {
- log.Infof("sap asset fail", rp.Dest.MessText)
- pp.Print(r)
- }
- } else {
- log.Error(e)
- }
- var update error
- var count int
- for _, v := range rp.Data.Master {
- update = nil
- go updateEQ(&v)
- update = <-assetChan
- if update != nil {
- log.Error(update)
- count++
- l := model.SapDetailLog{Name: "asset", Code: fmt.Sprintf("%s-%s", v.AssetCode, v.SonCode), ErrorText: update.Error()}
- if e := s.d.DB.Create(&l); e != nil {
- log.Errorf("add sapdetail log: %v", e)
- }
- }
- }
- if e != nil {
- log.Error(e)
- } else if update != nil {
- e = errors.Errorf("asset update fail sum:%d", count)
- } else {
- log.Infof("asset update success sum:%d", len(rp.Data.Master)-count)
- }
- s.AddSapLog(r, e)
- return
- }
- // 更新设备资产数据
- func updateEQ(p *http.Asset) {
- eq := new(model.Equipment)
- pa := new(model.Pasture)
- pv := new(model.Provider)
- if e := s.d.DB.First(pa, model.Pasture{CompanyCode: p.CompanyCode}).Error; e != nil {
- assetChan <- errors.Wrapf(e, "pasture companycode:%s", p.CompanyCode)
- }
- if e := s.d.DB.First(pa, model.Provider{ProviderNumber: p.Supplier}).Error; e != nil {
- assetChan <- errors.Wrapf(e, "Provider ProviderNumber:%s", p.Supplier)
- }
- if len(p.Udate) == 8 {
- p.Udate = p.Udate[:4] + "-" + p.Udate[4:6] + "-" + p.Udate[6:]
- }
- if len(p.Utime) == 6 {
- p.Utime = p.Utime[:2] + ":" + p.Utime[2:4] + ":" + p.Utime[4:]
- }
- if p.Utime != "" {
- p.Udate = p.Udate + " " + p.Utime
- }
- if t, e := time.Parse("2006-01-02 15:04:05", p.Udate); e != nil {
- assetChan <- errors.Errorf("util ParseDateAndTime : %s, %s", p.Udate)
- } else {
- eq.ModifyTime = t
- }
- eq.PastureId = pa.ID
- eq.SHStatus = 1
- eq.Status = 17
- eq.EqName = p.Name
- eq.AssetCode = p.AssetCode
- eq.FinanceCode = p.AssetCode
- eq.SonCode = p.SonCode
- eq.EqCode = fmt.Sprintf("%s-%s", p.AssetCode, p.SonCode)
- eq.EqClassName = p.Type
- eq.Specification = p.Model
- eq.EntranceDate = p.BuyDate
- eq.Yuanzhi = p.BuyValue
- eq.CompanyCode = p.CompanyCode
- eq.Quantity = p.Quantity
- eq.CostCenter = p.CostCenter
- eq.Unit = p.Unit
- eq.ProfitCenter = p.ProfitCenter
- eq.EqCode = p.AssetCode
- eq.ProId = pv.ID
- if p.Dflag == "" && p.TransferFlag != "X" {
- eq.Enable = 1
- }
- if e := s.d.DB.Where(model.Equipment{EqCode: eq.EqCode}).Assign(eq).FirstOrCreate(&eq).Error; e != nil {
- assetChan <- errors.Wrapf(e, "equipment update : %s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
- } else {
- log.Infof("equipment update sucess :%s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
- assetChan <- nil
- }
- }
|