|
@@ -6,6 +6,7 @@ import (
|
|
|
"kpt-pasture/model"
|
|
|
"kpt-pasture/util"
|
|
|
"net/http"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
@@ -133,16 +134,25 @@ func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.EventCalv
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusEventResponse, error) {
|
|
|
+func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.EstrusItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusItemsResponse, error) {
|
|
|
estrusList := make([]*model.EventEstrus, 0)
|
|
|
var count int64 = 0
|
|
|
pref := s.DB.Table(new(model.EventEstrus).TableName()).
|
|
|
Where("is_show = ?", pasturePb.IsShow_Ok)
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
+ if len(req.CowIds) > 0 {
|
|
|
+ cowIds := strings.Split(util.ArrayInt32ToStrings(req.CowIds, ","), ",")
|
|
|
pref.Where("cow_id IN ?", cowIds)
|
|
|
}
|
|
|
|
|
|
+ if req.Level > 0 {
|
|
|
+ pref.Where("level = ?", req.Level)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(req.PenIds) > 0 {
|
|
|
+ penIds := strings.Split(util.ArrayInt32ToStrings(req.PenIds, ","), ",")
|
|
|
+ pref.Where("pen_id IN ?", penIds)
|
|
|
+ }
|
|
|
+
|
|
|
if err := pref.Order("id desc").
|
|
|
Count(&count).Limit(int(pagination.PageSize)).
|
|
|
Offset(int(pagination.PageOffset)).
|
|
@@ -150,11 +160,13 @@ func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.SearchEventR
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- return &pasturePb.EstrusEventResponse{
|
|
|
+ getCowInfoByCowId := GetCowInfoByCowId
|
|
|
+ getCowLastEvent := GetCowLastEvent
|
|
|
+ return &pasturePb.EstrusItemsResponse{
|
|
|
Code: http.StatusOK,
|
|
|
Message: "ok",
|
|
|
- Data: &pasturePb.SearchEstrusData{
|
|
|
- List: model.EstrusSlice(estrusList).ToPB(),
|
|
|
+ Data: &pasturePb.EstrusItemsData{
|
|
|
+ List: model.EstrusSlice(estrusList).ToPB(s.DB, getCowInfoByCowId, getCowLastEvent),
|
|
|
Total: int32(count),
|
|
|
PageSize: pagination.PageSize,
|
|
|
Page: pagination.Page,
|
|
@@ -162,53 +174,60 @@ func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.SearchEventR
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) EstrusCreate(ctx context.Context, req *pasturePb.EventEstrus) error {
|
|
|
- estrusInfo, err := s.EventEstrusCheck(ctx, req)
|
|
|
+func (s *StoreEntry) EstrusBatch(ctx context.Context, req *pasturePb.EventEstrus) error {
|
|
|
+ eventMatingList := make([]*model.EventMating, 0)
|
|
|
+ currentUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
if err != nil {
|
|
|
- return xerr.Custom("获取当前登录用户失败")
|
|
|
+ return xerr.Custom("当前用户信息错误")
|
|
|
+ }
|
|
|
+ operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.Customf("该用户不存在: %d", req.OperationId)
|
|
|
}
|
|
|
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
|
|
|
- // 发情信息
|
|
|
- if err = tx.Create(estrusInfo).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+ eventEstrusIds := make([]string, 0)
|
|
|
+ for _, cowId := range req.CowIds {
|
|
|
+ cowInfo := GetCowInfoByCowId(s.DB, int64(cowId))
|
|
|
+ if cowInfo == nil {
|
|
|
+ return xerr.Custom("牛只信息不存在")
|
|
|
}
|
|
|
|
|
|
- // 配种信息
|
|
|
- if estrusInfo.IsMating == pasturePb.IsShow_Ok {
|
|
|
- cow, _ := s.GetCowInfoByCowId(ctx, int64(req.CowId))
|
|
|
- planAt, _ := time.Parse(model.LayoutTime, estrusInfo.EstrusDate)
|
|
|
- newEventMating := model.NewEventMating(cow, planAt.Unix(), estrusInfo.ExposeEstrusType)
|
|
|
- if err = tx.Create(newEventMating).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
+ newEventMating := model.NewEventMating(cowInfo, time.Now().Unix(), pasturePb.ExposeEstrusType_Neck_Ring)
|
|
|
+ eventEstrus, ok := s.EventEstrusIsExist(ctx, int64(cowId))
|
|
|
+ if ok {
|
|
|
+ newEventMating.EventEstrusId = eventEstrus.Id
|
|
|
+ newEventMating.MatingTimes = cowInfo.MatingTimes + 1
|
|
|
+ newEventMating.Status = pasturePb.IsShow_Ok
|
|
|
+ newEventMating.RealityDay = int64(req.MatingAt)
|
|
|
+ newEventMating.OperationId = operationUser.Id
|
|
|
+ newEventMating.OperationName = operationUser.Name
|
|
|
+ newEventMating.FrozenSemenNumber = req.BullNumber
|
|
|
+ newEventMating.Remarks = req.Remarks
|
|
|
+ newEventMating.MessageId = currentUser.Id
|
|
|
+ newEventMating.MessageName = currentUser.Name
|
|
|
+ eventEstrusIds = append(eventEstrusIds, strconv.FormatInt(eventEstrus.Id, 10))
|
|
|
}
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+ eventMatingList = append(eventMatingList, newEventMating)
|
|
|
}
|
|
|
- return nil
|
|
|
-}
|
|
|
|
|
|
-func (s *StoreEntry) EstrusBatch(ctx context.Context, req *pasturePb.EventEstrusBatch) error {
|
|
|
- estrusList := make([]*model.EventEstrus, 0)
|
|
|
- eventMatingList := make([]*model.EventMating, 0)
|
|
|
- for _, v := range req.Item {
|
|
|
- estrusInfo, err := s.EventEstrusCheck(ctx, v)
|
|
|
- if err != nil {
|
|
|
- return xerr.Custom("获取当前登录用户失败")
|
|
|
- }
|
|
|
- estrusList = append(estrusList, estrusInfo)
|
|
|
- cow, _ := s.GetCowInfoByCowId(ctx, int64(v.CowId))
|
|
|
- planAt, _ := time.Parse(model.LayoutTime, estrusInfo.EstrusDate)
|
|
|
- eventMatingList = append(eventMatingList, model.NewEventMating(cow, planAt.Unix(), estrusInfo.ExposeEstrusType))
|
|
|
+ if len(eventMatingList) <= 0 {
|
|
|
+ return nil
|
|
|
}
|
|
|
- if err := s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- if err := tx.Create(estrusList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+
|
|
|
+ if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ if len(eventEstrusIds) > 0 {
|
|
|
+ if err = tx.Model(new(model.EventEstrus)).
|
|
|
+ Where("id IN ?", eventEstrusIds).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "is_show": pasturePb.IsShow_No,
|
|
|
+ "result": pasturePb.EstrusResult_Correct,
|
|
|
+ }).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
if len(eventMatingList) > 0 {
|
|
|
- if err := tx.Create(eventMatingList).Error; err != nil {
|
|
|
+ if err = tx.Create(eventMatingList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|