|
@@ -4,7 +4,6 @@ import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
"kpt-pasture/model"
|
|
|
- "kpt-pasture/util"
|
|
|
"net/http"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -188,87 +187,6 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) EstrusCowList(ctx context.Context, req *pasturePb.EstrusItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventEstrusResponse, error) {
|
|
|
- userModel, err := s.GetUserModel(ctx)
|
|
|
- if err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- systemBasic, err := s.FindSystemBasic(ctx, userModel.AppPasture.Id, model.EstrusWaringDays)
|
|
|
- if err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- nowTime := time.Now()
|
|
|
- startTime := time.Unix(util.TimeParseLocalUnix(nowTime.Format(model.LayoutDate2)), 0).Format(model.LayoutTime)
|
|
|
- entTime := time.Unix(util.TimeParseLocalEndUnix(nowTime.AddDate(0, 0, 1).Format(model.LayoutDate2)), 0).Format(model.LayoutTime)
|
|
|
- var count int64
|
|
|
- neckRingEstrusList := make([]*model.NeckRingEstrusWarning, 0)
|
|
|
- pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRingEstrusWarning).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
|
|
|
- Where("b.last_mating_at < UNIX_TIMESTAMP(a.date_time)").
|
|
|
- Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
|
|
|
- Where("b.is_not_mating = ?", pasturePb.IsShow_No).
|
|
|
- Where("a.level >= ?", pasturePb.EstrusLevel_Low).
|
|
|
- Where("a.pasture_id = ?", userModel.AppPasture.Id).
|
|
|
- Where("a.date_time BETWEEN ? AND ?", startTime, entTime).
|
|
|
- Where(s.DB.Where("b.last_mating_at < UNIX_TIMESTAMP(a.first_time)").
|
|
|
- Or(s.DB.Where("b.last_mating_at = ?", 0).
|
|
|
- Where("b.calving_age > ?", systemBasic.MinValue).
|
|
|
- Or("b.lact = ?", 0))).
|
|
|
- Where("a.is_show = ?", pasturePb.IsShow_Ok)
|
|
|
-
|
|
|
- if len(req.EarNumber) > 0 {
|
|
|
- pref.Where("a.ear_number = ?", req.EarNumber)
|
|
|
- }
|
|
|
-
|
|
|
- if req.Level > 0 {
|
|
|
- pref.Where("a.level = ?", req.Level)
|
|
|
- }
|
|
|
-
|
|
|
- if len(req.PenIds) > 0 {
|
|
|
- penIds := strings.Split(util.ArrayInt32ToStrings(req.PenIds, ","), ",")
|
|
|
- pref.Where("b.pen_id IN ?", penIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err = pref.Order("a.level DESC").
|
|
|
- Count(&count).
|
|
|
- Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&neckRingEstrusList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- cowMap := make(map[int64]*model.Cow)
|
|
|
- eventLogMap := make(map[int64]string)
|
|
|
- cowIds := make([]int64, 0)
|
|
|
- for _, v := range neckRingEstrusList {
|
|
|
- cowIds = append(cowIds, v.CowId)
|
|
|
- lastEventLog := s.GetCowLastEvent(userModel.AppPasture.Id, v.CowId, pasturePb.EventCategory_Breed)
|
|
|
- if lastEventLog != nil {
|
|
|
- eventLogMap[v.CowId] = lastEventLog.EventDescription
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if len(cowIds) > 0 {
|
|
|
- cowList, _ := s.GetCowInfoByCowIds(ctx, userModel.AppPasture.Id, cowIds)
|
|
|
- for _, cow := range cowList {
|
|
|
- cowMap[cow.Id] = cow
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return &pasturePb.EventEstrusResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Msg: "ok",
|
|
|
- Data: &pasturePb.EstrusItemsData{
|
|
|
- List: model.NeckRingEstrusWarningSlice(neckRingEstrusList).ToPB(cowMap, eventLogMap),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error) {
|
|
|
userModel, err := s.GetUserModel(ctx)
|