|  | @@ -3,6 +3,8 @@ package crontab
 | 
	
		
			
				|  |  |  import (
 | 
	
		
			
				|  |  |  	"fmt"
 | 
	
		
			
				|  |  |  	"kpt-pasture/model"
 | 
	
		
			
				|  |  | +	"math"
 | 
	
		
			
				|  |  | +	"strconv"
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 | 
	
		
			
				|  |  |  	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
 | 
	
	
		
			
				|  | @@ -102,3 +104,166 @@ func (e *Entry) FindDeathNumber(pastureList []*model.AppPastureList, startTime,
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  	return res
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// MultipartyAbortionNumber 成母牛流产数
 | 
	
		
			
				|  |  | +func (e *Entry) MultipartyAbortionNumber(pastureList []*model.AppPastureList, startTime, endTime int64, isAppend pasturePb.IsShow_Kind) map[int64]string {
 | 
	
		
			
				|  |  | +	res := make(map[int64]string)
 | 
	
		
			
				|  |  | +	for _, pasture := range pastureList {
 | 
	
		
			
				|  |  | +		var count int64
 | 
	
		
			
				|  |  | +		if err := e.DB.Model(new(model.EventAbortion)).
 | 
	
		
			
				|  |  | +			Where("pasture_id = ?", pasture.Id).
 | 
	
		
			
				|  |  | +			Where("abortion_at BETWEEN ? AND ?", startTime, endTime).
 | 
	
		
			
				|  |  | +			Where("is_append = ?", pasturePb.IsShow_Ok).
 | 
	
		
			
				|  |  | +			Where("lact > ?", 0).
 | 
	
		
			
				|  |  | +			Where("is_append = ?", isAppend).
 | 
	
		
			
				|  |  | +			Count(&count).Error; err != nil {
 | 
	
		
			
				|  |  | +			zaplog.Error("multipartyAbortionNumber", zap.Any("pastureId", pasture), zap.Any("err", err))
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		res[pasture.Id] = fmt.Sprintf("%d", count)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return res
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// MultipartyPregnancyNumber 成母牛怀孕头数
 | 
	
		
			
				|  |  | +func (e *Entry) MultipartyPregnancyNumber(pastureList []*model.AppPastureList) map[int64]string {
 | 
	
		
			
				|  |  | +	res := make(map[int64]string)
 | 
	
		
			
				|  |  | +	for _, pasture := range pastureList {
 | 
	
		
			
				|  |  | +		var count int64
 | 
	
		
			
				|  |  | +		if err := e.DB.Model(new(model.Cow)).
 | 
	
		
			
				|  |  | +			Where("pasture_id = ?", pasture.Id).
 | 
	
		
			
				|  |  | +			Where("sex = ?", pasturePb.Genders_Female).
 | 
	
		
			
				|  |  | +			Where("breed_status = ?", pasturePb.BreedStatus_Pregnant).
 | 
	
		
			
				|  |  | +			Where("lact > ?", 0).
 | 
	
		
			
				|  |  | +			Count(&count).Error; err != nil {
 | 
	
		
			
				|  |  | +			zaplog.Error("")
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		if count > 0 {
 | 
	
		
			
				|  |  | +			res[pasture.Id] = fmt.Sprintf("%d", count)
 | 
	
		
			
				|  |  | +		} else {
 | 
	
		
			
				|  |  | +			res[pasture.Id] = "0"
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return res
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// MultipartyAbortionRate 成母牛流产率
 | 
	
		
			
				|  |  | +func (e *Entry) MultipartyAbortionRate(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
 | 
	
		
			
				|  |  | +	abortionNumberMap := e.MultipartyAbortionNumber(pastureList, startTime, endTime, pasturePb.IsShow_Ok)
 | 
	
		
			
				|  |  | +	pregnancyNumberMap := e.MultipartyPregnancyNumber(pastureList)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	res := make(map[int64]string)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	for pastureId, value1 := range abortionNumberMap {
 | 
	
		
			
				|  |  | +		var info bool
 | 
	
		
			
				|  |  | +		if value2, ok := pregnancyNumberMap[pastureId]; ok {
 | 
	
		
			
				|  |  | +			v1, _ := strconv.ParseInt(value1, 10, 64)
 | 
	
		
			
				|  |  | +			v2, _ := strconv.ParseInt(value2, 10, 64)
 | 
	
		
			
				|  |  | +			if v2 > 0 {
 | 
	
		
			
				|  |  | +				info = true
 | 
	
		
			
				|  |  | +				abortionRate := float32(math.Round(float64(v1)/float64(v2)*100) / 100)
 | 
	
		
			
				|  |  | +				res[pastureId] = fmt.Sprintf("%f", abortionRate)
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		if !info {
 | 
	
		
			
				|  |  | +			res[pastureId] = "0"
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	return res
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// MultipartyOutNumber 成母牛淘汰牛头数
 | 
	
		
			
				|  |  | +func (e *Entry) MultipartyOutNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
 | 
	
		
			
				|  |  | +	res := make(map[int64]string)
 | 
	
		
			
				|  |  | +	for _, pasture := range pastureList {
 | 
	
		
			
				|  |  | +		var count int64
 | 
	
		
			
				|  |  | +		if err := e.DB.Table(fmt.Sprintf("%s as a", new(model.EventSaleCow).TableName())).
 | 
	
		
			
				|  |  | +			Joins(fmt.Sprintf("JOIN %s AS b on a.sale_id = b.id", new(model.EventSale).TableName())).
 | 
	
		
			
				|  |  | +			Select("COUNT(*) AS count").
 | 
	
		
			
				|  |  | +			Where("a.pasture_id = ?", pasture.Id).
 | 
	
		
			
				|  |  | +			Where("b.sale_at BETWEEN ? AND ?", startTime, endTime).
 | 
	
		
			
				|  |  | +			Where("a.out_reasons_kind = ?", pasturePb.SalesType_Out).
 | 
	
		
			
				|  |  | +			Where("a.lact > ?", 0).
 | 
	
		
			
				|  |  | +			Scan(&count).Error; err != nil {
 | 
	
		
			
				|  |  | +			zaplog.Error("MultipartyOutNumber", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		res[pasture.Id] = fmt.Sprintf("%d", count)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return res
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// MultipartyDieNumber 成母牛死亡数
 | 
	
		
			
				|  |  | +func (e *Entry) MultipartyDieNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
 | 
	
		
			
				|  |  | +	res := make(map[int64]string)
 | 
	
		
			
				|  |  | +	for _, pasture := range pastureList {
 | 
	
		
			
				|  |  | +		var count int64
 | 
	
		
			
				|  |  | +		if err := e.DB.Model(new(model.EventDeath)).
 | 
	
		
			
				|  |  | +			Where("pasture_id = ?", pasture.Id).
 | 
	
		
			
				|  |  | +			Where("death_at BETWEEN ? AND ?", startTime, endTime).
 | 
	
		
			
				|  |  | +			Where("lact > ?", 0).
 | 
	
		
			
				|  |  | +			Scan(&count).Error; err != nil {
 | 
	
		
			
				|  |  | +			zaplog.Error("MultipartyOutNumber", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		res[pasture.Id] = fmt.Sprintf("%d", count)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return res
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// CalvingDieRate 产后指定天数死亡率
 | 
	
		
			
				|  |  | +func (e *Entry) CalvingDieRate(pastureList []*model.AppPastureList, startTime, endTime int64, calvingAge int32) map[int64]string {
 | 
	
		
			
				|  |  | +	res := make(map[int64]string)
 | 
	
		
			
				|  |  | +	for _, pasture := range pastureList {
 | 
	
		
			
				|  |  | +		eventDeathList := make([]*model.EventDeath, 0)
 | 
	
		
			
				|  |  | +		if err := e.DB.Model(new(model.EventDeath)).
 | 
	
		
			
				|  |  | +			Where("pasture_id = ?", pasture.Id).
 | 
	
		
			
				|  |  | +			Where("death_at BETWEEN ? AND ?", startTime, endTime).
 | 
	
		
			
				|  |  | +			Find(&eventDeathList).Error; err != nil {
 | 
	
		
			
				|  |  | +			zaplog.Error("Calving60DieRate", zap.Any("pastureId", pasture.Id), zap.Any("err", err))
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		count := int32(0)
 | 
	
		
			
				|  |  | +		for _, v := range eventDeathList {
 | 
	
		
			
				|  |  | +			if v.CalvingAge <= calvingAge {
 | 
	
		
			
				|  |  | +				count++
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if count > 0 {
 | 
	
		
			
				|  |  | +			res[pasture.Id] = fmt.Sprintf("%f", float32(math.Round(float64(count)/float64(len(eventDeathList))*100)/100))
 | 
	
		
			
				|  |  | +		} else {
 | 
	
		
			
				|  |  | +			res[pasture.Id] = "0"
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return res
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +// CalvingOutRate 产后指定天数淘汰率
 | 
	
		
			
				|  |  | +func (e *Entry) CalvingOutRate(pastureList []*model.AppPastureList, startTime, endTime int64, calvingAge int32) map[int64]string {
 | 
	
		
			
				|  |  | +	res := make(map[int64]string)
 | 
	
		
			
				|  |  | +	for _, pasture := range pastureList {
 | 
	
		
			
				|  |  | +		eventSaleCowList := make([]*model.EventSaleCow, 0)
 | 
	
		
			
				|  |  | +		if err := e.DB.Table(fmt.Sprintf("%s as a", new(model.EventSaleCow).TableName())).
 | 
	
		
			
				|  |  | +			Joins(fmt.Sprintf("JOIN %s AS b on a.sale_id = b.id", new(model.EventSale).TableName())).
 | 
	
		
			
				|  |  | +			Select("a.*").
 | 
	
		
			
				|  |  | +			Where("a.pasture_id = ?", pasture.Id).
 | 
	
		
			
				|  |  | +			Where("b.sale_at BETWEEN ? AND ?", startTime, endTime).
 | 
	
		
			
				|  |  | +			Where("a.out_reasons_kind = ?", pasturePb.SalesType_Out).
 | 
	
		
			
				|  |  | +			Find(&eventSaleCowList).Error; err != nil {
 | 
	
		
			
				|  |  | +			zaplog.Error("MultipartyOutNumber", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		count := int32(0)
 | 
	
		
			
				|  |  | +		for _, v := range eventSaleCowList {
 | 
	
		
			
				|  |  | +			if v.CalvingAge <= calvingAge {
 | 
	
		
			
				|  |  | +				count++
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +		if count > 0 {
 | 
	
		
			
				|  |  | +			res[pasture.Id] = fmt.Sprintf("%f", float32(math.Round(float64(count)/float64(len(eventSaleCowList))*100)/100))
 | 
	
		
			
				|  |  | +		} else {
 | 
	
		
			
				|  |  | +			res[pasture.Id] = "0"
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return res
 | 
	
		
			
				|  |  | +}
 |