enum_options.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. Msg: "ok",
  14. Data: s.BarnTypeEnumList(),
  15. }, nil
  16. }
  17. func (s *StoreEntry) BarnListOptions(ctx context.Context, penType int, isAll string) (*pasturePb.ConfigOptionsListResponse, error) {
  18. userModel, err := s.GetUserModel(ctx)
  19. if err != nil {
  20. return nil, xerr.WithStack(err)
  21. }
  22. penList := make([]*model.Pen, 0)
  23. pref := s.DB.Table(new(model.Pen).TableName()).
  24. Where("pasture_id = ?", userModel.AppPasture.Id).
  25. Where("is_delete = ?", pasturePb.IsShow_Ok)
  26. if penType != -1 {
  27. pref.Where("pen_type = ?", penType)
  28. }
  29. if err = pref.Find(&penList).Error; err != nil {
  30. return nil, err
  31. }
  32. return &pasturePb.ConfigOptionsListResponse{
  33. Code: http.StatusOK,
  34. Msg: "ok",
  35. Data: model.PenSlice(penList).ToPB2(s.BarnTypeEnumList(), isAll),
  36. }, nil
  37. }
  38. func (s *StoreEntry) DiseaseTypeOptions(ctx context.Context, isChildren string) (*pasturePb.ConfigOptionsListResponse, error) {
  39. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  40. pref := s.DB.Table(new(model.ConfigDiseaseType).TableName()).
  41. Where("is_show =? ", pasturePb.IsShow_Ok)
  42. if err := pref.Find(&diseaseTypeList).Error; err != nil {
  43. return nil, xerr.WithStack(err)
  44. }
  45. diseaseList := make([]*model.Disease, 0)
  46. if isChildren == model.IsAllYes {
  47. if err := s.DB.Table(new(model.Disease).TableName()).
  48. Where("is_show =? ", pasturePb.IsShow_Ok).Find(&diseaseList).Error; err != nil {
  49. return nil, xerr.WithStack(err)
  50. }
  51. }
  52. return &pasturePb.ConfigOptionsListResponse{
  53. Code: http.StatusOK,
  54. Msg: "ok",
  55. Data: model.ConfigDiseaseTypeSlice(diseaseTypeList).ToPB2(diseaseList),
  56. }, nil
  57. }
  58. func (s *StoreEntry) DiseaseOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  59. diseaseList := make([]*model.Disease, 0)
  60. pref := s.DB.Table(new(model.Disease).TableName()).
  61. Where("is_show =? ", pasturePb.IsShow_Ok)
  62. if err := pref.Find(&diseaseList).Error; err != nil {
  63. return nil, xerr.WithStack(err)
  64. }
  65. return &pasturePb.ConfigOptionsListResponse{
  66. Code: http.StatusOK,
  67. Msg: "ok",
  68. Data: model.DiseaseSlice(diseaseList).ToPB2(),
  69. }, nil
  70. }
  71. func (s *StoreEntry) PrescriptionOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  72. prescriptionList := make([]*model.Prescription, 0)
  73. pref := s.DB.Table(new(model.Prescription).TableName()).
  74. Where("is_show =? ", pasturePb.IsShow_Ok)
  75. if err := pref.Find(&prescriptionList).Error; err != nil {
  76. return nil, xerr.WithStack(err)
  77. }
  78. return &pasturePb.ConfigOptionsListResponse{
  79. Code: http.StatusOK,
  80. Msg: "ok",
  81. Data: model.PrescriptionSlice(prescriptionList).ToPB2(),
  82. }, nil
  83. }
  84. func (s *StoreEntry) BreedStatusOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  85. return &pasturePb.ConfigOptionsListResponse{
  86. Code: http.StatusOK,
  87. Msg: "ok",
  88. Data: s.BreedStatusEnumList(),
  89. }, nil
  90. }
  91. func (s *StoreEntry) CowKindOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  92. return &pasturePb.ConfigOptionsListResponse{
  93. Code: http.StatusOK,
  94. Msg: "ok",
  95. Data: s.CowKindEnumList(),
  96. }, nil
  97. }
  98. func (s *StoreEntry) CowSourceOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  99. return &pasturePb.ConfigOptionsListResponse{
  100. Code: http.StatusOK,
  101. Msg: "ok",
  102. Data: s.CowSourceEnumList(),
  103. }, nil
  104. }
  105. func (s *StoreEntry) CowTypeOptions(ctx context.Context, optionName, isAll string) (*pasturePb.ConfigOptionsListResponse, error) {
  106. return &pasturePb.ConfigOptionsListResponse{
  107. Code: http.StatusOK,
  108. Msg: "ok",
  109. Data: s.CowTypeEnumList(optionName, isAll),
  110. }, nil
  111. }
  112. func (s *StoreEntry) CowTransferPenReasonOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  113. return &pasturePb.ConfigOptionsListResponse{
  114. Code: http.StatusOK,
  115. Msg: "ok",
  116. Data: s.TransferPenEnumList(""),
  117. }, nil
  118. }
  119. func (s *StoreEntry) SystemUserOptions(ctx context.Context, depId int) (*pasturePb.ConfigOptionsListResponse, error) {
  120. _, err := s.GetUserModel(ctx)
  121. if err != nil {
  122. return nil, xerr.WithStack(err)
  123. }
  124. systemUserList := make([]*model.SystemUser, 0)
  125. pref := s.DB.Table(new(model.SystemUser).TableName()).
  126. Where("is_delete = ?", pasturePb.IsShow_Ok).
  127. Where("is_show =? ", pasturePb.IsShow_Ok)
  128. if depId != -1 && depId > 0 {
  129. pref = pref.Where("dept_id = ?", depId)
  130. }
  131. if err = pref.Find(&systemUserList).Error; err != nil {
  132. return nil, xerr.WithStack(err)
  133. }
  134. return &pasturePb.ConfigOptionsListResponse{
  135. Code: http.StatusOK,
  136. Msg: "ok",
  137. Data: model.SystemUserSlice(systemUserList).ToPB2(),
  138. }, nil
  139. }
  140. func (s *StoreEntry) BullOptions(ctx context.Context) (*pasturePb.BullOptionsListResponse, error) {
  141. userModel, err := s.GetUserModel(ctx)
  142. if err != nil {
  143. return nil, xerr.WithStack(err)
  144. }
  145. return &pasturePb.BullOptionsListResponse{
  146. Code: http.StatusOK,
  147. Msg: "ok",
  148. Data: s.BullNumberEnumList("", userModel.AppPasture.Id),
  149. }, nil
  150. }
  151. func (s *StoreEntry) SystemBaseConfigOptions(ctx context.Context, optionsName, isAll string) (*pasturePb.ConfigOptionsListResponse, error) {
  152. if optionsName == "" {
  153. return nil, xerr.New("optionsName is empty")
  154. }
  155. getConfigFuncMap := map[string]func(isAll string) []*pasturePb.ConfigOptionsList{
  156. "childNumber": s.ChildNumberEnumList,
  157. "calvingLevel": s.CalvingLevelEnumList,
  158. "dystociaReason": s.DystociaReasonEnumList,
  159. "drugCategory": s.DrugCategoryEnumList,
  160. "drugUsage": s.DrugUsageEnumList,
  161. "unit": s.UnitEnumList,
  162. "pregnantCheckResult": s.PregnantCheckResultEnumList,
  163. "pregnantCheckMethod": s.PregnantCheckMethodEnumList,
  164. "exposeEstrusType": s.ExposeEstrusTypeEnumList,
  165. "frozenSemenType": s.FrozenSemenTypeEnumList,
  166. "week": s.WeekEnumList,
  167. "month": s.MonthEnumList,
  168. "sameTimeCowType": s.SameTimeCowTypeEnumList,
  169. "sameTimeType": s.SameTimeTypeEnumList,
  170. "immunizationCowType": s.ImmunizationCowTypeEnumList,
  171. "workOrderFrequency": s.WorkOrderFrequencyEnumList,
  172. "workOrderSubUnit": s.WorkOrderSubUnitEnumList,
  173. "workOrderPriority": s.WorkOrderPriorityEnumList,
  174. "workOrderCategory": s.WorkOrderCategoryEnumList,
  175. "immunizationConditions": s.ImmunizationConditionsEnumList,
  176. "abortionReasons": s.AbortionReasonsEnumList,
  177. "healthStatus": s.HealthStatusEnumList,
  178. "calendarType": CalendarTypeEnumList,
  179. "calvingAnalysisMethod": s.CalvingAnalysisMethodEnumList,
  180. "lact": s.LactEnumList,
  181. "diseaseAnalysisMethod": s.DiseaseAnalysisMethodEnumList,
  182. "singleFactorAnalysisMethod": s.SingleFactorAnalysisMethodEnumList,
  183. "lactIntervalSymbol": s.LactIntervalSymbolEnumList,
  184. "multiFactorAnalysisMethod": s.MultiFactorAnalysisMethodEnumList,
  185. "saleCowAnalysisMethod": s.SaleCowAnalysisMethodEnumList,
  186. "neckRingStatus": s.NeckRingStatusEnumList,
  187. "outType": s.OutTypeEnumList,
  188. "auditStatus": s.AuditStatusEnumList,
  189. "pregnantCheckName": s.PregnantCheckNameEnumList,
  190. "unMatingReasons": s.UnMatingReasonsEnumList,
  191. "evenType": s.EventTypeEnumList,
  192. "departureType": s.DepartureTypeEnumList,
  193. "outReason": s.OutReasonEnumList,
  194. "deadReason": s.DeadReasonEnumList,
  195. "categoryKind": s.EventCategoryEnumList,
  196. "indicatorsDetails": s.IndicatorsDetailsList,
  197. }
  198. getConfigFunc, ok := getConfigFuncMap[optionsName]
  199. if !ok {
  200. return nil, fmt.Errorf("invalid optionsName: %s", optionsName)
  201. }
  202. configOptions := getConfigFunc(isAll)
  203. if configOptions == nil {
  204. return nil, fmt.Errorf("failed to retrieve configOptions for %s", optionsName)
  205. }
  206. return &pasturePb.ConfigOptionsListResponse{
  207. Code: http.StatusOK,
  208. Msg: "ok",
  209. Data: configOptions,
  210. }, nil
  211. }