enum_options.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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) PrescriptionOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  60. prescriptionList := make([]*model.Prescription, 0)
  61. pref := s.DB.Table(new(model.Prescription).TableName()).
  62. Where("is_show =? ", pasturePb.IsShow_Ok)
  63. if err := pref.Find(&prescriptionList).Error; err != nil {
  64. return nil, xerr.WithStack(err)
  65. }
  66. return &pasturePb.ConfigOptionsListResponse{
  67. Code: http.StatusOK,
  68. Message: "ok",
  69. Data: model.PrescriptionSlice(prescriptionList).ToPB2(),
  70. }, nil
  71. }
  72. func (s *StoreEntry) BreedStatusOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  73. return &pasturePb.ConfigOptionsListResponse{
  74. Code: http.StatusOK,
  75. Message: "ok",
  76. Data: s.BreedStatusEnumList(),
  77. }, nil
  78. }
  79. func (s *StoreEntry) CowKindOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  80. return &pasturePb.ConfigOptionsListResponse{
  81. Code: http.StatusOK,
  82. Message: "ok",
  83. Data: s.CowKindEnumList(),
  84. }, nil
  85. }
  86. func (s *StoreEntry) CowSourceOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  87. return &pasturePb.ConfigOptionsListResponse{
  88. Code: http.StatusOK,
  89. Message: "ok",
  90. Data: s.CowSourceEnumList(),
  91. }, nil
  92. }
  93. func (s *StoreEntry) CowTypeOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  94. return &pasturePb.ConfigOptionsListResponse{
  95. Code: http.StatusOK,
  96. Message: "ok",
  97. Data: s.CowTypeEnumList(),
  98. }, nil
  99. }
  100. func (s *StoreEntry) CowTransferPenReasonOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  101. return &pasturePb.ConfigOptionsListResponse{
  102. Code: http.StatusOK,
  103. Message: "ok",
  104. Data: s.TransferPenEnumList(),
  105. }, nil
  106. }
  107. func (s *StoreEntry) SystemUserOptions(ctx context.Context, depId int) (*pasturePb.ConfigOptionsListResponse, error) {
  108. systemUserList := make([]*model.SystemUser, 0)
  109. pref := s.DB.Table(new(model.SystemUser).TableName()).
  110. Where("is_delete = ?", pasturePb.IsShow_Ok).
  111. Where("is_show =? ", pasturePb.IsShow_Ok)
  112. if depId != -1 && depId > 0 {
  113. pref = pref.Where("dept_id = ?", depId)
  114. }
  115. if err := pref.Find(&systemUserList).Error; err != nil {
  116. return nil, xerr.WithStack(err)
  117. }
  118. return &pasturePb.ConfigOptionsListResponse{
  119. Code: http.StatusOK,
  120. Message: "ok",
  121. Data: model.SystemUserSlice(systemUserList).ToPB2(),
  122. }, nil
  123. }
  124. func (s *StoreEntry) BullOptions(ctx context.Context) (*pasturePb.BullOptionsListResponse, error) {
  125. return &pasturePb.BullOptionsListResponse{
  126. Code: http.StatusOK,
  127. Message: "ok",
  128. Data: s.BullNumberEnumList(),
  129. }, nil
  130. }
  131. func (s *StoreEntry) SystemBaseConfigOptions(ctx context.Context, optionsName string) (*pasturePb.ConfigOptionsListResponse, error) {
  132. if optionsName == "" {
  133. return nil, xerr.New("optionsName is empty")
  134. }
  135. getConfigFuncMap := map[string]func() []*pasturePb.ConfigOptionsList{
  136. "childNumber": s.ChildNumberEnumList,
  137. "calvingLevel": s.CalvingLevelEnumList,
  138. "dystociaReason": s.DystociaReasonEnumList,
  139. "drugCategory": s.DrugCategoryEnumList,
  140. "drugUsage": s.DrugUsageEnumList,
  141. "unit": s.UnitEnumList,
  142. "pregnantCheckResult": s.PregnantCheckResultEnumList,
  143. "pregnantCheckMethod": s.PregnantCheckMethodEnumList,
  144. "exposeEstrusType": s.ExposeEstrusTypeEnumList,
  145. "frozenSemenType": s.FrozenSemenTypeEnumList,
  146. "week": s.WeekEnumList,
  147. "month": s.MonthEnumList,
  148. "sameTimeCowType": s.SameTimeCowTypeEnumList,
  149. "sameTimeType": s.SameTimeTypeEnumList,
  150. "immunizationCowType": s.ImmunizationCowTypeEnumList,
  151. "workOrderFrequency": s.WorkOrderFrequencyEnumList,
  152. "workOrderSubUnit": s.WorkOrderSubUnitEnumList,
  153. "workOrderPriority": s.WorkOrderPriorityEnumList,
  154. "workOrderCategory": s.WorkOrderCategoryEnumList,
  155. "immunizationConditions": s.ImmunizationConditionsEnumList,
  156. "abortionReasons": s.AbortionReasonsEnumList,
  157. "healthStatus": s.HealthStatusEnumList,
  158. "calendarType": CalendarTypeEnumList,
  159. }
  160. getConfigFunc, ok := getConfigFuncMap[optionsName]
  161. if !ok {
  162. return nil, fmt.Errorf("invalid optionsName: %s", optionsName)
  163. }
  164. configOptions := getConfigFunc()
  165. if configOptions == nil {
  166. return nil, fmt.Errorf("failed to retrieve configOptions for %s", optionsName)
  167. }
  168. return &pasturePb.ConfigOptionsListResponse{
  169. Code: http.StatusOK,
  170. Message: "ok",
  171. Data: configOptions,
  172. }, nil
  173. }