enum_options.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "net/http"
  7. "gitee.com/xuyiping_admin/pkg/xerr"
  8. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  9. )
  10. func (s *StoreEntry) BarnTypeOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  11. return &pasturePb.ConfigOptionsListResponse{
  12. Code: http.StatusOK,
  13. Message: "ok",
  14. Data: s.BarnTypeEnumList(),
  15. }, nil
  16. }
  17. func (s *StoreEntry) BarnListOptions(ctx context.Context, penType int) (*pasturePb.ConfigOptionsListResponse, error) {
  18. penList := make([]*model.Pen, 0)
  19. pref := s.DB.Table(new(model.Pen).TableName()).
  20. Where("is_delete = ?", pasturePb.IsShow_Ok)
  21. if penType != -1 {
  22. pref.Where("pen_type = ?", penType)
  23. }
  24. if err := pref.Find(&penList).Error; err != nil {
  25. return nil, err
  26. }
  27. return &pasturePb.ConfigOptionsListResponse{
  28. Code: http.StatusOK,
  29. Message: "ok",
  30. Data: model.PenSlice(penList).ToPB2(s.BarnTypeEnumList()),
  31. }, nil
  32. }
  33. func (s *StoreEntry) DiseaseTypeOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  34. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  35. pref := s.DB.Table(new(model.ConfigDiseaseType).TableName()).
  36. Where("is_show =? ", pasturePb.IsShow_Ok)
  37. if err := pref.Find(&diseaseTypeList).Error; err != nil {
  38. return nil, xerr.WithStack(err)
  39. }
  40. return &pasturePb.ConfigOptionsListResponse{
  41. Code: http.StatusOK,
  42. Message: "ok",
  43. Data: model.ConfigDiseaseTypeSlice(diseaseTypeList).ToPB2(),
  44. }, nil
  45. }
  46. func (s *StoreEntry) DiseaseOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  47. diseaseList := make([]*model.Disease, 0)
  48. pref := s.DB.Table(new(model.Disease).TableName()).
  49. Where("is_show =? ", pasturePb.IsShow_Ok)
  50. if err := pref.Find(&diseaseList).Error; err != nil {
  51. return nil, xerr.WithStack(err)
  52. }
  53. return &pasturePb.ConfigOptionsListResponse{
  54. Code: http.StatusOK,
  55. Message: "ok",
  56. Data: model.DiseaseSlice(diseaseList).ToPB2(),
  57. }, nil
  58. }
  59. func (s *StoreEntry) BreedStatusOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  60. return &pasturePb.ConfigOptionsListResponse{
  61. Code: http.StatusOK,
  62. Message: "ok",
  63. Data: s.BreedStatusEnumList(),
  64. }, nil
  65. }
  66. func (s *StoreEntry) CowKindOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  67. return &pasturePb.ConfigOptionsListResponse{
  68. Code: http.StatusOK,
  69. Message: "ok",
  70. Data: s.CowKindEnumList(),
  71. }, nil
  72. }
  73. func (s *StoreEntry) CowSourceOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  74. return &pasturePb.ConfigOptionsListResponse{
  75. Code: http.StatusOK,
  76. Message: "ok",
  77. Data: s.CowSourceEnumList(),
  78. }, nil
  79. }
  80. func (s *StoreEntry) CowTypeOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  81. return &pasturePb.ConfigOptionsListResponse{
  82. Code: http.StatusOK,
  83. Message: "ok",
  84. Data: s.CowTypeEnumList(),
  85. }, nil
  86. }
  87. func (s *StoreEntry) CowTransferPenReasonOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  88. return &pasturePb.ConfigOptionsListResponse{
  89. Code: http.StatusOK,
  90. Message: "ok",
  91. Data: s.TransferPenEnumList(),
  92. }, nil
  93. }
  94. func (s *StoreEntry) SystemUserOptions(ctx context.Context, depId int) (*pasturePb.ConfigOptionsListResponse, error) {
  95. systemUserList := make([]*model.SystemUser, 0)
  96. pref := s.DB.Table(new(model.SystemUser).TableName()).
  97. Where("is_delete = ?", pasturePb.IsShow_Ok).
  98. Where("is_show =? ", pasturePb.IsShow_Ok)
  99. if depId != -1 && depId > 0 {
  100. pref = pref.Where("dept_id = ?", depId)
  101. }
  102. if err := pref.Find(&systemUserList).Error; err != nil {
  103. return nil, xerr.WithStack(err)
  104. }
  105. return &pasturePb.ConfigOptionsListResponse{
  106. Code: http.StatusOK,
  107. Message: "ok",
  108. Data: model.SystemUserSlice(systemUserList).ToPB2(),
  109. }, nil
  110. }
  111. func (s *StoreEntry) BullOptions(ctx context.Context) (*pasturePb.BullOptionsListResponse, error) {
  112. return &pasturePb.BullOptionsListResponse{
  113. Code: http.StatusOK,
  114. Message: "ok",
  115. Data: s.BullNumberEnumList(),
  116. }, nil
  117. }
  118. func (s *StoreEntry) SystemBaseConfigOptions(ctx context.Context, optionsName string) (*pasturePb.ConfigOptionsListResponse, error) {
  119. if optionsName == "" {
  120. return nil, xerr.New("optionsName is empty")
  121. }
  122. getConfigFuncMap := map[string]func() []*pasturePb.ConfigOptionsList{
  123. "childNumber": s.ChildNumberEnumList,
  124. "calvingLevel": s.CalvingLevelEnumList,
  125. "dystociaReason": s.DystociaReasonEnumList,
  126. "drugCategory": s.DrugCategoryEnumList,
  127. "drugUsage": s.DrugUsageEnumList,
  128. "unit": s.UnitEnumList,
  129. "pregnantCheckResult": s.PregnantCheckResultEnumList,
  130. "pregnantCheckMethod": s.PregnantCheckMethodEnumList,
  131. "exposeEstrusType": s.ExposeEstrusTypeEnumList,
  132. "frozenSemenType": s.FrozenSemenTypeEnumList,
  133. "week": s.WeekEnumList,
  134. "month": s.MonthEnumList,
  135. "sameTimeCowType": s.SemeTimeCowTypeEnumList,
  136. "sameTimeType": s.SameTimeTypeEnumList,
  137. "immunizationCowType": s.ImmunizationCowTypeEnumList,
  138. "workOrderFrequency": s.WorkOrderFrequencyEnumList,
  139. "workOrderSubUnit": s.WorkOrderSubUnitEnumList,
  140. "workOrderPriority": s.WorkOrderPriorityEnumList,
  141. "workOrderCategory": s.WorkOrderCategoryEnumList,
  142. "immunizationConditions": s.ImmunizationConditionsEnumList,
  143. }
  144. getConfigFunc, ok := getConfigFuncMap[optionsName]
  145. if !ok {
  146. return nil, fmt.Errorf("invalid optionsName: %s", optionsName)
  147. }
  148. configOptions := getConfigFunc()
  149. if configOptions == nil {
  150. return nil, fmt.Errorf("failed to retrieve configOptions for %s", optionsName)
  151. }
  152. return &pasturePb.ConfigOptionsListResponse{
  153. Code: http.StatusOK,
  154. Message: "ok",
  155. Data: configOptions,
  156. }, nil
  157. }