package maintainance import ( "context" "encoding/json" "fmt" "time" "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 StyleItem(ctx context.Context, req *v1.MaintainManualRequest) (reply *v1.StyleItemReply, err error) { reply = &v1.StyleItemReply{} 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 } if err == gorm.ErrRecordNotFound { return reply, nil } reply.List = make([]*v1.StyleItem, 0, len(list)) for _, v := range list { reply.List = append(reply.List, &v1.StyleItem{ Id: v.ID, C2Name: v.C2Name, C2Id: v.C2Id, Useage: v.Useage, Detail: v.Detail, AttributeRule: v.AttributeRule, StyleId: req.StyleId, }) } return reply, nil } func UpdateStyleItem(ctx context.Context, req *v1.UpdateStyleItemRequest) (reply *v1.EmptyReply, err error) { reply = &v1.EmptyReply{} 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 len(req.List) == 0 { return reply, errors.ParamsError } var ( insert []model.GdStyleItem tx = database.DB().Begin() ) defer func() { if err != nil { tx.Rollback() return } tx.Commit() }() for _, v := range req.List { if v.Id == 0 { insert = append(insert, model.GdStyleItem{ StyleId: v.StyleId, C2Id: v.C2Id, Useage: v.Useage, Detail: v.Detail, AttributeRule: v.AttributeRule, CreatedAt: time.Now().Unix(), UpdatedAt: time.Now().Unix(), }) continue } values := map[string]interface{}{ "c2_id": v.C2Id, "useage": v.Useage, "detail": v.Detail, "attribute_rule": v.AttributeRule, "updated_at": time.Now().Unix(), } if err := model.NewStyleItemModel().Update(tx.Where("id = ?", v.Id), values); err != nil { return reply, errors.SystemError } } if len(insert) != 0 { if err := model.NewStyleItemModel().Insert(tx, insert); err != nil { return reply, errors.SystemError } } return reply, nil } func DeleteStyleItem(ctx context.Context, req *v1.DeleteStyleItemRequest) (reply *v1.EmptyReply, err error) { reply = &v1.EmptyReply{} 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.Id == 0 { return reply, errors.ParamsError } if err := model.NewStyleItemModel().Delete(database.DB().Where("id = ?", req.Id)); err != nil { return reply, errors.SystemError } return reply, nil }