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 ItemList(ctx context.Context, req *v1.ItemListRequest) (reply *v1.ItemListReply, err error) { reply = &v1.ItemListReply{} 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.ItemType < 1 || req.ItemType > 3 { 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 } itemList, err := model.NewMaintainItemModel(). List(database.DB().Where("item_type = ?", req.ItemType), "id, item_id, item_name") if err != nil { return nil, errors.SystemError } exist := func(itemId int64, list []model.MaintainDetailItem) bool { if len(list) == 0 { return false } for _, v := range list { if itemId == v.ItemId { return true } } return false } reply.List = make([]*v1.ItemListReply_Item, 0, len(itemList)) for _, v := range itemList { if !exist(v.ItemId, list) { reply.List = append(reply.List, &v1.ItemListReply_Item{ ItemId: v.ItemId, ItemName: v.ItemName, }) } } return reply, nil } func TemplateItemList(ctx context.Context, req *v1.TemplateItemListRequest) (reply *v1.ItemListReply, err error) { reply = &v1.ItemListReply{} 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.ItemType < 1 || req.ItemType > 3 { return reply, errors.ParamsError } list, err := model.NewMaintainDetailTemplateModel().ListByItem(database.DB().Where("t1.tid = ?", req.Tid)) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.SystemError } itemList, err := model.NewMaintainItemModel(). List(database.DB().Where("item_type = ?", req.ItemType), "id, item_id, item_name") if err != nil { return nil, errors.SystemError } exist := func(itemId int64, list []model.MaintenanceDetailTemplateItem) bool { if len(list) == 0 { return false } for _, v := range list { if itemId == v.ItemId { return true } } return false } reply.List = make([]*v1.ItemListReply_Item, 0, len(itemList)) for _, v := range itemList { if !exist(v.ItemId, list) { reply.List = append(reply.List, &v1.ItemListReply_Item{ ItemId: v.ItemId, ItemName: v.ItemName, }) } } return reply, nil }