asset.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 = "20221101"
  36. //r.Data.BudatE = "20221125"
  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.EqName = p.Name
  120. eq.Specification = p.Model
  121. //eq.ProId = p.ID
  122. eq.Unit = p.Unit
  123. eq.EqClassName = p.Type
  124. eq.CostCenter = p.CostCenter
  125. eq.ProfitCenter = p.ProfitCenter
  126. eq.SapCode = p.AssetCode
  127. eq.SonCode = p.SonCode
  128. eq.Enable = 1
  129. eq.CompanyCode = p.CompanyCode
  130. eq.Depreciation = p.DepreciationYear
  131. eq.SHStatus = 1
  132. eq.Status = 17
  133. }
  134. eq.Yuanzhi = p.BuyValue
  135. eq.Quantity = p.Quantity
  136. //eq.Salvage = p.Salvage
  137. eq.PastureId = pa.ID
  138. eq.PastureName = pa.Name
  139. eq.PurchaseDate = p.BuyDate
  140. eq.EntranceDate = p.BuyDate
  141. eq.Subtractvalue = p.Salvage + "%"
  142. dep := new(model.Department)
  143. s.d.DB.Where("costCenter_code = ? ", p.CostCenter).First(&dep)
  144. if dep.ID > 0 {
  145. eq.DepartmentId = strconv.Itoa(dep.ID)
  146. eq.DepartmentName = dep.Name
  147. }
  148. if p.ORD41 == "A3" {
  149. eq.Status = 19
  150. } else {
  151. eq.Status = 17
  152. }
  153. //ORD44 XSPEB
  154. if p.Dflag == "X" || p.TransferFlag == "X" {
  155. eq.Enable = 0
  156. }
  157. //if p.BuyValue == "" || p.BuyValue == "0" || p.BuyValue == "0.00 " {
  158. // return
  159. //}
  160. fmt.Println(eq)
  161. if oldeq.Id == 0 {
  162. if e := s.d.DB.Where(model.Equipment{FinanceCode: eq.FinanceCode}).Assign(eq).FirstOrCreate(&eq).Error; e != nil {
  163. //assetChan <- errors.Wrapf(e, "equipment update : %s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
  164. } else {
  165. log.Infof("equipment update sucess :%s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
  166. //assetChan <- nil
  167. }
  168. } else {
  169. eq.Id = oldeq.Id
  170. err := s.d.DB.Model(eq).Where("id = ? ", oldeq.Id).Updates(eq).Error
  171. if err != nil {
  172. log.Error(err)
  173. }
  174. }
  175. }
  176. type Equipment struct {
  177. PastureId int `gorm:"column:pastureId" json:"pastureId"`
  178. PastureName string `gorm:"column:pastureName" json:"pastureName"`
  179. EqName string `gorm:"column:eqName" json:"eqName"`
  180. //AssetCode interface{} `gorm:"column:assetCode" json:"assetCode"`
  181. EqClassName string `gorm:"column:eqClassName" json:"eqClassName"`
  182. Specification string `gorm:"column:specification" json:"specification"`
  183. DepartmentId string `gorm:"column:departmentId" json:"departmentId"`
  184. DepartmentName string `gorm:"column:departmentName" json:"departmentName"`
  185. EntranceDate string `gorm:"column:entranceDate" json:"entranceDate"`
  186. Purpose string `gorm:"column:purpose" json:"purpose"`
  187. Yuanzhi string `gorm:"column:yuanzhi" json:"yuanzhi"`
  188. Id int `gorm:"column:id" json:"id"`
  189. EqClassId int `gorm:"column:eqClassId" json:"eqClassId"`
  190. FinanceCode string `gorm:"column:financeCode" json:"financeCode"`
  191. ProName string `gorm:"column:proName" json:"proName"`
  192. ProId int `gorm:"column:proId" json:"proId"`
  193. BrandId string `gorm:"column:brandId" json:"brandId"`
  194. Brand string `gorm:"column:brand" json:"brand"`
  195. PurchaseDate string `gorm:"column:purchaseDate" json:"purchaseDate"`
  196. Subtractvalue string `gorm:"column:subtractvalue" json:"subtractvalue"`
  197. Salvage string `gorm:"column:salvage" json:"salvage"`
  198. Upkeepgrade string `gorm:"column:upkeepgrade" json:"upkeepgrade"`
  199. YearMaintainCost string `gorm:"column:yearMaintainCost" json:"yearMaintainCost"`
  200. YearUpkeepCost string `gorm:"column:yearUpkeepCost" json:"yearUpkeepCost"`
  201. Status int `gorm:"column:status" json:"status"`
  202. EmployeName string `gorm:"column:employeName" json:"employeName"`
  203. EmployeeId string `gorm:"column:employeeId" json:"employeeId"`
  204. InputDatetime string `gorm:"column:inputDatetime" json:"inputDatetime"`
  205. LeaveDate time.Time `gorm:"column:leaveDate" json:"leaveDate"`
  206. InputUser string `gorm:"column:inputUser" json:"inputUser"`
  207. EnabledTime time.Time `gorm:"column:enabledTime" json:"enabledTime"`
  208. BlockTime time.Time `gorm:"column:blockTime" json:"blockTime"`
  209. ChangeStatue string `gorm:"column:changeStatue" json:"changeStatue"`
  210. EnabledPerson int `gorm:"column:enabledPerson" json:"enabledPerson"`
  211. BlockPerson int `gorm:"column:blockPerson" json:"blockPerson"`
  212. SttId int `gorm:"column:sttId" json:"sttId"`
  213. BaseHours string `gorm:"column:baseHours" json:"baseHours"`
  214. ApplicatId int `gorm:"column:applicatId" json:"applicatId"`
  215. ApplicatDate time.Time `gorm:"column:applicatDate" json:"applicatDate"`
  216. WorkflowId int `gorm:"column:workflowId" json:"workflowId"`
  217. FlowCompeleted int `gorm:"column:flowCompeleted" json:"flowCompeleted"`
  218. Enable int `gorm:"column:enable" json:"enable"`
  219. ChangeTime time.Time `gorm:"column:changeTime" json:"changeTime"`
  220. SSTstatue string `gorm:"column:SSTstatue" json:"SSTstatue"`
  221. SHStatus int `gorm:"column:SHStatus" json:"SHStatus"`
  222. ChargeId int `gorm:"column:chargeId" json:"chargeId"`
  223. ChargeDate time.Time `gorm:"column:chargeDate" json:"chargeDate"`
  224. WorkflowNote string `gorm:"column:workflowNote" json:"workflowNote"`
  225. Picpath string `gorm:"column:picpath" json:"picpath"`
  226. Depreciation string `gorm:"column:depreciation" json:"depreciation"`
  227. Statue int `gorm:"column:statue" json:"statue"`
  228. ModifyTime time.Time `gorm:"column:modifyTime" json:"modifyTime"`
  229. CompanyCode string `gorm:"column:companyCode" json:"companyCode"`
  230. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  231. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  232. Quantity string `gorm:"column:quantity" json:"quantity"`
  233. CostCenter string `gorm:"column:costCenter" json:"costCenter"`
  234. Unit string `gorm:"column:unit" json:"unit"`
  235. ProfitCenter string `gorm:"column:profitCenter" json:"profitCenter"`
  236. SonCode string `gorm:"column:sonCode" json:"sonCode"`
  237. SapCode string `gorm:"column:sapCode" json:"sapCode"`
  238. }
  239. func (t *Equipment) TableName() string {
  240. return "equipment"
  241. }
  242. // 更新设备维修和保养费用
  243. func updateEqYearMaintainCostAndYearUpkeepCost(pastureId int, start, end string) {
  244. //eqList := make([]model.Equipment, 0)
  245. //s.d.DB.Raw(` select id from equipment where pastureid = ? `, pastureId).Find(&eqList)
  246. //for _, eq := range eqList {
  247. //更新设备维修费用
  248. err := s.d.DB.Debug().Exec(`update equipment e set yearMaintainCost = (
  249. SELECT
  250. sum( p.sumPrice )
  251. FROM
  252. bigpartuse bp
  253. JOIN partuse p ON p.bigid = bp.id
  254. WHERE
  255. p.eqCode = e.eqCode and bp.pastureId = ?
  256. AND
  257. IF
  258. ((
  259. SELECT
  260. count( 1 )
  261. FROM
  262. bigpartapply bpa
  263. WHERE
  264. bpa.applyCode = bp.oddCode and bpa.pastureId = bp.pastureId
  265. AND bpa.applyType = 1
  266. ) > 0 or (select count(1) from bigoutsourcing bo where bo.outsourcingCode = bp.oddCode ) >0,
  267. 1,
  268. 0
  269. ) = 1 and bp.receiveTime between ? and ? and p.dflag = 0 ) where e.pastureId = ?`, pastureId, start, end, pastureId).Error
  270. if err != nil {
  271. log.Error(err)
  272. }
  273. //更新设备保养费用
  274. err = s.d.DB.Debug().Exec(`update equipment e set yearUpkeepCost = (
  275. SELECT
  276. sum( p.sumPrice )
  277. FROM
  278. bigpartuse bp
  279. JOIN partuse p ON p.bigid = bp.id
  280. WHERE
  281. p.eqCode = e.eqCode and bp.pastureId = ?
  282. AND
  283. IF
  284. ((
  285. SELECT
  286. count( 1 )
  287. FROM
  288. bigpartapply bpa
  289. WHERE
  290. bpa.applyCode = bp.oddCode and bpa.pastureId = bp.pastureId
  291. AND bpa.applyType = 2
  292. ) > 0,
  293. 1,
  294. 0
  295. ) = 1 and bp.receiveTime between ? and ? and p.dflag = 0 ) where e.pastureId = ?`, pastureId, start, end, pastureId).Error
  296. if err != nil {
  297. log.Error(err)
  298. }
  299. //}
  300. err = s.d.DB.Debug().Exec(`UPDATE equipment
  301. SET leaveDate = DATE_ADD( DATE_FORMAT( entranceDate, '%Y-%m-%d' ), INTERVAL depreciation * 12 MONTH )
  302. WHERE
  303. entranceDate IS NOT NULL
  304. AND entranceDate != '' and entranceDate != '0000-00-00' and entranceDate != '00000000' and entranceDate != 'null'
  305. AND depreciation IS NOT NULL
  306. AND depreciation != '' and pastureId = ? `, pastureId).Error
  307. if err != nil {
  308. log.Error(err)
  309. }
  310. }
  311. func (s *Service) SapAssetSalvage(t time.Time, companyCode string, pastureId int) {
  312. //assetChan = make(chan error, 10)
  313. r := new(http.AssetSalvageReq)
  314. rp := new(http.AssetSalvageResp)
  315. var e error
  316. r.Dest.DestID = "EQMAN"
  317. r.Dest.BussTp = "FI010"
  318. r.Dest.Url = fmt.Sprintf("%s/EAM/FI010/QueryAssetNetValue", config.Conf.Http.Routing)
  319. r.Data.TBUKRS = []http.TBUKRS{{BUKRS: companyCode}}
  320. r.Data.TGJAHR = []http.TGJAHR{{GJAHR: t.Format("2006")}}
  321. rbyte, _ := json.Marshal(r)
  322. fmt.Println(string(rbyte))
  323. if e = s.SyncSap(r, rp, rbyte); e == nil {
  324. if rp.Dest.Status == "S" {
  325. log.Infof("sap asset success sum=%d", len(rp.Data.TANLA))
  326. } else {
  327. log.Infof("sap asset fail", rp.Dest.MessText)
  328. pp.Print(r)
  329. }
  330. } else {
  331. log.Error(e)
  332. }
  333. var update error
  334. var count int
  335. equipmentList := make([]*Equipment, 0)
  336. s.d.DB.Debug().Where("pastureId = ? ", pastureId).Find(&equipmentList)
  337. for _, eq := range equipmentList {
  338. exist := false
  339. for _, v := range rp.Data.TANLA {
  340. if eq.FinanceCode == fmt.Sprintf("%s-%s-%s", v.BUKRS, v.ANLN1, v.ANLN2) {
  341. exist = true
  342. fmt.Println(fmt.Sprintf("%s-%s-%s", v.BUKRS, v.ANLN1, v.ANLN2), v.ZJZ)
  343. 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
  344. if err != nil {
  345. log.Error(err.Error)
  346. panic("eeee")
  347. }
  348. break
  349. }
  350. }
  351. if !exist {
  352. err := s.d.DB.Debug().Exec(` update equipment set salvage = ? where id = ? `, 0, eq.Id).Error
  353. if err != nil {
  354. log.Error(err.Error)
  355. panic("eeee")
  356. }
  357. }
  358. }
  359. if e != nil {
  360. log.Error(e)
  361. } else if update != nil {
  362. e = errors.Errorf("asset update fail sum:%d", count)
  363. } else {
  364. log.Infof("asset update success sum:%d", len(rp.Data.TANLA)-count)
  365. }
  366. s.AddSapLog(r, e)
  367. return
  368. }