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