1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- 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.SearchBarnRequest, 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: 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
- }
|