123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package backend
- import (
- "context"
- "kpt-pasture/model"
- "kpt-pasture/util"
- "net/http"
- "gitee.com/xuyiping_admin/pkg/xerr"
- pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
- )
- func (s *StoreEntry) SingleFactorInfantSurvivalRateAnalysis(ctx context.Context, req *pasturePb.SingleFactorPregnancyRateRequest) (*pasturePb.SingleFactorPregnancyRateResponse, error) {
- startTimeUnix := util.TimeParseLocalUnix(req.StartDayTime)
- endTimeUnix := util.TimeParseLocalEndUnix(req.EndDayTime)
- if startTimeUnix == 0 || endTimeUnix == 0 || endTimeUnix <= startTimeUnix {
- return nil, xerr.Custom("开始时间不能大于结束时间")
- }
- var err error
- list := make([]*pasturePb.SingleFactorPregnancyRateList, 0)
- chart := &pasturePb.SingleFactorPregnancyRateChart{
- Headers: make([]string, 0),
- PregnantRate: make([]float32, 0),
- MaxValue: make([]int32, 0),
- MinValue: make([]int32, 0),
- AveragePregnantRate: 0,
- }
- switch req.AnalysisMethod {
- case pasturePb.SingleFactorAnalysisMethod_Cycle:
- list, err = s.SingleFactorAnalysisMethodCycle(ctx, req)
- case pasturePb.SingleFactorAnalysisMethod_Months:
- list, err = s.SingleFactorAnalysisMethodMonths(ctx, req)
- case pasturePb.SingleFactorAnalysisMethod_Mating_Times:
- case pasturePb.SingleFactorAnalysisMethod_Breeding_Method:
- case pasturePb.SingleFactorAnalysisMethod_Breeding_Company:
- case pasturePb.SingleFactorAnalysisMethod_Operation:
- case pasturePb.SingleFactorAnalysisMethod_Mating_Interval:
- case pasturePb.SingleFactorAnalysisMethod_Bull:
- case pasturePb.SingleFactorAnalysisMethod_Breeding_Cycle:
- case pasturePb.SingleFactorAnalysisMethod_Week:
- case pasturePb.SingleFactorAnalysisMethod_Lact:
- default:
- return nil, xerr.Custom("错误的统计方式")
- }
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- totalCountMap := make(map[int]int32, len(list))
- allTotalCount := int32(0)
- allPregnantRate := float64(0)
- for i, v := range list {
- chart.Headers = append(chart.Headers, v.StatisticMethod)
- pregnantRate := float64(0)
- if v.EmptyPregnantCount+v.PregnantCount > 0 {
- pregnantRate = float64(v.PregnantCount) / float64(v.EmptyPregnantCount+v.PregnantCount)
- }
- chart.PregnantRate = append(chart.PregnantRate, float32(util.RoundToTwoDecimals(pregnantRate*100)))
- v.TotalCount = v.PregnantCount + v.EmptyPregnantCount + v.OtherCount
- v.PregnantRate = float32(util.RoundToTwoDecimals(pregnantRate * 100))
- spcRate := float32(0)
- if v.PregnantRate > 0 {
- spcRate = float32(util.RoundToTwoDecimals((float64(v.PregnantCount) / float64(v.TotalCount)) * 100))
- }
- v.SpceRate = spcRate
- totalCountMap[i] = v.TotalCount
- allTotalCount += v.TotalCount
- allPregnantRate += pregnantRate
- }
- for i, v := range totalCountMap {
- if allTotalCount <= 0 || list[i].TotalCount <= 0 {
- continue
- }
- list[i].TotalRate = float32(util.RoundToTwoDecimals(float64(v) / float64(allTotalCount) * 100))
- }
- chart.AveragePregnantRate = float32(util.RoundToTwoDecimals(allPregnantRate/float64(len(list))) * 100)
- return &pasturePb.SingleFactorPregnancyRateResponse{
- Code: http.StatusOK,
- Message: "ok",
- Data: &pasturePb.SingleFactorPregnancyRateData{
- Total: int32(len(list)),
- List: list,
- Chart: chart,
- },
- }, nil
- }
- func (s *StoreEntry) SingleFactorAnalysisMethodCycle(ctx context.Context, req *pasturePb.SingleFactorPregnancyRateRequest) ([]*pasturePb.SingleFactorPregnancyRateList, error) {
-
- res := make([]*pasturePb.SingleFactorPregnancyRateList, 0)
- return res, nil
- }
- func (s *StoreEntry) SingleFactorAnalysisMethodMonths(ctx context.Context, req *pasturePb.SingleFactorPregnancyRateRequest) ([]*pasturePb.SingleFactorPregnancyRateList, error) {
- startDayTimeUnix := util.TimeParseLocalUnix(req.StartDayTime)
- endDayTimeUnix := util.TimeParseLocalEndUnix(req.EndDayTime)
- if startDayTimeUnix == 0 || endDayTimeUnix == 0 || endDayTimeUnix <= startDayTimeUnix {
- return nil, xerr.Custom("开始时间不能大于结束时间")
- }
- res := make([]*pasturePb.SingleFactorPregnancyRateList, 0)
- pref := s.DB.Model(new(model.EventMating)).
- Select(`
- DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m') AS months,
- DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m') AS statistic_method,
- COUNT(DISTINCT CASE WHEN mating_result = ? AND mating_result_at > 0 THEN cow_id END) AS pregnant_count, -- 怀孕头数
- COUNT(DISTINCT CASE WHEN mating_result = ? AND mating_result_at > 0 THEN cow_id END) AS empty_pregnant_count, -- 空怀头数
- COUNT(DISTINCT CASE WHEN mating_result = ? AND mating_result_at > 0 THEN cow_id END) AS abortion_count, -- 流产头数
- COUNT(DISTINCT CASE WHEN mating_result IN (?, ?) THEN cow_id END) AS other_count, -- 其他数
- (
- COUNT(DISTINCT CASE WHEN mating_result = 3 AND mating_result_at > 0 THEN cow_id END) +
- COUNT(DISTINCT CASE WHEN mating_result = 4 AND mating_result_at > 0 THEN cow_id END) +
- COUNT(DISTINCT CASE WHEN mating_result IN (1, 2) THEN cow_id END)
- ) AS total_count -- 总数
- `, pasturePb.MatingResult_Pregnant, pasturePb.MatingResult_Empty, pasturePb.MatingResult_Abort,
- pasturePb.MatingResult_Unknown, pasturePb.MatingResult_ReMatch).
- Where("status = ?", pasturePb.IsShow_Ok).
- Where("reality_day BETWEEN ? AND ?", startDayTimeUnix, endDayTimeUnix).
- Where("reality_day > 0")
- if req.CowType > 0 {
- pref.Where("cow_type = ?", req.CowType)
- }
- if req.CowKind > 0 {
- pref.Where("cow_kind = ?", req.CowKind)
- }
- if req.LactInterval > 0 {
- switch req.LactInterval {
- case pasturePb.LactIntervalSymbol_Less_Than:
- pref.Where("lact < ?", req.LactIntervalStartValue)
- case pasturePb.LactIntervalSymbol_Less_Than_Or_Equal_To:
- pref.Where("lact <= ?", req.LactIntervalStartValue)
- case pasturePb.LactIntervalSymbol_Greater_Than:
- pref.Where("lact > ?", req.LactIntervalStartValue)
- case pasturePb.LactIntervalSymbol_Greater_Than_Or_Equal_To:
- pref.Where("lact >= ?", req.LactIntervalStartValue)
- case pasturePb.LactIntervalSymbol_Equal_To:
- pref.Where("lact = ?", req.LactIntervalStartValue)
- case pasturePb.LactIntervalSymbol_Not_Equal_To:
- pref.Where("lact != ?", req.LactIntervalStartValue)
- case pasturePb.LactIntervalSymbol_Between:
- pref.Where("lact BETWEEN ? AND ? ", req.LactIntervalStartValue, req.LactIntervalEndValue)
- default:
- return nil, xerr.Custom("错误的胎次区间符号")
- }
- }
- if err := pref.Group("months").Order("months").Find(&res).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return res, nil
- }
|