123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678 |
- package service
- import (
- "fmt"
- "github.com/astaxie/beego/logs"
- "github.com/pkg/errors"
- "kpt.xdmy/apiserver/model"
- "strconv"
- "strings"
- "time"
- )
- func MaintainList(pastureName, eqClassIdStr string, startTime, endTime time.Time, offset, pagecount int) (int, []map[string]interface{}, error) {
- //eqClassIdList := []string{}
- var total int64
- eqList := make([]*model.EqClassAndPasture, 0)
- //if eqClassIdStr == "" {
- //eqList := make([]*model.EqClass, 0)
- //s.d.DB.Raw()
- s.d.DB.Raw("select e.id,p.id,e.typeName,p.name from eqclass e join pasture p where section = 3 "+
- "and (p.name = ? or ? = '现代牧业' or ?= '') and p.IsEnergy = 0 and p.name != '现代牧业' and (e.id = ? or ?= '') limit ?,?",
- pastureName, pastureName, pastureName, eqClassIdStr, eqClassIdStr, offset-1, pagecount).Find(&eqList)
- s.d.DB.Raw("select count(1) from eqclass e join pasture p where section = 3 "+
- "and (p.name = ? or ? = '现代牧业' or ?= '') and p.IsEnergy = 0 and p.name != '现代牧业' and (e.id = ? or ?= '') ",
- pastureName, pastureName, pastureName, eqClassIdStr, eqClassIdStr).Count(&total)
- //} else {
- // eqClassIdList = append(eqClassIdList, eqClassIdStr)
- // total = 1
- //}
- //pasture := new(model.Pasture)
- //s.d.DB.Where("name = ?", pastureName).First(&pasture)
- respMap := make([]map[string]interface{}, 0)
- for _, eq := range eqList {
- pastureMaintainList := make([]*model.Maintain, 0)
- err := s.d.DB.Raw(` SELECT m.* FROM maintain m JOIN equipment e on e.id = m.eqId WHERE e.eqClassId = ?
- and m.stopTime is not null and (m.pastureid = (select id from pasture where name = ? and name != '现代牧业') or ? = '现代牧业' )
- and m.stopTime between ? and ? and TIMESTAMPDIFF(hour, m.orderTime, m.stopTime) < 1000 order by requestTime,m.pastureid `,
- eq.EqClass.ID, eq.Pasture.Name, eq.Pasture.Name, startTime, endTime).Find(&pastureMaintainList).Error
- if err != nil {
- return 0, nil, errors.New(fmt.Sprintf("查看维修信息出错 error:%s", err.Error()))
- }
- maintainMap := make(map[string][]*model.Maintain)
- pastureIDList := []string{}
- for _, pasture := range pastureMaintainList {
- pastureIDList = append(pastureIDList, pasture.PastureId)
- maintainMap[pasture.PastureId] = append(maintainMap[pasture.PastureId], pasture)
- }
- eqClass := model.EqClass{}
- err = s.d.DB.Where("id = ? ", eq.EqClass.ID).First(&eqClass).Error
- if err != nil {
- return 0, nil, errors.New("查询设备类别出错!!!")
- }
- pastureIDList = RemoveRepeatElement(pastureIDList)
- for _, pastureId := range pastureIDList {
- maintainList := maintainMap[pastureId]
- count := float64(len(maintainList))
- var t2, t3 float64
- data := make(map[string]interface{}, 0)
- mainMap := make(map[int64][]*model.Maintain, 0)
- //将每个不同的资产分类放进map
- eqIdList := []int64{}
- for _, main := range maintainList {
- eqIdList = append(eqIdList, main.EqId)
- mainMap[main.EqId] = append(mainMap[main.EqId], main)
- }
- //var stopTime, orderTime, requestTime time.Time
- eqIdList = uniqueArr(eqIdList)
- for _, k := range eqIdList {
- var stopTime, orderTime, requestTime time.Time
- mainList := mainMap[k]
- for i, main := range mainList {
- if i == 0 {
- //记录下维修结束时间方便下次计算t1
- stopTime = main.StopTime
- orderTime = main.OrderTime
- requestTime = main.RequestTime
- //每个资产第一次都根据筛选开始时间计算出t1
- t2 += main.OrderTime.Sub(main.RequestTime).Hours()
- t3 += main.StopTime.Sub(main.OrderTime).Hours()
- //fmt.Println(main.StopTime, main.OrderTime, "111111")
- //t1 += main.RequestTime.Sub(startTime).Minutes()
- } else {
- //t1 += main.RequestTime.Sub(stopTime).Minutes()
- t2 += main.OrderTime.Sub(main.RequestTime).Hours()
- t3 += main.StopTime.Sub(main.OrderTime).Hours()
- //fmt.Println(main.StopTime, main.OrderTime, "22222222")
- //记录下维修结束时间方便下次计算t1
- stopTime = main.StopTime
- orderTime = main.OrderTime
- requestTime = main.RequestTime
- }
- }
- if stopTime.Format("2006") == "0001" {
- //如果还没有结束获取当前时间计算t1
- if orderTime.Format("2006") == "0001" {
- t2 += requestTime.Sub(time.Now()).Hours()
- } else {
- if time.Now().Unix() < endTime.Unix() {
- t3 += time.Now().Sub(orderTime).Hours()
- } else {
- t3 += endTime.Sub(orderTime).Hours()
- }
- }
- }
- }
- equipmentList := make([]*model.Equipment, 0)
- err = s.d.DB.Where("id not in(?) ", eqIdList).
- Where(" entranceDate < ? and ( blockTime is null or blockTime = '' or blockTime > ? or blockTime = '0000-00-00 00:00:00' ) "+
- "and pastureid = ? and eqClassId = ? ",
- endTime, startTime, pastureId, eq.EqClass.ID).Find(&equipmentList).Error
- if err != nil {
- return 0, nil, errors.New(fmt.Sprintf("查询资产信息出错 error:%v", err))
- }
- day := ((endTime.Unix() - startTime.Unix()) / 60 / 60 / 24)
- count1 := float64(len(mainMap))
- allCount := (24 * (count1 + float64(len(equipmentList)))) * float64(day)
- data["mtbf"] = FloatRound(allCount/count, 2)
- data["mttr"] = FloatRound((t2+t3)/count, 2)
- data["mttf"] = FloatRound((allCount-t2-t3)/count, 2)
- data["count"] = count
- data["eqClassId"] = eqClass.ID
- pasture := model.Pasture{}
- err := s.d.DB.Where("id = ? ", pastureId).First(&pasture).Error
- if err != nil {
- return 0, nil, errors.New("查询牧场信息出错!!!")
- }
- data["pastureId"] = pasture.ID
- data["pastureName"] = pasture.Name
- data["eqClassName"] = eqClass.TypeName
- respMap = append(respMap, data)
- }
- exist := false
- for _, p := range respMap {
- if p["eqClassId"] != nil {
- //str := strconv.Itoa(p["eqClassId"].(int))
- if p["eqClassId"].(int) == eq.EqClass.ID && p["pastureId"].(int) == eq.Pasture.ID {
- exist = true
- break
- }
- }
- }
- if !exist {
- data := make(map[string]interface{}, 0)
- data["pastureName"] = pastureName
- data["pastureId"] = eq.Pasture.ID
- data["eqClassName"] = eqClass.TypeName
- data["mtbf"] = 0
- data["mttr"] = 0
- data["mttf"] = 0
- data["count"] = 0
- data["eqClassId"] = eqClass.ID
- respMap = append(respMap, data)
- }
- //}
- }
- return int(total), respMap, nil
- }
- func FloatRound(f float64, n int) float64 {
- format := "%." + strconv.Itoa(n) + "f"
- res, _ := strconv.ParseFloat(fmt.Sprintf(format, f), 64)
- return res
- }
- func RemoveRepeatElement(list []string) []string {
- // 创建一个临时map用来存储数组元素
- temp := make(map[string]bool)
- var list2 []string
- index := 0
- for _, v := range list {
- // 遍历数组元素,判断此元素是否已经存在map中
- _, ok := temp[v]
- if !ok {
- list2 = append(list2, v)
- temp[v] = true
- }
- index++
- }
- return list2
- }
- func uniqueArr(arr []int64) []int64 {
- newArr := make([]int64, 0)
- tempArr := make(map[int64]bool, len(newArr))
- for _, v := range arr {
- if tempArr[v] == false {
- tempArr[v] = true
- newArr = append(newArr, v)
- }
- }
- return newArr
- }
- func GetOversee(pastureId, eqClassId, empname string, offset, pagecount int) ([]map[string]interface{}, error) {
- overseeList := make([]*model.Oversee, 0)
- eq := s.d.DB.Where(" ( ? = '' or pastureid = ? )", pastureId, pastureId)
- if eqClassId != "" {
- eq.Where("eqClassId = ?", eqClassId)
- }
- if empname != "" {
- eq.Where("maintenance like ?", "%"+empname+"%")
- }
- err := eq.Offset(offset).Limit(pagecount).Order("inputTime desc ").Find(&overseeList).Error
- if err != nil {
- logs.Error(err)
- return nil, errors.New(fmt.Sprintf("查询督办数据出错 errors: %v", err))
- }
- userIds := []string{}
- for _, oversee := range overseeList {
- u := strings.Split(oversee.Maintenance, ",")
- userIds = append(userIds, oversee.Levelone, oversee.Leveltwo, oversee.Levelthree, oversee.Entryperson)
- userIds = append(userIds, u...)
- }
- userIds = RemoveRepeatElement(userIds)
- userList := make([]*model.User, 0)
- if len(userIds) > 0 {
- 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
- if err != nil {
- logs.Error(err)
- return nil, errors.New(fmt.Sprintf("查询督办数据出错 errors: %v", err))
- }
- }
- userMap := make(map[string]*model.User)
- for _, user := range userList {
- userMap[user.Username] = user
- }
- respOversee := make([]map[string]interface{}, 0)
- for _, oversee := range overseeList {
- arg := make(map[string]interface{})
- arg["id"] = oversee.ID
- arg["pastureName"] = oversee.PastureName
- arg["pastureId"] = oversee.PastureId
- arg["pushTime"] = oversee.PushTime
- arg["inputTime"] = oversee.InputTime.Format("2006-01-02")
- arg["departmentId"] = oversee.DepartmentId
- arg["departmentName"] = oversee.DepartmentName
- arg["eqClassName"] = oversee.EqClassName
- arg["eqClassId"] = oversee.EqClassId
- arg["entryperson"] = oversee.Entryperson
- if _, ok := userMap[oversee.Entryperson]; ok {
- arg["entrypersonName"] = userMap[oversee.Entryperson].EmpName
- arg["entrypersonId"] = userMap[oversee.Entryperson].Username
- }
- arg["levelone"] = oversee.Levelone
- if _, ok := userMap[oversee.Levelone]; ok {
- arg["leveloneName"] = userMap[oversee.Levelone].EmpName
- arg["leveloneId"] = userMap[oversee.Levelone].Username
- }
- arg["leveltwo"] = oversee.Leveltwo
- if _, ok := userMap[oversee.Leveltwo]; ok {
- arg["leveltwo"] = userMap[oversee.Leveltwo].EmpName
- arg["leveltwoId"] = userMap[oversee.Leveltwo].Username
- }
- arg["levelthree"] = oversee.Levelthree
- if _, ok := userMap[oversee.Levelthree]; ok {
- arg["levelthree"] = userMap[oversee.Levelthree].EmpName
- arg["levelthreeId"] = userMap[oversee.Levelthree].Username
- }
- maintenance := make([]map[string]interface{}, 0)
- for _, u := range strings.Split(oversee.Maintenance, ",") {
- main := make(map[string]interface{})
- main["id"] = userMap[u].Username
- main["empname"] = userMap[u].EmpName
- maintenance = append(maintenance, main)
- }
- arg["maintenance"] = maintenance
- respOversee = append(respOversee, arg)
- }
- return respOversee, nil
- }
- func StartStopRecord(pastureId, eqName string, startTime, endTime time.Time, offset, pagecount int) (map[string]interface{}, error) {
- eqDb := s.d.DB.Where(" pastureid = ? or '' = ? ", pastureId, pastureId)
- if eqName != "" {
- eqDb.Where("eqName = ? ", eqName)
- }
- eqList := make([]*model.Equipment, 0)
- if offset > 0 || pagecount > 0 {
- eqDb.Offset(offset).Limit(pagecount)
- }
- err := eqDb.Order("id").Find(&eqList).Error
- if err != nil {
- return nil, err
- }
- eqDb1 := s.d.DB.Table("equipment").Where(" pastureid = ? or '' = ? ", pastureId, pastureId)
- if eqName != "" {
- eqDb1.Where("eqName = ? ", eqName)
- }
- var count int64
- err = eqDb1.Count(&count).Error
- if err != nil {
- return nil, err
- }
- eqIDList := []int{}
- for _, eq := range eqList {
- eqIDList = append(eqIDList, eq.Id)
- }
- assetStt := make([]*model.AssetStt, 0)
- err = s.d.DB.Raw(`select * from asset_stt ast where ast.eqId in(?) and blockTime is not null and enabledTime between ? and ?
- union all
- select t.* from (
- 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`,
- eqIDList, startTime, endTime, eqIDList, startTime, endTime).Find(&assetStt).Error
- if err != nil {
- return nil, err
- }
- dataList := make([]map[string]interface{}, 0)
- pastureList := make([]*model.Pasture, 0)
- err = s.d.DB.Where(" id = ? or ? = '' ", pastureId, pastureId).Order("id").Find(&pastureList).Error
- if err != nil {
- return nil, errors.New("查询牧场信息出错!!!")
- }
- for _, eq := range eqList {
- arg := make(map[string]interface{})
- arg["eqName"] = eq.EqName
- var run float64
- for _, asset := range assetStt {
- if asset.EqId == int64(eq.Id) {
- if asset.BlockTime.Format("2006") == "0001" {
- if time.Now().Unix() > endTime.Unix() {
- run += endTime.Sub(asset.EnabledTime).Minutes() / 60
- } else {
- run += time.Now().Sub(asset.EnabledTime).Minutes() / 60
- }
- } else {
- run += asset.BlockTime.Sub(asset.EnabledTime).Minutes() / 60
- }
- }
- }
- t := endTime.Sub(startTime).Hours()
- arg["eqCode"] = eq.EqCode
- arg["run"] = FloatRound(run, 2)
- arg["stop"] = FloatRound(t-run, 2)
- for _, pasture := range pastureList {
- if pasture.ID == eq.PastureId {
- arg["pastureName"] = pasture.Name
- break
- }
- }
- arg["id"] = eq.Id
- dataList = append(dataList, arg)
- }
- resp := make(map[string]interface{})
- resp["total"] = count
- resp["pageNum"] = offset
- resp["pageSize"] = pagecount
- resp["list"] = dataList
- return resp, nil
- }
- func MaintainListPid(pastureName, eqClassId string, startTime, endTime time.Time, offset, pagecount int) (int, []map[string]interface{}, error) {
- eqClassList1 := make([]*model.EqClass, 0)
- start := (offset - 1) * pagecount
- page := offset * pagecount
- if eqClassId == "" {
- err := s.d.DB.Where(" section = 2 ").Find(&eqClassList1).Error
- if err != nil {
- return 0, nil, errors.New(fmt.Sprintf("查看维修类型出错 error:%s", err.Error()))
- }
- } else {
- n, _ := strconv.Atoi(eqClassId)
- eqClassList1 = append(eqClassList1, &model.EqClass{ID: n})
- }
- pastureList := make([]*model.Pasture, 0)
- err := s.d.DB.Select("id,name").Where(" name = ? or ? = '' or ?= '现代牧业' ", pastureName, pastureName, pastureName).Where("id != 18").Order("id").Find(&pastureList).Error
- if err != nil {
- return 0, nil, errors.New("查询牧场信息出错!!!")
- }
- respMap := make([]map[string]interface{}, 0)
- pastureCount := len(pastureList)
- n := 0
- for i, eq1 := range eqClassList1 {
- if len(respMap) >= pagecount {
- continue
- }
- eqClassList := make([]*model.EqClass, 0)
- err := s.d.DB.Where(" pid = ? ", eq1.ID).Find(&eqClassList).Error
- if err != nil {
- return 0, nil, errors.New(fmt.Sprintf("查看维修类型出错 error:%s", err.Error()))
- }
- n = n + pastureCount*(i+1)
- if n >= (offset-1)*pagecount {
- //if (start < (i+1)*pastureCount && start >= (i)*pastureCount) || (page >= (i)*pastureCount && page < (i+1)*pastureCount) {
- eqClass := model.EqClass{}
- err = s.d.DB.Where("id = ? ", eq1.ID).First(&eqClass).Error
- if err != nil {
- return 0, nil, errors.New("查询设备类别出错!!!")
- }
- eqClassMap := make(map[string]map[string]float64)
- for _, pastureId := range pastureList {
- eqClassMap[strconv.Itoa(pastureId.ID)] = make(map[string]float64)
- }
- for _, eq := range eqClassList {
- pastureMaintainList := make([]*model.Maintain, 0)
- 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 = ?
- and m.stopTime is not null and (m.pastureid = (select id from pasture where name = ? and name != '现代牧业') or ? = '现代牧业' or ?= '' )
- and m.stopTime between ? and ? and TIMESTAMPDIFF(hour, m.orderTime, m.stopTime) < 1000 order by requestTime,m.pastureid `,
- eq.ID, pastureName, pastureName, pastureName, startTime, endTime).Find(&pastureMaintainList).Error
- if err != nil {
- return 0, nil, errors.New(fmt.Sprintf("查看维修信息出错 error:%s", err.Error()))
- }
- maintainMap := make(map[string][]*model.Maintain, 0)
- pastureIDList := []string{}
- for _, pasture := range pastureMaintainList {
- pastureIDList = append(pastureIDList, pasture.PastureId)
- maintainMap[pasture.PastureId] = append(maintainMap[pasture.PastureId], pasture)
- }
- pastureIDList = RemoveRepeatElement(pastureIDList)
- for _, pastureId := range pastureIDList {
- maintainList := maintainMap[pastureId]
- count := float64(len(maintainList))
- var t2, t3 float64
- //data := make(map[string]interface{}, 0)
- mainMap := make(map[int64][]*model.Maintain, 0)
- //将每个不同的资产分类放进map
- eqIdList := []int64{}
- for _, main := range maintainList {
- eqIdList = append(eqIdList, main.EqId)
- mainMap[main.EqId] = append(mainMap[main.EqId], main)
- }
- //var stopTime, orderTime, requestTime time.Time
- eqIdList = uniqueArr(eqIdList)
- for _, k := range eqIdList {
- var stopTime, orderTime, requestTime time.Time
- mainList := mainMap[k]
- for i, main := range mainList {
- if i == 0 {
- //记录下维修结束时间方便下次计算t1
- stopTime = main.StopTime
- orderTime = main.OrderTime
- requestTime = main.RequestTime
- //每个资产第一次都根据筛选开始时间计算出t1
- t2 += main.OrderTime.Sub(main.RequestTime).Hours()
- t3 += main.StopTime.Sub(main.OrderTime).Hours()
- //fmt.Println(main.StopTime, main.OrderTime, "111111")
- //t1 += main.RequestTime.Sub(startTime).Minutes()
- } else {
- //t1 += main.RequestTime.Sub(stopTime).Minutes()
- t2 += main.OrderTime.Sub(main.RequestTime).Hours()
- t3 += main.StopTime.Sub(main.OrderTime).Hours()
- //fmt.Println(main.StopTime, main.OrderTime, "22222222")
- //记录下维修结束时间方便下次计算t1
- stopTime = main.StopTime
- orderTime = main.OrderTime
- requestTime = main.RequestTime
- }
- }
- if stopTime.Format("2006") == "0001" {
- //如果还没有结束获取当前时间计算t1
- if orderTime.Format("2006") == "0001" {
- t2 += requestTime.Sub(time.Now()).Hours()
- } else {
- if time.Now().Unix() < endTime.Unix() {
- t3 += time.Now().Sub(orderTime).Hours()
- } else {
- t3 += endTime.Sub(orderTime).Hours()
- }
- }
- }
- }
- equipmentList := make([]*model.Equipment, 0)
- err = s.d.DB.Select("id").Where("id not in(?) ", eqIdList).
- Where(" entranceDate < ? and ( blockTime is null or blockTime = '' or blockTime > ? or blockTime = '0000-00-00 00:00:00' ) "+
- "and pastureid = ? and eqClassId = ? ",
- endTime, startTime, pastureId, eq.ID).Find(&equipmentList).Error
- if err != nil {
- return 0, nil, errors.New(fmt.Sprintf("查询资产信息出错 error:%v", err))
- }
- day := ((endTime.Unix() - startTime.Unix()) / 60 / 60 / 24)
- count1 := float64(len(mainMap))
- allCount := (24 * (count1 + float64(len(equipmentList)))) * float64(day)
- eqClassMap[pastureId]["allCount"] += allCount
- eqClassMap[pastureId]["count"] += count
- eqClassMap[pastureId]["t2"] += t2
- eqClassMap[pastureId]["t3"] += t3
- //data["mtbf"] = FloatRound(allCount/count, 2)
- //data["mttr"] = FloatRound((t2+t3)/count, 2)
- //data["mttf"] = FloatRound((allCount-t2-t3)/count, 2)
- //data["count"] = count
- //pasture := model.Pasture{}
- //err := s.d.DB.Where("id = ? ", pastureId).First(&pasture).Error
- //if err != nil {
- // return nil, errors.New("查询牧场信息出错!!!")
- //}
- //data["pastureName"] = pasture.Name
- //data["eqClassName"] = eqClass.TypeName
- //respMap = append(respMap, data)
- }
- }
- for n, item := range pastureList {
- if len(respMap) >= pagecount {
- break
- } else if (i*pastureCount)+n < page && (i*pastureCount)+n >= start {
- pastureId := strconv.Itoa(item.ID)
- data := make(map[string]interface{})
- data["pastureName"] = item.Name
- data["pastureId"] = item.ID
- data["eqClassName"] = eqClass.TypeName
- data["eqId"] = eqClass.ID
- if _, ok := eqClassMap[pastureId]["count"]; ok {
- data["mtbf"] = FloatRound(eqClassMap[pastureId]["allCount"]/eqClassMap[pastureId]["count"], 2)
- data["mttr"] = FloatRound((eqClassMap[pastureId]["t2"]+eqClassMap[pastureId]["t3"])/eqClassMap[pastureId]["count"], 2)
- data["mttf"] = FloatRound((eqClassMap[pastureId]["allCount"]-eqClassMap[pastureId]["t2"]-eqClassMap[pastureId]["t3"])/eqClassMap[pastureId]["count"], 2)
- }
- data["count"] = eqClassMap[pastureId]["count"]
- respMap = append(respMap, data)
- }
- }
- }
- }
- return pastureCount * len(eqClassList1), respMap, nil
- }
- func MaintainListEquipment(pastureName, eqClassId string, startTime, endTime time.Time, offset, pageSize int, eqName string) (int64, []map[string]interface{}, error) {
- pastureMaintainList := make([]*model.Maintain, 0)
- err := s.d.DB.Raw(` SELECT m.* FROM maintain m JOIN equipment e on e.id = m.eqId WHERE e.eqClassId = ?
- and m.stopTime is not null and (m.pastureid = (select id from pasture where name = ? and name != '现代牧业') or ? = '现代牧业' )
- and m.stopTime between ? and ? and TIMESTAMPDIFF(hour, m.orderTime, m.stopTime) < 1000 order by requestTime,m.pastureid `,
- eqClassId, pastureName, pastureName, startTime, endTime).Find(&pastureMaintainList).Error
- if err != nil {
- return 0, nil, errors.New(fmt.Sprintf("查看维修信息出错 error:%s", err.Error()))
- }
- maintainMap := make(map[string][]*model.Maintain)
- eqid := []int64{}
- for _, pasture := range pastureMaintainList {
- eqid = append(eqid, pasture.EqId)
- maintainMap[pasture.PastureId] = append(maintainMap[pasture.PastureId], pasture)
- }
- eqClass := model.EqClass{}
- err = s.d.DB.Where("id = ? ", eqClassId).First(&eqClass).Error
- if err != nil {
- return 0, nil, errors.New("查询设备类别出错!!!")
- }
- //pastureIDList = RemoveRepeatElement(pastureIDList)
- pastureList := make([]*model.Pasture, 0)
- s.d.DB.Where("name = ?", pastureName).Find(&pastureList)
- eqList := make([]*model.Equipment, 0)
- 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
- if err != nil {
- return 0, nil, errors.New("查询设备出错!!!")
- }
- var total int64
- 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)
- respMap := make([]map[string]interface{}, 0)
- for _, p := range pastureList {
- pastureId := strconv.Itoa(p.ID)
- maintainList := maintainMap[pastureId]
- //count := float64(len(maintainList))
- var t2, t3 float64
- mainMap := make(map[int64][]*model.Maintain, 0)
- //将每个不同的资产分类放进map
- //eqIdList := []int64{}
- for _, main := range maintainList {
- //eqIdList = append(eqIdList, main.EqId)
- mainMap[main.EqId] = append(mainMap[main.EqId], main)
- }
- //var stopTime, orderTime, requestTime time.Time
- //eqIdList = uniqueArr(eqIdList)
- //for _, k := range eqIdList {
- for _, eq := range eqList {
- var stopTime, orderTime, requestTime time.Time
- mainList := make([]*model.Maintain, 0)
- if _, ok := mainMap[int64(eq.Id)]; ok {
- mainList = mainMap[int64(eq.Id)]
- }
- for i, main := range mainList {
- if i == 0 {
- //记录下维修结束时间方便下次计算t1
- stopTime = main.StopTime
- orderTime = main.OrderTime
- requestTime = main.RequestTime
- //每个资产第一次都根据筛选开始时间计算出t1
- t2 += main.OrderTime.Sub(main.RequestTime).Hours()
- t3 += main.StopTime.Sub(main.OrderTime).Hours()
- //fmt.Println(main.StopTime, main.OrderTime, "111111")
- //t1 += main.RequestTime.Sub(startTime).Minutes()
- } else {
- //t1 += main.RequestTime.Sub(stopTime).Minutes()
- t2 += main.OrderTime.Sub(main.RequestTime).Hours()
- t3 += main.StopTime.Sub(main.OrderTime).Hours()
- //fmt.Println(main.StopTime, main.OrderTime, "22222222")
- //记录下维修结束时间方便下次计算t1
- stopTime = main.StopTime
- orderTime = main.OrderTime
- requestTime = main.RequestTime
- }
- }
- data := make(map[string]interface{}, 0)
- if len(mainList) > 0 {
- if stopTime.Format("2006") == "0001" {
- //如果还没有结束获取当前时间计算t1
- if orderTime.Format("2006") == "0001" {
- t2 += requestTime.Sub(time.Now()).Hours()
- } else {
- if time.Now().Unix() < endTime.Unix() {
- t3 += time.Now().Sub(orderTime).Hours()
- } else {
- t3 += endTime.Sub(orderTime).Hours()
- }
- }
- }
- day := ((endTime.Unix() - startTime.Unix()) / 60 / 60 / 24)
- count1 := float64(len(mainList))
- allCount := (24 * (count1 + 1)) * float64(day)
- data["mtbf"] = FloatRound(allCount/count1, 2)
- data["mttr"] = FloatRound((t2+t3)/count1, 2)
- data["mttf"] = FloatRound((allCount-t2-t3)/count1, 2)
- data["count"] = count1
- } else {
- data["mtbf"] = 0
- data["mttr"] = 0
- data["mttf"] = 0
- data["count"] = 0
- }
- data["eqCode"] = eq.EqCode
- data["eqName"] = eq.EqName
- data["pastureId"] = pastureId
- data["eqClassName"] = eqClass.TypeName
- respMap = append(respMap, data)
- }
- }
- return total, respMap, nil
- }
|