|  | @@ -117,7 +117,7 @@ func (s *StoreEntry) MedicalEquipmentCreateOrUpdate(ctx context.Context, req *pa
 | 
	
		
			
				|  |  |  	return nil
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -func (s *StoreEntry) NeckRingLogCreateOrUpdate(ctx context.Context, req *pasturePb.NeckRingCreateRequest) error {
 | 
	
		
			
				|  |  | +func (s *StoreEntry) NeckRingCreateOrUpdate(ctx context.Context, req *pasturePb.NeckRingCreateRequest) error {
 | 
	
		
			
				|  |  |  	currentUser, err := s.GetCurrentSystemUser(ctx)
 | 
	
		
			
				|  |  |  	if err != nil {
 | 
	
		
			
				|  |  |  		return xerr.Custom("登录人信息失效")
 | 
	
	
		
			
				|  | @@ -127,54 +127,98 @@ func (s *StoreEntry) NeckRingLogCreateOrUpdate(ctx context.Context, req *pasture
 | 
	
		
			
				|  |  |  		return xerr.Custom("请选择要脖环数据")
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	newNeckRingLogList := model.NewNeckRingLogList(req.Items, currentUser)
 | 
	
		
			
				|  |  | -	cowIds := make([]int64, 0)
 | 
	
		
			
				|  |  | -	for _, v := range newNeckRingLogList {
 | 
	
		
			
				|  |  | -		cowIds = append(cowIds, v.CowId)
 | 
	
		
			
				|  |  | -	}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  	if err = s.DB.Transaction(func(tx *gorm.DB) error {
 | 
	
		
			
				|  |  | -		// 解绑脖环号
 | 
	
		
			
				|  |  | -		if err = tx.Model(new(model.NeckRingLog)).
 | 
	
		
			
				|  |  | -			Where("cow_id IN ?", cowIds).
 | 
	
		
			
				|  |  | -			Updates(map[string]interface{}{
 | 
	
		
			
				|  |  | -				"unbind_at":      time.Now().Unix(),
 | 
	
		
			
				|  |  | -				"status":         pasturePb.NeckRingStatus_Unbind,
 | 
	
		
			
				|  |  | -				"operation_id":   currentUser.Id,
 | 
	
		
			
				|  |  | -				"operation_name": currentUser.Name,
 | 
	
		
			
				|  |  | -			}).Error; err != nil {
 | 
	
		
			
				|  |  | -			return xerr.WithStack(err)
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -		// 绑定新脖环
 | 
	
		
			
				|  |  | -		if err = tx.Create(newNeckRingLogList).Error; err != nil {
 | 
	
		
			
				|  |  | -			return xerr.WithStack(err)
 | 
	
		
			
				|  |  | -		}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  		for _, v := range req.Items {
 | 
	
		
			
				|  |  | -			if v.CowId > 0 {
 | 
	
		
			
				|  |  | +			switch req.Status {
 | 
	
		
			
				|  |  | +			// 绑定
 | 
	
		
			
				|  |  | +			case pasturePb.NeckRingOperationStatus_Bind:
 | 
	
		
			
				|  |  | +				_, err = s.GetCowInfoByCowId(ctx, int64(v.CowId))
 | 
	
		
			
				|  |  | +				if err != nil {
 | 
	
		
			
				|  |  | +					return xerr.Customf("该牛不存在")
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +				neckRing, ok := s.NeckRingIsExist(ctx, v.Number)
 | 
	
		
			
				|  |  | +				if !ok {
 | 
	
		
			
				|  |  | +					newNeckRing := model.NewNeckRing(v.Number, int64(v.CowId), currentUser)
 | 
	
		
			
				|  |  | +					if err = tx.Create(newNeckRing).Error; err != nil {
 | 
	
		
			
				|  |  | +						return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				if ok {
 | 
	
		
			
				|  |  | +					neckRing.Update(int64(v.CowId))
 | 
	
		
			
				|  |  | +					if err = tx.Model(new(model.NeckRing)).
 | 
	
		
			
				|  |  | +						Select("cow_id,wear_at").
 | 
	
		
			
				|  |  | +						Where("id = ?", neckRing.Id).
 | 
	
		
			
				|  |  | +						Updates(neckRing).Error; err != nil {
 | 
	
		
			
				|  |  | +						return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				newNeckRingLog := model.NewNeckRingBindLog(v.Number, int64(v.CowId), currentUser)
 | 
	
		
			
				|  |  | +				if err = tx.Create(newNeckRingLog).Error; err != nil {
 | 
	
		
			
				|  |  | +					return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  |  				if err = tx.Model(new(model.Cow)).
 | 
	
		
			
				|  |  |  					Where("id = ?", v.CowId).
 | 
	
		
			
				|  |  | -					Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
 | 
	
		
			
				|  |  | +					Update("neck_ring_number", v.Number).
 | 
	
		
			
				|  |  | +					Error; err != nil {
 | 
	
		
			
				|  |  | +					return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +				// 解绑
 | 
	
		
			
				|  |  | +			case pasturePb.NeckRingOperationStatus_UnBind:
 | 
	
		
			
				|  |  | +				if err = tx.Model(new(model.NeckRing)).
 | 
	
		
			
				|  |  | +					Where("number = ?", v.Number).
 | 
	
		
			
				|  |  |  					Updates(map[string]interface{}{
 | 
	
		
			
				|  |  | -						"neck_ring_number": v.Number,
 | 
	
		
			
				|  |  | +						"wear_at": 0,
 | 
	
		
			
				|  |  | +						"cow_id":  0,
 | 
	
		
			
				|  |  | +						"status":  pasturePb.NeckRingStatus_Unbind,
 | 
	
		
			
				|  |  |  					}).Error; err != nil {
 | 
	
		
			
				|  |  |  					return xerr.WithStack(err)
 | 
	
		
			
				|  |  |  				}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				if err = tx.Model(new(model.Cow)).
 | 
	
		
			
				|  |  | +					Where("neck_ring_number = ?", v.Number).
 | 
	
		
			
				|  |  | +					Updates(map[string]interface{}{
 | 
	
		
			
				|  |  | +						"neck_ring_number": "",
 | 
	
		
			
				|  |  | +					}).Error; err != nil {
 | 
	
		
			
				|  |  | +					return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				if err = tx.Model(new(model.NeckRingBindLog)).
 | 
	
		
			
				|  |  | +					Where("number = ?", v.Number).
 | 
	
		
			
				|  |  | +					Update("un_bind_at", time.Now().Unix()).Error; err != nil {
 | 
	
		
			
				|  |  | +					return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +				// 编辑
 | 
	
		
			
				|  |  | +			case pasturePb.NeckRingOperationStatus_Edit:
 | 
	
		
			
				|  |  | +				if err = tx.Model(new(model.NeckRing)).
 | 
	
		
			
				|  |  | +					Where("cow_id = ?", v.CowId).
 | 
	
		
			
				|  |  | +					Update("number", v.Number).Error; err != nil {
 | 
	
		
			
				|  |  | +					return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +				if err = tx.Model(new(model.Cow)).
 | 
	
		
			
				|  |  | +					Where("id = ?", v.CowId).
 | 
	
		
			
				|  |  | +					Update("neck_ring_number", v.Number).Error; err != nil {
 | 
	
		
			
				|  |  | +					return xerr.WithStack(err)
 | 
	
		
			
				|  |  | +				}
 | 
	
		
			
				|  |  |  			}
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  		return nil
 | 
	
		
			
				|  |  |  	}); err != nil {
 | 
	
		
			
				|  |  |  		return xerr.WithStack(err)
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	return nil
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -func (s *StoreEntry) NeckRingLogList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) {
 | 
	
		
			
				|  |  | -	neckRingLogList := make([]*model.NeckRingLog, 0)
 | 
	
		
			
				|  |  | +func (s *StoreEntry) NeckRingList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) {
 | 
	
		
			
				|  |  | +	neckRingLogList := make([]*model.NeckRing, 0)
 | 
	
		
			
				|  |  |  	var count int64 = 0
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	pref := s.DB.Model(new(model.NeckRingLog)).Where("status > ?", pasturePb.NeckRingStatus_Unbind)
 | 
	
		
			
				|  |  | +	pref := s.DB.Model(new(model.NeckRing)).
 | 
	
		
			
				|  |  | +		Where("status >= ?", pasturePb.NeckRingStatus_Unbind).
 | 
	
		
			
				|  |  | +		Where("number != ''")
 | 
	
		
			
				|  |  |  	if req.Status > 0 {
 | 
	
		
			
				|  |  |  		pref.Where("status = ?", req.Status)
 | 
	
		
			
				|  |  |  	}
 | 
	
	
		
			
				|  | @@ -200,7 +244,7 @@ func (s *StoreEntry) NeckRingLogList(ctx context.Context, req *pasturePb.SearchN
 | 
	
		
			
				|  |  |  		Code:    http.StatusOK,
 | 
	
		
			
				|  |  |  		Message: "ok",
 | 
	
		
			
				|  |  |  		Data: &pasturePb.SearchNeckRingData{
 | 
	
		
			
				|  |  | -			List:     model.NeckRingLogSlice(neckRingLogList).ToPB(neckRingStatusMap),
 | 
	
		
			
				|  |  | +			List:     model.NeckRingSlice(neckRingLogList).ToPB(neckRingStatusMap, s.DB, GetCowPenInfoByCowId),
 | 
	
		
			
				|  |  |  			Total:    int32(count),
 | 
	
		
			
				|  |  |  			PageSize: pagination.PageSize,
 | 
	
		
			
				|  |  |  			Page:     pagination.Page,
 |