asset.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "kpt.xdmy/apiserver/config"
  6. "strconv"
  7. "time"
  8. "github.com/k0kubun/pp/v3"
  9. "github.com/pkg/errors"
  10. _ "github.com/go-sql-driver/mysql"
  11. log "github.com/sirupsen/logrus"
  12. "kpt.xdmy/apiserver/model"
  13. "kpt.xdmy/apiserver/model/http"
  14. )
  15. //var assetChan chan error
  16. // sap 设备资产数据拉取
  17. // 定时拉取,参数 为空;手动拉取可以传入参数
  18. // 参数:时间,设备编码
  19. func (s *Service) SapAsset(t time.Time, code string, companyCode string) {
  20. //assetChan = make(chan error, 10)
  21. r := new(http.AssetReq)
  22. rp := new(http.AssetResp)
  23. var e error
  24. r.Dest.DestID = "EQMAN"
  25. r.Dest.BussTp = "FI004"
  26. //r.Dest.Url = "http://192.168.61.117/SAPP0/EAM/FI004/QueryAsset"
  27. r.Dest.Url = fmt.Sprintf("%s/EAM/FI004/QueryAsset", config.Conf.Http.Routing)
  28. if t.IsZero() {
  29. t = time.Now()
  30. }
  31. r.Data.BudatB = t.Format("20060102")
  32. r.Data.BudatE = t.Format("20060102")
  33. r.Data.EqType = []http.ANLKL{}
  34. //r.Data.BudatB = "20221101"
  35. //r.Data.BudatE = "20221125"
  36. r.Data.CompanyCode = []http.Company{
  37. {Code: companyCode},
  38. }
  39. if code != "" {
  40. r.Data.Codes = []http.TANLN1{{Code: code}}
  41. }
  42. rbyte, _ := json.Marshal(r)
  43. fmt.Println(string(rbyte))
  44. if e = s.SyncSap(r, rp, rbyte); e == nil {
  45. if rp.Dest.Status == "S" {
  46. log.Infof("sap asset success sum=%d", len(rp.Data.Master))
  47. } else {
  48. log.Infof("sap asset fail", rp.Dest.MessText)
  49. pp.Print(r)
  50. }
  51. } else {
  52. log.Error(e)
  53. }
  54. var update error
  55. var count int
  56. for _, v := range rp.Data.Master {
  57. if v.BuyValue == "" || v.BuyValue == "0" {
  58. continue
  59. }
  60. update = nil
  61. //v.CompanyCode = companyCode
  62. updateEQ(&v)
  63. //update = <-assetChan
  64. //if update != nil {
  65. // log.Error(update)
  66. // count++
  67. // l := model.SapDetailLog{Name: "asset", Code: fmt.Sprintf("%s-%s", v.AssetCode, v.SonCode), ErrorText: update.Error()}
  68. // if e := s.d.DB.Create(&l); e != nil {
  69. // log.Errorf("add sapdetail log: %v", e)
  70. // }
  71. //}
  72. }
  73. if e != nil {
  74. log.Error(e)
  75. } else if update != nil {
  76. e = errors.Errorf("asset update fail sum:%d", count)
  77. } else {
  78. log.Infof("asset update success sum:%d", len(rp.Data.Master)-count)
  79. }
  80. s.AddSapLog(r, e)
  81. return
  82. }
  83. // 更新设备资产数据
  84. func updateEQ(p *http.Asset) {
  85. eq := new(Equipment)
  86. pa := new(model.Pasture)
  87. //pv := new(model.Provider)
  88. if p.CompanyCode == "1002" || p.CompanyCode == "1042" || p.CompanyCode == "3003" || p.CompanyCode == "1036" {
  89. if e := s.d.DB.Where(" company_code = ?", p.CompanyCode).Where(" lrzx = ?", p.ProfitCenter).First(pa).Error; e != nil {
  90. //assetChan <- errors.Wrapf(e, "pasture companycode:%s", p.CompanyCode)
  91. }
  92. } else {
  93. if e := s.d.DB.First(pa, model.Pasture{CompanyCode: p.CompanyCode}).Error; e != nil {
  94. //assetChan <- errors.Wrapf(e, "pasture companycode:%s", p.CompanyCode)
  95. }
  96. }
  97. //if e := s.d.DB.Table("provider").Where("providerNumber = ? ",p.Supplier).First(pv, model.Provider{ProviderNumber: }).Error; e != nil {
  98. // //assetChan <- errors.Wrapf(e, "Provider ProviderNumber:%s", p.Supplier)
  99. //}
  100. if len(p.Udate) == 8 {
  101. p.Udate = p.Udate[:4] + "-" + p.Udate[4:6] + "-" + p.Udate[6:]
  102. }
  103. if len(p.Utime) == 6 {
  104. p.Utime = p.Utime[:2] + ":" + p.Utime[2:4] + ":" + p.Utime[4:]
  105. }
  106. if p.Utime != "" {
  107. p.Udate = p.Udate + " " + p.Utime
  108. }
  109. if t, e := time.Parse("2006-01-02 15:04:05", p.Udate); e != nil {
  110. //assetChan <- errors.Errorf("util ParseDateAndTime : %s, %s", p.Udate)
  111. } else {
  112. eq.ModifyTime = t
  113. }
  114. eq.FinanceCode = fmt.Sprintf("%s-%s-%s", p.CompanyCode, p.AssetCode, p.SonCode)
  115. oldeq := new(model.Equipment)
  116. s.d.DB.Where("financeCode = ? ", eq.FinanceCode).First(&oldeq)
  117. if oldeq.EqName == "" {
  118. eq.EqName = p.Name
  119. eq.Specification = p.Model
  120. //eq.ProId = p.ID
  121. eq.Unit = p.Unit
  122. eq.EqClassName = p.Type
  123. eq.CostCenter = p.CostCenter
  124. eq.ProfitCenter = p.ProfitCenter
  125. eq.SapCode = p.AssetCode
  126. eq.SonCode = p.SonCode
  127. eq.Enable = 1
  128. eq.CompanyCode = p.CompanyCode
  129. eq.Depreciation = p.DepreciationYear
  130. eq.SHStatus = 1
  131. eq.Status = 17
  132. eq.PurchaseDate = p.BuyDate
  133. eq.EntranceDate = p.BuyDate
  134. }
  135. eq.Yuanzhi = p.BuyValue
  136. eq.Quantity = p.Quantity
  137. eq.PastureId = pa.ID
  138. eq.Salvage = p.Salvage
  139. //eq.EntranceDate = p.BuyDate
  140. dep := new(model.Department)
  141. s.d.DB.Where("costCenter_code = ? ", p.CostCenter).First(&dep)
  142. if dep.ID > 0 {
  143. eq.DepartmentId = strconv.Itoa(dep.ID)
  144. eq.DepartmentName = dep.Name
  145. }
  146. if p.TransferFlag == "X" {
  147. eq.Statue = 19
  148. }
  149. if p.BuyValue == "" || p.BuyValue == "0" || p.BuyValue == "0.00 " {
  150. return
  151. }
  152. fmt.Println(eq)
  153. if oldeq.Id == 0 {
  154. if e := s.d.DB.Where(model.Equipment{FinanceCode: eq.FinanceCode}).Assign(eq).FirstOrCreate(&eq).Error; e != nil {
  155. //assetChan <- errors.Wrapf(e, "equipment update : %s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
  156. } else {
  157. log.Infof("equipment update sucess :%s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
  158. //assetChan <- nil
  159. }
  160. } else {
  161. s.d.DB.Where(model.Equipment{FinanceCode: eq.FinanceCode}).Updates(&eq)
  162. }
  163. }
  164. type Equipment struct {
  165. PastureId int `gorm:"column:pastureId" json:"pastureId"`
  166. PastureName string `gorm:"column:pastureName" json:"pastureName"`
  167. EqName string `gorm:"column:eqName" json:"eqName"`
  168. //AssetCode interface{} `gorm:"column:assetCode" json:"assetCode"`
  169. EqClassName string `gorm:"column:eqClassName" json:"eqClassName"`
  170. Specification string `gorm:"column:specification" json:"specification"`
  171. DepartmentId string `gorm:"column:departmentId" json:"departmentId"`
  172. DepartmentName string `gorm:"column:departmentName" json:"departmentName"`
  173. EntranceDate string `gorm:"column:entranceDate" json:"entranceDate"`
  174. Purpose string `gorm:"column:purpose" json:"purpose"`
  175. Yuanzhi string `gorm:"column:yuanzhi" json:"yuanzhi"`
  176. Id int `gorm:"column:id" json:"id"`
  177. //EqCode interface{} `gorm:"column:eqCode" json:"eqCode"`
  178. EqClassId int `gorm:"column:eqClassId" json:"eqClassId"`
  179. FinanceCode string `gorm:"column:financeCode" json:"financeCode"`
  180. ProName string `gorm:"column:proName" json:"proName"`
  181. ProId int `gorm:"column:proId" json:"proId"`
  182. BrandId string `gorm:"column:brandId" json:"brandId"`
  183. Brand string `gorm:"column:brand" json:"brand"`
  184. PurchaseDate string `gorm:"column:purchaseDate" json:"purchaseDate"`
  185. Subtractvalue string `gorm:"column:subtractvalue" json:"subtractvalue"`
  186. Salvage string `gorm:"column:salvage" json:"salvage"`
  187. Upkeepgrade string `gorm:"column:upkeepgrade" json:"upkeepgrade"`
  188. YearMaintainCost string `gorm:"column:yearMaintainCost" json:"yearMaintainCost"`
  189. YearUpkeepCost string `gorm:"column:yearUpkeepCost" json:"yearUpkeepCost"`
  190. Status int `gorm:"column:status" json:"status"`
  191. EmployeName string `gorm:"column:employeName" json:"employeName"`
  192. EmployeeId string `gorm:"column:employeeId" json:"employeeId"`
  193. InputDatetime string `gorm:"column:inputDatetime" json:"inputDatetime"`
  194. LeaveDate time.Time `gorm:"column:leaveDate" json:"leaveDate"`
  195. InputUser string `gorm:"column:inputUser" json:"inputUser"`
  196. EnabledTime time.Time `gorm:"column:enabledTime" json:"enabledTime"`
  197. BlockTime time.Time `gorm:"column:blockTime" json:"blockTime"`
  198. ChangeStatue string `gorm:"column:changeStatue" json:"changeStatue"`
  199. EnabledPerson int `gorm:"column:enabledPerson" json:"enabledPerson"`
  200. BlockPerson int `gorm:"column:blockPerson" json:"blockPerson"`
  201. SttId int `gorm:"column:sttId" json:"sttId"`
  202. BaseHours string `gorm:"column:baseHours" json:"baseHours"`
  203. ApplicatId int `gorm:"column:applicatId" json:"applicatId"`
  204. ApplicatDate time.Time `gorm:"column:applicatDate" json:"applicatDate"`
  205. WorkflowId int `gorm:"column:workflowId" json:"workflowId"`
  206. FlowCompeleted int `gorm:"column:flowCompeleted" json:"flowCompeleted"`
  207. Enable int `gorm:"column:enable" json:"enable"`
  208. ChangeTime time.Time `gorm:"column:changeTime" json:"changeTime"`
  209. SSTstatue string `gorm:"column:SSTstatue" json:"SSTstatue"`
  210. SHStatus int `gorm:"column:SHStatus" json:"SHStatus"`
  211. ChargeId int `gorm:"column:chargeId" json:"chargeId"`
  212. ChargeDate time.Time `gorm:"column:chargeDate" json:"chargeDate"`
  213. WorkflowNote string `gorm:"column:workflowNote" json:"workflowNote"`
  214. Picpath string `gorm:"column:picpath" json:"picpath"`
  215. Depreciation string `gorm:"column:depreciation" json:"depreciation"`
  216. Statue int `gorm:"column:statue" json:"statue"`
  217. ModifyTime time.Time `gorm:"column:modifyTime" json:"modifyTime"`
  218. CompanyCode string `gorm:"column:companyCode" json:"companyCode"`
  219. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  220. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  221. Quantity string `gorm:"column:quantity" json:"quantity"`
  222. CostCenter string `gorm:"column:costCenter" json:"costCenter"`
  223. Unit string `gorm:"column:unit" json:"unit"`
  224. ProfitCenter string `gorm:"column:profitCenter" json:"profitCenter"`
  225. SonCode string `gorm:"column:sonCode" json:"sonCode"`
  226. SapCode string `gorm:"column:sapCode" json:"sapCode"`
  227. }