123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- 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) ParseCowIds(ctx context.Context, cowIds string) ([]*model.Cow, error) {
- if len(cowIds) == 0 {
- return nil, xerr.Custom("cow id is required")
- }
- cowIdStr := strings.Split(cowIds, ",")
- var cowIdInts = make([]int64, 0)
- for _, v := range cowIdStr {
- cowId, err := strconv.ParseInt(v, 10, 64)
- if err != nil {
- return nil, xerr.Customf("错误的牛号: %s", v)
- }
- cowIdInts = append(cowIdInts, cowId)
- }
- cowList, err := s.GetCowInfoByCowIds(ctx, cowIdInts)
- if err != nil {
- return nil, xerr.WithStack(err)
- }
- return cowList, nil
- }
- func (s *StoreEntry) EnterList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchEnterEventResponse, error) {
- eventEnterList := make([]*model.EventEnter, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.EventEnter))
- 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(&eventEnterList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- penMap := s.PenMap(ctx)
- breedStatusMap := s.CowBreedStatusMap()
- cowSourceMap := s.CowSourceMap()
- cowTypeMap := s.CowTypeMap()
- cowKindMap := s.CowKindMap()
- return &pasturePb.SearchEnterEventResponse{
- Code: http.StatusOK,
- Message: "ok",
- Data: &pasturePb.SearchEnterEventData{
- List: model.EventEnterSlice(eventEnterList).ToPB(penMap, breedStatusMap, cowSourceMap, cowTypeMap, cowKindMap),
- Total: int32(count),
- PageSize: pagination.PageSize,
- Page: pagination.Page,
- },
- }, nil
- }
- func (s *StoreEntry) CreateEnter(ctx context.Context, req *pasturePb.EventEnterRequest) error {
- currentUser, err := s.GetCurrentSystemUser(ctx)
- if err != nil || currentUser.Id <= 0 {
- return xerr.Customf("当前用户信息错误")
- }
- req.MessengerId = int32(currentUser.Id)
- req.MessengerName = currentUser.Name
- if req.OperationId > 0 {
- systemUser, _ := s.GetSystemUserById(ctx, int64(req.OperationId))
- req.OperationName = systemUser.Name
- }
- newCowData := model.NewCow(req)
- newEventEnter := model.NewEventEnter(newCowData.Id, req)
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
- if err = tx.Create(newCowData).Error; err != nil {
- return xerr.WithStack(err)
- }
- if err = tx.Create(newEventEnter).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.EventTransferGroupData, 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.pen_in_id as transfer_in_pen_id,a.pen_out_id as transfer_out_pen_id,a.lact,a.remarks,a.transfer_reason_id,a.transfer_reason_name,
- a.transfer_date,a.created_at,a.operation_id,a.operation_name,b.name as transfer_in_pen_name,c.name as transfer_out_pen_name,
- f.lact,f.ear_number`).
- Joins(fmt.Sprintf("JOIN %s AS b ON a.pen_in_id = b.id", new(model.Pen).TableName())).
- Joins(fmt.Sprintf("JOIN %s AS c on a.pen_out_id = c.id", new(model.Pen).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 {
- currentUser, err := s.GetCurrentSystemUser(ctx)
- if err != nil || currentUser.Id <= 0 {
- return xerr.Customf("当前用户信息错误")
- }
- newEventTransferGroupList := make([]*model.EventTransferGroup, 0)
- for _, v := range req.Body {
- cow, err := s.GetCowInfoByCowId(ctx, int64(v.CowId))
- if err != nil {
- return xerr.WithStack(err)
- }
- // 转去栏舍和当前栏舍相同,则不处理
- if cow.PenId == v.TransferInPenId {
- continue
- }
- operationUser, err := s.GetSystemUserById(ctx, int64(v.OperationId))
- if err != nil {
- return xerr.WithStack(err)
- }
- newEventTransferGroup := model.NewEventTransferGroup(cow, v, currentUser, operationUser)
- newEventTransferGroupList = append(newEventTransferGroupList, newEventTransferGroup)
- }
- if len(newEventTransferGroupList) <= 0 {
- return nil
- }
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
- if err = tx.Create(newEventTransferGroupList).Error; err != nil {
- return xerr.WithStack(err)
- }
- for _, v := range newEventTransferGroupList {
- cow, err := s.GetCowInfoByCowId(ctx, v.CowId)
- if err != nil {
- return xerr.WithStack(err)
- }
- if err = s.DB.Model(cow).Update("pen_id", v.PenInId).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.BodyScoreList, 0)
- var count int64 = 0
- pref := s.DB.Model(new(model.EventBodyScore)).Select("*,score as body_score")
- 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(&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, err := s.GetCurrentSystemUser(ctx)
- if err != nil {
- return xerr.Customf("获取当前用户失败: %s", err.Error())
- }
- operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
- if err != nil {
- return xerr.WithStack(err)
- }
- req.OperationName = operationUser.Name
- bodyScourEvent := make([]*model.EventBodyScore, 0)
- cowList, err := s.ParseCowIds(ctx, req.CowId)
- if err != nil {
- return xerr.WithStack(err)
- }
- for _, cow := range cowList {
- bodyScourEvent = append(bodyScourEvent, model.NewEventBodyScore(cow, currentSystemUser, req))
- }
- if len(bodyScourEvent) <= 0 {
- return nil
- }
- return s.DB.Create(bodyScourEvent).Error
- }
- 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.message_id,a.operation_id,a.message_name,a.operation_name`)
- 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) WeightBatch(ctx context.Context, req *pasturePb.EventWeight) error {
- if len(req.WeightItems) <= 0 {
- return xerr.Custom("请选择相关牛只")
- }
- currentUser, err := s.GetCurrentSystemUser(ctx)
- if err != nil {
- return xerr.Customf("获取当前登录用户失败: %s", err.Error())
- }
- operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
- if err != nil {
- return xerr.WithStack(err)
- }
- req.OperationName = operationUser.Name
- weightEvent := make([]*model.EventWeight, 0)
- for _, item := range req.WeightItems {
- cow, err := s.GetCowInfoByCowId(ctx, int64(item.CowId))
- if err != nil {
- return xerr.WithStack(err)
- }
- var weight = int32(item.Weight * 100)
- weightEvent = append(weightEvent, model.NewEventWeight(cow, currentUser, weight, item.Height, 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).
- Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
- Updates(map[string]interface{}{
- "last_second_weight_at": gorm.Expr("last_weight_at"),
- "last_second_weight": gorm.Expr("current_weight"),
- "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
- }
|