asset.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.Salvage = p.Salvage
  138. eq.PastureId = pa.ID
  139. dep := new(model.Department)
  140. s.d.DB.Where("costCenter_code = ? ", p.CostCenter).First(&dep)
  141. if dep.ID > 0 {
  142. eq.DepartmentId = strconv.Itoa(dep.ID)
  143. eq.DepartmentName = dep.Name
  144. }
  145. if p.ORD41 == "A3" {
  146. eq.Status = 19
  147. } else {
  148. eq.Status = 17
  149. }
  150. //if p.BuyValue == "" || p.BuyValue == "0" || p.BuyValue == "0.00 " {
  151. // return
  152. //}
  153. fmt.Println(eq)
  154. if oldeq.Id == 0 {
  155. if e := s.d.DB.Where(model.Equipment{FinanceCode: eq.FinanceCode}).Assign(eq).FirstOrCreate(&eq).Error; e != nil {
  156. //assetChan <- errors.Wrapf(e, "equipment update : %s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
  157. } else {
  158. log.Infof("equipment update sucess :%s, %s,%s,%s", p.AssetCode, p.SonCode, p.Name, p.CompanyCode)
  159. //assetChan <- nil
  160. }
  161. } else {
  162. eq.Id = oldeq.Id
  163. err := s.d.DB.Model(eq).Where("id = ? ", oldeq.Id).Updates(eq).Error
  164. if err != nil {
  165. log.Error(err)
  166. }
  167. }
  168. }
  169. type Equipment struct {
  170. PastureId int `gorm:"column:pastureId" json:"pastureId"`
  171. PastureName string `gorm:"column:pastureName" json:"pastureName"`
  172. EqName string `gorm:"column:eqName" json:"eqName"`
  173. //AssetCode interface{} `gorm:"column:assetCode" json:"assetCode"`
  174. EqClassName string `gorm:"column:eqClassName" json:"eqClassName"`
  175. Specification string `gorm:"column:specification" json:"specification"`
  176. DepartmentId string `gorm:"column:departmentId" json:"departmentId"`
  177. DepartmentName string `gorm:"column:departmentName" json:"departmentName"`
  178. EntranceDate string `gorm:"column:entranceDate" json:"entranceDate"`
  179. Purpose string `gorm:"column:purpose" json:"purpose"`
  180. Yuanzhi string `gorm:"column:yuanzhi" json:"yuanzhi"`
  181. Id int `gorm:"column:id" json:"id"`
  182. EqClassId int `gorm:"column:eqClassId" json:"eqClassId"`
  183. FinanceCode string `gorm:"column:financeCode" json:"financeCode"`
  184. ProName string `gorm:"column:proName" json:"proName"`
  185. ProId int `gorm:"column:proId" json:"proId"`
  186. BrandId string `gorm:"column:brandId" json:"brandId"`
  187. Brand string `gorm:"column:brand" json:"brand"`
  188. PurchaseDate string `gorm:"column:purchaseDate" json:"purchaseDate"`
  189. Subtractvalue string `gorm:"column:subtractvalue" json:"subtractvalue"`
  190. Salvage string `gorm:"column:salvage" json:"salvage"`
  191. Upkeepgrade string `gorm:"column:upkeepgrade" json:"upkeepgrade"`
  192. YearMaintainCost string `gorm:"column:yearMaintainCost" json:"yearMaintainCost"`
  193. YearUpkeepCost string `gorm:"column:yearUpkeepCost" json:"yearUpkeepCost"`
  194. Status int `gorm:"column:status" json:"status"`
  195. EmployeName string `gorm:"column:employeName" json:"employeName"`
  196. EmployeeId string `gorm:"column:employeeId" json:"employeeId"`
  197. InputDatetime string `gorm:"column:inputDatetime" json:"inputDatetime"`
  198. LeaveDate time.Time `gorm:"column:leaveDate" json:"leaveDate"`
  199. InputUser string `gorm:"column:inputUser" json:"inputUser"`
  200. EnabledTime time.Time `gorm:"column:enabledTime" json:"enabledTime"`
  201. BlockTime time.Time `gorm:"column:blockTime" json:"blockTime"`
  202. ChangeStatue string `gorm:"column:changeStatue" json:"changeStatue"`
  203. EnabledPerson int `gorm:"column:enabledPerson" json:"enabledPerson"`
  204. BlockPerson int `gorm:"column:blockPerson" json:"blockPerson"`
  205. SttId int `gorm:"column:sttId" json:"sttId"`
  206. BaseHours string `gorm:"column:baseHours" json:"baseHours"`
  207. ApplicatId int `gorm:"column:applicatId" json:"applicatId"`
  208. ApplicatDate time.Time `gorm:"column:applicatDate" json:"applicatDate"`
  209. WorkflowId int `gorm:"column:workflowId" json:"workflowId"`
  210. FlowCompeleted int `gorm:"column:flowCompeleted" json:"flowCompeleted"`
  211. Enable int `gorm:"column:enable" json:"enable"`
  212. ChangeTime time.Time `gorm:"column:changeTime" json:"changeTime"`
  213. SSTstatue string `gorm:"column:SSTstatue" json:"SSTstatue"`
  214. SHStatus int `gorm:"column:SHStatus" json:"SHStatus"`
  215. ChargeId int `gorm:"column:chargeId" json:"chargeId"`
  216. ChargeDate time.Time `gorm:"column:chargeDate" json:"chargeDate"`
  217. WorkflowNote string `gorm:"column:workflowNote" json:"workflowNote"`
  218. Picpath string `gorm:"column:picpath" json:"picpath"`
  219. Depreciation string `gorm:"column:depreciation" json:"depreciation"`
  220. Statue int `gorm:"column:statue" json:"statue"`
  221. ModifyTime time.Time `gorm:"column:modifyTime" json:"modifyTime"`
  222. CompanyCode string `gorm:"column:companyCode" json:"companyCode"`
  223. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  224. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  225. Quantity string `gorm:"column:quantity" json:"quantity"`
  226. CostCenter string `gorm:"column:costCenter" json:"costCenter"`
  227. Unit string `gorm:"column:unit" json:"unit"`
  228. ProfitCenter string `gorm:"column:profitCenter" json:"profitCenter"`
  229. SonCode string `gorm:"column:sonCode" json:"sonCode"`
  230. SapCode string `gorm:"column:sapCode" json:"sapCode"`
  231. }
  232. func (t *Equipment) TableName() string {
  233. return "equipment"
  234. }
  235. // 更新设备维修和保养费用
  236. func updateEqYearMaintainCostAndYearUpkeepCost(pastureId int, start, end string) {
  237. //eqList := make([]model.Equipment, 0)
  238. //s.d.DB.Raw(` select id from equipment where pastureid = ? `, pastureId).Find(&eqList)
  239. //for _, eq := range eqList {
  240. //更新设备维修费用
  241. err := s.d.DB.Debug().Exec(`update equipment e set yearMaintainCost = (
  242. SELECT
  243. sum( p.sumPrice )
  244. FROM
  245. bigpartuse bp
  246. JOIN partuse p ON p.bigid = bp.id
  247. WHERE
  248. p.eqCode = e.eqCode and bp.pastureId = ?
  249. AND
  250. IF
  251. ((
  252. SELECT
  253. count( 1 )
  254. FROM
  255. bigpartapply bpa
  256. WHERE
  257. bpa.applyCode = bp.oddCode and bpa.pastureId = bp.pastureId
  258. AND bpa.applyType = 1
  259. ) > 0 or (select count(1) from bigoutsourcing bo where bo.outsourcingCode = bp.oddCode ) >0,
  260. 1,
  261. 0
  262. ) = 1 and bp.receiveTime between ? and ? and p.dflag = 0 ) where e.pastureId = ?`, pastureId, start, end, pastureId).Error
  263. if err != nil {
  264. log.Error(err)
  265. }
  266. //更新设备保养费用
  267. err = s.d.DB.Debug().Exec(`update equipment e set yearUpkeepCost = (
  268. SELECT
  269. sum( p.sumPrice )
  270. FROM
  271. bigpartuse bp
  272. JOIN partuse p ON p.bigid = bp.id
  273. WHERE
  274. p.eqCode = e.eqCode and bp.pastureId = ?
  275. AND
  276. IF
  277. ((
  278. SELECT
  279. count( 1 )
  280. FROM
  281. bigpartapply bpa
  282. WHERE
  283. bpa.applyCode = bp.oddCode and bpa.pastureId = bp.pastureId
  284. AND bpa.applyType = 2
  285. ) > 0,
  286. 1,
  287. 0
  288. ) = 1 and bp.receiveTime between ? and ? and p.dflag = 0 ) where e.pastureId = ?`, pastureId, start, end, pastureId).Error
  289. if err != nil {
  290. log.Error(err)
  291. }
  292. //}
  293. err = s.d.DB.Debug().Exec(`UPDATE equipment
  294. SET leaveDate = DATE_ADD( DATE_FORMAT( entranceDate, '%Y-%m-%d' ), INTERVAL depreciation * 12 MONTH )
  295. WHERE
  296. entranceDate IS NOT NULL
  297. AND entranceDate != '' and entranceDate != '0000-00-00' and entranceDate != '00000000' and entranceDate != 'null'
  298. AND depreciation IS NOT NULL
  299. AND depreciation != '' and pastureId = ? `, pastureId).Error
  300. if err != nil {
  301. log.Error(err)
  302. }
  303. }