123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package backend
- import (
- "context"
- "fmt"
- "kpt-pasture/model"
- "kpt-pasture/util"
- "net/http"
- pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
- "gitee.com/xuyiping_admin/pkg/xerr"
- )
- // PregnancyReport 孕检报告
- func (s *StoreEntry) PregnancyReport(ctx context.Context, req *pasturePb.PregnancyReportRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnancyReportResponse, error) {
- startDayUnix := util.TimeParseLocalUnix(req.StartDayTime)
- endDayUnix := util.TimeParseLocalEndUnix(req.EndDayTime)
- if startDayUnix > endDayUnix || startDayUnix == 0 || endDayUnix == 0 {
- return nil, xerr.Custom("开始时间不能大于结束时间")
- }
- eventPregnantCheckList := make([]*model.EventPregnantCheck, 0)
- pref := s.DB.Model(new(model.EventPregnantCheck)).Where("status = ?", pasturePb.IsShow_Ok).Where("cow_type = ?", req.CowType)
- if startDayUnix > 0 && endDayUnix > 0 && endDayUnix >= startDayUnix {
- pref = pref.Where("create_time BETWEEN ? AND ?", startDayUnix, endDayUnix)
- }
- if req.PregnantCheckResult > 0 {
- pref = pref.Where("pregnant_check_result = ?", req.PregnantCheckResult)
- }
- var count int64 = 0
- if err := pref.Count(&count).
- Limit(int(pagination.PageSize)).
- Offset(int(pagination.PageOffset)).
- Order("id desc").
- Find(&eventPregnantCheckList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- pregnantCheckResultMap := s.PregnantCheckResultMap()
- pregnantCheckMethodMap := s.PregnantCheckMethodMap()
- return &pasturePb.PregnancyReportResponse{
- Code: http.StatusOK,
- Message: "ok",
- Data: &pasturePb.PregnancyReportData{
- List: model.EventPregnantCheckSlice(eventPregnantCheckList).ToPB3(pregnantCheckResultMap, pregnantCheckMethodMap),
- Total: int32(count),
- PageSize: pagination.PageSize,
- Page: pagination.Page,
- },
- }, nil
- }
- // CalvingReport 产犊报告
- func (s *StoreEntry) CalvingReport(ctx context.Context, req *pasturePb.CalvingReportRequest) (*pasturePb.CalvingReportResponse, error) {
- lastDayOfMonth, err := util.GetLastDayOfMonth(req.EndDayTime)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- endDayTimeUnix := util.TimeParseLocalEndUnix(lastDayOfMonth)
- startDayTimeUnix := util.TimeParseLocalUnix(fmt.Sprintf("%s-01", req.StartDayTime))
- if startDayTimeUnix == 0 || endDayTimeUnix == 0 || endDayTimeUnix <= startDayTimeUnix {
- return nil, xerr.Custom("开始时间不能大于结束时间")
- }
- eventCalving1 := make([]*pasturePb.CalvingReportTable, 0)
- pref1 := s.DB.Model(new(model.EventCalving)).
- Select(fmt.Sprint(`DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m') AS statistic_method,cow_kind,`)+fmt.Sprintf(
- `SUM(child_number) AS totalCount,
- SUM(CASE WHEN child_number > 1 THEN child_number ELSE 0 END) AS twins,
- SUM(CASE WHEN pregnancy_age <= %d THEN 1 ELSE 0 END) AS premature_labor_count,
- SUM(CASE WHEN pregnancy_age >= %d THEN 1 ELSE 0 END) AS late_labor_count,
- count(*) as total_count`, req.PrematureLabor, req.LateLabor),
- ).Where("reality_day BETWEEN ? AND ? ", startDayTimeUnix, endDayTimeUnix)
- if req.AnalysisMethod > 0 {
- switch req.AnalysisMethod {
- case pasturePb.CalvingAnalysisMethod_Months:
- pref1.Group("statistic_method").Order("statistic_method")
- case pasturePb.CalvingAnalysisMethod_CowKind:
- pref1.Group("cow_kind").Order("cow_kind")
- }
- }
- if err = pref1.Find(&eventCalving1).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- eventCalving2 := make([]*pasturePb.CalvingReportTable, 0)
- pref2 := s.DB.Model(new(model.CalvingCalf)).
- Select(fmt.Sprint(`DATE_FORMAT(FROM_UNIXTIME(birth_at), '%Y-%m') AS statistic_method, cow_kind,
- SUM(CASE WHEN sex = 1 THEN 1 ELSE 0 END) AS bulls,
- SUM(CASE WHEN sex = 2 THEN 1 ELSE 0 END) AS cows,
- SUM(CASE WHEN is_live = 1 THEN 1 ELSE 0 END) AS survive_count,
- SUM(CASE WHEN is_live = 2 THEN 1 ELSE 0 END) AS die_count,
- SUM(CASE WHEN is_live = 2 AND sex = 1 THEN 1 ELSE 0 END) AS bulls_die_count,
- SUM(CASE WHEN is_live = 2 AND sex = 2 THEN 1 ELSE 0 END) AS cows_die_count,
- SUM(CASE WHEN is_adoption = 1 THEN 1 ELSE 0 END) AS adopt_count`)).
- Where("birth_at BETWEEN ? AND ?", startDayTimeUnix, endDayTimeUnix)
- if req.AnalysisMethod > 0 {
- switch req.AnalysisMethod {
- case pasturePb.CalvingAnalysisMethod_Months:
- pref2.Group("statistic_method").Order("statistic_method")
- case pasturePb.CalvingAnalysisMethod_CowKind:
- pref2.Group("cow_kind").Order("cow_kind")
- }
- }
- if err = pref2.Find(&eventCalving2).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- // 合并数据集
- for _, v1 := range eventCalving1 {
- for _, v2 := range eventCalving2 {
- if v1.StatisticMethod != v2.StatisticMethod && v1.CowKind != v2.CowKind {
- continue
- }
- v1.Bulls = v2.Bulls
- v1.Cows = v2.Cows
- v1.SurviveCount = v2.SurviveCount
- v1.DieCount = v2.DieCount
- v1.BullsDieCount = v2.BullsDieCount
- v1.CowsDieCount = v2.CowsDieCount
- v1.AdoptCount = v2.AdoptCount
- }
- }
- cowKindMap := s.CowKindMap()
- for _, v := range eventCalving1 {
- if req.AnalysisMethod == pasturePb.CalvingAnalysisMethod_CowKind {
- v.StatisticMethod = cowKindMap[v.CowKind]
- }
- if v.TotalCount > 0 {
- v.TwinsRate = float32(util.RoundToTwoDecimals(float64(v.Twins) / float64(v.TotalCount) * 100))
- v.BullsDieRate = float32(util.RoundToTwoDecimals(float64(v.Bulls) / float64(v.TotalCount) * 100))
- v.CowsRate = float32(util.RoundToTwoDecimals(float64(v.Cows) / float64(v.TotalCount) * 100))
- v.DieRate = float32(util.RoundToTwoDecimals(float64(v.DieCount) / float64(v.TotalCount) * 100))
- }
- if v.Bulls > 0 {
- v.BullsDieRate = float32(util.RoundToTwoDecimals(float64(v.BullsDieCount) / float64(v.Bulls) * 100))
- }
- if v.Cows > 0 {
- v.CowsDieRate = float32(util.RoundToTwoDecimals(float64(v.CowsDieCount) / float64(v.Cows) * 100))
- }
- v.NormalLaborCount = v.TotalCount - (v.PrematureLaborCount + v.LateLaborCount)
- if v.NormalLaborCount <= 0 {
- v.NormalLaborCount = 0
- }
- }
- return &pasturePb.CalvingReportResponse{
- Code: http.StatusOK,
- Message: "ok",
- Data: &pasturePb.CalvingReportData{
- List: eventCalving1,
- Total: int32(len(eventCalving1)),
- },
- }, nil
- }
|