123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- package service
- import (
- "encoding/json"
- "fmt"
- "kpt.xdmy/apiserver/config"
- "strconv"
- "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 = "20221101"
- //r.Data.BudatE = "20221125"
- 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" {
- 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, p.AssetCode, p.SonCode)
- oldeq := new(model.Equipment)
- s.d.DB.Where("financeCode = ? ", eq.FinanceCode).First(&oldeq)
- if oldeq.EqName == "" {
- eq.EqName = p.Name
- 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.PurchaseDate = p.BuyDate
- eq.EntranceDate = p.BuyDate
- }
- eq.Yuanzhi = p.BuyValue
- eq.Quantity = p.Quantity
- eq.Salvage = p.Salvage
- eq.PastureId = pa.ID
- dep := new(model.Department)
- s.d.DB.Where("costCenter_code = ? ", p.CostCenter).First(&dep)
- if dep.ID > 0 {
- eq.DepartmentId = strconv.Itoa(dep.ID)
- eq.DepartmentName = dep.Name
- }
- if p.ORD41 == "A3" {
- eq.Status = 19
- } else {
- eq.Status = 17
- }
- //if p.BuyValue == "" || p.BuyValue == "0" || p.BuyValue == "0.00 " {
- // return
- //}
- fmt.Println(eq)
- if oldeq.Id == 0 {
- 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.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"`
- //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"`
- }
- 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)
- }
- }
|