123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- 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) {
- penList := make([]*model.Pen, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.Pen)).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(&penList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- //configBarnTypeList := make([]*model.ConfigPenType, 0)
- barnTypeMap := s.BarnTypeMap()
- return &pasturePb.SearchBarnResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &pasturePb.SearchBarnData{
- List: model.PenSlice(penList).ToPB(barnTypeMap),
- Total: int32(count),
- PageSize: pagination.PageSize,
- Page: pagination.Page,
- },
- }, nil
- }
- func (s *StoreEntry) CreateOrUpdateBarn(ctx context.Context, req *pasturePb.SearchBarnList) error {
- userModel, err := s.GetUserModel(ctx)
- if err != nil {
- return xerr.WithStack(err)
- }
- if req.Id > 0 {
- barn := &model.Pen{Id: req.Id}
- if err = s.DB.Model(&model.Pen{}).First(barn).Error; err != nil {
- if !errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.WithStack(err)
- }
- }
- }
- if err = s.DB.Model(&model.Pen{}).Where(map[string]interface{}{
- "id": req.Id,
- }).Assign(map[string]interface{}{
- "name": req.Name,
- "remarks": req.Remarks,
- "pen_type": req.BarnTypeId,
- "lengths": req.Lengths,
- "widths": req.Widths,
- "doctrinal_capacity": req.DoctrinalCapacity,
- "bed_number": req.BedNumber,
- "neck_number": req.NeckNumber,
- "pasture_id": userModel.AppPasture.Id,
- "is_delete": pasturePb.IsShow_Ok,
- "is_show": pasturePb.IsShow_Ok,
- }).FirstOrCreate(&model.Pen{}).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.ConfigPenType, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.ConfigPenType))
- 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).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &pasturePb.SearchBaseConfigResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &pasturePb.SearchBaseConfigData{
- List: model.ConfigBarnTypeSlice(barnTypeList).ToPB(),
- 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.ConfigPenType{Id: int64(req.Id)}
- if err := s.DB.Model(&model.ConfigPenType{}).First(barn).Error; err != nil {
- if !errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.WithStack(err)
- }
- }
- }
- if err := s.DB.Model(&model.ConfigPenType{}).Where(map[string]interface{}{
- "id": req.Id,
- }).Assign(map[string]interface{}{
- "name": req.Name,
- "remarks": req.Remarks,
- "is_show": pasturePb.IsShow_Ok,
- }).FirstOrCreate(&model.ConfigPenType{}).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).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &pasturePb.SearchBaseConfigResponse{
- Code: http.StatusOK,
- Msg: "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).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &pasturePb.SearchBaseConfigResponse{
- Code: http.StatusOK,
- Msg: "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
- }
- func (s *StoreEntry) SearchCowStatusList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
- configCowStatusList := make([]*model.ConfigCowStatus, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.ConfigCowStatus))
- 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(&configCowStatusList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &pasturePb.SearchBaseConfigResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &pasturePb.SearchBaseConfigData{
- List: model.ConfigCowStatusSlice(configCowStatusList).ToPB(),
- Total: int32(count),
- PageSize: pagination.PageSize,
- Page: pagination.Page,
- },
- }, nil
- }
- func (s *StoreEntry) CreateOrUpdateCowStatus(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
- if req.Id > 0 {
- barn := &model.ConfigCowStatus{Id: int64(req.Id)}
- if err := s.DB.Model(&model.ConfigCowStatus{}).First(barn).Error; err != nil {
- if !errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.WithStack(err)
- }
- }
- }
- if err := s.DB.Model(&model.ConfigCowStatus{}).Where(map[string]interface{}{
- "id": req.Id,
- }).Assign(map[string]interface{}{
- "name": req.Name,
- "remarks": req.Remarks,
- "is_show": pasturePb.IsShow_Ok,
- }).FirstOrCreate(&model.ConfigCowStatus{}).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- func (s *StoreEntry) SearchCowTypeList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
- configCowTypeList := make([]*model.ConfigCowType, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.ConfigCowType))
- 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(&configCowTypeList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &pasturePb.SearchBaseConfigResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &pasturePb.SearchBaseConfigData{
- List: model.ConfigCowTypeSlice(configCowTypeList).ToPB(),
- Total: int32(count),
- PageSize: pagination.PageSize,
- Page: pagination.Page,
- },
- }, nil
- }
- func (s *StoreEntry) CreateOrUpdateCowType(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
- if req.Id > 0 {
- barn := &model.ConfigCowType{Id: int64(req.Id)}
- if err := s.DB.Model(&model.ConfigCowType{}).First(barn).Error; err != nil {
- if !errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.WithStack(err)
- }
- }
- }
- if err := s.DB.Model(&model.ConfigCowType{}).Where(map[string]interface{}{
- "id": req.Id,
- }).Assign(map[string]interface{}{
- "name": req.Name,
- "remarks": req.Remarks,
- "is_show": pasturePb.IsShow_Ok,
- }).FirstOrCreate(&model.ConfigCowType{}).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- func (s *StoreEntry) SearchTransferPenReasonList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
- configTransferPenReasonList := make([]*model.ConfigTransferPenReason, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.ConfigTransferPenReason))
- 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(&configTransferPenReasonList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &pasturePb.SearchBaseConfigResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &pasturePb.SearchBaseConfigData{
- List: model.ConfigTransferPenReasonSlice(configTransferPenReasonList).ToPB(),
- Total: int32(count),
- PageSize: pagination.PageSize,
- Page: pagination.Page,
- },
- }, nil
- }
- func (s *StoreEntry) CreateOrUpdateTransferPenReason(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
- if req.Id > 0 {
- barn := &model.ConfigTransferPenReason{Id: int64(req.Id)}
- if err := s.DB.Model(&model.ConfigTransferPenReason{}).First(barn).Error; err != nil {
- if !errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.WithStack(err)
- }
- }
- }
- if err := s.DB.Model(&model.ConfigTransferPenReason{}).Where(map[string]interface{}{
- "id": req.Id,
- }).Assign(map[string]interface{}{
- "name": req.Name,
- "remarks": req.Remarks,
- "is_show": pasturePb.IsShow_Ok,
- }).FirstOrCreate(&model.ConfigTransferPenReason{}).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
- func (s *StoreEntry) SearchCowSourceList(ctx context.Context, req *pasturePb.SearchNameRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBaseConfigResponse, error) {
- configCowSourceList := make([]*model.ConfigCowSource, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.ConfigCowSource))
- 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(&configCowSourceList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &pasturePb.SearchBaseConfigResponse{
- Code: http.StatusOK,
- Msg: "ok",
- Data: &pasturePb.SearchBaseConfigData{
- List: model.ConfigCowSourceSlice(configCowSourceList).ToPB(),
- Total: int32(count),
- PageSize: pagination.PageSize,
- Page: pagination.Page,
- },
- }, nil
- }
- func (s *StoreEntry) CreateOrUpdateCowSource(ctx context.Context, req *pasturePb.SearchBaseConfigList) error {
- if req.Id > 0 {
- barn := &model.ConfigCowSource{Id: int64(req.Id)}
- if err := s.DB.Model(&model.ConfigCowSource{}).First(barn).Error; err != nil {
- if !errors.Is(err, gorm.ErrRecordNotFound) {
- return xerr.WithStack(err)
- }
- }
- }
- if err := s.DB.Model(&model.ConfigCowSource{}).Where(map[string]interface{}{
- "id": req.Id,
- }).Assign(map[string]interface{}{
- "name": req.Name,
- "remarks": req.Remarks,
- "is_show": pasturePb.IsShow_Ok,
- }).FirstOrCreate(&model.ConfigCowSource{}).Error; err != nil {
- return xerr.WithStack(err)
- }
- return nil
- }
|