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, eqClassPIdStr string) (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 ?= '') and (e.pid = ? or ?= '') limit ?,?", pastureName, pastureName, pastureName, eqClassIdStr, eqClassIdStr, eqClassPIdStr, eqClassPIdStr, offset-1, pagecount).Order(" eqsort asc ").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 ?= '') and (e.pid = ? or ?= '') ", pastureName, pastureName, pastureName, eqClassIdStr, eqClassIdStr, eqClassPIdStr, eqClassPIdStr).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.*,(select DATE_FORMAT(bpu.creatDate,'%Y-%m-%d %H:%i:%s') from bigpartapply bp join bigpartuse bpu on bpu.oddCode = bp.applyCode 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 ? = '') 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.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() if main.CreatDate == "" { t3 += main.StopTime.Sub(main.OrderTime).Hours() requestTime = main.RequestTime } else { layout := "2006-01-02 15:04:05" t, err := time.Parse(layout, main.CreatDate) if err != nil { fmt.Println(err) continue } tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05")) if err != nil { fmt.Println(err) continue } t3 += tStop.Sub(t).Hours() requestTime = t } //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() if main.CreatDate == "" { t3 += main.StopTime.Sub(main.OrderTime).Hours() requestTime = main.RequestTime } else { layout := "2006-01-02 15:04:05" t, err := time.Parse(layout, main.CreatDate) if err != nil { fmt.Println(err) continue } tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05")) if err != nil { fmt.Println(err) continue } t3 += tStop.Sub(t).Hours() requestTime = t } //fmt.Println(main.StopTime, main.OrderTime, "22222222") //记录下维修结束时间方便下次计算t1 stopTime = main.StopTime orderTime = main.OrderTime } } 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) pasture := model.Pasture{} err := s.d.DB.Where("id = ? ", eq.Pasture.ID).First(&pasture).Error if err != nil { return 0, nil, errors.New("查询牧场信息出错!!!") } data["pastureName"] = pasture.Name 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 ").Order(" eqsort asc ").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,center").Where(" name = ? or ? = '' or ?= '现代牧业' ", pastureName, pastureName, pastureName).Where("id != 18").Order("center_sort,id").Find(&pastureList).Error if err != nil { return 0, nil, errors.New("查询牧场信息出错!!!") } respMap := make([]map[string]interface{}, 0) pastureCount := len(pastureList) n := 0 var jtCount, jtAllCount, jtT2, jtT3 float64 centerMap := make(map[string][]map[string]float64, 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,(select DATE_FORMAT(bpu.creatDate,'%Y-%m-%d %H:%i:%s') from bigpartapply bp join bigpartuse bpu on bpu.oddCode = bp.applyCode 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 = ? 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 t2 += main.OrderTime.Sub(main.RequestTime).Hours() if main.CreatDate == "" { t3 += main.StopTime.Sub(main.OrderTime).Hours() requestTime = main.RequestTime } else { layout := "2006-01-02 15:04:05" t, err := time.Parse(layout, main.CreatDate) if err != nil { fmt.Println(err) continue } tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05")) if err != nil { fmt.Println(err) continue } t3 += tStop.Sub(t).Hours() requestTime = t } //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() if main.CreatDate == "" { t3 += main.StopTime.Sub(main.OrderTime).Hours() requestTime = main.RequestTime } else { layout := "2006-01-02 15:04:05" t, err := time.Parse(layout, main.CreatDate) if err != nil { fmt.Println(err) continue } tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05")) if err != nil { fmt.Println(err) continue } t3 += tStop.Sub(t).Hours() requestTime = t } //记录下维修结束时间方便下次计算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 } } //jtData := make(map[string]interface{}) //centerMap := make([]map[string]interface{}, 0) if offset == 1 { if pastureName == "现代牧业" || pastureName == "" { for _, item := range pastureList { if item.ID == 18 { continue } pastureId := strconv.Itoa(item.ID) jtAllCount += eqClassMap[pastureId]["allCount"] jtCount += eqClassMap[pastureId]["count"] fmt.Println(eqClassMap[pastureId]["count"]) jtT2 += eqClassMap[pastureId]["t2"] jtT3 += eqClassMap[pastureId]["t3"] } centerList := []string{"一中心", "二中心", "三中心"} for _, centerName := range centerList { var allCount, count, t2, t3 float64 data := make(map[string]float64) for _, item := range pastureList { if item.ID == 18 { continue } if centerName == item.Center { pastureId := strconv.Itoa(item.ID) allCount += eqClassMap[pastureId]["allCount"] count += eqClassMap[pastureId]["count"] t2 += eqClassMap[pastureId]["t2"] t3 += eqClassMap[pastureId]["t3"] } } data["allCount"] += allCount data["count"] += count data["t2"] += t2 data["t3"] += t2 //if allCount > 0 && count > 0 && t2 > 0 && t3 > 0 { //if allCount != 0 && count != 0 { // data["mtbf"] = FloatRound(allCount/count, 2) //} else { // data["mtbf"] = 0 //} //if (t2 != 0 || t3 != 0) && count != 0 { // data["mttr"] = FloatRound((t2+t3)/count, 2) //} else { // data["mttr"] = 0 //} //if allCount != 0 && count != 0 { // data["mttf"] = FloatRound((allCount-t2-t3)/count, 2) //} else { // data["mttf"] = 0 //} //data["count"] = count centerMap[centerName] = append(centerMap[centerName], data) //} //data["pastureName"] = centerName //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"] fmt.Println(eqClassMap[pastureId]["count"]) respMap = append(respMap, data) } } } } respMap1 := make([]map[string]interface{}, 0) data := make(map[string]interface{}) if jtAllCount != 0 && jtCount != 0 { data["mtbf"] = FloatRound(jtAllCount/jtCount, 2) } else { data["mtbf"] = 0 } if (jtT3 != 0 || jtT2 != 0) && jtCount != 0 { data["mttr"] = FloatRound((jtT2+jtT3)/jtCount, 2) } else { data["mttr"] = 0 } if jtCount != 0 && jtAllCount != 0 { data["mttf"] = FloatRound((jtAllCount-jtT2-jtT3)/jtCount, 2) } else { data["mttf"] = 0 } data["count"] = jtCount data["pastureName"] = "现代牧业" respMap1 = append(respMap1, data) for name, centerList := range centerMap { data := make(map[string]interface{}) var allCount, count, t3, t2 float64 for _, center := range centerList { allCount += center["allCount"] count += center["count"] t2 += center["t2"] t3 += center["t3"] } if allCount != 0 && count != 0 { data["mtbf"] = FloatRound(allCount/count, 2) } else { data["mtbf"] = 0 } if (t2 != 0 || t3 != 0) && count != 0 { data["mttr"] = FloatRound((t2+t3)/count, 2) } else { data["mttr"] = 0 } if allCount != 0 && count != 0 { data["mttf"] = FloatRound((allCount-t2-t3)/count, 2) } else { data["mttf"] = 0 } data["count"] = count data["pastureName"] = name respMap1 = append(respMap1, data) } respMap1 = append(respMap1, respMap...) return pastureCount*len(eqClassList1) + 3, respMap1, 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.*,(select DATE_FORMAT(bpu.creatDate,'%Y-%m-%d %H:%i:%s') from bigpartapply bp join bigpartuse bpu on bpu.oddCode = bp.applyCode 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 = ? 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, 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.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) respMap := make([]map[string]interface{}, 0) for _, p := range pastureList { pastureId := strconv.Itoa(p.ID) maintainList := maintainMap[pastureId] //count := float64(len(maintainList)) 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 t2, t3 float64 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() if main.CreatDate == "" { t3 += main.StopTime.Sub(main.OrderTime).Hours() requestTime = main.RequestTime } else { layout := "2006-01-02 15:04:05" t, err := time.Parse(layout, main.CreatDate) if err != nil { fmt.Println(err) continue } tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05")) if err != nil { fmt.Println(err) continue } t3 += tStop.Sub(t).Hours() requestTime = t } //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() if main.CreatDate == "" { t3 += main.StopTime.Sub(main.OrderTime).Hours() fmt.Println(main.StopTime.Sub(main.OrderTime).Hours()) requestTime = main.RequestTime } else { layout := "2006-01-02 15:04:05" t, err := time.Parse(layout, main.CreatDate) if err != nil { fmt.Println(err) continue } tStop, err := time.Parse(layout, main.StopTime.Format("2006-01-02 15:04:05")) if err != nil { fmt.Println(err) continue } t3 += tStop.Sub(t).Hours() fmt.Println(tStop.Sub(t).Hours()) requestTime = t } //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 }