|  | @@ -9,6 +9,9 @@ import (
 | 
	
		
			
				|  |  |  	"strings"
 | 
	
		
			
				|  |  |  	"time"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
 | 
	
		
			
				|  |  | +	"go.uber.org/zap"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	"gitee.com/xuyiping_admin/pkg/xerr"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 | 
	
	
		
			
				|  | @@ -404,6 +407,18 @@ func (s *StoreEntry) AbortionRate(ctx context.Context, req *pasturePb.AbortionRa
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  func (s *StoreEntry) TwentyonePregnantRate(ctx context.Context, req *pasturePb.TwentyOnePregnantRateRequest) (*pasturePb.TwentyOnePregnantRateResponse, error) {
 | 
	
		
			
				|  |  | +	startUnix := util.TimeParseLocalUnix(req.StartDate)
 | 
	
		
			
				|  |  | +	endUnix := util.TimeParseLocalUnix(req.EndDate)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	if startUnix > endUnix {
 | 
	
		
			
				|  |  | +		return nil, xerr.Customf("开始时间不能大于结束时间: %s ~ %d", req.StartDate, req.EndDate)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	nowDateTime := time.Now()
 | 
	
		
			
				|  |  | +	if endUnix > nowDateTime.Unix() {
 | 
	
		
			
				|  |  | +		return nil, xerr.Customf("结束时间不能大于当前时间: %s ~ %s", req.EndDate, nowDateTime.Format(model.LayoutDate2))
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	dataRange, err := util.Get21DayPeriods(req.StartDate, req.EndDate)
 | 
	
		
			
				|  |  |  	if err != nil {
 | 
	
		
			
				|  |  |  		return nil, xerr.WithStack(err)
 | 
	
	
		
			
				|  | @@ -418,9 +433,9 @@ func (s *StoreEntry) TwentyonePregnantRate(ctx context.Context, req *pasturePb.T
 | 
	
		
			
				|  |  |  	systemBasicName := ""
 | 
	
		
			
				|  |  |  	switch req.CowType {
 | 
	
		
			
				|  |  |  	case pasturePb.CowType_Breeding_Calf:
 | 
	
		
			
				|  |  | -		systemBasicName = "proactively_stop_breeding_for_adult"
 | 
	
		
			
				|  |  | +		systemBasicName = model.ProactivelyStopBreedingForAdult
 | 
	
		
			
				|  |  |  	case pasturePb.CowType_Reserve_Calf:
 | 
	
		
			
				|  |  | -		systemBasicName = "proactively_stop_breeding_for_backup"
 | 
	
		
			
				|  |  | +		systemBasicName = model.ProactivelyStopBreedingForBackup
 | 
	
		
			
				|  |  |  	default:
 | 
	
		
			
				|  |  |  		return nil, xerr.Customf("不支持的牛只类型: %d", req.CowType)
 | 
	
		
			
				|  |  |  	}
 | 
	
	
		
			
				|  | @@ -430,17 +445,31 @@ func (s *StoreEntry) TwentyonePregnantRate(ctx context.Context, req *pasturePb.T
 | 
	
		
			
				|  |  |  		return nil, xerr.WithStack(err)
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	stopBreedingDay := systemBasic.MinValue
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -	fmt.Println(stopBreedingDay)
 | 
	
		
			
				|  |  | +	stopBreedingDay := systemBasic.MinValue * 86400
 | 
	
		
			
				|  |  | +	dateCowList := make([][]*model.Cow, len(dataRange))
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	for _, v := range dataRange {
 | 
	
		
			
				|  |  | +	twentyOnePregnantRateList := make([]*pasturePb.TwentyOnePregnantRateList, 0)
 | 
	
		
			
				|  |  | +	for i, v := range dataRange {
 | 
	
		
			
				|  |  |  		middleDay, err := util.GetRangeDayMiddleDay(v, 11)
 | 
	
		
			
				|  |  |  		if err != nil {
 | 
	
		
			
				|  |  |  			return nil, xerr.WithStack(err)
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -		chart.Header = append(chart.Header, middleDay)
 | 
	
		
			
				|  |  | +		middleDayUnix := util.TimeParseLocalEndUnix(middleDay)
 | 
	
		
			
				|  |  | +		chart.Header = append(chart.Header, fmt.Sprintf("%s ~ %s", v[0], v[1]))
 | 
	
		
			
				|  |  | +		cowList := s.TwentyonePregnantCowList(ctx, req.CowType, stopBreedingDay, middleDayUnix, []int64{})
 | 
	
		
			
				|  |  | +		twentyOnePregnantRateList = append(twentyOnePregnantRateList, &pasturePb.TwentyOnePregnantRateList{
 | 
	
		
			
				|  |  | +			StartDay:             v[0],
 | 
	
		
			
				|  |  | +			EndDay:               v[1],
 | 
	
		
			
				|  |  | +			ShouldBreedCount:     int32(len(cowList)),
 | 
	
		
			
				|  |  | +			RealityBreedCount:    0,
 | 
	
		
			
				|  |  | +			BreedRate:            0,
 | 
	
		
			
				|  |  | +			ShouldPregnantCount:  0,
 | 
	
		
			
				|  |  | +			RealityPregnantCount: 0,
 | 
	
		
			
				|  |  | +			PregnantRate:         0,
 | 
	
		
			
				|  |  | +			RealityAbortionCount: 0,
 | 
	
		
			
				|  |  | +			AbortionRate:         0,
 | 
	
		
			
				|  |  | +		})
 | 
	
		
			
				|  |  | +		dateCowList[i] = cowList
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	return &pasturePb.TwentyOnePregnantRateResponse{
 | 
	
	
		
			
				|  | @@ -449,11 +478,44 @@ func (s *StoreEntry) TwentyonePregnantRate(ctx context.Context, req *pasturePb.T
 | 
	
		
			
				|  |  |  		Data: &pasturePb.TwentyOnePregnantRateData{
 | 
	
		
			
				|  |  |  			Chart: chart,
 | 
	
		
			
				|  |  |  			Table: &pasturePb.TwentyOnePregnantRateTable{
 | 
	
		
			
				|  |  | -				List:     nil,
 | 
	
		
			
				|  |  | -				Total:    0,
 | 
	
		
			
				|  |  | -				PageSize: 0,
 | 
	
		
			
				|  |  | -				Page:     0,
 | 
	
		
			
				|  |  | +				List:  twentyOnePregnantRateList,
 | 
	
		
			
				|  |  | +				Total: int32(len(dataRange)),
 | 
	
		
			
				|  |  |  			},
 | 
	
		
			
				|  |  |  		},
 | 
	
		
			
				|  |  |  	}, nil
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// TwentyonePregnantCowList 21天牛只停配期牛只列表
 | 
	
		
			
				|  |  | +func (s *StoreEntry) TwentyonePregnantCowList(
 | 
	
		
			
				|  |  | +	ctx context.Context,
 | 
	
		
			
				|  |  | +	cowType pasturePb.CowType_Kind,
 | 
	
		
			
				|  |  | +	stopBreedingDay int32,
 | 
	
		
			
				|  |  | +	middleDay int64,
 | 
	
		
			
				|  |  | +	notInCow []int64,
 | 
	
		
			
				|  |  | +) []*model.Cow {
 | 
	
		
			
				|  |  | +	cowList := make([]*model.Cow, 0)
 | 
	
		
			
				|  |  | +	switch cowType {
 | 
	
		
			
				|  |  | +	case pasturePb.CowType_Reserve_Calf:
 | 
	
		
			
				|  |  | +		pref := s.DB.Model(new(model.Cow)).
 | 
	
		
			
				|  |  | +			Where("cow_type = ?", cowType).
 | 
	
		
			
				|  |  | +			Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
 | 
	
		
			
				|  |  | +			Where("is_pregnant = ?", pasturePb.IsShow_No).
 | 
	
		
			
				|  |  | +			Where("lact = ?", 0).
 | 
	
		
			
				|  |  | +			Where("birth_at + ? < ?", stopBreedingDay, middleDay)
 | 
	
		
			
				|  |  | +		if len(notInCow) > 0 {
 | 
	
		
			
				|  |  | +			pref = pref.Where("id NOT IN ?", notInCow)
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		if err := pref.Find(&cowList).Error; err != nil {
 | 
	
		
			
				|  |  | +			zaplog.Error("TwentyonePregnantCowList",
 | 
	
		
			
				|  |  | +				zap.Any("cowType", cowType),
 | 
	
		
			
				|  |  | +				zap.Any("stopBreedingDay", stopBreedingDay),
 | 
	
		
			
				|  |  | +				zap.Any("middleDay", middleDay),
 | 
	
		
			
				|  |  | +				zap.Any("notInCow", notInCow),
 | 
	
		
			
				|  |  | +			)
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	case pasturePb.CowType_Breeding_Calf:
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return cowList
 | 
	
		
			
				|  |  | +}
 |