|
@@ -1,613 +0,0 @@
|
|
|
-package backend
|
|
|
-
|
|
|
-import (
|
|
|
- "context"
|
|
|
- "fmt"
|
|
|
- "kpt-pasture/model"
|
|
|
- "net/http"
|
|
|
- "strconv"
|
|
|
- "strings"
|
|
|
-
|
|
|
- pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
|
|
|
- "gitee.com/xuyiping_admin/pkg/xerr"
|
|
|
- "gorm.io/gorm"
|
|
|
-)
|
|
|
-
|
|
|
-func (s *StoreEntry) EventTemplate(ctx context.Context, req *pasturePb.SearchEventRequest) {
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) CreateEventTemplate(ctx context.Context, req *pasturePb.SearchEventRequest) *pasturePb.EventData {
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) EnterList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchEnterEventResponse, error) {
|
|
|
- eventEnterList := make([]*pasturePb.SearchEnterData, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventEnter).TableName())).
|
|
|
- Select(`a.id,a.birth_at,a.weaning_at,a.dry_milk_at,a.cow_source_id,a.remarks,a.mating_at,a.lact,
|
|
|
- a.breed_status,a.mother_id,a.cow_kind,a.cow_id,a.ear_number,a.sex,a.created_at,a.updated_at,a.enter_at,
|
|
|
- a.weight / 100 as weight,a.price / 100 as price,b.name as breed_status_name,c.name as cow_source_name,d.name as cow_type_name,
|
|
|
- e.name as cow_kind_name,f.name as pen_name,g.name as operation_name,h.name as staff_member_name`).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS b ON a.breed_status = b.id", new(model.ConfigBreedStatus).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS c on a.cow_source_id = c.id", new(model.ConfigCowSource).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS d on a.cow_type = d.id", new(model.ConfigCowType).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS e ON a.cow_kind = e.id", new(model.ConfigCowKind).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS f on a.pen_id = f.id", new(model.Pen).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS g on a.operation_id = g.id", new(model.SystemUser).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS h on a.staff_member_id = h.id", new(model.SystemUser).TableName()))
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
- pref.Where("a.cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err := pref.Order("a.id desc").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&eventEnterList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return &pasturePb.SearchEnterEventResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SearchEnterEventData{
|
|
|
- List: eventEnterList,
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) CreateEnter(ctx context.Context, req *pasturePb.SearchEnterData) error {
|
|
|
- if err := s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- newCowData := model.NewCow(req)
|
|
|
- if err := tx.Create(newCowData).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- operationUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
- if err := tx.Create(model.NewEventEnter(newCowData.Id, operationUser.Id, req)).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) GroupTransferList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchTransferGroupEventResponse, error) {
|
|
|
- eventGroupTransferList := make([]*pasturePb.SearchTransferGroupData, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventTransferGroup).TableName())).
|
|
|
- Select(`a.id,a.cow_id,a.transfer_in_pen_id,a.transfer_out_pen_id,a.lact,a.remarks,a.transfer_reason_id,
|
|
|
- a.transfer_date,a.created_at,a.staff_member_id,b.name as transfer_in_pen_name,c.name as transfer_out_pen_name,
|
|
|
- d.name as transfer_reason_name,e.name as staff_member_name,f.lact,f.ear_number`).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS b ON a.transfer_in_pen_id = b.id", new(model.Pen).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS c on a.transfer_out_pen_id = c.id", new(model.Pen).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS d on a.transfer_reason_id = d.id", new(model.ConfigTransferPenReason).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS e ON a.staff_member_id = e.id", new(model.SystemUser).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS f ON a.cow_id = f.id", new(model.Cow).TableName()))
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
- pref.Where("a.cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err := pref.Order("a.id desc").Group("a.id").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&eventGroupTransferList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return &pasturePb.SearchTransferGroupEventResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SearchTransferGroupEventData{
|
|
|
- List: eventGroupTransferList,
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) CreateGroupTransfer(ctx context.Context, req *pasturePb.TransferGroupEventRequest) error {
|
|
|
- system, _ := s.GetCurrentSystemUser(ctx)
|
|
|
- res := make([]*model.EventTransferGroup, 0)
|
|
|
- for _, v := range req.Body {
|
|
|
- cow, err := s.GetCowInfo(ctx, int64(v.CowId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- // 转去栏舍和当前栏舍相同,则不处理
|
|
|
- if cow.PenId == int64(v.TransferInPenId) {
|
|
|
- continue
|
|
|
- }
|
|
|
- pen, err := s.GetPenInfo(ctx, int64(v.TransferInPenId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- transferReasonData, err := s.GetTransferReasonInfo(ctx, int64(v.TransferReasonId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- staffMemberData, err := s.GetSystemUserInfo(ctx, int64(v.StaffMemberId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- res = append(res, &model.EventTransferGroup{
|
|
|
- CowId: int64(v.CowId),
|
|
|
- TransferInPenId: int64(v.TransferInPenId),
|
|
|
- TransferOutPenId: pen.Id,
|
|
|
- Lact: cow.Lact,
|
|
|
- DayAge: cow.GetDayAge(),
|
|
|
- TransferDate: v.TransferDate,
|
|
|
- TransferReasonId: transferReasonData.Id,
|
|
|
- Remarks: v.Remarks,
|
|
|
- StaffMemberId: staffMemberData.Id,
|
|
|
- OperationId: system.Id,
|
|
|
- })
|
|
|
- }
|
|
|
- if len(res) <= 0 {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- if err := s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- if err := tx.Create(res).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- for _, v := range res {
|
|
|
- cow, err := s.GetCowInfo(ctx, v.CowId)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- if err = s.DB.Model(cow).Update("pen_id", v.TransferInPenId).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) BodyScoreList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchBodyScoreEventResponse, error) {
|
|
|
- bodyScoreList := make([]*pasturePb.SearchBodyScoreList, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventBodyScore).TableName())).
|
|
|
- Select(`a.id,a.cow_id,a.ear_number,a.score as body_score,a.lact,a.day_age,a.score_at,a.remarks,a.created_at,
|
|
|
- a.updated_at,a.staff_member_id,a.operation_id,b.name as staff_member_name,c.name as operation_name`).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS b on a.staff_member_id = b.id", new(model.SystemUser).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS c on a.operation_id = c.id", new(model.SystemUser).TableName()))
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
- pref.Where("a.cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err := pref.Order("a.id desc").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&bodyScoreList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return &pasturePb.SearchBodyScoreEventResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SearchBodyScoreData{
|
|
|
- List: bodyScoreList,
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) CreateBodyScore(ctx context.Context, req *pasturePb.BodyScoreEventRequest) error {
|
|
|
- if len(req.CowId) <= 0 {
|
|
|
- return xerr.Custom("请选择相关牛只")
|
|
|
- }
|
|
|
- currentSystemUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
-
|
|
|
- cowList := strings.Split(req.CowId, ",")
|
|
|
- bodyScourEvent := make([]*model.EventBodyScore, 0)
|
|
|
- for _, cowIdStr := range cowList {
|
|
|
- cowId, _ := strconv.ParseInt(cowIdStr, 10, 64)
|
|
|
- cow, err := s.GetCowInfo(ctx, cowId)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- bodyScourEvent = append(bodyScourEvent, model.NewEventBodyScore(cow, currentSystemUser.Id, req))
|
|
|
- }
|
|
|
- if len(bodyScourEvent) <= 0 {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- return s.DB.Create(bodyScourEvent).Error
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) CalvingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchLavingEventResponse, error) {
|
|
|
- lavingList := make([]*model.EventCalvingList, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCalving).TableName())).
|
|
|
- Select(`a.*,b.name as pen_name,c.name as staff_member_name`).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS b on a.pen_id = b.id", new(model.Pen).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS c on a.staff_member_id = c.id", new(model.SystemUser).TableName()))
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
- pref.Where("a.cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err := pref.Order("a.id desc").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&lavingList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- calvingIds := make([]int64, 0)
|
|
|
- for _, v := range lavingList {
|
|
|
- calvingIds = append(calvingIds, v.Id)
|
|
|
- }
|
|
|
- calvingCalfList := make([]*model.CalvingCalf, 0)
|
|
|
- if err := s.DB.Model(new(model.CalvingCalf)).Where("calving_id IN ?", calvingIds).Find(&calvingCalfList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return &pasturePb.SearchLavingEventResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SearchLavingData{
|
|
|
- List: model.EventCalvingListSlice(lavingList).ToPB(calvingCalfList),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) CalvingCreate(ctx context.Context, req *pasturePb.CalvingEventRequest) error {
|
|
|
- if len(req.CowId) <= 0 {
|
|
|
- return xerr.Custom("请选择相关牛只")
|
|
|
- }
|
|
|
-
|
|
|
- cowList := strings.Split(req.CowId, ",")
|
|
|
- cowId, _ := strconv.ParseInt(cowList[0], 10, 64)
|
|
|
- cow, err := s.GetCowInfo(ctx, cowId)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- // 母牛信息
|
|
|
- newCalving := model.NewEventCalving(cow, req)
|
|
|
- if err = tx.Create(newCalving).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- // 犊牛信息
|
|
|
- newCalvingCalfList := model.NewEventCalvingCalf(cow.Id, newCalving.Id, req)
|
|
|
- for _, v := range newCalvingCalfList {
|
|
|
- if v.IsLive == pasturePb.IsShow_No || v.IsAdoption == pasturePb.IsShow_No {
|
|
|
- continue
|
|
|
- }
|
|
|
- // 留养犊牛
|
|
|
- newCow := model.NewCalfCow(cow.Id, cow.LastBullId, v)
|
|
|
- if err = tx.Create(newCow).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- v.CowId = newCow.Id
|
|
|
- }
|
|
|
- if err = tx.Create(newCalvingCalfList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) PregnantCheckList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnantCheckEventResponse, error) {
|
|
|
- pregnantCheckList := make([]*model.EventPregnantCheck, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(new(model.EventPregnantCheck).TableName())
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
- pref.Where("cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err := pref.Order("id desc").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&pregnantCheckList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- systemUserList, _ := s.SystemUserList(ctx)
|
|
|
- pregnantCheckResult := s.PregnantCheckResultEnumList()
|
|
|
- pregnantCheckMethod := s.PregnantCheckMethodEnumList()
|
|
|
-
|
|
|
- return &pasturePb.PregnantCheckEventResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SearchPregnantCheckData{
|
|
|
- List: model.EventPregnantCheckSlice(pregnantCheckList).ToPB(systemUserList, pregnantCheckResult, pregnantCheckMethod),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) PregnantCheckCreate(ctx context.Context, req *pasturePb.PregnantCheckEventRequest) error {
|
|
|
- if len(req.CowId) <= 0 {
|
|
|
- return xerr.Custom("请选择相关牛只")
|
|
|
- }
|
|
|
- cowList := strings.Split(req.CowId, ",")
|
|
|
- eventPregnantCheckList := make([]*model.EventPregnantCheck, 0)
|
|
|
- currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
-
|
|
|
- for _, v := range cowList {
|
|
|
- cowId, _ := strconv.ParseInt(v, 10, 64)
|
|
|
- cow, err := s.GetCowInfo(ctx, cowId)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- eventPregnantCheckList = append(eventPregnantCheckList, model.NewEventPregnantCheck(cow, currentUser, req))
|
|
|
- }
|
|
|
-
|
|
|
- if err := s.DB.Create(eventPregnantCheckList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) MatingList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingEventResponse, error) {
|
|
|
- matingList := make([]*model.EventMating, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(new(model.EventMating).TableName())
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
- pref.Where("cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err := pref.Order("id desc").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&matingList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- systemUserList, _ := s.SystemUserList(ctx)
|
|
|
- exposeEstrusTypeMap := s.ExposeEstrusTypeMap()
|
|
|
- return &pasturePb.MatingEventResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SearchMatingData{
|
|
|
- List: model.EventMatingSlice(matingList).ToPB(systemUserList, exposeEstrusTypeMap),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) MatingCreate(ctx context.Context, req *pasturePb.MatingEventRequest) error {
|
|
|
- if len(req.CowId) <= 0 {
|
|
|
- return xerr.Custom("请选择相关牛只")
|
|
|
- }
|
|
|
- cowList := strings.Split(req.CowId, ",")
|
|
|
- matingList := make([]*model.EventMating, 0)
|
|
|
- eventFrozenSemenLogList := make([]*model.EventFrozenSemenLog, 0)
|
|
|
- currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
-
|
|
|
- for _, v := range cowList {
|
|
|
- cowId, _ := strconv.ParseInt(v, 10, 64)
|
|
|
- cow, err := s.GetCowInfo(ctx, cowId)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- matingList = append(matingList, model.NewEventMating(cow, currentUser, req))
|
|
|
- eventFrozenSemenLogList = append(eventFrozenSemenLogList, model.NewEventFrozenSemenLog(req.BullId, cow, int64(req.StaffMemberId)))
|
|
|
- }
|
|
|
-
|
|
|
- frozenSemen := &model.EventFrozenSemen{}
|
|
|
- if err := s.DB.Where("bull_id = ?", req.BullId).First(frozenSemen).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- if err := s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- if err := tx.Create(matingList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- // 创建冻精使用记录日志
|
|
|
- if err := tx.Create(eventFrozenSemenLogList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- if err := tx.Table(new(model.EventFrozenSemen).TableName()).
|
|
|
- Where("bull_id = ?", req.BullId).
|
|
|
- Where("quantity > 0").UpdateColumn("quantity", gorm.Expr("quantity - ?", len(matingList))).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) EstrusList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EstrusEventResponse, error) {
|
|
|
- estrusList := make([]*model.EventEstrus, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(new(model.EventEstrus).TableName())
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
- pref.Where("cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err := pref.Order("id desc").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&estrusList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- systemUserList, _ := s.SystemUserList(ctx)
|
|
|
- return &pasturePb.EstrusEventResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SearchEstrusData{
|
|
|
- List: model.EstrusSlice(estrusList).ToPB(systemUserList),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) EstrusCreate(ctx context.Context, req *pasturePb.EstrusEventRequest) error {
|
|
|
- if len(req.CowId) <= 0 {
|
|
|
- return xerr.Custom("请选择相关牛只")
|
|
|
- }
|
|
|
- cowList := strings.Split(req.CowId, ",")
|
|
|
- estrusList := make([]*model.EventEstrus, 0)
|
|
|
- currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
-
|
|
|
- for _, v := range cowList {
|
|
|
- cowId, _ := strconv.ParseInt(v, 10, 64)
|
|
|
- cow, err := s.GetCowInfo(ctx, cowId)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- estrusList = append(estrusList, model.NewEventEstrus(cow, currentUser, req))
|
|
|
- }
|
|
|
-
|
|
|
- if err := s.DB.Create(estrusList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) FrozenSemenList(ctx context.Context, req *pasturePb.FrozenSemenRequest, pagination *pasturePb.PaginationModel) (*pasturePb.FrozenSemenResponse, error) {
|
|
|
- frozenSemenList := make([]*model.EventFrozenSemen, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(new(model.EventFrozenSemen).TableName())
|
|
|
- if req.BullId != "" {
|
|
|
- pref.Where("bull_id = ?", req.BullId)
|
|
|
- }
|
|
|
-
|
|
|
- if req.Producer != "" {
|
|
|
- pref.Where("producer = ?", req.Producer)
|
|
|
- }
|
|
|
-
|
|
|
- if err := pref.Order("id desc").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&frozenSemenList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- frozenSemenTypeMap := s.FrozenSemenTypeMap()
|
|
|
- unitMap := s.UnitMap()
|
|
|
-
|
|
|
- return &pasturePb.FrozenSemenResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SearchFrozenSemenData{
|
|
|
- List: model.EventFrozenSemenSlice(frozenSemenList).ToPB(frozenSemenTypeMap, unitMap),
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) FrozenSemenCreate(ctx context.Context, req *pasturePb.SearchFrozenSemenList) error {
|
|
|
- currentUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
- req.CowKindName = s.CowKindMap()[req.CowKind]
|
|
|
- newFrozenSemen := model.NewEventFrozenSemen(req, currentUser)
|
|
|
- if err := s.DB.Create(newFrozenSemen).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) WeightList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchWeightEventResponse, error) {
|
|
|
- weightList := make([]*pasturePb.SearchWeightList, 0)
|
|
|
- var count int64 = 0
|
|
|
- pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeight).TableName())).
|
|
|
- Select(`a.id,a.cow_id,a.ear_number,a.weight / 100 as weight,a.lact,a.day_age,a.weight_at,a.remarks,a.created_at,
|
|
|
- a.updated_at,a.staff_member_id,a.operation_id,b.name as staff_member_name,c.name as operation_name`).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS b on a.staff_member_id = b.id", new(model.SystemUser).TableName())).
|
|
|
- Joins(fmt.Sprintf("JOIN %s AS c on a.operation_id = c.id", new(model.SystemUser).TableName()))
|
|
|
- if len(req.CowId) > 0 {
|
|
|
- cowIds := strings.Split(req.CowId, ",")
|
|
|
- pref.Where("a.cow_id IN ?", cowIds)
|
|
|
- }
|
|
|
-
|
|
|
- if err := pref.Order("a.id desc").
|
|
|
- Count(&count).Limit(int(pagination.PageSize)).
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
- Find(&weightList).Error; err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- return &pasturePb.SearchWeightEventResponse{
|
|
|
- Code: http.StatusOK,
|
|
|
- Message: "ok",
|
|
|
- Data: &pasturePb.SearchWeightData{
|
|
|
- List: weightList,
|
|
|
- Total: int32(count),
|
|
|
- PageSize: pagination.PageSize,
|
|
|
- Page: pagination.Page,
|
|
|
- },
|
|
|
- }, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (s *StoreEntry) CreateWeight(ctx context.Context, req *pasturePb.WeightEventRequest) error {
|
|
|
- if len(req.CowId) <= 0 {
|
|
|
- return xerr.Custom("请选择相关牛只")
|
|
|
- }
|
|
|
- currentSystemUser, _ := s.GetCurrentSystemUser(ctx)
|
|
|
- weightEvent := make([]*model.EventWeight, 0)
|
|
|
- for _, item := range req.WeightItems {
|
|
|
- cow, err := s.GetCowInfo(ctx, int64(item.CowId))
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- var weight = int32(item.Weight * 100)
|
|
|
- weightEvent = append(weightEvent, model.NewEventWeight(cow, currentSystemUser, weight, req))
|
|
|
- }
|
|
|
- if len(weightEvent) <= 0 {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- if err := s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- for _, item := range weightEvent {
|
|
|
- if err := tx.Model(new(model.Cow)).Where("id = ?", item.CowId).Updates(map[string]interface{}{
|
|
|
- "last_weight_at": item.WeightAt,
|
|
|
- "current_weight": item.Weight,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- }
|
|
|
- if err := tx.Create(weightEvent).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
- }); err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- return nil
|
|
|
-}
|