package backend import ( "context" "errors" "fmt" "kpt-pasture/model" "net/http" "gitee.com/xuyiping_admin/pkg/xerr" "gorm.io/gorm" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) func (s *StoreEntry) SearchBarnList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBarnResponse, error) { barnList := make([]*model.Bran, 0) var count int64 = 0 pref := s.DB.Model(new(model.Bran)).Where("is_delete = ?", pasturePb.IsShow_Ok) 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(&barnList).Debug().Error; err != nil { return nil, xerr.WithStack(err) } configBarnTypeList := make([]*model.ConfigBarnType, 0) if err := s.DB.Model(new(model.ConfigBarnType)).Find(&configBarnTypeList).Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.SearchBarnResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.SearchBarnData{ List: model.BarnSlice(barnList).ToPB(configBarnTypeList), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) CreateOrUpdateBarn(ctx context.Context, req *pasturePb.SearchBarnList) error { if req.Id > 0 { barn := &model.Bran{Id: int64(req.Id)} if err := s.DB.Model(&model.Bran{}).First(barn).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return xerr.WithStack(err) } } } if err := s.DB.Model(&model.Bran{}).Where(map[string]interface{}{ "id": req.Id, }).Assign(map[string]interface{}{ "name": req.Name, "remarks": req.Remarks, "barn_type": req.BarnTypeId, "lengths": req.Lengths, "widths": req.Widths, "doctrinal_capacity": req.DoctrinalCapacity, "bed_number": req.BedNumber, "neck_number": req.NeckNumber, "is_delete": pasturePb.IsShow_Ok, "is_show": pasturePb.IsShow_Ok, }).FirstOrCreate(&model.Bran{}).Error; err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) SearchBarnTypeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) { barnTypeList := make([]*model.ConfigBarnType, 0) var count int64 = 0 pref := s.DB.Model(new(model.ConfigBarnType)) 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(&barnTypeList).Debug().Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.SearchBaseConfigResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.SearchBaseConfigData{ List: model.ConfigBarnTypeSlice(barnTypeList).ToPB2(), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) CreateOrUpdateBarnType(ctx context.Context, req *pasturePb.SearchBaseConfigList) error { if req.Id > 0 { barn := &model.ConfigBarnType{Id: int64(req.Id)} if err := s.DB.Model(&model.ConfigBarnType{}).First(barn).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return xerr.WithStack(err) } } } if err := s.DB.Model(&model.ConfigBarnType{}).Where(map[string]interface{}{ "id": req.Id, }).Assign(map[string]interface{}{ "name": req.Name, "remarks": req.Remarks, "is_show": pasturePb.IsShow_Ok, }).FirstOrCreate(&model.ConfigBarnType{}).Error; err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) SearchBreedStatusList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) { breedStatusList := make([]*model.ConfigBreedStatus, 0) var count int64 = 0 pref := s.DB.Model(new(model.ConfigBreedStatus)) 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(&breedStatusList).Debug().Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.SearchBaseConfigResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.SearchBaseConfigData{ List: model.ConfigBreedStatusSlice(breedStatusList).ToPB(), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) CreateOrUpdateBreedStatus(ctx context.Context, req *pasturePb.SearchBaseConfigList) error { if req.Id > 0 { barn := &model.ConfigBreedStatus{Id: int64(req.Id)} if err := s.DB.Model(&model.ConfigBreedStatus{}).First(barn).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return xerr.WithStack(err) } } } if err := s.DB.Model(&model.ConfigBreedStatus{}).Where(map[string]interface{}{ "id": req.Id, }).Assign(map[string]interface{}{ "name": req.Name, "remarks": req.Remarks, "is_show": pasturePb.IsShow_Ok, }).FirstOrCreate(&model.ConfigBreedStatus{}).Error; err != nil { return xerr.WithStack(err) } 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 }