123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- 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
- }
|