asset.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "kpt.xdmy/apiserver/config"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/k0kubun/pp/v3"
  10. "github.com/pkg/errors"
  11. _ "github.com/go-sql-driver/mysql"
  12. log "github.com/sirupsen/logrus"
  13. "kpt.xdmy/apiserver/model"
  14. "kpt.xdmy/apiserver/model/http"
  15. )
  16. //var assetChan chan error
  17. // sap 设备资产数据拉取
  18. // 定时拉取,参数 为空;手动拉取可以传入参数
  19. // 参数:时间,设备编码
  20. func (s *Service) SapAsset(t time.Time, code string, companyCode string) {
  21. //assetChan = make(chan error, 10)
  22. r := new(http.AssetReq)
  23. rp := new(http.AssetResp)
  24. var e error
  25. r.Dest.DestID = "EQMAN"
  26. r.Dest.BussTp = "FI004"
  27. //r.Dest.Url = "http://192.168.61.117/SAPP0/EAM/FI004/QueryAsset"
  28. r.Dest.Url = fmt.Sprintf("%s/EAM/FI004/QueryAsset", config.Conf.Http.Routing)
  29. if t.IsZero() {
  30. t = time.Now()
  31. }
  32. r.Data.BudatB = t.Format("20060102")
  33. r.Data.BudatE = t.Format("20060102")
  34. r.Data.EqType = []http.ANLKL{}
  35. //r.Data.BudatB = "20230929"
  36. //r.Data.BudatE = "20230929"
  37. r.Data.CompanyCode = []http.Company{
  38. {Code: companyCode},
  39. }
  40. if code != "" {
  41. r.Data.Codes = []http.TANLN1{{Code: code}}
  42. }
  43. rbyte, _ := json.Marshal(r)
  44. fmt.Println(string(rbyte))
  45. if e = s.SyncSap(r, rp, rbyte); e == nil {
  46. if rp.Dest.Status == "S" {
  47. log.Infof("sap asset success sum=%d", len(rp.Data.Master))
  48. } else {
  49. log.Infof("sap asset fail", rp.Dest.MessText)
  50. pp.Print(r)
  51. }
  52. } else {
  53. log.Error(e)
  54. }
  55. var update error
  56. var count int
  57. for _, v := range rp.Data.Master {
  58. if v.BuyValue == "" || v.BuyValue == "0" {
  59. continue
  60. }
  61. update = nil
  62. //v.CompanyCode = companyCode
  63. updateEQ(&v)
  64. //update = <-assetChan
  65. //if update != nil {
  66. // log.Error(update)
  67. // count++
  68. // l := model.SapDetailLog{Name: "asset", Code: fmt.Sprintf("%s-%s", v.AssetCode, v.SonCode), ErrorText: update.Error()}
  69. // if e := s.d.DB.Create(&l); e != nil {
  70. // log.Errorf("add sapdetail log: %v", e)
  71. // }
  72. //}
  73. }
  74. if e != nil {
  75. log.Error(e)
  76. } else if update != nil {
  77. e = errors.Errorf("asset update fail sum:%d", count)
  78. } else {
  79. log.Infof("asset update success sum:%d", len(rp.Data.Master)-count)
  80. }
  81. s.AddSapLog(r, e)
  82. return
  83. }
  84. // 更新设备资产数据
  85. func updateEQ(p *http.Asset) {
  86. eq := new(Equipment)
  87. pa := new(model.Pasture)
  88. //pv := new(model.Provider)
  89. if p.CompanyCode == "1002" || p.CompanyCode == "1042" || p.CompanyCode == "3003" || p.CompanyCode == "1036" || p.CompanyCode == "1057" {
  90. if e := s.d.DB.Where(" company_code = ?", p.CompanyCode).Where(" lrzx = ?", p.ProfitCenter).First(pa).Error; e != nil {
  91. //assetChan <- errors.Wrapf(e, "pasture companycode:%s", p.CompanyCode)
  92. }
  93. } else {
  94. if e := s.d.DB.First(pa, model.Pasture{CompanyCode: p.CompanyCode}).Error; e != nil {
  95. //assetChan <- errors.Wrapf(e, "pasture companycode:%s", p.CompanyCode)
  96. }
  97. }
  98. //if e := s.d.DB.Table("provider").Where("providerNumber = ? ",p.Supplier).First(pv, model.Provider{ProviderNumber: }).Error; e != nil {
  99. // //assetChan <- errors.Wrapf(e, "Provider ProviderNumber:%s", p.Supplier)
  100. //}
  101. if len(p.Udate) == 8 {
  102. p.Udate = p.Udate[:4] + "-" + p.Udate[4:6] + "-" + p.Udate[6:]
  103. }
  104. if len(p.Utime) == 6 {
  105. p.Utime = p.Utime[:2] + ":" + p.Utime[2:4] + ":" + p.Utime[4:]
  106. }
  107. if p.Utime != "" {
  108. p.Udate = p.Udate + " " + p.Utime
  109. }
  110. if t, e := time.Parse("2006-01-02 15:04:05", p.Udate); e != nil {
  111. //assetChan <- errors.Errorf("util ParseDateAndTime : %s, %s", p.Udate)
  112. } else {
  113. eq.ModifyTime = t
  114. }
  115. eq.FinanceCode = fmt.Sprintf("%s-%s-%s", p.CompanyCode, strings.ReplaceAll(p.AssetCode, " ", ""), strings.ReplaceAll(p.SonCode, " ", ""))
  116. oldeq := new(model.Equipment)
  117. s.d.DB.Where("financeCode = ? ", eq.FinanceCode).First(&oldeq)
  118. if oldeq.EqName == "" {
  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. }
  133. eq.EqName = p.Name
  134. eq.Yuanzhi = p.BuyValue
  135. eq.Quantity = p.Quantity
  136. eq.Purpose = p.ZUSAGE
  137. if p.ZSELFNB != "" {
  138. eq.EqCode = p.ZSELFNB
  139. }
  140. //ZUSAGE string `json:"ZUSAGE"` // 用途
  141. //ZSELFNB string `json:"ZSELFNB"` // 自编号
  142. //eq.Salvage = p.Salvage
  143. eq.PastureId = pa.ID
  144. eq.PastureName = pa.Name
  145. eq.PurchaseDate = p.BuyDate
  146. eq.EntranceDate = p.BuyDate
  147. eq.Subtractvalue = p.Salvage + "%"
  148. dep := new(model.Department)
  149. if p.CostCenter != "" {
  150. s.d.DB.Where("costCenter_code = ? ", p.CostCenter).First(&dep)
  151. if dep.ID > 0 {
  152. //eq.DepartmentId = strconv.Itoa(dep.ID)
  153. //eq.DepartmentName = dep.Name
  154. eq.DeptId = strconv.Itoa(dep.ID)
  155. }
  156. }
  157. department := new(model.Department)
  158. s.d.DB.Where("costCenter_code = ? ", p.KOSTL).First(&department)
  159. //if dep.ID > 0 {
  160. //eq.DeptId = strconv.Itoa(department.ID)
  161. eq.DepartmentId = strconv.Itoa(department.ID)
  162. eq.DepartmentName = department.Name
  163. //}
  164. if dep.ID == 0 {
  165. eq.DeptId = strconv.Itoa(department.ID)
  166. //eq.DepartmentName = department.Name
  167. }
  168. if p.ORD41 == "A3" {
  169. eq.Status = 19
  170. } else if p.ORD41 == "A4" {
  171. eq.Status = 18
  172. } else if p.ORD41 == "A2" {
  173. eq.Status = 234
  174. } else {
  175. eq.Status = 17
  176. }
  177. //ORD44 XSPEB
  178. if p.Dflag == "X" || p.TransferFlag == "X" {
  179. eq.Enable = 0
  180. }
  181. //if p.BuyValue == "" || p.BuyValue == "0" || p.BuyValue == "0.00 " {
  182. // return
  183. //}
  184. //if eq.FinanceCode == "1008-41201000494-0" {
  185. // eq.EqCode = "00a000012"
  186. //}
  187. fmt.Println(eq)
  188. if oldeq.Id == 0 {
  189. //if p.ZSELFNB == "" {
  190. // //eq.EqCode = "NULL"
  191. //}
  192. if e := s.d.DB.Where(model.Equipment{FinanceCode: eq.FinanceCode}).Assign(eq).FirstOrCreate(&eq).Error; e != nil {
  193. //assetChan <- errors.Wrapf(e, "equipment update : %s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
  194. } else {
  195. log.Infof("equipment update sucess :%s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
  196. //assetChan <- nil
  197. }
  198. } else {
  199. eq.Id = oldeq.Id
  200. err := s.d.DB.Debug().Model(eq).Where("id = ? ", oldeq.Id).Updates(eq).Error
  201. if err != nil {
  202. log.Error(err)
  203. }
  204. }
  205. }
  206. type Equipment struct {
  207. PastureId int `gorm:"column:pastureId" json:"pastureId"`
  208. PastureName string `gorm:"column:pastureName" json:"pastureName"`
  209. EqName string `gorm:"column:eqName" json:"eqName"`
  210. EqCode string `gorm:"default:NULL;column:eqCode" json:"eqCode"`
  211. //AssetCode interface{} `gorm:"column:assetCode" json:"assetCode"`
  212. EqClassName string `gorm:"column:eqClassName" json:"eqClassName"`
  213. Specification string `gorm:"column:specification" json:"specification"`
  214. DepartmentId string `gorm:"column:departmentId" json:"departmentId"`
  215. DepartmentName string `gorm:"column:departmentName" json:"departmentName"`
  216. EntranceDate string `gorm:"column:entranceDate" json:"entranceDate"`
  217. Purpose string `gorm:"column:purpose" json:"purpose"`
  218. Yuanzhi string `gorm:"column:yuanzhi" json:"yuanzhi"`
  219. Id int `gorm:"column:id" json:"id"`
  220. EqClassId int `gorm:"column:eqClassId" json:"eqClassId"`
  221. FinanceCode string `gorm:"column:financeCode" json:"financeCode"`
  222. ProName string `gorm:"column:proName" json:"proName"`
  223. ProId int `gorm:"column:proId" json:"proId"`
  224. BrandId string `gorm:"column:brandId" json:"brandId"`
  225. Brand string `gorm:"column:brand" json:"brand"`
  226. PurchaseDate string `gorm:"column:purchaseDate" json:"purchaseDate"`
  227. Subtractvalue string `gorm:"column:subtractvalue" json:"subtractvalue"`
  228. Salvage string `gorm:"column:salvage" json:"salvage"`
  229. Upkeepgrade string `gorm:"column:upkeepgrade" json:"upkeepgrade"`
  230. YearMaintainCost string `gorm:"column:yearMaintainCost" json:"yearMaintainCost"`
  231. YearUpkeepCost string `gorm:"column:yearUpkeepCost" json:"yearUpkeepCost"`
  232. Status int `gorm:"column:status" json:"status"`
  233. EmployeName string `gorm:"column:employeName" json:"employeName"`
  234. EmployeeId string `gorm:"column:employeeId" json:"employeeId"`
  235. InputDatetime string `gorm:"column:inputDatetime" json:"inputDatetime"`
  236. LeaveDate time.Time `gorm:"column:leaveDate" json:"leaveDate"`
  237. InputUser string `gorm:"column:inputUser" json:"inputUser"`
  238. EnabledTime time.Time `gorm:"column:enabledTime" json:"enabledTime"`
  239. BlockTime time.Time `gorm:"column:blockTime" json:"blockTime"`
  240. ChangeStatue string `gorm:"column:changeStatue" json:"changeStatue"`
  241. EnabledPerson int `gorm:"column:enabledPerson" json:"enabledPerson"`
  242. BlockPerson int `gorm:"column:blockPerson" json:"blockPerson"`
  243. SttId int `gorm:"column:sttId" json:"sttId"`
  244. BaseHours string `gorm:"column:baseHours" json:"baseHours"`
  245. ApplicatId int `gorm:"column:applicatId" json:"applicatId"`
  246. ApplicatDate time.Time `gorm:"column:applicatDate" json:"applicatDate"`
  247. WorkflowId int `gorm:"column:workflowId" json:"workflowId"`
  248. FlowCompeleted int `gorm:"column:flowCompeleted" json:"flowCompeleted"`
  249. Enable int `gorm:"column:enable" json:"enable"`
  250. ChangeTime time.Time `gorm:"column:changeTime" json:"changeTime"`
  251. SSTstatue string `gorm:"column:SSTstatue" json:"SSTstatue"`
  252. SHStatus int `gorm:"column:SHStatus" json:"SHStatus"`
  253. ChargeId int `gorm:"column:chargeId" json:"chargeId"`
  254. ChargeDate time.Time `gorm:"column:chargeDate" json:"chargeDate"`
  255. WorkflowNote string `gorm:"column:workflowNote" json:"workflowNote"`
  256. Picpath string `gorm:"column:picpath" json:"picpath"`
  257. Depreciation string `gorm:"column:depreciation" json:"depreciation"`
  258. Statue int `gorm:"column:statue" json:"statue"`
  259. ModifyTime time.Time `gorm:"column:modifyTime" json:"modifyTime"`
  260. CompanyCode string `gorm:"column:companyCode" json:"companyCode"`
  261. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  262. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  263. Quantity string `gorm:"column:quantity" json:"quantity"`
  264. CostCenter string `gorm:"column:costCenter" json:"costCenter"`
  265. Unit string `gorm:"column:unit" json:"unit"`
  266. ProfitCenter string `gorm:"column:profitCenter" json:"profitCenter"`
  267. SonCode string `gorm:"column:sonCode" json:"sonCode"`
  268. SapCode string `gorm:"column:sapCode" json:"sapCode"`
  269. DeptId string `gorm:"column:deptId" json:"deptId"`
  270. }
  271. func (t *Equipment) TableName() string {
  272. return "equipment"
  273. }
  274. // 更新设备维修和保养费用
  275. func updateEqYearMaintainCostAndYearUpkeepCost(pastureId int, start, end string) {
  276. //eqList := make([]model.Equipment, 0)
  277. //s.d.DB.Raw(` select id from equipment where pastureid = ? `, pastureId).Find(&eqList)
  278. //for _, eq := range eqList {
  279. //更新设备维修费用
  280. err := s.d.DB.Debug().Exec(`update equipment e set yearMaintainCost = (
  281. SELECT
  282. sum( p.sumPrice )
  283. FROM
  284. bigpartuse bp
  285. JOIN partuse p ON p.bigid = bp.id
  286. WHERE
  287. p.eqCode = e.eqCode and bp.pastureId = ?
  288. AND
  289. IF
  290. ((
  291. SELECT
  292. count( 1 )
  293. FROM
  294. bigpartapply bpa
  295. WHERE
  296. bpa.applyCode = bp.oddCode and bpa.pastureId = bp.pastureId
  297. AND bpa.applyType = 1
  298. ) > 0 or (select count(1) from bigoutsourcing bo where bo.outsourcingCode = bp.oddCode ) >0,
  299. 1,
  300. 0
  301. ) = 1 and bp.receiveTime between ? and ? and p.dflag = 0 ) where e.pastureId = ?`, pastureId, start, end, pastureId).Error
  302. if err != nil {
  303. log.Error(err)
  304. }
  305. //更新设备保养费用
  306. err = s.d.DB.Debug().Exec(`update equipment e set yearUpkeepCost = (
  307. SELECT
  308. sum( p.sumPrice )
  309. FROM
  310. bigpartuse bp
  311. JOIN partuse p ON p.bigid = bp.id
  312. WHERE
  313. p.eqCode = e.eqCode and bp.pastureId = ?
  314. AND
  315. IF
  316. ((
  317. SELECT
  318. count( 1 )
  319. FROM
  320. bigpartapply bpa
  321. WHERE
  322. bpa.applyCode = bp.oddCode and bpa.pastureId = bp.pastureId
  323. AND bpa.applyType = 2
  324. ) > 0,
  325. 1,
  326. 0
  327. ) = 1 and bp.receiveTime between ? and ? and p.dflag = 0 ) where e.pastureId = ?`, pastureId, start, end, pastureId).Error
  328. if err != nil {
  329. log.Error(err)
  330. }
  331. //}
  332. err = s.d.DB.Debug().Exec(`UPDATE equipment
  333. SET leaveDate = DATE_ADD( DATE_FORMAT( entranceDate, '%Y-%m-%d' ), INTERVAL depreciation * 12 MONTH )
  334. WHERE
  335. entranceDate IS NOT NULL
  336. AND entranceDate != '' and entranceDate != '0000-00-00' and entranceDate != '00000000' and entranceDate != 'null'
  337. AND depreciation IS NOT NULL
  338. AND depreciation != '' and pastureId = ? `, pastureId).Error
  339. if err != nil {
  340. log.Error(err)
  341. }
  342. }
  343. func (s *Service) SapAssetSalvage(t time.Time, companyCode string, pastureId int) {
  344. //assetChan = make(chan error, 10)
  345. r := new(http.AssetSalvageReq)
  346. rp := new(http.AssetSalvageResp)
  347. var e error
  348. r.Dest.DestID = "EQMAN"
  349. r.Dest.BussTp = "FI010"
  350. r.Dest.Url = fmt.Sprintf("%s/EAM/FI010/QueryAssetNetValue", config.Conf.Http.Routing)
  351. r.Data.TBUKRS = []http.TBUKRS{{BUKRS: companyCode}}
  352. r.Data.TGJAHR = []http.TGJAHR{{GJAHR: t.Format("2006")}}
  353. rbyte, _ := json.Marshal(r)
  354. fmt.Println(string(rbyte))
  355. if e = s.SyncSap(r, rp, rbyte); e == nil {
  356. if rp.Dest.Status == "S" {
  357. log.Infof("sap asset success sum=%d", len(rp.Data.TANLA))
  358. } else {
  359. log.Infof("sap asset fail", rp.Dest.MessText)
  360. pp.Print(r)
  361. }
  362. } else {
  363. log.Error(e)
  364. }
  365. var update error
  366. var count int
  367. equipmentList := make([]*Equipment, 0)
  368. s.d.DB.Debug().Where("pastureId = ? ", pastureId).Find(&equipmentList)
  369. for _, eq := range equipmentList {
  370. exist := false
  371. for _, v := range rp.Data.TANLA {
  372. if eq.FinanceCode == fmt.Sprintf("%s-%s-%s", v.BUKRS, v.ANLN1, v.ANLN2) {
  373. exist = true
  374. fmt.Println(fmt.Sprintf("%s-%s-%s", v.BUKRS, v.ANLN1, v.ANLN2), v.ZJZ)
  375. 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
  376. if err != nil {
  377. log.Error(err.Error)
  378. panic("eeee")
  379. }
  380. break
  381. }
  382. }
  383. if !exist {
  384. err := s.d.DB.Debug().Exec(` update equipment set salvage = ? where id = ? `, 0, eq.Id).Error
  385. if err != nil {
  386. log.Error(err.Error)
  387. panic("eeee")
  388. }
  389. }
  390. }
  391. if e != nil {
  392. log.Error(e)
  393. } else if update != nil {
  394. e = errors.Errorf("asset update fail sum:%d", count)
  395. } else {
  396. log.Infof("asset update success sum:%d", len(rp.Data.TANLA)-count)
  397. }
  398. s.AddSapLog(r, e)
  399. return
  400. }