Parcourir la source

cow_kind: 牛只品种模块

Yi il y a 11 mois
Parent
commit
a810c4fd24

+ 48 - 0
http/handler/pasture/barn.go

@@ -159,3 +159,51 @@ func CreatedOrUpdateBreedStatus(c *gin.Context) {
 		Data: &operationPb.Success{Success: true},
 	})
 }
+
+func SearchCowKindList(c *gin.Context) {
+	var req pasturePb.SearchNameRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	pagination := &pasturePb.PaginationModel{
+		Page:       int32(c.GetInt(middleware.Page)),
+		PageSize:   int32(c.GetInt(middleware.PageSize)),
+		PageOffset: int32(c.GetInt(middleware.PageOffset)),
+	}
+
+	res, err := middleware.Dependency(c).StoreEventHub.OpsService.SearchCowKindList(c, &req, pagination)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}
+
+// CreatedOrUpdateCowKind 添加或者更新牛只种类
+func CreatedOrUpdateCowKind(c *gin.Context) {
+	var req pasturePb.SearchBaseConfigList
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := valid.ValidateStruct(&req,
+		valid.Field(&req.Name, valid.Required),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := middleware.BackendOperation(c).OpsService.CreateOrUpdateCowKind(c, &req); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}

+ 2 - 0
http/route/pasture_api.go

@@ -19,5 +19,7 @@ func PastureManageAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		pastureRoute.POST("/barn/type/createOrUpdate", pasture.CreatedOrUpdateBarnType)
 		pastureRoute.POST("/breed/status/list", pasture.SearchBreedStatusList)
 		pastureRoute.POST("/breed/status/createOrUpdate", pasture.CreatedOrUpdateBreedStatus)
+		pastureRoute.POST("/cow/kind/list", pasture.SearchCowKindList)
+		pastureRoute.POST("/cow/kind/createOrUpdate", pasture.CreatedOrUpdateCowKind)
 	}
 }

+ 1 - 13
model/config_breed_status.go

@@ -19,19 +19,7 @@ func (c *ConfigBreedStatus) TableName() string {
 
 type ConfigBreedStatusSlice []*ConfigBreedStatus
 
-func (c ConfigBreedStatusSlice) ToPB() []*pasturePb.BarnTypeList {
-	res := make([]*pasturePb.BarnTypeList, len(c))
-	for i, d := range c {
-		res[i] = &pasturePb.BarnTypeList{
-			Value:    int32(d.Id),
-			Label:    d.Name,
-			Disabled: true,
-		}
-	}
-	return res
-}
-
-func (c ConfigBreedStatusSlice) ToPB2() []*pasturePb.SearchBaseConfigList {
+func (c ConfigBreedStatusSlice) ToPB() []*pasturePb.SearchBaseConfigList {
 	res := make([]*pasturePb.SearchBaseConfigList, len(c))
 	for i, d := range c {
 		res[i] = &pasturePb.SearchBaseConfigList{

+ 35 - 0
model/config_cow_kind.go

@@ -0,0 +1,35 @@
+package model
+
+import (
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+)
+
+type ConfigCowKind struct {
+	Id        int64                 `json:"id"`
+	Name      string                `json:"name"`
+	Remarks   string                `json:"remarks"`
+	IsShow    pasturePb.IsShow_Kind `json:"is_show"`
+	CreatedAt int64                 `json:"created_at"`
+	UpdatedAt int64                 `json:"updated_at"`
+}
+
+func (c *ConfigCowKind) TableName() string {
+	return "config_cow_kind"
+}
+
+type ConfigCowKindSlice []*ConfigCowKind
+
+func (c ConfigCowKindSlice) ToPB() []*pasturePb.SearchBaseConfigList {
+	res := make([]*pasturePb.SearchBaseConfigList, len(c))
+	for i, d := range c {
+		res[i] = &pasturePb.SearchBaseConfigList{
+			Id:        int32(d.Id),
+			Name:      d.Name,
+			Remarks:   d.Remarks,
+			IsShow:    d.IsShow,
+			CreatedAt: int32(d.CreatedAt),
+			UpdatedAt: int32(d.UpdatedAt),
+		}
+	}
+	return res
+}

+ 3 - 1
module/backend/interface.go

@@ -90,9 +90,11 @@ type PastureManageService interface {
 	CreateOrUpdateBarn(ctx context.Context, req *pasturePb.SearchBarnList) error
 	SearchBarnTypeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error)
 	CreateOrUpdateBarnType(ctx context.Context, req *pasturePb.SearchBaseConfigList) error
-
 	SearchBreedStatusList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error)
 	CreateOrUpdateBreedStatus(ctx context.Context, req *pasturePb.SearchBaseConfigList) error
+
+	SearchCowKindList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error)
+	CreateOrUpdateCowKind(ctx context.Context, req *pasturePb.SearchBaseConfigList) error
 }
 
 //go:generate mockgen -destination mock/ConfigDataService.go -package kptservicemock kpt-pasture/module/backend ConfigDataService

+ 49 - 1
module/backend/pasture_manage.go

@@ -139,7 +139,7 @@ func (s *StoreEntry) SearchBreedStatusList(ctx context.Context, req *pasturePb.S
 		Code:    http.StatusOK,
 		Message: "ok",
 		Data: &pasturePb.SearchBaseConfigData{
-			List:     model.ConfigBreedStatusSlice(breedStatusList).ToPB2(),
+			List:     model.ConfigBreedStatusSlice(breedStatusList).ToPB(),
 			Total:    int32(count),
 			PageSize: pagination.PageSize,
 			Page:     pagination.Page,
@@ -168,3 +168,51 @@ func (s *StoreEntry) CreateOrUpdateBreedStatus(ctx context.Context, req *pasture
 	}
 	return nil
 }
+
+func (s *StoreEntry) SearchCowKindList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
+	configCowKindList := make([]*model.ConfigCowKind, 0)
+	var count int64 = 0
+
+	pref := s.DB.Model(new(model.ConfigCowKind))
+	if req.Name != "" {
+		pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
+	}
+
+	if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
+		Find(&configCowKindList).Debug().Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+
+	return &pasturePb.SearchBaseConfigResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data: &pasturePb.SearchBaseConfigData{
+			List:     model.ConfigCowKindSlice(configCowKindList).ToPB(),
+			Total:    int32(count),
+			PageSize: pagination.PageSize,
+			Page:     pagination.Page,
+		},
+	}, nil
+}
+
+func (s *StoreEntry) CreateOrUpdateCowKind(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
+	if req.Id > 0 {
+		barn := &model.ConfigCowKind{Id: int64(req.Id)}
+		if err := s.DB.Model(&model.ConfigCowKind{}).First(barn).Error; err != nil {
+			if !errors.Is(err, gorm.ErrRecordNotFound) {
+				return xerr.WithStack(err)
+			}
+		}
+	}
+
+	if err := s.DB.Model(&model.ConfigCowKind{}).Where(map[string]interface{}{
+		"id": req.Id,
+	}).Assign(map[string]interface{}{
+		"name":    req.Name,
+		"remarks": req.Remarks,
+		"is_show": pasturePb.IsShow_Ok,
+	}).FirstOrCreate(&model.ConfigCowKind{}).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+	return nil
+}