123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package backend
- import (
- "context"
- "kpt-pasture/model"
- "net/http"
- "strings"
- "time"
- "gitee.com/xuyiping_admin/pkg/xerr"
- pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
- )
- // GrowthCurve 生长曲线 获取图表数据
- func (s *StoreEntry) GrowthCurve(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest) (*pasturePb.GrowthCurvesResponse, error) {
- // 查询数据
- cowList := make([]*model.Cow, 0)
- pref := s.DB.Model(new(model.Cow)).Where("is_remove = ?", pasturePb.IsShow_Ok)
- if req.GetCowId() != "" {
- pref.Where("id IN ?", strings.Split(req.CowId, ","))
- }
- if len(req.BirthDate) == 2 && req.BirthDate[0] != "" && req.BirthDate[1] != "" {
- t0, _ := time.Parse(time.RFC3339, req.BirthDate[0])
- t1, _ := time.Parse(time.RFC3339, req.BirthDate[1])
- pref.Where("birth_at BETWEEN ? AND ?", t0.Unix(), t1.Unix()+86399)
- }
- if err := pref.Find(&cowList).Error; err != nil {
- return nil, err
- }
- penList, err := s.GetPenList(ctx)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- // 计算图表数据
- chartsList := &pasturePb.Charts{
- CowId: make([]int32, 0),
- Weight: make([]float32, 0),
- DayAge: make([]int32, 0),
- }
- cowData := make([]*pasturePb.CowList, 0)
- for _, cow := range cowList {
- currentWeight := float32(cow.CurrentWeight) / 100
- penName := ""
- for _, v := range penList {
- if int64(cow.PenId) != v.Id {
- continue
- }
- penName = v.Name
- }
- cowData = append(cowData, &pasturePb.CowList{
- CowId: int32(cow.Id),
- EarNumber: cow.EarNumber,
- DayAge: cow.GetDayAge(),
- PenName: penName,
- CurrentWeight: currentWeight,
- BirthAt: int32(cow.BirthAt),
- BirthWeight: float32(cow.BirthWeight) / 100,
- LastWeightAt: int32(cow.LastWeightAt),
- DailyWeightGain: float32(cow.GetDayWeight() / 100),
- AverageDailyWeightGain: float32(cow.GetAverageDailyWeight() / 100),
- })
- chartsList.CowId = append(chartsList.CowId, int32(cow.Id))
- chartsList.Weight = append(chartsList.Weight, currentWeight)
- chartsList.DayAge = append(chartsList.DayAge, cow.GetDayAge())
- }
- // 返回数据
- return &pasturePb.GrowthCurvesResponse{
- Code: http.StatusOK,
- Message: "success",
- Data: &pasturePb.GrowthCurveData{
- Table: cowData,
- Charts: chartsList,
- },
- }, nil
- }
- func (s *StoreEntry) WeightRange(ctx context.Context, req *pasturePb.WeightRangeRequest) (*pasturePb.WeightRangeResponse, error) {
- cowWeightRange := make([]*model.CowWeightRange, 0)
- prefix := s.DB.Model(new(model.Cow)).Where("is_remove = ?", pasturePb.IsShow_Ok)
- if req.CowKind > 0 {
- prefix.Where("cow_kind = ?", req.CowKind)
- }
- if err := prefix.Select(`
- CASE
- WHEN current_weight BETWEEN 0 AND 50000 THEN '0-50'
- WHEN current_weight BETWEEN 50001 AND 100000 THEN '51-100'
- WHEN current_weight BETWEEN 100001 AND 150000 THEN '101-150'
- WHEN current_weight BETWEEN 150001 AND 200000 THEN '151-200'
- WHEN current_weight BETWEEN 200001 AND 250000 THEN '201-250'
- WHEN current_weight BETWEEN 250001 AND 300000 THEN '251-300'
- WHEN current_weight BETWEEN 300001 AND 350000 THEN '301-350'
- WHEN current_weight BETWEEN 350001 AND 400000 THEN '351-400'
- WHEN current_weight BETWEEN 400001 AND 450000 THEN '401-450'
- WHEN current_weight BETWEEN 450001 AND 500000 THEN '451-500'
- WHEN current_weight BETWEEN 500001 AND 550000 THEN '500-550'
- WHEN current_weight BETWEEN 550001 AND 600000 THEN '551-600'
- WHEN current_weight BETWEEN 600001 AND 650000 THEN '601-650'
- WHEN current_weight BETWEEN 650001 AND 700000 THEN '651-700'
- WHEN current_weight BETWEEN 700001 AND 750000 THEN '701-750'
- ELSE '750+'
- END AS weight_range,
- COUNT(*) AS count `,
- ).Group("weight_range").Order("MIN(current_weight)").Find(&cowWeightRange).Error; err != nil {
- return nil, err
- }
- if len(cowWeightRange) == 0 {
- return nil, xerr.Customf("没有数据")
- }
- header := make([]string, 0)
- data := make([]int32, 0)
- for _, v := range cowWeightRange {
- header = append(header, v.WeightRange)
- data = append(data, v.Count)
- }
- // 牛只详情列表
- pref := s.DB.Model(new(model.Cow)).Where("is_remove = ?", pasturePb.IsShow_Ok)
- if req.CowKind > 0 {
- pref.Where("cow_kind = ?", req.CowKind)
- }
- cowList := make([]*model.Cow, 0)
- if req.MinWeight >= 0 && req.MaxWeight >= 0 && req.MinWeight < req.MaxWeight {
- pref.Where("current_weight BETWEEN ? AND ? ", req.MinWeight*1000, req.MaxWeight*1000)
- }
- if err := pref.Find(&cowList).Error; err != nil {
- return nil, err
- }
- penMap := s.PenMap(ctx)
- return &pasturePb.WeightRangeResponse{
- Code: http.StatusOK,
- Message: "ok",
- Data: &pasturePb.WeightRangeData{
- CowList: model.CowSlice(cowList).WeightRangeToPB(penMap),
- WeightBarChart: &pasturePb.WeightBarChart{
- Header: header,
- Data: data,
- },
- },
- }, nil
- }
|