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