package maintainance import ( "context" "encoding/json" "fmt" "adm-vehicle-style/consts" "adm-vehicle-style/errors" "adm-vehicle-style/model" v1 "adm-vehicle-style/pb/v1" "git.getensh.com/common/gopkgsv2/database" "git.getensh.com/common/gopkgsv2/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "gorm.io/gorm" ) func Manual(ctx context.Context, req *v1.MaintainManualRequest) (reply *v1.MaintainManualReply, err error) { reply = &v1.MaintainManualReply{} defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() if req.StyleId == "" { return reply, errors.ParamsError } list, err := model.NewMaintainDetailModel().MaintainDetailList(database.DB().Where("t1.style_id = ?", req.StyleId)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } for _, v := range list { i := v1.MaintainItem{ Id: v.ID, Item: v.ItemName, StartMile: v.StartMile, StartDate: v.StartDate, MileCycle: v.MileCycle, DateCycle: v.DateCycle, StyleId: req.StyleId, ItemId: v.ItemId, } switch v.ItemType { case consts.Maintainance: reply.Maintainance = append(reply.Maintainance, &i) case consts.Repair: reply.Repair = append(reply.Repair, &i) case consts.Wash: reply.Wash = append(reply.Wash, &i) } } return reply, nil } func judgeCycle(mile, age int64, v model.MaintainDetailItem, m, d int64) bool { if mile > 0 && v.MileCycle > 0 { if mile >= v.StartMile && (mile-m)%v.MileCycle == 0 { return true } } if age > 0 && v.DateCycle > 0 { if age >= v.StartDate && (age-d)%v.DateCycle == 0 { return true } } return false } func generateMaintainace( list []model.MaintainDetailItem, title *model.GdMaintainTitle, reply *v1.MaintainManualInfoReply, ) { var ( mile int64 age int64 maitainLen int64 ) if title.MaintainDateMinCycle == 0 && title.MaintainMileMinCycle == 0 { return } for { // 当前周期 if title.StartMile != 0 && mile < title.StartMile { mile = title.StartMile } else { mile += title.MaintainMileMinCycle } if title.StartDate != 0 && age < title.StartDate { age = title.StartDate } else { age += title.MaintainDateMinCycle } if mile > 300000 { break } if age > 180 { break } maintain := v1.MaintainManualInfoReply_Maintain{ Mile: uint32(mile), Age: uint32(age), } for _, v := range list { item := v1.MaintainManualInfoReply_Items{ ItemId: uint32(v.ItemId), ItemName: v.ItemName, } if judgeCycle(mile, age, v, title.StartMile, title.StartDate) { item.Level = 2 if maitainLen > 0 { for k := range reply.Maintain[maitainLen-1].Items { if reply.Maintain[maitainLen-1].Items[k].ItemId == item.ItemId { if reply.Maintain[maitainLen-1].Items[k].Level == 0 { reply.Maintain[maitainLen-1].Items[k].Level = 1 } } } } } maintain.Items = append(maintain.Items, &item) } reply.Maintain = append(reply.Maintain, &maintain) maitainLen++ } reply.Cycle = &v1.MaintainManualInfoReply_Cycle{ StartMile: title.StartMile, MileCycle: title.MaintainMileMinCycle, StartDate: title.StartDate, DateCycle: title.MaintainDateMinCycle, } } func Info(ctx context.Context, req *v1.MaintainManualInfoRequest) (reply *v1.MaintainManualInfoReply, err error) { reply = &v1.MaintainManualInfoReply{} defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() if req.StyleId == "" { return reply, errors.ParamsError } list, err := model.NewMaintainDetailModel().MaintainDetailList(database.DB().Where("t1.style_id = ?", req.StyleId)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } title, err := model.NewMaintainTitleModel().Get(database.DB().Where("style_id = ?", req.StyleId)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } maintainList := []model.MaintainDetailItem{} for _, v := range list { switch v.ItemType { case consts.Maintainance: reply.MaintainItem = append(reply.MaintainItem, &v1.MaintainManualInfoReply_MaintainItem{ ItemId: uint32(v.ItemId), ItemName: v.ItemName, StartMile: v.StartMile, StartDate: v.StartDate, MileCycle: v.MileCycle, DateCycle: v.DateCycle, StyleId: v.StyleId, }) maintainList = append(maintainList, v) case consts.Repair: reply.Repair = append(reply.Repair, &v1.MaintainManualInfoReply_Repair{ Cycle: uint32(v.MileCycle), ItemName: v.ItemName, ItemId: uint32(v.ItemId), StartMile: v.StartMile, StartDate: v.StartDate, MileCycle: v.MileCycle, DateCycle: v.DateCycle, StyleId: v.StyleId, }) case consts.Wash: reply.Wash = append(reply.Wash, &v1.MaintainManualInfoReply_Wash{ Cycle: uint32(v.MileCycle), ItemName: v.ItemName, ItemId: uint32(v.ItemId), StartMile: v.StartMile, StartDate: v.StartDate, MileCycle: v.MileCycle, DateCycle: v.DateCycle, StyleId: v.StyleId, }) } } if len(maintainList) > 0 { generateMaintainace(maintainList, title, reply) } return reply, nil }