package backend import ( "context" "errors" "kpt-pasture/model" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "go.uber.org/zap" operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/xerr" "gorm.io/gorm" ) // GetCurrentUserName 获取当前用户名 func (s *StoreEntry) GetCurrentUserName(ctx context.Context) (string, error) { userNameInter := ctx.Value(CurrentUserName) if userNameInter == nil { return "", xerr.Customf("cannot userName") } if userName, ok := userNameInter.(string); ok { return userName, nil } else { return "", xerr.Customf("waring userName") } } func (s *StoreEntry) GetCurrentSystemUser(ctx context.Context) (*model.SystemUser, error) { // 解析token userName, err := s.GetCurrentUserName(ctx) if err != nil { return nil, xerr.WithStack(err) } // 根据用户token获取用户数据 systemUser := &model.SystemUser{Name: userName} if err = s.DB.Model(new(model.SystemUser)).Where("name = ?", userName). Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok). First(systemUser).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, xerr.Custom("当前登录用户数据不存在") } return nil, xerr.WithStack(err) } return systemUser, nil } func (s *StoreEntry) GetSystemUserById(ctx context.Context, userId int64) (*model.SystemUser, error) { systemUser := &model.SystemUser{Id: userId} if err := s.DB.Model(new(model.SystemUser)).First(systemUser).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, xerr.Customf("该系统用户数据不存在: %d", userId) } return nil, xerr.WithStack(err) } return systemUser, nil } func (s *StoreEntry) SystemDeptList(ctx context.Context) ([]*model.SystemDept, error) { deptList := make([]*model.SystemDept, 0) if err := s.DB.Model(new(model.SystemDept)). Where("is_delete = ?", pasturePb.IsShow_Ok). Find(&deptList).Error; err != nil { return nil, xerr.WithStack(err) } return deptList, nil } func (s *StoreEntry) GetPenById(ctx context.Context, penId int32) (*model.Pen, error) { penData := &model.Pen{Id: penId} if err := s.DB.Model(new(model.Pen)).First(penData).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, xerr.Customf("该栏舍数据不存在: %d", penId) } return nil, xerr.WithStack(err) } return penData, nil } func (s *StoreEntry) GetPenList(ctx context.Context) ([]*model.Pen, error) { penList := make([]*model.Pen, 0) if err := s.DB.Model(new(model.Pen)). Where("is_delete = ?", pasturePb.IsShow_Ok). Find(&penList).Error; err != nil { return nil, xerr.WithStack(err) } return penList, nil } func (s *StoreEntry) PenMap(ctx context.Context) map[int32]*model.Pen { penList, _ := s.GetPenList(ctx) penMap := make(map[int32]*model.Pen) for _, v := range penList { penMap[v.Id] = v } return penMap } func (s *StoreEntry) GetCowList(ctx context.Context, cowIds []int64) ([]*model.Cow, error) { cowList := make([]*model.Cow, 0) if err := s.DB.Model(new(model.Cow)). Where("id IN ?", cowIds). Where("admission_status = ?", pasturePb.AdmissionStatus_Admission). Find(&cowList).Error; err != nil { return nil, xerr.WithStack(err) } return cowList, nil } func (s *StoreEntry) GetCowInfoByCowId(ctx context.Context, cowId int64) (*model.Cow, error) { cowData := &model.Cow{Id: cowId} if err := s.DB.Model(new(model.Cow)). Where("admission_status = ?", pasturePb.AdmissionStatus_Admission). First(cowData).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, xerr.Customf("该牛只数据不存在: %d", cowId) } return nil, xerr.WithStack(err) } return cowData, nil } func (s *StoreEntry) GetCowInfoByCowIds(ctx context.Context, cowIds []int64) ([]*model.Cow, error) { cowList := make([]*model.Cow, 0) if err := s.DB.Model(&model.Cow{}). Where("admission_status = ?", pasturePb.AdmissionStatus_Admission). Where("id IN ?", cowIds). Find(&cowList).Error; err != nil { return nil, xerr.WithStack(err) } return cowList, nil } func (s *StoreEntry) GetTransferReasonInfo(ctx context.Context, reasonId int64) (*model.ConfigTransferPenReason, error) { configTransferPenReasonData := &model.ConfigTransferPenReason{ Id: reasonId, } if err := s.DB.Model(new(model.ConfigTransferPenReason)).First(configTransferPenReasonData).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, xerr.Customf("该转群原因数据不存在: %d", reasonId) } return nil, xerr.WithStack(err) } return configTransferPenReasonData, nil } func (s *StoreEntry) GetCowWeightByLastSecond(ctx context.Context, cowId, lastWeightAt int64) (*model.EventWeight, error) { cowWeightData := &model.EventWeight{} if err := s.DB.Model(new(model.EventWeight)).Where("cow_id = ?", cowId).Where("weight_at < ?", lastWeightAt).First(cowWeightData).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, xerr.Customf("该牛只体重数据不存在: %d", cowId) } return nil, xerr.WithStack(err) } return cowWeightData, nil } func (s *StoreEntry) GetDrugsById(ctx context.Context, id int64) (*model.Drugs, error) { drugs := &model.Drugs{Id: id} if err := s.DB.Model(new(model.Drugs)).First(drugs).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, xerr.Customf("该药品数据不存在: %d", id) } return nil, xerr.WithStack(err) } return drugs, nil } func (s *StoreEntry) DiseaseTypeList(ctx context.Context) ([]*model.ConfigDiseaseType, error) { diseaseTypeList := make([]*model.ConfigDiseaseType, 0) if err := s.DB.Model(new(model.ConfigDiseaseType)).Where("is_show = ?", pasturePb.IsShow_Ok).Find(&diseaseTypeList).Error; err != nil { return nil, xerr.WithStack(err) } return diseaseTypeList, nil } func (s *StoreEntry) GetDiseaseById(ctx context.Context, id int32) (*model.Disease, error) { newDisease := &model.Disease{} if err := s.DB.Model(new(model.Disease)).Where("id = ?", id).First(newDisease).Error; err != nil { return nil, xerr.WithStack(err) } return newDisease, nil } func (s *StoreEntry) DiseaseListByIds(ctx context.Context, ids []int32) ([]*model.Disease, error) { diseaseList := make([]*model.Disease, 0) if err := s.DB.Model(new(model.Disease)).Where("id IN ?", ids).Find(&diseaseList).Error; err != nil { return nil, xerr.WithStack(err) } return diseaseList, nil } func (s *StoreEntry) GetPrescriptionById(ctx context.Context, id int32) (*model.Prescription, error) { newPrescription := &model.Prescription{} if err := s.DB.Model(new(model.Prescription)).Where("id = ?", id). Where("is_show = ?", pasturePb.IsShow_Ok). First(&newPrescription).Error; err != nil { return nil, xerr.WithStack(err) } return newPrescription, nil } func (s *StoreEntry) PrescriptionDrugsByPrescriptionId(ctx context.Context, prescriptionId int32) ([]*model.PrescriptionDrugs, error) { newPrescriptionDrugsList := make([]*model.PrescriptionDrugs, 0) if err := s.DB.Model(new(model.PrescriptionDrugs)).Where("prescription_id = ?", prescriptionId). Where("is_show = ?", pasturePb.IsShow_Ok). Find(&newPrescriptionDrugsList).Error; err != nil { return nil, xerr.WithStack(err) } return newPrescriptionDrugsList, nil } func (s *StoreEntry) GetImmunizationById(ctx context.Context, id int64) (*model.ImmunizationPlan, error) { immunizationPlan := &model.ImmunizationPlan{Id: id} if err := s.DB.Model(new(model.ImmunizationPlan)).First(immunizationPlan).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, xerr.Customf("该免疫计划不存在: %d", id) } return nil, xerr.WithStack(err) } return immunizationPlan, nil } func (s *StoreEntry) GetWorkOrderSubByWorkOrderId(ctx context.Context, workOrderId int64) ([]*model.WorkOrderSub, error) { workOrderSubList := make([]*model.WorkOrderSub, 0) if err := s.DB.Model(new(model.WorkOrderSub)).Where("work_order_id = ?", workOrderId).Find(&workOrderSubList).Error; err != nil { return nil, xerr.WithStack(err) } return workOrderSubList, nil } func (s *StoreEntry) GetEventCowSameTimeById(ctx context.Context, id int64) (*model.EventCowSameTime, error) { eventCowSameTime := &model.EventCowSameTime{Id: id} if err := s.DB.Model(new(model.EventCowSameTime)). First(eventCowSameTime).Error; err != nil { return nil, xerr.WithStack(err) } return eventCowSameTime, nil } func (s *StoreEntry) SearchSystemDeptListByIds(ctx context.Context, ids []int64) ([]*model.SystemDept, error) { systemDeptList := make([]*model.SystemDept, 0) if err := s.DB.Model(new(model.SystemDept)). Where("is_show = ?", operationPb.IsShow_OK). Find(&systemDeptList, ids).Error; err != nil { return nil, xerr.WithStack(err) } return systemDeptList, nil } func (s *StoreEntry) GetSystemDeptListById(ctx context.Context, id int64) (*model.SystemDept, error) { systemDept := &model.SystemDept{Id: id} if err := s.DB.Model(new(model.SystemDept)).First(systemDept).Error; err != nil { return nil, xerr.WithStack(err) } return systemDept, nil } func (s *StoreEntry) GetSystemBasicByName(ctx context.Context, name string) (*model.SystemBasic, error) { systemBasic := &model.SystemBasic{} if err := s.DB.Model(new(model.SystemBasic)). Where("name = ?", name). Where("is_show = ?", pasturePb.IsShow_Ok). First(systemBasic).Error; err != nil { return nil, xerr.WithStack(err) } return systemBasic, nil } // UpdateCowPenId 更新牛舍Id func (s *StoreEntry) UpdateCowPenId(ctx context.Context, cowId, penId int64) { if penId == 0 || cowId == 0 { return } cow, err := s.GetCowInfoByCowId(ctx, cowId) if err != nil { zaplog.Error("UpdateCowPenId", zap.Any("cowId", err)) return } if err = s.DB.Model(new(model.Cow)).Where("id = ?", cow.Id).Update("pen_id", penId).Error; err != nil { zaplog.Error("UpdateCowPenId", zap.Any("cow_id", cowId), zap.Any("pen_id", penId), zap.Any("err", err)) } } func (s *StoreEntry) GetLastEventMating(ctx context.Context, cowId int64) (*model.EventMating, error) { res := &model.EventMating{} if err := s.DB.Model(new(model.EventMating)). Where("cow_id = ?", cowId). Order("reality_day desc"). First(res).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return res, nil } else { return nil, xerr.WithStack(err) } } return res, nil } func (s *StoreEntry) GetOutboundById(ctx context.Context, id int64) (*model.Outbound, error) { res := &model.Outbound{} if err := s.DB.Model(new(model.Outbound)).Where("id = ?", id).First(res).Error; err != nil { return nil, xerr.WithStack(err) } return res, nil } func (s *StoreEntry) GetOutboundLogsByOutboundId(ctx context.Context, id int64) ([]*model.OutboundLog, error) { list := make([]*model.OutboundLog, 0) if err := s.DB.Model(new(model.OutboundLog)).Where("outbound_id = ?", id).Find(&list).Error; err != nil { return nil, xerr.WithStack(err) } return list, nil } func (s *StoreEntry) GetEventMatingIsExIstByCowId( ctx context.Context, cow *model.Cow, ) (*model.EventMating, error) { newEventMating := &model.EventMating{} if err := s.DB.Model(new(model.EventMating)). Where("cow_id = ?", cow.Id). Where("lact = ?", cow.Lact). Order("id desc"). First(newEventMating).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return nil, xerr.WithStack(err) } } return newEventMating, nil } func (s *StoreEntry) GetEventPregnantCheckIsExIstByCowId( ctx context.Context, cow *model.Cow, ) (*model.EventPregnantCheck, error) { newEventPregnantCheck := &model.EventPregnantCheck{} if err := s.DB.Model(new(model.EventMating)). Where("cow_id = ?", cow.Id). Where("lact = ?", cow.Lact). Where("status = ?", pasturePb.IsShow_No). Order("id desc"). First(newEventPregnantCheck).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return nil, xerr.WithStack(err) } } return newEventPregnantCheck, nil } func (s *StoreEntry) DiseaseMaps(ctx context.Context) (map[int64]*model.Disease, error) { res := make(map[int64]*model.Disease) list := make([]*model.Disease, 0) if err := s.DB.Model(new(model.Disease)).Where("is_show = ?", pasturePb.IsShow_Ok).Find(&list).Error; err != nil { return nil, xerr.WithStack(err) } for _, v := range list { res[v.Id] = v } return res, nil }