report.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. package service
  2. import (
  3. "fmt"
  4. "github.com/astaxie/beego/logs"
  5. "github.com/pkg/errors"
  6. "kpt.xdmy/apiserver/model"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. func MaintainList(pastureName, eqClassIdStr string, startTime, endTime time.Time, offset, pagecount int) (int, []map[string]interface{}, error) {
  12. //eqClassIdList := []string{}
  13. var total int64
  14. eqList := make([]*model.EqClassAndPasture, 0)
  15. //if eqClassIdStr == "" {
  16. //eqList := make([]*model.EqClass, 0)
  17. //s.d.DB.Raw()
  18. s.d.DB.Raw("select e.id,p.id,e.typeName,p.name from eqclass e join pasture p where section = 3 "+
  19. "and (p.name = ? or ? = '现代牧业' or ?= '') and p.IsEnergy = 0 and p.name != '现代牧业' and (e.id = ? or ?= '') limit ?,?",
  20. pastureName, pastureName, pastureName, eqClassIdStr, eqClassIdStr, offset-1, pagecount).Find(&eqList)
  21. s.d.DB.Raw("select count(1) from eqclass e join pasture p where section = 3 "+
  22. "and (p.name = ? or ? = '现代牧业' or ?= '') and p.IsEnergy = 0 and p.name != '现代牧业' and (e.id = ? or ?= '') ",
  23. pastureName, pastureName, pastureName, eqClassIdStr, eqClassIdStr).Count(&total)
  24. //} else {
  25. // eqClassIdList = append(eqClassIdList, eqClassIdStr)
  26. // total = 1
  27. //}
  28. //pasture := new(model.Pasture)
  29. //s.d.DB.Where("name = ?", pastureName).First(&pasture)
  30. respMap := make([]map[string]interface{}, 0)
  31. for _, eq := range eqList {
  32. pastureMaintainList := make([]*model.Maintain, 0)
  33. err := s.d.DB.Raw(` SELECT m.* FROM maintain m JOIN equipment e on e.id = m.eqId WHERE e.eqClassId = ?
  34. and m.stopTime is not null and (m.pastureid = (select id from pasture where name = ? and name != '现代牧业') or ? = '现代牧业' )
  35. and m.stopTime between ? and ? and TIMESTAMPDIFF(hour, m.orderTime, m.stopTime) < 1000 order by requestTime,m.pastureid `,
  36. eq.EqClass.ID, eq.Pasture.Name, eq.Pasture.Name, startTime, endTime).Find(&pastureMaintainList).Error
  37. if err != nil {
  38. return 0, nil, errors.New(fmt.Sprintf("查看维修信息出错 error:%s", err.Error()))
  39. }
  40. maintainMap := make(map[string][]*model.Maintain)
  41. pastureIDList := []string{}
  42. for _, pasture := range pastureMaintainList {
  43. pastureIDList = append(pastureIDList, pasture.PastureId)
  44. maintainMap[pasture.PastureId] = append(maintainMap[pasture.PastureId], pasture)
  45. }
  46. eqClass := model.EqClass{}
  47. err = s.d.DB.Where("id = ? ", eq.EqClass.ID).First(&eqClass).Error
  48. if err != nil {
  49. return 0, nil, errors.New("查询设备类别出错!!!")
  50. }
  51. pastureIDList = RemoveRepeatElement(pastureIDList)
  52. for _, pastureId := range pastureIDList {
  53. maintainList := maintainMap[pastureId]
  54. count := float64(len(maintainList))
  55. var t2, t3 float64
  56. data := make(map[string]interface{}, 0)
  57. mainMap := make(map[int64][]*model.Maintain, 0)
  58. //将每个不同的资产分类放进map
  59. eqIdList := []int64{}
  60. for _, main := range maintainList {
  61. eqIdList = append(eqIdList, main.EqId)
  62. mainMap[main.EqId] = append(mainMap[main.EqId], main)
  63. }
  64. //var stopTime, orderTime, requestTime time.Time
  65. eqIdList = uniqueArr(eqIdList)
  66. for _, k := range eqIdList {
  67. var stopTime, orderTime, requestTime time.Time
  68. mainList := mainMap[k]
  69. for i, main := range mainList {
  70. if i == 0 {
  71. //记录下维修结束时间方便下次计算t1
  72. stopTime = main.StopTime
  73. orderTime = main.OrderTime
  74. requestTime = main.RequestTime
  75. //每个资产第一次都根据筛选开始时间计算出t1
  76. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  77. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  78. //fmt.Println(main.StopTime, main.OrderTime, "111111")
  79. //t1 += main.RequestTime.Sub(startTime).Minutes()
  80. } else {
  81. //t1 += main.RequestTime.Sub(stopTime).Minutes()
  82. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  83. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  84. //fmt.Println(main.StopTime, main.OrderTime, "22222222")
  85. //记录下维修结束时间方便下次计算t1
  86. stopTime = main.StopTime
  87. orderTime = main.OrderTime
  88. requestTime = main.RequestTime
  89. }
  90. }
  91. if stopTime.Format("2006") == "0001" {
  92. //如果还没有结束获取当前时间计算t1
  93. if orderTime.Format("2006") == "0001" {
  94. t2 += requestTime.Sub(time.Now()).Hours()
  95. } else {
  96. if time.Now().Unix() < endTime.Unix() {
  97. t3 += time.Now().Sub(orderTime).Hours()
  98. } else {
  99. t3 += endTime.Sub(orderTime).Hours()
  100. }
  101. }
  102. }
  103. }
  104. equipmentList := make([]*model.Equipment, 0)
  105. err = s.d.DB.Where("id not in(?) ", eqIdList).
  106. Where(" entranceDate < ? and ( blockTime is null or blockTime = '' or blockTime > ? or blockTime = '0000-00-00 00:00:00' ) "+
  107. "and pastureid = ? and eqClassId = ? ",
  108. endTime, startTime, pastureId, eq.EqClass.ID).Find(&equipmentList).Error
  109. if err != nil {
  110. return 0, nil, errors.New(fmt.Sprintf("查询资产信息出错 error:%v", err))
  111. }
  112. day := ((endTime.Unix() - startTime.Unix()) / 60 / 60 / 24)
  113. count1 := float64(len(mainMap))
  114. allCount := (24 * (count1 + float64(len(equipmentList)))) * float64(day)
  115. data["mtbf"] = FloatRound(allCount/count, 2)
  116. data["mttr"] = FloatRound((t2+t3)/count, 2)
  117. data["mttf"] = FloatRound((allCount-t2-t3)/count, 2)
  118. data["count"] = count
  119. data["eqClassId"] = eqClass.ID
  120. pasture := model.Pasture{}
  121. err := s.d.DB.Where("id = ? ", pastureId).First(&pasture).Error
  122. if err != nil {
  123. return 0, nil, errors.New("查询牧场信息出错!!!")
  124. }
  125. data["pastureId"] = pasture.ID
  126. data["pastureName"] = pasture.Name
  127. data["eqClassName"] = eqClass.TypeName
  128. respMap = append(respMap, data)
  129. }
  130. exist := false
  131. for _, p := range respMap {
  132. if p["eqClassId"] != nil {
  133. //str := strconv.Itoa(p["eqClassId"].(int))
  134. if p["eqClassId"].(int) == eq.EqClass.ID && p["pastureId"].(int) == eq.Pasture.ID {
  135. exist = true
  136. break
  137. }
  138. }
  139. }
  140. if !exist {
  141. data := make(map[string]interface{}, 0)
  142. data["pastureName"] = pastureName
  143. data["pastureId"] = eq.Pasture.ID
  144. data["eqClassName"] = eqClass.TypeName
  145. data["mtbf"] = 0
  146. data["mttr"] = 0
  147. data["mttf"] = 0
  148. data["count"] = 0
  149. data["eqClassId"] = eqClass.ID
  150. respMap = append(respMap, data)
  151. }
  152. //}
  153. }
  154. return int(total), respMap, nil
  155. }
  156. func FloatRound(f float64, n int) float64 {
  157. format := "%." + strconv.Itoa(n) + "f"
  158. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  159. return res
  160. }
  161. func RemoveRepeatElement(list []string) []string {
  162. // 创建一个临时map用来存储数组元素
  163. temp := make(map[string]bool)
  164. var list2 []string
  165. index := 0
  166. for _, v := range list {
  167. // 遍历数组元素,判断此元素是否已经存在map中
  168. _, ok := temp[v]
  169. if !ok {
  170. list2 = append(list2, v)
  171. temp[v] = true
  172. }
  173. index++
  174. }
  175. return list2
  176. }
  177. func uniqueArr(arr []int64) []int64 {
  178. newArr := make([]int64, 0)
  179. tempArr := make(map[int64]bool, len(newArr))
  180. for _, v := range arr {
  181. if tempArr[v] == false {
  182. tempArr[v] = true
  183. newArr = append(newArr, v)
  184. }
  185. }
  186. return newArr
  187. }
  188. func GetOversee(pastureId, eqClassId, empname string, offset, pagecount int) ([]map[string]interface{}, error) {
  189. overseeList := make([]*model.Oversee, 0)
  190. eq := s.d.DB.Where(" ( ? = '' or pastureid = ? )", pastureId, pastureId)
  191. if eqClassId != "" {
  192. eq.Where("eqClassId = ?", eqClassId)
  193. }
  194. if empname != "" {
  195. eq.Where("maintenance like ?", "%"+empname+"%")
  196. }
  197. err := eq.Offset(offset).Limit(pagecount).Order("inputTime desc ").Find(&overseeList).Error
  198. if err != nil {
  199. logs.Error(err)
  200. return nil, errors.New(fmt.Sprintf("查询督办数据出错 errors: %v", err))
  201. }
  202. userIds := []string{}
  203. for _, oversee := range overseeList {
  204. u := strings.Split(oversee.Maintenance, ",")
  205. userIds = append(userIds, oversee.Levelone, oversee.Leveltwo, oversee.Levelthree, oversee.Entryperson)
  206. userIds = append(userIds, u...)
  207. }
  208. userIds = RemoveRepeatElement(userIds)
  209. userList := make([]*model.User, 0)
  210. if len(userIds) > 0 {
  211. err = s.d.DB.Raw(` select u.id,u.username,u.empid,e.empname from user u join emp e on e.empcode = u.username where u.username in(?)`, userIds).Find(&userList).Error
  212. if err != nil {
  213. logs.Error(err)
  214. return nil, errors.New(fmt.Sprintf("查询督办数据出错 errors: %v", err))
  215. }
  216. }
  217. userMap := make(map[string]*model.User)
  218. for _, user := range userList {
  219. userMap[user.Username] = user
  220. }
  221. respOversee := make([]map[string]interface{}, 0)
  222. for _, oversee := range overseeList {
  223. arg := make(map[string]interface{})
  224. arg["id"] = oversee.ID
  225. arg["pastureName"] = oversee.PastureName
  226. arg["pastureId"] = oversee.PastureId
  227. arg["pushTime"] = oversee.PushTime
  228. arg["inputTime"] = oversee.InputTime.Format("2006-01-02")
  229. arg["departmentId"] = oversee.DepartmentId
  230. arg["departmentName"] = oversee.DepartmentName
  231. arg["eqClassName"] = oversee.EqClassName
  232. arg["eqClassId"] = oversee.EqClassId
  233. arg["entryperson"] = oversee.Entryperson
  234. if _, ok := userMap[oversee.Entryperson]; ok {
  235. arg["entrypersonName"] = userMap[oversee.Entryperson].EmpName
  236. arg["entrypersonId"] = userMap[oversee.Entryperson].Username
  237. }
  238. arg["levelone"] = oversee.Levelone
  239. if _, ok := userMap[oversee.Levelone]; ok {
  240. arg["leveloneName"] = userMap[oversee.Levelone].EmpName
  241. arg["leveloneId"] = userMap[oversee.Levelone].Username
  242. }
  243. arg["leveltwo"] = oversee.Leveltwo
  244. if _, ok := userMap[oversee.Leveltwo]; ok {
  245. arg["leveltwo"] = userMap[oversee.Leveltwo].EmpName
  246. arg["leveltwoId"] = userMap[oversee.Leveltwo].Username
  247. }
  248. arg["levelthree"] = oversee.Levelthree
  249. if _, ok := userMap[oversee.Levelthree]; ok {
  250. arg["levelthree"] = userMap[oversee.Levelthree].EmpName
  251. arg["levelthreeId"] = userMap[oversee.Levelthree].Username
  252. }
  253. maintenance := make([]map[string]interface{}, 0)
  254. for _, u := range strings.Split(oversee.Maintenance, ",") {
  255. main := make(map[string]interface{})
  256. main["id"] = userMap[u].Username
  257. main["empname"] = userMap[u].EmpName
  258. maintenance = append(maintenance, main)
  259. }
  260. arg["maintenance"] = maintenance
  261. respOversee = append(respOversee, arg)
  262. }
  263. return respOversee, nil
  264. }
  265. func StartStopRecord(pastureId, eqName string, startTime, endTime time.Time, offset, pagecount int) (map[string]interface{}, error) {
  266. eqDb := s.d.DB.Where(" pastureid = ? or '' = ? ", pastureId, pastureId)
  267. if eqName != "" {
  268. eqDb.Where("eqName = ? ", eqName)
  269. }
  270. eqList := make([]*model.Equipment, 0)
  271. if offset > 0 || pagecount > 0 {
  272. eqDb.Offset(offset).Limit(pagecount)
  273. }
  274. err := eqDb.Order("id").Find(&eqList).Error
  275. if err != nil {
  276. return nil, err
  277. }
  278. eqDb1 := s.d.DB.Table("equipment").Where(" pastureid = ? or '' = ? ", pastureId, pastureId)
  279. if eqName != "" {
  280. eqDb1.Where("eqName = ? ", eqName)
  281. }
  282. var count int64
  283. err = eqDb1.Count(&count).Error
  284. if err != nil {
  285. return nil, err
  286. }
  287. eqIDList := []int{}
  288. for _, eq := range eqList {
  289. eqIDList = append(eqIDList, eq.Id)
  290. }
  291. assetStt := make([]*model.AssetStt, 0)
  292. err = s.d.DB.Raw(`select * from asset_stt ast where ast.eqId in(?) and blockTime is not null and enabledTime between ? and ?
  293. union all
  294. select t.* from (
  295. select * from asset_stt ast where ast.eqId in(?) and blockTime is null and enabledTime between ? and ? order by enabledTime desc limit 999999999) t group by t.eqid`,
  296. eqIDList, startTime, endTime, eqIDList, startTime, endTime).Find(&assetStt).Error
  297. if err != nil {
  298. return nil, err
  299. }
  300. dataList := make([]map[string]interface{}, 0)
  301. pastureList := make([]*model.Pasture, 0)
  302. err = s.d.DB.Where(" id = ? or ? = '' ", pastureId, pastureId).Order("id").Find(&pastureList).Error
  303. if err != nil {
  304. return nil, errors.New("查询牧场信息出错!!!")
  305. }
  306. for _, eq := range eqList {
  307. arg := make(map[string]interface{})
  308. arg["eqName"] = eq.EqName
  309. var run float64
  310. for _, asset := range assetStt {
  311. if asset.EqId == int64(eq.Id) {
  312. if asset.BlockTime.Format("2006") == "0001" {
  313. if time.Now().Unix() > endTime.Unix() {
  314. run += endTime.Sub(asset.EnabledTime).Minutes() / 60
  315. } else {
  316. run += time.Now().Sub(asset.EnabledTime).Minutes() / 60
  317. }
  318. } else {
  319. run += asset.BlockTime.Sub(asset.EnabledTime).Minutes() / 60
  320. }
  321. }
  322. }
  323. t := endTime.Sub(startTime).Hours()
  324. arg["eqCode"] = eq.EqCode
  325. arg["run"] = FloatRound(run, 2)
  326. arg["stop"] = FloatRound(t-run, 2)
  327. for _, pasture := range pastureList {
  328. if pasture.ID == eq.PastureId {
  329. arg["pastureName"] = pasture.Name
  330. break
  331. }
  332. }
  333. arg["id"] = eq.Id
  334. dataList = append(dataList, arg)
  335. }
  336. resp := make(map[string]interface{})
  337. resp["total"] = count
  338. resp["pageNum"] = offset
  339. resp["pageSize"] = pagecount
  340. resp["list"] = dataList
  341. return resp, nil
  342. }
  343. func MaintainListPid(pastureName, eqClassId string, startTime, endTime time.Time, offset, pagecount int) (int, []map[string]interface{}, error) {
  344. eqClassList1 := make([]*model.EqClass, 0)
  345. start := (offset - 1) * pagecount
  346. page := offset * pagecount
  347. if eqClassId == "" {
  348. err := s.d.DB.Where(" section = 2 ").Find(&eqClassList1).Error
  349. if err != nil {
  350. return 0, nil, errors.New(fmt.Sprintf("查看维修类型出错 error:%s", err.Error()))
  351. }
  352. } else {
  353. n, _ := strconv.Atoi(eqClassId)
  354. eqClassList1 = append(eqClassList1, &model.EqClass{ID: n})
  355. }
  356. pastureList := make([]*model.Pasture, 0)
  357. err := s.d.DB.Select("id,name").Where(" name = ? or ? = '' or ?= '现代牧业' ", pastureName, pastureName, pastureName).Where("id != 18").Order("id").Find(&pastureList).Error
  358. if err != nil {
  359. return 0, nil, errors.New("查询牧场信息出错!!!")
  360. }
  361. respMap := make([]map[string]interface{}, 0)
  362. pastureCount := len(pastureList)
  363. n := 0
  364. for i, eq1 := range eqClassList1 {
  365. if len(respMap) >= pagecount {
  366. continue
  367. }
  368. eqClassList := make([]*model.EqClass, 0)
  369. err := s.d.DB.Where(" pid = ? ", eq1.ID).Find(&eqClassList).Error
  370. if err != nil {
  371. return 0, nil, errors.New(fmt.Sprintf("查看维修类型出错 error:%s", err.Error()))
  372. }
  373. n = n + pastureCount*(i+1)
  374. if n >= (offset-1)*pagecount {
  375. //if (start < (i+1)*pastureCount && start >= (i)*pastureCount) || (page >= (i)*pastureCount && page < (i+1)*pastureCount) {
  376. eqClass := model.EqClass{}
  377. err = s.d.DB.Where("id = ? ", eq1.ID).First(&eqClass).Error
  378. if err != nil {
  379. return 0, nil, errors.New("查询设备类别出错!!!")
  380. }
  381. eqClassMap := make(map[string]map[string]float64)
  382. for _, pastureId := range pastureList {
  383. eqClassMap[strconv.Itoa(pastureId.ID)] = make(map[string]float64)
  384. }
  385. for _, eq := range eqClassList {
  386. pastureMaintainList := make([]*model.Maintain, 0)
  387. err = s.d.DB.Raw(` SELECT m.eqId,m.pastureId,m.requestTime,m.orderTime,m.stopTime FROM maintain m JOIN equipment e on e.id = m.eqId WHERE e.eqClassid = ?
  388. and m.stopTime is not null and (m.pastureid = (select id from pasture where name = ? and name != '现代牧业') or ? = '现代牧业' or ?= '' )
  389. and m.stopTime between ? and ? and TIMESTAMPDIFF(hour, m.orderTime, m.stopTime) < 1000 order by requestTime,m.pastureid `,
  390. eq.ID, pastureName, pastureName, pastureName, startTime, endTime).Find(&pastureMaintainList).Error
  391. if err != nil {
  392. return 0, nil, errors.New(fmt.Sprintf("查看维修信息出错 error:%s", err.Error()))
  393. }
  394. maintainMap := make(map[string][]*model.Maintain, 0)
  395. pastureIDList := []string{}
  396. for _, pasture := range pastureMaintainList {
  397. pastureIDList = append(pastureIDList, pasture.PastureId)
  398. maintainMap[pasture.PastureId] = append(maintainMap[pasture.PastureId], pasture)
  399. }
  400. pastureIDList = RemoveRepeatElement(pastureIDList)
  401. for _, pastureId := range pastureIDList {
  402. maintainList := maintainMap[pastureId]
  403. count := float64(len(maintainList))
  404. var t2, t3 float64
  405. //data := make(map[string]interface{}, 0)
  406. mainMap := make(map[int64][]*model.Maintain, 0)
  407. //将每个不同的资产分类放进map
  408. eqIdList := []int64{}
  409. for _, main := range maintainList {
  410. eqIdList = append(eqIdList, main.EqId)
  411. mainMap[main.EqId] = append(mainMap[main.EqId], main)
  412. }
  413. //var stopTime, orderTime, requestTime time.Time
  414. eqIdList = uniqueArr(eqIdList)
  415. for _, k := range eqIdList {
  416. var stopTime, orderTime, requestTime time.Time
  417. mainList := mainMap[k]
  418. for i, main := range mainList {
  419. if i == 0 {
  420. //记录下维修结束时间方便下次计算t1
  421. stopTime = main.StopTime
  422. orderTime = main.OrderTime
  423. requestTime = main.RequestTime
  424. //每个资产第一次都根据筛选开始时间计算出t1
  425. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  426. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  427. //fmt.Println(main.StopTime, main.OrderTime, "111111")
  428. //t1 += main.RequestTime.Sub(startTime).Minutes()
  429. } else {
  430. //t1 += main.RequestTime.Sub(stopTime).Minutes()
  431. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  432. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  433. //fmt.Println(main.StopTime, main.OrderTime, "22222222")
  434. //记录下维修结束时间方便下次计算t1
  435. stopTime = main.StopTime
  436. orderTime = main.OrderTime
  437. requestTime = main.RequestTime
  438. }
  439. }
  440. if stopTime.Format("2006") == "0001" {
  441. //如果还没有结束获取当前时间计算t1
  442. if orderTime.Format("2006") == "0001" {
  443. t2 += requestTime.Sub(time.Now()).Hours()
  444. } else {
  445. if time.Now().Unix() < endTime.Unix() {
  446. t3 += time.Now().Sub(orderTime).Hours()
  447. } else {
  448. t3 += endTime.Sub(orderTime).Hours()
  449. }
  450. }
  451. }
  452. }
  453. equipmentList := make([]*model.Equipment, 0)
  454. err = s.d.DB.Select("id").Where("id not in(?) ", eqIdList).
  455. Where(" entranceDate < ? and ( blockTime is null or blockTime = '' or blockTime > ? or blockTime = '0000-00-00 00:00:00' ) "+
  456. "and pastureid = ? and eqClassId = ? ",
  457. endTime, startTime, pastureId, eq.ID).Find(&equipmentList).Error
  458. if err != nil {
  459. return 0, nil, errors.New(fmt.Sprintf("查询资产信息出错 error:%v", err))
  460. }
  461. day := ((endTime.Unix() - startTime.Unix()) / 60 / 60 / 24)
  462. count1 := float64(len(mainMap))
  463. allCount := (24 * (count1 + float64(len(equipmentList)))) * float64(day)
  464. eqClassMap[pastureId]["allCount"] += allCount
  465. eqClassMap[pastureId]["count"] += count
  466. eqClassMap[pastureId]["t2"] += t2
  467. eqClassMap[pastureId]["t3"] += t3
  468. //data["mtbf"] = FloatRound(allCount/count, 2)
  469. //data["mttr"] = FloatRound((t2+t3)/count, 2)
  470. //data["mttf"] = FloatRound((allCount-t2-t3)/count, 2)
  471. //data["count"] = count
  472. //pasture := model.Pasture{}
  473. //err := s.d.DB.Where("id = ? ", pastureId).First(&pasture).Error
  474. //if err != nil {
  475. // return nil, errors.New("查询牧场信息出错!!!")
  476. //}
  477. //data["pastureName"] = pasture.Name
  478. //data["eqClassName"] = eqClass.TypeName
  479. //respMap = append(respMap, data)
  480. }
  481. }
  482. for n, item := range pastureList {
  483. if len(respMap) >= pagecount {
  484. break
  485. } else if (i*pastureCount)+n < page && (i*pastureCount)+n >= start {
  486. pastureId := strconv.Itoa(item.ID)
  487. data := make(map[string]interface{})
  488. data["pastureName"] = item.Name
  489. data["pastureId"] = item.ID
  490. data["eqClassName"] = eqClass.TypeName
  491. data["eqId"] = eqClass.ID
  492. if _, ok := eqClassMap[pastureId]["count"]; ok {
  493. data["mtbf"] = FloatRound(eqClassMap[pastureId]["allCount"]/eqClassMap[pastureId]["count"], 2)
  494. data["mttr"] = FloatRound((eqClassMap[pastureId]["t2"]+eqClassMap[pastureId]["t3"])/eqClassMap[pastureId]["count"], 2)
  495. data["mttf"] = FloatRound((eqClassMap[pastureId]["allCount"]-eqClassMap[pastureId]["t2"]-eqClassMap[pastureId]["t3"])/eqClassMap[pastureId]["count"], 2)
  496. }
  497. data["count"] = eqClassMap[pastureId]["count"]
  498. respMap = append(respMap, data)
  499. }
  500. }
  501. }
  502. }
  503. return pastureCount * len(eqClassList1), respMap, nil
  504. }
  505. func MaintainListEquipment(pastureName, eqClassId string, startTime, endTime time.Time, offset, pageSize int, eqName string) (int64, []map[string]interface{}, error) {
  506. pastureMaintainList := make([]*model.Maintain, 0)
  507. err := s.d.DB.Raw(` SELECT m.* FROM maintain m JOIN equipment e on e.id = m.eqId WHERE e.eqClassId = ?
  508. and m.stopTime is not null and (m.pastureid = (select id from pasture where name = ? and name != '现代牧业') or ? = '现代牧业' )
  509. and m.stopTime between ? and ? and TIMESTAMPDIFF(hour, m.orderTime, m.stopTime) < 1000 order by requestTime,m.pastureid `,
  510. eqClassId, pastureName, pastureName, startTime, endTime).Find(&pastureMaintainList).Error
  511. if err != nil {
  512. return 0, nil, errors.New(fmt.Sprintf("查看维修信息出错 error:%s", err.Error()))
  513. }
  514. maintainMap := make(map[string][]*model.Maintain)
  515. eqid := []int64{}
  516. for _, pasture := range pastureMaintainList {
  517. eqid = append(eqid, pasture.EqId)
  518. maintainMap[pasture.PastureId] = append(maintainMap[pasture.PastureId], pasture)
  519. }
  520. eqClass := model.EqClass{}
  521. err = s.d.DB.Where("id = ? ", eqClassId).First(&eqClass).Error
  522. if err != nil {
  523. return 0, nil, errors.New("查询设备类别出错!!!")
  524. }
  525. //pastureIDList = RemoveRepeatElement(pastureIDList)
  526. pastureList := make([]*model.Pasture, 0)
  527. s.d.DB.Where("name = ?", pastureName).Find(&pastureList)
  528. eqList := make([]*model.Equipment, 0)
  529. err = s.d.DB.Where(" id in ? ", eqid).Where(" (pastureid = (select id from pasture where name = ? ) or ?='现代牧业') ", pastureName).Where(" ( eqName like concat('%',?,'%') or ?= '')", eqName, eqName).Offset(offset - 1).Limit(pageSize).Find(&eqList).Error
  530. if err != nil {
  531. return 0, nil, errors.New("查询设备出错!!!")
  532. }
  533. var total int64
  534. s.d.DB.Table("equipment").Where(" id in ? ", eqid).Where(" (pastureid = (select id from pasture where name = ? ) ) or ?='现代牧业 ", pastureName).Where(" ( eqName like concat('%',?,'%') or ?= '')", eqName, eqName).Count(&total)
  535. respMap := make([]map[string]interface{}, 0)
  536. for _, p := range pastureList {
  537. pastureId := strconv.Itoa(p.ID)
  538. maintainList := maintainMap[pastureId]
  539. //count := float64(len(maintainList))
  540. var t2, t3 float64
  541. mainMap := make(map[int64][]*model.Maintain, 0)
  542. //将每个不同的资产分类放进map
  543. //eqIdList := []int64{}
  544. for _, main := range maintainList {
  545. //eqIdList = append(eqIdList, main.EqId)
  546. mainMap[main.EqId] = append(mainMap[main.EqId], main)
  547. }
  548. //var stopTime, orderTime, requestTime time.Time
  549. //eqIdList = uniqueArr(eqIdList)
  550. //for _, k := range eqIdList {
  551. for _, eq := range eqList {
  552. var stopTime, orderTime, requestTime time.Time
  553. mainList := make([]*model.Maintain, 0)
  554. if _, ok := mainMap[int64(eq.Id)]; ok {
  555. mainList = mainMap[int64(eq.Id)]
  556. }
  557. for i, main := range mainList {
  558. if i == 0 {
  559. //记录下维修结束时间方便下次计算t1
  560. stopTime = main.StopTime
  561. orderTime = main.OrderTime
  562. requestTime = main.RequestTime
  563. //每个资产第一次都根据筛选开始时间计算出t1
  564. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  565. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  566. //fmt.Println(main.StopTime, main.OrderTime, "111111")
  567. //t1 += main.RequestTime.Sub(startTime).Minutes()
  568. } else {
  569. //t1 += main.RequestTime.Sub(stopTime).Minutes()
  570. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  571. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  572. //fmt.Println(main.StopTime, main.OrderTime, "22222222")
  573. //记录下维修结束时间方便下次计算t1
  574. stopTime = main.StopTime
  575. orderTime = main.OrderTime
  576. requestTime = main.RequestTime
  577. }
  578. }
  579. data := make(map[string]interface{}, 0)
  580. if len(mainList) > 0 {
  581. if stopTime.Format("2006") == "0001" {
  582. //如果还没有结束获取当前时间计算t1
  583. if orderTime.Format("2006") == "0001" {
  584. t2 += requestTime.Sub(time.Now()).Hours()
  585. } else {
  586. if time.Now().Unix() < endTime.Unix() {
  587. t3 += time.Now().Sub(orderTime).Hours()
  588. } else {
  589. t3 += endTime.Sub(orderTime).Hours()
  590. }
  591. }
  592. }
  593. day := ((endTime.Unix() - startTime.Unix()) / 60 / 60 / 24)
  594. count1 := float64(len(mainList))
  595. allCount := (24 * (count1 + 1)) * float64(day)
  596. data["mtbf"] = FloatRound(allCount/count1, 2)
  597. data["mttr"] = FloatRound((t2+t3)/count1, 2)
  598. data["mttf"] = FloatRound((allCount-t2-t3)/count1, 2)
  599. data["count"] = count1
  600. } else {
  601. data["mtbf"] = 0
  602. data["mttr"] = 0
  603. data["mttf"] = 0
  604. data["count"] = 0
  605. }
  606. data["eqCode"] = eq.EqCode
  607. data["eqName"] = eq.EqName
  608. data["pastureId"] = pastureId
  609. data["eqClassName"] = eqClass.TypeName
  610. respMap = append(respMap, data)
  611. }
  612. }
  613. return total, respMap, nil
  614. }