package maintainance import ( "context" "encoding/json" "fmt" "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 C2List(ctx context.Context, req *v1.C2ListRequest) (reply *v1.C2ListReply, err error) { reply = &v1.C2ListReply{} 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")) } } }() list, err := model.NewStyleItemModel().List(database.DB().Where("style_id = ?", req.StyleId)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } c2List, err := model.NewC2ItemModel().List(database.DB()) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } if err == gorm.ErrRecordNotFound { return reply, nil } exist := func(c2Id int64, list []model.StyleItem) bool { if len(list) == 0 { return false } for _, v := range list { if c2Id == v.C2Id { return true } } return false } reply.List = make([]*v1.C2ListReply_Info, 0, len(c2List)) for _, v := range c2List { if !exist(v.ID, list) { reply.List = append(reply.List, &v1.C2ListReply_Info{ Id: v.ID, Name: v.Name, }) } } return reply, nil }