package service import ( "encoding/json" "fmt" "kpt.xdmy/apiserver/config" "strconv" "strings" "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, companyCode 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 = "http://192.168.61.117/SAPP0/EAM/FI004/QueryAsset" r.Dest.Url = fmt.Sprintf("%s/EAM/FI004/QueryAsset", config.Conf.Http.Routing) if t.IsZero() { t = time.Now() } r.Data.BudatB = t.Format("20060102") r.Data.BudatE = t.Format("20060102") r.Data.EqType = []http.ANLKL{} //r.Data.BudatB = "20230929" //r.Data.BudatE = "20230929" r.Data.CompanyCode = []http.Company{ {Code: companyCode}, } if code != "" { r.Data.Codes = []http.TANLN1{{Code: code}} } rbyte, _ := json.Marshal(r) fmt.Println(string(rbyte)) if e = s.SyncSap(r, rp, rbyte); 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 { if v.BuyValue == "" || v.BuyValue == "0" { continue } update = nil //v.CompanyCode = companyCode 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(Equipment) pa := new(model.Pasture) //pv := new(model.Provider) if p.CompanyCode == "1002" || p.CompanyCode == "1042" || p.CompanyCode == "3003" || p.CompanyCode == "1036" || p.CompanyCode == "1057" { if e := s.d.DB.Where(" company_code = ?", p.CompanyCode).Where(" lrzx = ?", p.ProfitCenter).First(pa).Error; e != nil { //assetChan <- errors.Wrapf(e, "pasture companycode:%s", p.CompanyCode) } } else { 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.Table("provider").Where("providerNumber = ? ",p.Supplier).First(pv, model.Provider{ProviderNumber: }).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.FinanceCode = fmt.Sprintf("%s-%s-%s", p.CompanyCode, strings.ReplaceAll(p.AssetCode, " ", ""), strings.ReplaceAll(p.SonCode, " ", "")) oldeq := new(model.Equipment) s.d.DB.Where("financeCode = ? ", eq.FinanceCode).First(&oldeq) if oldeq.EqName == "" { eq.Specification = p.Model //eq.ProId = p.ID eq.Unit = p.Unit eq.EqClassName = p.Type eq.CostCenter = p.CostCenter eq.ProfitCenter = p.ProfitCenter eq.SapCode = p.AssetCode eq.SonCode = p.SonCode eq.Enable = 1 eq.CompanyCode = p.CompanyCode eq.Depreciation = p.DepreciationYear eq.SHStatus = 1 eq.Status = 17 } eq.EqName = p.Name eq.Yuanzhi = p.BuyValue eq.Quantity = p.Quantity eq.Purpose = p.ZUSAGE if p.ZSELFNB != "" { eq.EqCode = p.ZSELFNB } //ZUSAGE string `json:"ZUSAGE"` // 用途 //ZSELFNB string `json:"ZSELFNB"` // 自编号 //eq.Salvage = p.Salvage eq.PastureId = pa.ID eq.PastureName = pa.Name eq.PurchaseDate = p.BuyDate eq.EntranceDate = p.BuyDate eq.Subtractvalue = p.Salvage + "%" dep := new(model.Department) if p.CostCenter != "" { s.d.DB.Where("costCenter_code = ? ", p.CostCenter).First(&dep) if dep.ID > 0 { //eq.DepartmentId = strconv.Itoa(dep.ID) //eq.DepartmentName = dep.Name eq.DeptId = strconv.Itoa(dep.ID) } } department := new(model.Department) s.d.DB.Where("costCenter_code = ? ", p.KOSTL).First(&department) //if dep.ID > 0 { //eq.DeptId = strconv.Itoa(department.ID) eq.DepartmentId = strconv.Itoa(department.ID) eq.DepartmentName = department.Name //} if dep.ID == 0 { eq.DeptId = strconv.Itoa(department.ID) //eq.DepartmentName = department.Name } if p.ORD41 == "A3" { eq.Status = 19 } else if p.ORD41 == "A4" { eq.Status = 18 } else if p.ORD41 == "A2" { eq.Status = 234 } else { eq.Status = 17 } //ORD44 XSPEB if p.Dflag == "X" || p.TransferFlag == "X" { eq.Enable = 0 } //if p.BuyValue == "" || p.BuyValue == "0" || p.BuyValue == "0.00 " { // return //} //if eq.FinanceCode == "1008-41201000494-0" { // eq.EqCode = "00a000012" //} fmt.Println(eq) if oldeq.Id == 0 { //if p.ZSELFNB == "" { // //eq.EqCode = "NULL" //} if e := s.d.DB.Where(model.Equipment{FinanceCode: eq.FinanceCode}).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 } } else { eq.Id = oldeq.Id err := s.d.DB.Debug().Model(eq).Where("id = ? ", oldeq.Id).Updates(eq).Error if err != nil { log.Error(err) } } } type Equipment struct { PastureId int `gorm:"column:pastureId" json:"pastureId"` PastureName string `gorm:"column:pastureName" json:"pastureName"` EqName string `gorm:"column:eqName" json:"eqName"` EqCode string `gorm:"default:NULL;column:eqCode" json:"eqCode"` //AssetCode interface{} `gorm:"column:assetCode" json:"assetCode"` EqClassName string `gorm:"column:eqClassName" json:"eqClassName"` Specification string `gorm:"column:specification" json:"specification"` DepartmentId string `gorm:"column:departmentId" json:"departmentId"` DepartmentName string `gorm:"column:departmentName" json:"departmentName"` EntranceDate string `gorm:"column:entranceDate" json:"entranceDate"` Purpose string `gorm:"column:purpose" json:"purpose"` Yuanzhi string `gorm:"column:yuanzhi" json:"yuanzhi"` Id int `gorm:"column:id" json:"id"` EqClassId int `gorm:"column:eqClassId" json:"eqClassId"` FinanceCode string `gorm:"column:financeCode" json:"financeCode"` ProName string `gorm:"column:proName" json:"proName"` ProId int `gorm:"column:proId" json:"proId"` BrandId string `gorm:"column:brandId" json:"brandId"` Brand string `gorm:"column:brand" json:"brand"` PurchaseDate string `gorm:"column:purchaseDate" json:"purchaseDate"` Subtractvalue string `gorm:"column:subtractvalue" json:"subtractvalue"` Salvage string `gorm:"column:salvage" json:"salvage"` Upkeepgrade string `gorm:"column:upkeepgrade" json:"upkeepgrade"` YearMaintainCost string `gorm:"column:yearMaintainCost" json:"yearMaintainCost"` YearUpkeepCost string `gorm:"column:yearUpkeepCost" json:"yearUpkeepCost"` Status int `gorm:"column:status" json:"status"` EmployeName string `gorm:"column:employeName" json:"employeName"` EmployeeId string `gorm:"column:employeeId" json:"employeeId"` InputDatetime string `gorm:"column:inputDatetime" json:"inputDatetime"` LeaveDate time.Time `gorm:"column:leaveDate" json:"leaveDate"` InputUser string `gorm:"column:inputUser" json:"inputUser"` EnabledTime time.Time `gorm:"column:enabledTime" json:"enabledTime"` BlockTime time.Time `gorm:"column:blockTime" json:"blockTime"` ChangeStatue string `gorm:"column:changeStatue" json:"changeStatue"` EnabledPerson int `gorm:"column:enabledPerson" json:"enabledPerson"` BlockPerson int `gorm:"column:blockPerson" json:"blockPerson"` SttId int `gorm:"column:sttId" json:"sttId"` BaseHours string `gorm:"column:baseHours" json:"baseHours"` ApplicatId int `gorm:"column:applicatId" json:"applicatId"` ApplicatDate time.Time `gorm:"column:applicatDate" json:"applicatDate"` WorkflowId int `gorm:"column:workflowId" json:"workflowId"` FlowCompeleted int `gorm:"column:flowCompeleted" json:"flowCompeleted"` Enable int `gorm:"column:enable" json:"enable"` ChangeTime time.Time `gorm:"column:changeTime" json:"changeTime"` SSTstatue string `gorm:"column:SSTstatue" json:"SSTstatue"` SHStatus int `gorm:"column:SHStatus" json:"SHStatus"` ChargeId int `gorm:"column:chargeId" json:"chargeId"` ChargeDate time.Time `gorm:"column:chargeDate" json:"chargeDate"` WorkflowNote string `gorm:"column:workflowNote" json:"workflowNote"` Picpath string `gorm:"column:picpath" json:"picpath"` Depreciation string `gorm:"column:depreciation" json:"depreciation"` Statue int `gorm:"column:statue" json:"statue"` ModifyTime time.Time `gorm:"column:modifyTime" json:"modifyTime"` CompanyCode string `gorm:"column:companyCode" json:"companyCode"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` Quantity string `gorm:"column:quantity" json:"quantity"` CostCenter string `gorm:"column:costCenter" json:"costCenter"` Unit string `gorm:"column:unit" json:"unit"` ProfitCenter string `gorm:"column:profitCenter" json:"profitCenter"` SonCode string `gorm:"column:sonCode" json:"sonCode"` SapCode string `gorm:"column:sapCode" json:"sapCode"` DeptId string `gorm:"column:deptId" json:"deptId"` } func (t *Equipment) TableName() string { return "equipment" } // 更新设备维修和保养费用 func updateEqYearMaintainCostAndYearUpkeepCost(pastureId int, start, end string) { //eqList := make([]model.Equipment, 0) //s.d.DB.Raw(` select id from equipment where pastureid = ? `, pastureId).Find(&eqList) //for _, eq := range eqList { //更新设备维修费用 err := s.d.DB.Debug().Exec(`update equipment e set yearMaintainCost = ( SELECT sum( p.sumPrice ) FROM bigpartuse bp JOIN partuse p ON p.bigid = bp.id WHERE p.eqCode = e.eqCode and bp.pastureId = ? AND IF (( SELECT count( 1 ) FROM bigpartapply bpa WHERE bpa.applyCode = bp.oddCode and bpa.pastureId = bp.pastureId AND bpa.applyType = 1 ) > 0 or (select count(1) from bigoutsourcing bo where bo.outsourcingCode = bp.oddCode ) >0, 1, 0 ) = 1 and bp.receiveTime between ? and ? and p.dflag = 0 ) where e.pastureId = ?`, pastureId, start, end, pastureId).Error if err != nil { log.Error(err) } //更新设备保养费用 err = s.d.DB.Debug().Exec(`update equipment e set yearUpkeepCost = ( SELECT sum( p.sumPrice ) FROM bigpartuse bp JOIN partuse p ON p.bigid = bp.id WHERE p.eqCode = e.eqCode and bp.pastureId = ? AND IF (( SELECT count( 1 ) FROM bigpartapply bpa WHERE bpa.applyCode = bp.oddCode and bpa.pastureId = bp.pastureId AND bpa.applyType = 2 ) > 0, 1, 0 ) = 1 and bp.receiveTime between ? and ? and p.dflag = 0 ) where e.pastureId = ?`, pastureId, start, end, pastureId).Error if err != nil { log.Error(err) } //} err = s.d.DB.Debug().Exec(`UPDATE equipment SET leaveDate = DATE_ADD( DATE_FORMAT( entranceDate, '%Y-%m-%d' ), INTERVAL depreciation * 12 MONTH ) WHERE entranceDate IS NOT NULL AND entranceDate != '' and entranceDate != '0000-00-00' and entranceDate != '00000000' and entranceDate != 'null' AND depreciation IS NOT NULL AND depreciation != '' and pastureId = ? `, pastureId).Error if err != nil { log.Error(err) } } func (s *Service) SapAssetSalvage(t time.Time, companyCode string, pastureId int) { //assetChan = make(chan error, 10) r := new(http.AssetSalvageReq) rp := new(http.AssetSalvageResp) var e error r.Dest.DestID = "EQMAN" r.Dest.BussTp = "FI010" r.Dest.Url = fmt.Sprintf("%s/EAM/FI010/QueryAssetNetValue", config.Conf.Http.Routing) r.Data.TBUKRS = []http.TBUKRS{{BUKRS: companyCode}} r.Data.TGJAHR = []http.TGJAHR{{GJAHR: t.Format("2006")}} rbyte, _ := json.Marshal(r) fmt.Println(string(rbyte)) if e = s.SyncSap(r, rp, rbyte); e == nil { if rp.Dest.Status == "S" { log.Infof("sap asset success sum=%d", len(rp.Data.TANLA)) } else { log.Infof("sap asset fail", rp.Dest.MessText) pp.Print(r) } } else { log.Error(e) } var update error var count int equipmentList := make([]*Equipment, 0) s.d.DB.Debug().Where("pastureId = ? ", pastureId).Find(&equipmentList) for _, eq := range equipmentList { exist := false for _, v := range rp.Data.TANLA { if eq.FinanceCode == fmt.Sprintf("%s-%s-%s", v.BUKRS, v.ANLN1, v.ANLN2) { exist = true fmt.Println(fmt.Sprintf("%s-%s-%s", v.BUKRS, v.ANLN1, v.ANLN2), v.ZJZ) err := s.d.DB.Debug().Exec(` update equipment set salvage = ? where financeCode = ? `, strings.ReplaceAll(v.ZJZ, " ", ""), fmt.Sprintf("%s-%s-%s", v.BUKRS, v.ANLN1, v.ANLN2)).Error if err != nil { log.Error(err.Error) panic("eeee") } break } } if !exist { err := s.d.DB.Debug().Exec(` update equipment set salvage = ? where id = ? `, 0, eq.Id).Error if err != nil { log.Error(err.Error) panic("eeee") } } } 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.TANLA)-count) } s.AddSapLog(r, e) return }