report.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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, eqClassPIdStr string) (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 ?= '') and (e.pid = ? or ?= '') limit ?,?",
  20. pastureName, pastureName, pastureName, eqClassIdStr, eqClassIdStr, eqClassPIdStr, eqClassPIdStr, offset-1, pagecount).Order(" eqsort asc ").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 ?= '') and (e.pid = ? or ?= '') ",
  23. pastureName, pastureName, pastureName, eqClassIdStr, eqClassIdStr, eqClassPIdStr, eqClassPIdStr).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.*,(select DATE_FORMAT(bpu.creatDate,'%Y-%m-%d %H:%i:%s') from bigpartapply bp join bigpartuse bpu on bpu.oddCode = bp.applyCode
  34. where bp.RUCode =m.repairCode order by bpu.creatDate desc limit 1) creatDate FROM maintain m JOIN equipment e on e.id = m.eqId WHERE (e.eqClassId = ? or ? = '')
  35. and m.stopTime is not null and (m.pastureid = (select id from pasture where name = ? and name != '现代牧业') or ? = '现代牧业' )
  36. and m.stopTime between ? and ? and TIMESTAMPDIFF(hour, m.orderTime, m.stopTime) < 1000 order by requestTime,m.pastureid `,
  37. eq.EqClass.ID, eq.EqClass.ID, eq.Pasture.Name, eq.Pasture.Name, startTime, endTime).Find(&pastureMaintainList).Error
  38. if err != nil {
  39. return 0, nil, errors.New(fmt.Sprintf("查看维修信息出错 error:%s", err.Error()))
  40. }
  41. maintainMap := make(map[string][]*model.Maintain)
  42. pastureIDList := []string{}
  43. for _, pasture := range pastureMaintainList {
  44. pastureIDList = append(pastureIDList, pasture.PastureId)
  45. maintainMap[pasture.PastureId] = append(maintainMap[pasture.PastureId], pasture)
  46. }
  47. eqClass := model.EqClass{}
  48. err = s.d.DB.Where("id = ? ", eq.EqClass.ID).First(&eqClass).Error
  49. if err != nil {
  50. return 0, nil, errors.New("查询设备类别出错!!!")
  51. }
  52. pastureIDList = RemoveRepeatElement(pastureIDList)
  53. for _, pastureId := range pastureIDList {
  54. maintainList := maintainMap[pastureId]
  55. count := float64(len(maintainList))
  56. var t2, t3 float64
  57. data := make(map[string]interface{}, 0)
  58. mainMap := make(map[int64][]*model.Maintain, 0)
  59. //将每个不同的资产分类放进map
  60. eqIdList := []int64{}
  61. for _, main := range maintainList {
  62. eqIdList = append(eqIdList, main.EqId)
  63. mainMap[main.EqId] = append(mainMap[main.EqId], main)
  64. }
  65. //var stopTime, orderTime, requestTime time.Time
  66. eqIdList = uniqueArr(eqIdList)
  67. for _, k := range eqIdList {
  68. var stopTime, orderTime, requestTime time.Time
  69. mainList := mainMap[k]
  70. for i, main := range mainList {
  71. if i == 0 {
  72. //记录下维修结束时间方便下次计算t1
  73. stopTime = main.StopTime
  74. orderTime = main.OrderTime
  75. //requestTime = main.RequestTime
  76. //每个资产第一次都根据筛选开始时间计算出t1
  77. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  78. if main.CreatDate == "" {
  79. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  80. requestTime = main.RequestTime
  81. } else {
  82. layout := "2006-01-02 15:04:05"
  83. t, err := time.Parse(layout, main.CreatDate)
  84. if err != nil {
  85. fmt.Println(err)
  86. continue
  87. }
  88. tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05"))
  89. if err != nil {
  90. fmt.Println(err)
  91. continue
  92. }
  93. t3 += tStop.Sub(t).Hours()
  94. requestTime = t
  95. }
  96. //fmt.Println(main.StopTime, main.OrderTime, "111111")
  97. //t1 += main.RequestTime.Sub(startTime).Minutes()
  98. } else {
  99. //t1 += main.RequestTime.Sub(stopTime).Minutes()
  100. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  101. if main.CreatDate == "" {
  102. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  103. requestTime = main.RequestTime
  104. } else {
  105. layout := "2006-01-02 15:04:05"
  106. t, err := time.Parse(layout, main.CreatDate)
  107. if err != nil {
  108. fmt.Println(err)
  109. continue
  110. }
  111. tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05"))
  112. if err != nil {
  113. fmt.Println(err)
  114. continue
  115. }
  116. t3 += tStop.Sub(t).Hours()
  117. requestTime = t
  118. }
  119. //fmt.Println(main.StopTime, main.OrderTime, "22222222")
  120. //记录下维修结束时间方便下次计算t1
  121. stopTime = main.StopTime
  122. orderTime = main.OrderTime
  123. }
  124. }
  125. if stopTime.Format("2006") == "0001" {
  126. //如果还没有结束获取当前时间计算t1
  127. if orderTime.Format("2006") == "0001" {
  128. t2 += requestTime.Sub(time.Now()).Hours()
  129. } else {
  130. if time.Now().Unix() < endTime.Unix() {
  131. t3 += time.Now().Sub(orderTime).Hours()
  132. } else {
  133. t3 += endTime.Sub(orderTime).Hours()
  134. }
  135. }
  136. }
  137. }
  138. equipmentList := make([]*model.Equipment, 0)
  139. err = s.d.DB.Where("id not in(?) ", eqIdList).
  140. Where(" entranceDate < ? and ( blockTime is null or blockTime = '' or blockTime > ? or blockTime = '0000-00-00 00:00:00' ) "+
  141. "and pastureid = ? and eqClassId = ? ",
  142. endTime, startTime, pastureId, eq.EqClass.ID).Find(&equipmentList).Error
  143. if err != nil {
  144. return 0, nil, errors.New(fmt.Sprintf("查询资产信息出错 error:%v", err))
  145. }
  146. day := ((endTime.Unix() - startTime.Unix()) / 60 / 60 / 24)
  147. count1 := float64(len(mainMap))
  148. allCount := (24 * (count1 + float64(len(equipmentList)))) * float64(day)
  149. data["mtbf"] = FloatRound(allCount/count, 2)
  150. data["mttr"] = FloatRound((t2+t3)/count, 2)
  151. data["mttf"] = FloatRound((allCount-t2-t3)/count, 2)
  152. data["count"] = count
  153. data["eqClassId"] = eqClass.ID
  154. pasture := model.Pasture{}
  155. err := s.d.DB.Where("id = ? ", pastureId).First(&pasture).Error
  156. if err != nil {
  157. return 0, nil, errors.New("查询牧场信息出错!!!")
  158. }
  159. data["pastureId"] = pasture.ID
  160. data["pastureName"] = pasture.Name
  161. data["eqClassName"] = eqClass.TypeName
  162. respMap = append(respMap, data)
  163. }
  164. exist := false
  165. for _, p := range respMap {
  166. if p["eqClassId"] != nil {
  167. //str := strconv.Itoa(p["eqClassId"].(int))
  168. if p["eqClassId"].(int) == eq.EqClass.ID && p["pastureId"].(int) == eq.Pasture.ID {
  169. exist = true
  170. break
  171. }
  172. }
  173. }
  174. if !exist {
  175. data := make(map[string]interface{}, 0)
  176. pasture := model.Pasture{}
  177. err := s.d.DB.Where("id = ? ", eq.Pasture.ID).First(&pasture).Error
  178. if err != nil {
  179. return 0, nil, errors.New("查询牧场信息出错!!!")
  180. }
  181. data["pastureName"] = pasture.Name
  182. data["pastureId"] = eq.Pasture.ID
  183. data["eqClassName"] = eqClass.TypeName
  184. data["mtbf"] = 0
  185. data["mttr"] = 0
  186. data["mttf"] = 0
  187. data["count"] = 0
  188. data["eqClassId"] = eqClass.ID
  189. respMap = append(respMap, data)
  190. }
  191. //}
  192. }
  193. return int(total), respMap, nil
  194. }
  195. func FloatRound(f float64, n int) float64 {
  196. format := "%." + strconv.Itoa(n) + "f"
  197. res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
  198. return res
  199. }
  200. func RemoveRepeatElement(list []string) []string {
  201. // 创建一个临时map用来存储数组元素
  202. temp := make(map[string]bool)
  203. var list2 []string
  204. index := 0
  205. for _, v := range list {
  206. // 遍历数组元素,判断此元素是否已经存在map中
  207. _, ok := temp[v]
  208. if !ok {
  209. list2 = append(list2, v)
  210. temp[v] = true
  211. }
  212. index++
  213. }
  214. return list2
  215. }
  216. func uniqueArr(arr []int64) []int64 {
  217. newArr := make([]int64, 0)
  218. tempArr := make(map[int64]bool, len(newArr))
  219. for _, v := range arr {
  220. if tempArr[v] == false {
  221. tempArr[v] = true
  222. newArr = append(newArr, v)
  223. }
  224. }
  225. return newArr
  226. }
  227. func GetOversee(pastureId, eqClassId, empname string, offset, pagecount int) ([]map[string]interface{}, error) {
  228. overseeList := make([]*model.Oversee, 0)
  229. eq := s.d.DB.Where(" ( ? = '' or pastureid = ? )", pastureId, pastureId)
  230. if eqClassId != "" {
  231. eq.Where("eqClassId = ?", eqClassId)
  232. }
  233. if empname != "" {
  234. eq.Where("maintenance like ?", "%"+empname+"%")
  235. }
  236. err := eq.Offset(offset).Limit(pagecount).Order("inputTime desc ").Find(&overseeList).Error
  237. if err != nil {
  238. logs.Error(err)
  239. return nil, errors.New(fmt.Sprintf("查询督办数据出错 errors: %v", err))
  240. }
  241. userIds := []string{}
  242. for _, oversee := range overseeList {
  243. u := strings.Split(oversee.Maintenance, ",")
  244. userIds = append(userIds, oversee.Levelone, oversee.Leveltwo, oversee.Levelthree, oversee.Entryperson)
  245. userIds = append(userIds, u...)
  246. }
  247. userIds = RemoveRepeatElement(userIds)
  248. userList := make([]*model.User, 0)
  249. if len(userIds) > 0 {
  250. 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
  251. if err != nil {
  252. logs.Error(err)
  253. return nil, errors.New(fmt.Sprintf("查询督办数据出错 errors: %v", err))
  254. }
  255. }
  256. userMap := make(map[string]*model.User)
  257. for _, user := range userList {
  258. userMap[user.Username] = user
  259. }
  260. respOversee := make([]map[string]interface{}, 0)
  261. for _, oversee := range overseeList {
  262. arg := make(map[string]interface{})
  263. arg["id"] = oversee.ID
  264. arg["pastureName"] = oversee.PastureName
  265. arg["pastureId"] = oversee.PastureId
  266. arg["pushTime"] = oversee.PushTime
  267. arg["inputTime"] = oversee.InputTime.Format("2006-01-02")
  268. arg["departmentId"] = oversee.DepartmentId
  269. arg["departmentName"] = oversee.DepartmentName
  270. arg["eqClassName"] = oversee.EqClassName
  271. arg["eqClassId"] = oversee.EqClassId
  272. arg["entryperson"] = oversee.Entryperson
  273. if _, ok := userMap[oversee.Entryperson]; ok {
  274. arg["entrypersonName"] = userMap[oversee.Entryperson].EmpName
  275. arg["entrypersonId"] = userMap[oversee.Entryperson].Username
  276. }
  277. arg["levelone"] = oversee.Levelone
  278. if _, ok := userMap[oversee.Levelone]; ok {
  279. arg["leveloneName"] = userMap[oversee.Levelone].EmpName
  280. arg["leveloneId"] = userMap[oversee.Levelone].Username
  281. }
  282. arg["leveltwo"] = oversee.Leveltwo
  283. if _, ok := userMap[oversee.Leveltwo]; ok {
  284. arg["leveltwo"] = userMap[oversee.Leveltwo].EmpName
  285. arg["leveltwoId"] = userMap[oversee.Leveltwo].Username
  286. }
  287. arg["levelthree"] = oversee.Levelthree
  288. if _, ok := userMap[oversee.Levelthree]; ok {
  289. arg["levelthree"] = userMap[oversee.Levelthree].EmpName
  290. arg["levelthreeId"] = userMap[oversee.Levelthree].Username
  291. }
  292. maintenance := make([]map[string]interface{}, 0)
  293. for _, u := range strings.Split(oversee.Maintenance, ",") {
  294. main := make(map[string]interface{})
  295. main["id"] = userMap[u].Username
  296. main["empname"] = userMap[u].EmpName
  297. maintenance = append(maintenance, main)
  298. }
  299. arg["maintenance"] = maintenance
  300. respOversee = append(respOversee, arg)
  301. }
  302. return respOversee, nil
  303. }
  304. func StartStopRecord(pastureId, eqName string, startTime, endTime time.Time, offset, pagecount int) (map[string]interface{}, error) {
  305. eqDb := s.d.DB.Where(" pastureid = ? or '' = ? ", pastureId, pastureId)
  306. if eqName != "" {
  307. eqDb.Where("eqName = ? ", eqName)
  308. }
  309. eqList := make([]*model.Equipment, 0)
  310. if offset > 0 || pagecount > 0 {
  311. eqDb.Offset(offset).Limit(pagecount)
  312. }
  313. err := eqDb.Order("id").Find(&eqList).Error
  314. if err != nil {
  315. return nil, err
  316. }
  317. eqDb1 := s.d.DB.Table("equipment").Where(" pastureid = ? or '' = ? ", pastureId, pastureId)
  318. if eqName != "" {
  319. eqDb1.Where("eqName = ? ", eqName)
  320. }
  321. var count int64
  322. err = eqDb1.Count(&count).Error
  323. if err != nil {
  324. return nil, err
  325. }
  326. eqIDList := []int{}
  327. for _, eq := range eqList {
  328. eqIDList = append(eqIDList, eq.Id)
  329. }
  330. assetStt := make([]*model.AssetStt, 0)
  331. err = s.d.DB.Raw(`select * from asset_stt ast where ast.eqId in(?) and blockTime is not null and enabledTime between ? and ?
  332. union all
  333. select t.* from (
  334. 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`,
  335. eqIDList, startTime, endTime, eqIDList, startTime, endTime).Find(&assetStt).Error
  336. if err != nil {
  337. return nil, err
  338. }
  339. dataList := make([]map[string]interface{}, 0)
  340. pastureList := make([]*model.Pasture, 0)
  341. err = s.d.DB.Where(" id = ? or ? = '' ", pastureId, pastureId).Order("id").Find(&pastureList).Error
  342. if err != nil {
  343. return nil, errors.New("查询牧场信息出错!!!")
  344. }
  345. for _, eq := range eqList {
  346. arg := make(map[string]interface{})
  347. arg["eqName"] = eq.EqName
  348. var run float64
  349. for _, asset := range assetStt {
  350. if asset.EqId == int64(eq.Id) {
  351. if asset.BlockTime.Format("2006") == "0001" {
  352. if time.Now().Unix() > endTime.Unix() {
  353. run += endTime.Sub(asset.EnabledTime).Minutes() / 60
  354. } else {
  355. run += time.Now().Sub(asset.EnabledTime).Minutes() / 60
  356. }
  357. } else {
  358. run += asset.BlockTime.Sub(asset.EnabledTime).Minutes() / 60
  359. }
  360. }
  361. }
  362. t := endTime.Sub(startTime).Hours()
  363. arg["eqCode"] = eq.EqCode
  364. arg["run"] = FloatRound(run, 2)
  365. arg["stop"] = FloatRound(t-run, 2)
  366. for _, pasture := range pastureList {
  367. if pasture.ID == eq.PastureId {
  368. arg["pastureName"] = pasture.Name
  369. break
  370. }
  371. }
  372. arg["id"] = eq.Id
  373. dataList = append(dataList, arg)
  374. }
  375. resp := make(map[string]interface{})
  376. resp["total"] = count
  377. resp["pageNum"] = offset
  378. resp["pageSize"] = pagecount
  379. resp["list"] = dataList
  380. return resp, nil
  381. }
  382. func MaintainListPid(pastureName, eqClassId string, startTime, endTime time.Time, offset, pagecount int) (int, []map[string]interface{}, error) {
  383. eqClassList1 := make([]*model.EqClass, 0)
  384. start := (offset - 1) * pagecount
  385. page := offset * pagecount
  386. if eqClassId == "" {
  387. err := s.d.DB.Where(" section = 2 ").Order(" eqsort asc ").Find(&eqClassList1).Error
  388. if err != nil {
  389. return 0, nil, errors.New(fmt.Sprintf("查看维修类型出错 error:%s", err.Error()))
  390. }
  391. } else {
  392. n, _ := strconv.Atoi(eqClassId)
  393. eqClassList1 = append(eqClassList1, &model.EqClass{ID: n})
  394. }
  395. pastureList := make([]*model.Pasture, 0)
  396. err := s.d.DB.Select("id,name,center").Where(" name = ? or ? = '' or ?= '现代牧业' ", pastureName, pastureName, pastureName).Where("id != 18").Order("center_sort,id").Find(&pastureList).Error
  397. if err != nil {
  398. return 0, nil, errors.New("查询牧场信息出错!!!")
  399. }
  400. respMap := make([]map[string]interface{}, 0)
  401. pastureCount := len(pastureList)
  402. n := 0
  403. var jtCount, jtAllCount, jtT2, jtT3 float64
  404. centerMap := make(map[string][]map[string]float64, 0)
  405. for i, eq1 := range eqClassList1 {
  406. if len(respMap) >= pagecount {
  407. continue
  408. }
  409. eqClassList := make([]*model.EqClass, 0)
  410. err := s.d.DB.Where(" pid = ? ", eq1.ID).Find(&eqClassList).Error
  411. if err != nil {
  412. return 0, nil, errors.New(fmt.Sprintf("查看维修类型出错 error:%s", err.Error()))
  413. }
  414. n = n + pastureCount*(i+1)
  415. if n >= (offset-1)*pagecount {
  416. //if (start < (i+1)*pastureCount && start >= (i)*pastureCount) || (page >= (i)*pastureCount && page < (i+1)*pastureCount) {
  417. eqClass := model.EqClass{}
  418. err = s.d.DB.Where("id = ? ", eq1.ID).First(&eqClass).Error
  419. if err != nil {
  420. return 0, nil, errors.New("查询设备类别出错!!!")
  421. }
  422. eqClassMap := make(map[string]map[string]float64)
  423. for _, pastureId := range pastureList {
  424. eqClassMap[strconv.Itoa(pastureId.ID)] = make(map[string]float64)
  425. }
  426. for _, eq := range eqClassList {
  427. pastureMaintainList := make([]*model.Maintain, 0)
  428. err = s.d.DB.Raw(` SELECT m.eqId,m.pastureId,m.requestTime,m.orderTime,m.stopTime,(select DATE_FORMAT(bpu.creatDate,'%Y-%m-%d %H:%i:%s') from bigpartapply bp join bigpartuse bpu on bpu.oddCode = bp.applyCode
  429. where bp.RUCode =m.repairCode order by bpu.creatDate desc limit 1) creatDate FROM maintain m JOIN equipment e on e.id = m.eqId WHERE e.eqClassid = ?
  430. and m.stopTime is not null and (m.pastureid = (select id from pasture where name = ? and name != '现代牧业') or ? = '现代牧业' or ?= '' )
  431. and m.stopTime between ? and ? and TIMESTAMPDIFF(hour, m.orderTime, m.stopTime) < 1000 order by requestTime,m.pastureid `,
  432. eq.ID, pastureName, pastureName, pastureName, startTime, endTime).Find(&pastureMaintainList).Error
  433. if err != nil {
  434. return 0, nil, errors.New(fmt.Sprintf("查看维修信息出错 error:%s", err.Error()))
  435. }
  436. maintainMap := make(map[string][]*model.Maintain, 0)
  437. pastureIDList := []string{}
  438. for _, pasture := range pastureMaintainList {
  439. pastureIDList = append(pastureIDList, pasture.PastureId)
  440. maintainMap[pasture.PastureId] = append(maintainMap[pasture.PastureId], pasture)
  441. }
  442. pastureIDList = RemoveRepeatElement(pastureIDList)
  443. for _, pastureId := range pastureIDList {
  444. maintainList := maintainMap[pastureId]
  445. count := float64(len(maintainList))
  446. var t2, t3 float64
  447. //data := make(map[string]interface{}, 0)
  448. mainMap := make(map[int64][]*model.Maintain, 0)
  449. //将每个不同的资产分类放进map
  450. eqIdList := []int64{}
  451. for _, main := range maintainList {
  452. eqIdList = append(eqIdList, main.EqId)
  453. mainMap[main.EqId] = append(mainMap[main.EqId], main)
  454. }
  455. //var stopTime, orderTime, requestTime time.Time
  456. eqIdList = uniqueArr(eqIdList)
  457. for _, k := range eqIdList {
  458. var stopTime, orderTime, requestTime time.Time
  459. mainList := mainMap[k]
  460. for i, main := range mainList {
  461. if i == 0 {
  462. //记录下维修结束时间方便下次计算t1
  463. stopTime = main.StopTime
  464. orderTime = main.OrderTime
  465. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  466. if main.CreatDate == "" {
  467. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  468. requestTime = main.RequestTime
  469. } else {
  470. layout := "2006-01-02 15:04:05"
  471. t, err := time.Parse(layout, main.CreatDate)
  472. if err != nil {
  473. fmt.Println(err)
  474. continue
  475. }
  476. tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05"))
  477. if err != nil {
  478. fmt.Println(err)
  479. continue
  480. }
  481. t3 += tStop.Sub(t).Hours()
  482. requestTime = t
  483. }
  484. //fmt.Println(main.StopTime, main.OrderTime, "111111")
  485. //t1 += main.RequestTime.Sub(startTime).Minutes()
  486. } else {
  487. //t1 += main.RequestTime.Sub(stopTime).Minutes()
  488. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  489. if main.CreatDate == "" {
  490. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  491. requestTime = main.RequestTime
  492. } else {
  493. layout := "2006-01-02 15:04:05"
  494. t, err := time.Parse(layout, main.CreatDate)
  495. if err != nil {
  496. fmt.Println(err)
  497. continue
  498. }
  499. tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05"))
  500. if err != nil {
  501. fmt.Println(err)
  502. continue
  503. }
  504. t3 += tStop.Sub(t).Hours()
  505. requestTime = t
  506. }
  507. //记录下维修结束时间方便下次计算t1
  508. stopTime = main.StopTime
  509. orderTime = main.OrderTime
  510. //requestTime = main.RequestTime
  511. }
  512. }
  513. if stopTime.Format("2006") == "0001" {
  514. //如果还没有结束获取当前时间计算t1
  515. if orderTime.Format("2006") == "0001" {
  516. t2 += requestTime.Sub(time.Now()).Hours()
  517. } else {
  518. if time.Now().Unix() < endTime.Unix() {
  519. t3 += time.Now().Sub(orderTime).Hours()
  520. } else {
  521. t3 += endTime.Sub(orderTime).Hours()
  522. }
  523. }
  524. }
  525. }
  526. equipmentList := make([]*model.Equipment, 0)
  527. err = s.d.DB.Select("id").Where("id not in(?) ", eqIdList).
  528. Where(" entranceDate < ? and ( blockTime is null or blockTime = '' or blockTime > ? or blockTime = '0000-00-00 00:00:00' ) "+
  529. "and pastureid = ? and eqClassId = ? ",
  530. endTime, startTime, pastureId, eq.ID).Find(&equipmentList).Error
  531. if err != nil {
  532. return 0, nil, errors.New(fmt.Sprintf("查询资产信息出错 error:%v", err))
  533. }
  534. day := ((endTime.Unix() - startTime.Unix()) / 60 / 60 / 24)
  535. count1 := float64(len(mainMap))
  536. allCount := (24 * (count1 + float64(len(equipmentList)))) * float64(day)
  537. eqClassMap[pastureId]["allCount"] += allCount
  538. eqClassMap[pastureId]["count"] += count
  539. eqClassMap[pastureId]["t2"] += t2
  540. eqClassMap[pastureId]["t3"] += t3
  541. }
  542. }
  543. //jtData := make(map[string]interface{})
  544. //centerMap := make([]map[string]interface{}, 0)
  545. if offset == 1 {
  546. if pastureName == "现代牧业" || pastureName == "" {
  547. for _, item := range pastureList {
  548. if item.ID == 18 {
  549. continue
  550. }
  551. pastureId := strconv.Itoa(item.ID)
  552. jtAllCount += eqClassMap[pastureId]["allCount"]
  553. jtCount += eqClassMap[pastureId]["count"]
  554. fmt.Println(eqClassMap[pastureId]["count"])
  555. jtT2 += eqClassMap[pastureId]["t2"]
  556. jtT3 += eqClassMap[pastureId]["t3"]
  557. }
  558. centerList := []string{"一中心", "二中心", "三中心"}
  559. for _, centerName := range centerList {
  560. var allCount, count, t2, t3 float64
  561. data := make(map[string]float64)
  562. for _, item := range pastureList {
  563. if item.ID == 18 {
  564. continue
  565. }
  566. if centerName == item.Center {
  567. pastureId := strconv.Itoa(item.ID)
  568. allCount += eqClassMap[pastureId]["allCount"]
  569. count += eqClassMap[pastureId]["count"]
  570. t2 += eqClassMap[pastureId]["t2"]
  571. t3 += eqClassMap[pastureId]["t3"]
  572. }
  573. }
  574. data["allCount"] += allCount
  575. data["count"] += count
  576. data["t2"] += t2
  577. data["t3"] += t2
  578. //if allCount > 0 && count > 0 && t2 > 0 && t3 > 0 {
  579. //if allCount != 0 && count != 0 {
  580. // data["mtbf"] = FloatRound(allCount/count, 2)
  581. //} else {
  582. // data["mtbf"] = 0
  583. //}
  584. //if (t2 != 0 || t3 != 0) && count != 0 {
  585. // data["mttr"] = FloatRound((t2+t3)/count, 2)
  586. //} else {
  587. // data["mttr"] = 0
  588. //}
  589. //if allCount != 0 && count != 0 {
  590. // data["mttf"] = FloatRound((allCount-t2-t3)/count, 2)
  591. //} else {
  592. // data["mttf"] = 0
  593. //}
  594. //data["count"] = count
  595. centerMap[centerName] = append(centerMap[centerName], data)
  596. //}
  597. //data["pastureName"] = centerName
  598. //respMap = append(respMap, data)
  599. }
  600. }
  601. }
  602. for n, item := range pastureList {
  603. if len(respMap) >= pagecount {
  604. break
  605. } else if (i*pastureCount)+n < page && (i*pastureCount)+n >= start {
  606. pastureId := strconv.Itoa(item.ID)
  607. data := make(map[string]interface{})
  608. data["pastureName"] = item.Name
  609. data["pastureId"] = item.ID
  610. data["eqClassName"] = eqClass.TypeName
  611. data["eqId"] = eqClass.ID
  612. if _, ok := eqClassMap[pastureId]["count"]; ok {
  613. data["mtbf"] = FloatRound(eqClassMap[pastureId]["allCount"]/eqClassMap[pastureId]["count"], 2)
  614. data["mttr"] = FloatRound((eqClassMap[pastureId]["t2"]+eqClassMap[pastureId]["t3"])/eqClassMap[pastureId]["count"], 2)
  615. data["mttf"] = FloatRound((eqClassMap[pastureId]["allCount"]-eqClassMap[pastureId]["t2"]-eqClassMap[pastureId]["t3"])/eqClassMap[pastureId]["count"], 2)
  616. }
  617. data["count"] = eqClassMap[pastureId]["count"]
  618. fmt.Println(eqClassMap[pastureId]["count"])
  619. respMap = append(respMap, data)
  620. }
  621. }
  622. }
  623. }
  624. respMap1 := make([]map[string]interface{}, 0)
  625. data := make(map[string]interface{})
  626. if jtAllCount != 0 && jtCount != 0 {
  627. data["mtbf"] = FloatRound(jtAllCount/jtCount, 2)
  628. } else {
  629. data["mtbf"] = 0
  630. }
  631. if (jtT3 != 0 || jtT2 != 0) && jtCount != 0 {
  632. data["mttr"] = FloatRound((jtT2+jtT3)/jtCount, 2)
  633. } else {
  634. data["mttr"] = 0
  635. }
  636. if jtCount != 0 && jtAllCount != 0 {
  637. data["mttf"] = FloatRound((jtAllCount-jtT2-jtT3)/jtCount, 2)
  638. } else {
  639. data["mttf"] = 0
  640. }
  641. data["count"] = jtCount
  642. data["pastureName"] = "现代牧业"
  643. respMap1 = append(respMap1, data)
  644. for name, centerList := range centerMap {
  645. data := make(map[string]interface{})
  646. var allCount, count, t3, t2 float64
  647. for _, center := range centerList {
  648. allCount += center["allCount"]
  649. count += center["count"]
  650. t2 += center["t2"]
  651. t3 += center["t3"]
  652. }
  653. if allCount != 0 && count != 0 {
  654. data["mtbf"] = FloatRound(allCount/count, 2)
  655. } else {
  656. data["mtbf"] = 0
  657. }
  658. if (t2 != 0 || t3 != 0) && count != 0 {
  659. data["mttr"] = FloatRound((t2+t3)/count, 2)
  660. } else {
  661. data["mttr"] = 0
  662. }
  663. if allCount != 0 && count != 0 {
  664. data["mttf"] = FloatRound((allCount-t2-t3)/count, 2)
  665. } else {
  666. data["mttf"] = 0
  667. }
  668. data["count"] = count
  669. data["pastureName"] = name
  670. respMap1 = append(respMap1, data)
  671. }
  672. respMap1 = append(respMap1, respMap...)
  673. return pastureCount*len(eqClassList1) + 3, respMap1, nil
  674. }
  675. func MaintainListEquipment(pastureName, eqClassId string, startTime, endTime time.Time, offset, pageSize int, eqName string) (int64, []map[string]interface{}, error) {
  676. pastureMaintainList := make([]*model.Maintain, 0)
  677. err := s.d.DB.Raw(` SELECT m.*,(select DATE_FORMAT(bpu.creatDate,'%Y-%m-%d %H:%i:%s') from bigpartapply bp join bigpartuse bpu on bpu.oddCode = bp.applyCode
  678. where bp.RUCode =m.repairCode order by bpu.creatDate desc limit 1) creatDate FROM maintain m JOIN equipment e on e.id = m.eqId WHERE e.eqClassId = ?
  679. and m.stopTime is not null and (m.pastureid = (select id from pasture where name = ? and name != '现代牧业') or ? = '现代牧业' )
  680. and m.stopTime between ? and ? and TIMESTAMPDIFF(hour, m.orderTime, m.stopTime) < 1000 order by requestTime,m.pastureid `,
  681. eqClassId, pastureName, pastureName, startTime, endTime).Find(&pastureMaintainList).Error
  682. if err != nil {
  683. return 0, nil, errors.New(fmt.Sprintf("查看维修信息出错 error:%s", err.Error()))
  684. }
  685. maintainMap := make(map[string][]*model.Maintain)
  686. eqid := []int64{}
  687. for _, pasture := range pastureMaintainList {
  688. eqid = append(eqid, pasture.EqId)
  689. maintainMap[pasture.PastureId] = append(maintainMap[pasture.PastureId], pasture)
  690. }
  691. eqClass := model.EqClass{}
  692. err = s.d.DB.Where("id = ? ", eqClassId).First(&eqClass).Error
  693. if err != nil {
  694. return 0, nil, errors.New("查询设备类别出错!!!")
  695. }
  696. //pastureIDList = RemoveRepeatElement(pastureIDList)
  697. pastureList := make([]*model.Pasture, 0)
  698. s.d.DB.Where("name = ?", pastureName).Find(&pastureList)
  699. eqList := make([]*model.Equipment, 0)
  700. err = s.d.DB.Where(" id in (?) ", eqid).Where(" (pastureid = (select id from pasture where name = ? ) or ?='现代牧业') ", pastureName, pastureName).Where(" ( eqName like concat('%',?,'%') or ? = '')", eqName, eqName).Offset(offset - 1).Limit(pageSize).Find(&eqList).Error
  701. if err != nil {
  702. return 0, nil, errors.New("查询设备出错!!!")
  703. }
  704. var total int64
  705. s.d.DB.Debug().Table("equipment").Where(" id in ? ", eqid).Where(" (pastureid = (select id from pasture where name = ? ) ) or ?='现代牧业' ", pastureName, pastureName).Where(" ( eqName like concat('%', ? ,'%') or ? = '')", eqName, eqName).Count(&total)
  706. respMap := make([]map[string]interface{}, 0)
  707. for _, p := range pastureList {
  708. pastureId := strconv.Itoa(p.ID)
  709. maintainList := maintainMap[pastureId]
  710. //count := float64(len(maintainList))
  711. mainMap := make(map[int64][]*model.Maintain, 0)
  712. //将每个不同的资产分类放进map
  713. //eqIdList := []int64{}
  714. for _, main := range maintainList {
  715. //eqIdList = append(eqIdList, main.EqId)
  716. mainMap[main.EqId] = append(mainMap[main.EqId], main)
  717. }
  718. //var stopTime, orderTime, requestTime time.Time
  719. //eqIdList = uniqueArr(eqIdList)
  720. //for _, k := range eqIdList {
  721. for _, eq := range eqList {
  722. var t2, t3 float64
  723. var stopTime, orderTime, requestTime time.Time
  724. mainList := make([]*model.Maintain, 0)
  725. if _, ok := mainMap[int64(eq.Id)]; ok {
  726. mainList = mainMap[int64(eq.Id)]
  727. }
  728. for i, main := range mainList {
  729. if i == 0 {
  730. //记录下维修结束时间方便下次计算t1
  731. stopTime = main.StopTime
  732. orderTime = main.OrderTime
  733. //requestTime = main.RequestTime
  734. //每个资产第一次都根据筛选开始时间计算出t1
  735. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  736. if main.CreatDate == "" {
  737. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  738. requestTime = main.RequestTime
  739. } else {
  740. layout := "2006-01-02 15:04:05"
  741. t, err := time.Parse(layout, main.CreatDate)
  742. if err != nil {
  743. fmt.Println(err)
  744. continue
  745. }
  746. tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05"))
  747. if err != nil {
  748. fmt.Println(err)
  749. continue
  750. }
  751. t3 += tStop.Sub(t).Hours()
  752. requestTime = t
  753. }
  754. //fmt.Println(main.StopTime, main.OrderTime, "111111")
  755. //t1 += main.RequestTime.Sub(startTime).Minutes()
  756. } else {
  757. //t1 += main.RequestTime.Sub(stopTime).Minutes()
  758. t2 += main.OrderTime.Sub(main.RequestTime).Hours()
  759. if main.CreatDate == "" {
  760. t3 += main.StopTime.Sub(main.OrderTime).Hours()
  761. fmt.Println(main.StopTime.Sub(main.OrderTime).Hours())
  762. requestTime = main.RequestTime
  763. } else {
  764. layout := "2006-01-02 15:04:05"
  765. t, err := time.Parse(layout, main.CreatDate)
  766. if err != nil {
  767. fmt.Println(err)
  768. continue
  769. }
  770. tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05"))
  771. if err != nil {
  772. fmt.Println(err)
  773. continue
  774. }
  775. t3 += tStop.Sub(t).Hours()
  776. fmt.Println(tStop.Sub(t).Hours())
  777. requestTime = t
  778. }
  779. //fmt.Println(main.StopTime, main.OrderTime, "22222222")
  780. //记录下维修结束时间方便下次计算t1
  781. stopTime = main.StopTime
  782. orderTime = main.OrderTime
  783. //requestTime = main.RequestTime
  784. }
  785. }
  786. data := make(map[string]interface{}, 0)
  787. if len(mainList) > 0 {
  788. if stopTime.Format("2006") == "0001" {
  789. //如果还没有结束获取当前时间计算t1
  790. if orderTime.Format("2006") == "0001" {
  791. t2 += requestTime.Sub(time.Now()).Hours()
  792. } else {
  793. if time.Now().Unix() < endTime.Unix() {
  794. t3 += time.Now().Sub(orderTime).Hours()
  795. } else {
  796. t3 += endTime.Sub(orderTime).Hours()
  797. }
  798. }
  799. }
  800. day := ((endTime.Unix() - startTime.Unix()) / 60 / 60 / 24)
  801. count1 := float64(len(mainList))
  802. allCount := (24 * (count1 + 1)) * float64(day)
  803. data["mtbf"] = FloatRound(allCount/count1, 2)
  804. data["mttr"] = FloatRound((t2+t3)/count1, 2)
  805. data["mttf"] = FloatRound((allCount-t2-t3)/count1, 2)
  806. data["count"] = count1
  807. } else {
  808. data["mtbf"] = 0
  809. data["mttr"] = 0
  810. data["mttf"] = 0
  811. data["count"] = 0
  812. }
  813. data["eqCode"] = eq.EqCode
  814. data["eqName"] = eq.EqName
  815. data["pastureId"] = pastureId
  816. data["eqClassName"] = eqClass.TypeName
  817. respMap = append(respMap, data)
  818. }
  819. }
  820. return total, respMap, nil
  821. }