liugong.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/astaxie/beego/logs"
  6. "kpt.xdmy/apiserver/model"
  7. "kpt.xdmy/apiserver/model/liugong"
  8. "kpt.xdmy/pkg/util"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. ////柳工客户平台注册
  14. //func (s *Service) RegisterLG() {
  15. // registerReq := new(liugong.RegisterLG)
  16. // registerReq.UserName = s.http.Liugong.UserName
  17. // registerReq.PassWord = s.http.Liugong.PassWord
  18. // reqStr, _ := json.Marshal(registerReq)
  19. // registerByte, err := util.PostPush(s.http.Liugong.Routing, reqStr)
  20. // if err != nil {
  21. // logging.Error("柳工接口注册失败error-1:" + err.Error())
  22. // return
  23. // }
  24. //
  25. // resp := new(liugong.RegisterLGResp)
  26. // err = json.Unmarshal(registerByte, &resp)
  27. // if err != nil {
  28. // logging.Error("柳工接口注册失败error-2:" + err.Error())
  29. // return
  30. // }
  31. //
  32. // liugongData := new(liugong.LiuGongToken)
  33. // liugongData.Token = resp.Token
  34. // liugongData.UpdateDate = time.Now()
  35. // var count int64
  36. // s.d.DB.Model(liugongData).Count(&count)
  37. // if count == 0 {
  38. // err = s.d.DB.Debug().FirstOrCreate(liugongData).Error
  39. // if err != nil {
  40. // logging.Error("柳工接口注册失败error-3:" + err.Error())
  41. // return
  42. // }
  43. // } else {
  44. // err = s.d.DB.Debug().Where("1=1").Updates(liugongData).Error
  45. // if err != nil {
  46. // logging.Error("柳工接口注册失败error-3:" + err.Error())
  47. // return
  48. // }
  49. // }
  50. //}
  51. //
  52. ////客户令牌获取
  53. //func (s *Service) GetLiuGongToken() (*liugong.LiuGongToken, error) {
  54. // resp := new(liugong.LiuGongToken)
  55. // err := s.d.DB.Debug().First(&resp).Error
  56. // if err != nil {
  57. // logging.Error(err.Error())
  58. // return nil, err
  59. // }
  60. //
  61. // return resp, nil
  62. //}
  63. //
  64. ////柳工设备数据推送
  65. //func (s *Service) LGPushEquipmentInfo(req *liugong.PushEquipmentInfoReq) ([]string, error) {
  66. // var errorList []string
  67. // for _, eq := range req.EquipmentList {
  68. // //date, err1 := time.Parse("2006-01-02 15:04:05", eq.Date)
  69. // //fmt.Println(err1)
  70. // vehicle := new(liugong.Vehicle)
  71. // vehicle.License = eq.License
  72. // vehicle.WorkHour = eq.WorkHour
  73. // vehicle.IdleHour = eq.IdleHour
  74. // vehicle.FuelConsumption = eq.FuelConsumption
  75. // vehicle.IdleFuel = eq.IdleFuel
  76. // vehicle.HoursConsumption = eq.HoursConsumption
  77. // vehicle.Longitude = eq.Longitude
  78. // vehicle.Latitude = eq.Latitude
  79. // vehicle.FuelLevel = eq.FuelLevel
  80. // vehicle.PowerConsumption = eq.PowerConsumption
  81. // vehicle.HoursPowerConsumption = eq.HoursPowerConsumption
  82. // vehicle.ChargeCapacity = eq.ChargeCapacity
  83. // vehicle.ChargeHour = eq.ChargeHour
  84. // vehicle.ChargeStatus = eq.ChargeStatus
  85. // vehicle.BatteryPackSoc = eq.BatteryPackSoc
  86. // vehicle.Date = eq.Date[:11]
  87. // //eqData := new(model.Equipment)
  88. // //err := s.d.DB.Select("id,eqCode,pastureId").Where("license = ?", eq.License).First(&eqData).Error
  89. // //if err != nil {
  90. // // logging.Error(err)
  91. // // errorList = append(errorList, fmt.Sprintf("%s %s 未找到对应设备;", err.Error(), eq.License))
  92. // // continue
  93. // //}
  94. // //vehicle.EqId = int64(eqData.Id)
  95. // //vehicle.EqCode = eqData.EqCode
  96. // //vehicle.PastureId = eqData.PastureId
  97. // //`eqId`, `eqCode`, `license`, `dataTime`
  98. // var count int64
  99. // err := s.d.DB.Debug().Model(new(liugong.Vehicle)).Where(liugong.Vehicle{License: eq.License, Date: vehicle.Date}).Count(&count).Error
  100. // if count == 0 {
  101. // err = s.d.DB.Where(liugong.Vehicle{License: eq.License, Date: vehicle.Date}).FirstOrCreate(&vehicle).Error
  102. // if err != nil {
  103. // logging.Error(err)
  104. // continue
  105. // }
  106. // } else {
  107. // err = s.d.DB.Where(liugong.Vehicle{License: eq.License, Date: vehicle.Date}).Updates(&vehicle).Error
  108. // if err != nil {
  109. // logging.Error(err)
  110. // continue
  111. // }
  112. // }
  113. //
  114. // }
  115. //
  116. // now := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  117. // err := s.d.DB.Exec(` update vehicle v set v.currentWorkHour =v.workHour - (select ifnull(workHour,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1 ) ,
  118. // v.currentIdleHour = v.idleHour-(select ifnull(idleHour,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1 ),
  119. // v.currentFuelConsumption = v.fuelConsumption -(select ifnull(fuelConsumption,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1 ),
  120. // v.currentIdleFuel = v.idleFuel-(select ifnull(idleFuel,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1),
  121. // v.currentPowerConsumption = v.powerConsumption-(select ifnull(powerConsumption,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1),
  122. // v.currentChargeCapacity = v.chargeCapacity-(select ifnull(chargeCapacity,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1),
  123. // v.currentChargeHour = v.chargeHour-(select ifnull(chargeHour,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1)
  124. // where dataTime = ? `, now, now, now, now, now, now, now, now).Error
  125. // if err != nil {
  126. // logging.Error(err)
  127. // return nil, err
  128. // }
  129. // //currentPowerConsumption
  130. // //currentChargeCapacity
  131. // //currentChargeHour
  132. // now = time.Now().Format("2006-01-02")
  133. // err = s.d.DB.Exec(` update vehicle v set v.currentWorkHour =v.workHour - (select ifnull(workHour,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1 ) ,
  134. // v.currentIdleHour = v.idleHour-(select ifnull(idleHour,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1 ),
  135. // v.currentFuelConsumption = v.fuelConsumption -(select ifnull(fuelConsumption,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1 ),
  136. // v.currentIdleFuel = v.idleFuel-(select ifnull(idleFuel,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1),
  137. // v.currentPowerConsumption = v.powerConsumption-(select ifnull(powerConsumption,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1),
  138. // v.currentChargeCapacity = v.chargeCapacity-(select ifnull(chargeCapacity,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1),
  139. // v.currentChargeHour = v.chargeHour-(select ifnull(chargeHour,0) from vehicle where dataTime < ? and license = v.license order by dataTime desc limit 1)
  140. // where dataTime = ? `, now, now, now, now, now, now, now, now).Error
  141. // if err != nil {
  142. // logging.Error(err)
  143. // return nil, err
  144. // }
  145. //
  146. // return errorList, nil
  147. //}
  148. //
  149. //func (s *Service) GetVehicleList(req *liugong.GetVehicleListReq) ([]*liugong.VehicleAndPasture, int64, error) {
  150. // tx := s.d.DB.Table(new(liugong.Vehicle).TableName())
  151. //
  152. // tx.Joins(" join equipment on equipment.license = vehicle.license")
  153. // tx.Joins(" join pasture on pasture.id = equipment.pastureId")
  154. //
  155. // tx.Select("vehicle.id,equipment.Id eqId,equipment.eqCode,equipment.pastureId,vehicle.license," +
  156. // "vehicle.workHour,vehicle.idleHour,vehicle.fuelConsumption,vehicle.idleFuel,vehicle.hoursConsumption," +
  157. // "vehicle.longitude,vehicle.latitude,vehicle.fuelLevel,DATE_FORMAT(vehicle.dataTime,'%Y-%m-%d') dataTime ,vehicle.createTime,vehicle.currentWorkHour," +
  158. // "vehicle.currentIdleHour,vehicle.currentIdleFuel,vehicle.powerConsumption,vehicle.hoursPowerConsumption," +
  159. // "vehicle.chargeCapacity,vehicle.chargeHour,vehicle.chargeStatus,vehicle.batteryPackSoc,vehicle.currentPowerConsumption," +
  160. // "vehicle.currentChargeCapacity,vehicle.currentChargeHour,vehicle.currentFuelConsumption,pasture.name")
  161. // if req.PastureId != "" {
  162. // tx.Where("pastureId = ?", req.PastureId)
  163. // }
  164. // if req.StartTime != "" {
  165. // tx.Where(" dataTime between ? and ?", req.StartTime, req.EndTime)
  166. // }
  167. //
  168. // vehicleList := make([]*liugong.VehicleAndPasture, 0)
  169. // var count int64
  170. // tx.Count(&count)
  171. // tx.Limit(req.PageCount).Offset(req.Offset)
  172. //
  173. // err := tx.Debug().Order(" dataTime desc,pastureId asc ").Find(&vehicleList).Error
  174. // if err != nil {
  175. // return nil, 0, err
  176. // }
  177. // return vehicleList, count, nil
  178. //}
  179. func (s *Service) SyncEquipmentDate() {
  180. eqList := make([]*model.EquipmentAndDepartment, 0)
  181. err := s.d.DB.Raw(`select * from equipment e join eqclass ec on ec.id = e.eqClassId
  182. join department d on d.id = e.departmentId where e.license is not null and e.license != '' and e.pushStatus in(0,1,2) `).Find(&eqList).Error
  183. if err != nil {
  184. logs.Error(err)
  185. return
  186. }
  187. if len(eqList) == 0 {
  188. return
  189. }
  190. pastureList := make([]*model.Pasture, 0)
  191. err = s.d.DB.Raw(`select p.id,p.center,p.name,p.pastureNumber,d1.name region,d1.id region_id
  192. from pasture p join department d on d.id = p.id join department d1 on d1.id = d.parentid where p.vendor = 1 `).Find(&pastureList).Error
  193. if err != nil {
  194. logs.Error(err)
  195. }
  196. vehicleList := make([]*liugong.VehicleInfo, 0)
  197. for _, eq := range eqList {
  198. arg := new(liugong.VehicleInfo)
  199. //OperateCenterName string `json:"operateCenterName"` //运营中心
  200. //AreaName string `json:"areaName"` //大区
  201. //RanchName string `json:"ranchName"` //牧场
  202. for _, pasture := range pastureList {
  203. if pasture.ID == eq.PastureId {
  204. arg.RanchName = pasture.Name
  205. arg.AreaCode = fmt.Sprintf("%d", pasture.RegionID)
  206. arg.AreaName = pasture.Region
  207. center := 0
  208. if pasture.Center == "一中心" {
  209. center = 1
  210. } else if pasture.Center == "二中心" {
  211. center = 2
  212. } else if pasture.Center == "三中心" {
  213. center = 3
  214. }
  215. arg.OperationCode = fmt.Sprintf("%d", center)
  216. arg.OperateCenterName = pasture.Center
  217. break
  218. }
  219. }
  220. arg.RancheCode = fmt.Sprintf("%d", eq.Equipment.PastureId)
  221. arg.VehicleType = eq.EqClass.TypeName
  222. arg.VehicleLicense = eq.EqCode
  223. arg.DeviceLicense = eq.License
  224. arg.Specification = eq.Specification
  225. arg.Brand = eq.Brand
  226. arg.Usage = eq.Purpose
  227. arg.DeptName = eq.Department.Name
  228. purchaseDate, err := time.Parse("20060102", eq.PurchaseDate)
  229. if err != nil {
  230. arg.PurchaseDate = eq.PurchaseDate
  231. } else {
  232. arg.PurchaseDate = purchaseDate.Format("2006-01-02")
  233. }
  234. entranceDate, err := time.Parse("20060102", eq.EntranceDate)
  235. if err != nil {
  236. entranceDate1, err := time.Parse("2006-01-02", eq.EntranceDate)
  237. if err == nil {
  238. arg.EnterDate = entranceDate1.Format("2006-01-02")
  239. } else {
  240. continue
  241. }
  242. } else {
  243. arg.EnterDate = entranceDate.Format("2006-01-02")
  244. }
  245. arg.ScrapDate = eq.LeaveDate.Format("2006-01-02")
  246. arg.StandardHour = eq.Duration
  247. arg.Status = eq.PushStatus
  248. if eq.Statue == 19 {
  249. arg.Status = 0
  250. } else if strings.Index(eq.License, "-") >= 0 {
  251. arg.Status = 3
  252. fmt.Println(strings.Index(eq.License, "-"))
  253. eq.License = strings.Replace(eq.License, "-", "", -1)
  254. fmt.Println(eq.License)
  255. }
  256. arg.VehicleName = eq.EqName
  257. arg.WorkClass = eq.WorkClass
  258. vehicleList = append(vehicleList, arg)
  259. }
  260. vehicleListReq := make([]*liugong.VehicleInfo, 0)
  261. for _, vehicle := range vehicleList {
  262. vehicleListReq = append(vehicleListReq, vehicle)
  263. if len(vehicleListReq) > 50 {
  264. dataByte, err := json.Marshal(vehicleListReq)
  265. if err != nil {
  266. logs.Error(err)
  267. return
  268. }
  269. fmt.Println(string(dataByte))
  270. url := "http://meadow.nandasoft-its.com:26101/vientiane/pushVehicleInfo"
  271. respByte, err := util.PostPush(url, dataByte)
  272. if err != nil {
  273. logs.Error(err)
  274. }
  275. fmt.Println(string(respByte))
  276. vientianeResp := new(liugong.VientianeResp)
  277. err = json.Unmarshal(respByte, &vientianeResp)
  278. if err != nil {
  279. logs.Error(err)
  280. return
  281. }
  282. fmt.Println(string(dataByte))
  283. if vientianeResp.Code == 200 {
  284. //for _, eq := range eqList {
  285. // err := s.d.DB.Exec(` update equipment set pushStatus = 3 where id = ? `, eq.Equipment.Id).Error
  286. // if err != nil {
  287. // logs.Error(err)
  288. // return
  289. // }
  290. //}
  291. vientianeResp.Status = vientianeResp.Code
  292. }
  293. err = s.d.DB.Exec(` insert into vehiclelog(url,param,status,msgtext,resp)values(?,?,?,?,?) `, url, string(dataByte), vientianeResp.Status, vientianeResp.Msg, string(respByte)).Error
  294. if err != nil {
  295. logs.Error(err)
  296. return
  297. }
  298. vehicleListReq = make([]*liugong.VehicleInfo, 0)
  299. }
  300. }
  301. if len(vehicleListReq) > 0 {
  302. dataByte, err := json.Marshal(vehicleListReq)
  303. if err != nil {
  304. logs.Error(err)
  305. return
  306. }
  307. fmt.Println(string(dataByte))
  308. url := "http://meadow.nandasoft-its.com:26101/vientiane/pushVehicleInfo"
  309. respByte, err := util.PostPush(url, dataByte)
  310. if err != nil {
  311. logs.Error(err)
  312. }
  313. fmt.Println(string(respByte))
  314. vientianeResp := new(liugong.VientianeResp)
  315. err = json.Unmarshal(respByte, &vientianeResp)
  316. if err != nil {
  317. logs.Error(err)
  318. return
  319. }
  320. fmt.Println(string(dataByte))
  321. if vientianeResp.Code == 200 {
  322. //for _, eq := range eqList {
  323. // err := s.d.DB.Exec(` update equipment set pushStatus = 3 where id = ? `, eq.Equipment.Id).Error
  324. // if err != nil {
  325. // logs.Error(err)
  326. // return
  327. // }
  328. //}
  329. vientianeResp.Status = vientianeResp.Code
  330. }
  331. err = s.d.DB.Exec(` insert into vehiclelog(url,param,status,msgtext,resp)values(?,?,?,?,?) `, url, string(dataByte), vientianeResp.Status, vientianeResp.Msg, string(respByte)).Error
  332. if err != nil {
  333. logs.Error(err)
  334. return
  335. }
  336. }
  337. }
  338. func (s *Service) SyncPasture() {
  339. pastureList := make([]*model.Pasture, 0)
  340. err := s.d.DB.Raw(`select p.id,p.center,p.name,p.pastureNumber,d1.name region,d1.id region_id
  341. from pasture p join department d on d.id = p.id join department d1 on d1.id = d.parentid where p.vendor = 1 and p.IsEnergy = 0 `).Find(&pastureList).Error
  342. if err != nil {
  343. logs.Error(err)
  344. }
  345. pasturePushList := make([]map[string]interface{}, 0)
  346. center1 := make(map[string]interface{}, 0)
  347. center1["code"] = 1
  348. center1["name"] = "一中心"
  349. center2 := make(map[string]interface{}, 0)
  350. center2["code"] = 2
  351. center2["name"] = "二中心"
  352. center3 := make(map[string]interface{}, 0)
  353. center3["code"] = 3
  354. center3["name"] = "三中心"
  355. regionList := make([]map[string]interface{}, 0)
  356. regionNameList := make([]string, 0)
  357. for _, pasture := range pastureList {
  358. if len(regionNameList) == 0 {
  359. regionNameList = append(regionNameList, pasture.Region)
  360. } else {
  361. exist := false
  362. for _, regionName := range regionNameList {
  363. if regionName == pasture.Region {
  364. exist = true
  365. break
  366. }
  367. }
  368. if exist {
  369. continue
  370. }
  371. regionNameList = append(regionNameList, pasture.Region)
  372. }
  373. region := make(map[string]interface{}, 0)
  374. region["code"] = pasture.RegionID
  375. region["name"] = pasture.Region
  376. region["center"] = pasture.Center
  377. pastureMapList := make([]map[string]interface{}, 0)
  378. for _, pasture1 := range pastureList {
  379. if pasture.Region == pasture1.Region {
  380. pastureMap := make(map[string]interface{}, 0)
  381. pastureMap["code"] = pasture1.ID
  382. pastureMap["name"] = pasture1.Name
  383. pastureMapList = append(pastureMapList, pastureMap)
  384. }
  385. }
  386. region["subDepts"] = pastureMapList
  387. regionList = append(regionList, region)
  388. }
  389. pasturePushList = append(pasturePushList, center1)
  390. pasturePushList = append(pasturePushList, center2)
  391. pasturePushList = append(pasturePushList, center3)
  392. for _, center := range pasturePushList {
  393. regionData := make([]map[string]interface{}, 0)
  394. for _, region := range regionList {
  395. if center["name"].(string) == region["center"].(string) {
  396. regionData = append(regionData, region)
  397. }
  398. }
  399. center["subDepts"] = regionData
  400. }
  401. dataByte, err := json.Marshal(pasturePushList)
  402. if err != nil {
  403. logs.Error(err)
  404. }
  405. url := "http://meadow.nandasoft-its.com:26101/vientiane/pushOrgStructure"
  406. respByte, err := util.PostPush(url, dataByte)
  407. if err != nil {
  408. logs.Error(err)
  409. }
  410. fmt.Println(string(dataByte))
  411. vientianeResp := new(liugong.VientianeResp)
  412. err = json.Unmarshal(respByte, &vientianeResp)
  413. if err != nil {
  414. logs.Error(err)
  415. return
  416. }
  417. if vientianeResp.Code == 200 {
  418. vientianeResp.Status = vientianeResp.Code
  419. }
  420. err = s.d.DB.Exec(` insert into vehiclelog(url,param,status,msgtext,resp)values(?,?,?,?,?) `, url, string(dataByte), vientianeResp.Status, vientianeResp.Msg, string(respByte)).Error
  421. if err != nil {
  422. logs.Error(err)
  423. return
  424. }
  425. }
  426. func (s *Service) EditVehicle(vehicleList []*liugong.EditVehicleReq) []string {
  427. var errors []string
  428. for _, vehicle := range vehicleList {
  429. err := s.d.DB.Debug().Exec(` insert into vehicle(pastureId,pastureName,date,eqId,eqCode,hour,eqClass,eqName,
  430. license,specification,purpose,purchaseDate,entranceDate,leaveDate,duration,departmentId,departmentName,brand)
  431. select e.pastureId,e.pastureName,?,e.id,e.eqCode,?,e.eqClassName,e.eqname,e.license,e.specification,e.purpose
  432. ,e.purchaseDate,e.entranceDate,e.leaveDate,e.duration,e.departmentId,(select name from department where id = e.departmentId ),e.brand
  433. from equipment e where e.eqCode = ?
  434. ON DUPLICATE KEY UPDATE hour = ? `, vehicle.Date, vehicle.WorkHour,
  435. vehicle.VehicleLicense, vehicle.WorkHour).Error
  436. if err != nil {
  437. logs.Error(err)
  438. errors = append(errors, err.Error())
  439. }
  440. }
  441. reqByte, _ := json.Marshal(vehicleList)
  442. err := s.d.DB.Exec(` insert into vehiclelog(url,param,status,msgtext,resp)values(?,?,?,?,?) `,
  443. "", string(reqByte), "", "", strings.Join(errors, ",")).Error
  444. if err != nil {
  445. logs.Error(err)
  446. }
  447. return errors
  448. }
  449. func (s *Service) GetVehicle(pastureId, startdate, enddate, eqCode, eqName, departmentId string, offset, pagecount int) (*liugong.GetVehicleResp, error) {
  450. vehicleList := make([]*liugong.VehicleResp, 0)
  451. resp := new(liugong.GetVehicleResp)
  452. tx := s.d.DB.Table("vehicle")
  453. if pastureId != "" {
  454. tx.Where("pastureId = ? ", pastureId)
  455. }
  456. if startdate != "" {
  457. tx.Where(" date between ? and ? ", startdate, enddate)
  458. }
  459. if eqCode != "" {
  460. tx.Where(" eqCode like ? ", "%"+eqCode+"%")
  461. }
  462. if eqName != "" {
  463. tx.Where(" eqName like ? ", "%"+eqName+"%")
  464. }
  465. if departmentId != "" {
  466. tx.Where(" departmentId = ? ", departmentId)
  467. }
  468. err := tx.Order(" date desc ").Limit(pagecount).Offset(offset - 1).Find(&vehicleList).Error
  469. if err != nil {
  470. logs.Error(err)
  471. return resp, err
  472. }
  473. tx1 := s.d.DB.Table("vehicle")
  474. if pastureId != "" {
  475. tx1.Where("pastureId = ? ", pastureId)
  476. }
  477. if startdate != "" {
  478. tx1.Where(" date between ? and ? ", startdate, enddate)
  479. }
  480. if eqCode != "" {
  481. tx1.Where(" eqCode like ? ", "%"+eqCode+"%")
  482. }
  483. if eqName != "" {
  484. tx1.Where(" eqName like ? ", "%"+eqName+"%")
  485. }
  486. if departmentId != "" {
  487. tx1.Where(" departmentId = ? ", departmentId)
  488. }
  489. var count int64
  490. err = tx1.Count(&count).Error
  491. if err != nil {
  492. logs.Error(err)
  493. return resp, err
  494. }
  495. pastureList := make([]*model.Pasture, 0)
  496. err = s.d.DB.Where("vendor = 1 ").Find(&pastureList).Error
  497. if err != nil {
  498. logs.Error(err)
  499. return resp, err
  500. }
  501. for _, v := range vehicleList {
  502. for _, pasture := range pastureList {
  503. if v.PastureId == pasture.ID {
  504. v.Region = pasture.Region
  505. v.Center = pasture.Center
  506. break
  507. }
  508. }
  509. }
  510. for _, v := range vehicleList {
  511. purchaseDate, err := time.Parse("20060102", v.PurchaseDate)
  512. if err == nil {
  513. v.PurchaseDate = purchaseDate.Format("2006-01-02")
  514. }
  515. entranceDate, err := time.Parse("20060102", v.EntranceDate)
  516. if err == nil {
  517. v.EntranceDate = entranceDate.Format("2006-01-02")
  518. }
  519. leaveDate, err := time.Parse("2006-01-02", v.LeaveDate)
  520. if err == nil {
  521. v.LeaveDate = leaveDate.Format("2006-01-02")
  522. }
  523. date, err := time.Parse("2006-01-02", v.Date[:len("2006-01-02")])
  524. if err == nil {
  525. v.Date = date.Format("2006-01-02")
  526. }
  527. }
  528. resp.VehicleList = vehicleList
  529. resp.Offset = offset
  530. resp.PageCount = pagecount
  531. resp.Count = count
  532. return resp, err
  533. }
  534. func (s *Service) GetRunningRateDetail(date, pastureId, eqCode string, offset, pagecount int) ([]map[string]interface{}, error) {
  535. dataList := make([]map[string]interface{}, 0)
  536. eqList := make([]*model.EquipmentAndDepartment, 0)
  537. err := s.d.DB.Raw(`select * from equipment e join eqclass ec on ec.id = e.eqClassId
  538. join department d on d.id = e.departmentId where e.license is not null and e.license != '' and e.pushStatus in(0,1,2)
  539. and (e.pastureId = ? or ? = 18) and ( eqCode like concat('%',?,'%') or ? ='' ) limit ?,? `,
  540. pastureId, pastureId, eqCode, eqCode, offset, pagecount).Find(&eqList).Error
  541. if err != nil {
  542. logs.Error(err)
  543. return dataList, nil
  544. }
  545. vehicleList := make([]*liugong.Vehicle, 0)
  546. var eqIdList []int
  547. for _, eq := range eqList {
  548. eqIdList = append(eqIdList, eq.Id)
  549. }
  550. err = s.d.DB.Debug().Table(new(liugong.Vehicle).TableName()).Select("pastureName,eqId,license,date_format(date ,'%Y-%m-%d') date,hour,duration").
  551. Where(" date_format(date ,'%Y-%m') = ? ", date).Where(" eqId in (?) ", eqIdList).Find(&vehicleList).Error
  552. if err != nil {
  553. logs.Error(err)
  554. return dataList, nil
  555. }
  556. now := time.Now()
  557. var dateList []string
  558. firstDay := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  559. nextMonthFirstDay := firstDay.AddDate(0, 1, 0)
  560. lastDay := nextMonthFirstDay.Add(-24 * time.Hour)
  561. for t := firstDay; !t.After(lastDay); t = t.AddDate(0, 0, 1) {
  562. fmt.Println(t.Format("2006-01-02"))
  563. dateList = append(dateList, t.Format("2006-01-02"))
  564. }
  565. num, err := strconv.Atoi(now.Format("02"))
  566. if err != nil {
  567. fmt.Printf("转换出错: %v\n", err)
  568. return dataList, nil
  569. }
  570. for _, eq := range eqList {
  571. data := make(map[string]interface{}, 0)
  572. data["id"] = eq.Id
  573. data["eqName"] = eq.EqName
  574. data["eqCode"] = eq.EqCode
  575. data["eqClassName"] = eq.EqClassName
  576. data["duration"] = eq.Duration
  577. data["license"] = eq.License
  578. var hour float64
  579. for i, date := range dateList {
  580. data[fmt.Sprintf("%d", i+1)] = 0
  581. for _, vehicle := range vehicleList {
  582. if vehicle.EqId == fmt.Sprintf("%d", eq.Id) && vehicle.Date == date {
  583. data["pastureName"] = vehicle.PastureName
  584. data["license"] = vehicle.License
  585. data[fmt.Sprintf("%d", i+1)] = vehicle.Hour
  586. float64Value, err := strconv.ParseFloat(vehicle.Hour, 64)
  587. if err != nil {
  588. return nil, err
  589. }
  590. hour += float64Value
  591. }
  592. }
  593. }
  594. if hour > 0 && num > 0 && eq.Duration > 0 {
  595. data["runningRate"] = util.Round(hour/float64(num*eq.Duration)*100, 2)
  596. } else {
  597. data["runningRate"] = 0
  598. }
  599. dataList = append(dataList, data)
  600. }
  601. return dataList, nil
  602. }