enum_options.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. userModel, err := s.GetUserModel(ctx)
  60. if err != nil {
  61. return nil, xerr.WithStack(err)
  62. }
  63. diseaseList := make([]*model.Disease, 0)
  64. pref := s.DB.Table(new(model.Disease).TableName()).
  65. Where("is_show =? ", pasturePb.IsShow_Ok).
  66. Where("pasture_id =? ", userModel.AppPasture.Id)
  67. if err = pref.Find(&diseaseList).Error; err != nil {
  68. return nil, xerr.WithStack(err)
  69. }
  70. return &pasturePb.ConfigOptionsListResponse{
  71. Code: http.StatusOK,
  72. Msg: "ok",
  73. Data: model.DiseaseSlice(diseaseList).ToPB2(),
  74. }, nil
  75. }
  76. func (s *StoreEntry) PrescriptionOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  77. userModel, err := s.GetUserModel(ctx)
  78. if err != nil {
  79. return nil, xerr.WithStack(err)
  80. }
  81. prescriptionList := make([]*model.Prescription, 0)
  82. pref := s.DB.Table(new(model.Prescription).TableName()).
  83. Where("pasture_id = ? ", userModel.AppPasture.Id).
  84. Where("is_show = ? ", pasturePb.IsShow_Ok)
  85. if err = pref.Find(&prescriptionList).Error; err != nil {
  86. return nil, xerr.WithStack(err)
  87. }
  88. return &pasturePb.ConfigOptionsListResponse{
  89. Code: http.StatusOK,
  90. Msg: "ok",
  91. Data: model.PrescriptionSlice(prescriptionList).ToPB2(),
  92. }, nil
  93. }
  94. func (s *StoreEntry) BreedStatusOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  95. return &pasturePb.ConfigOptionsListResponse{
  96. Code: http.StatusOK,
  97. Msg: "ok",
  98. Data: s.BreedStatusEnumList(),
  99. }, nil
  100. }
  101. func (s *StoreEntry) CowKindOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  102. return &pasturePb.ConfigOptionsListResponse{
  103. Code: http.StatusOK,
  104. Msg: "ok",
  105. Data: s.CowKindEnumList(),
  106. }, nil
  107. }
  108. func (s *StoreEntry) CowSourceOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  109. return &pasturePb.ConfigOptionsListResponse{
  110. Code: http.StatusOK,
  111. Msg: "ok",
  112. Data: s.CowSourceEnumList(),
  113. }, nil
  114. }
  115. func (s *StoreEntry) CowTypeOptions(ctx context.Context, optionName, isAll string) (*pasturePb.ConfigOptionsListResponse, error) {
  116. return &pasturePb.ConfigOptionsListResponse{
  117. Code: http.StatusOK,
  118. Msg: "ok",
  119. Data: s.CowTypeEnumList(optionName, isAll),
  120. }, nil
  121. }
  122. func (s *StoreEntry) CowTransferPenReasonOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  123. return &pasturePb.ConfigOptionsListResponse{
  124. Code: http.StatusOK,
  125. Msg: "ok",
  126. Data: s.TransferPenEnumList(""),
  127. }, nil
  128. }
  129. func (s *StoreEntry) SystemUserOptions(ctx context.Context, depId int) (*pasturePb.ConfigOptionsListResponse, error) {
  130. _, err := s.GetUserModel(ctx)
  131. if err != nil {
  132. return nil, xerr.WithStack(err)
  133. }
  134. systemUserList := make([]*model.SystemUser, 0)
  135. pref := s.DB.Table(new(model.SystemUser).TableName()).
  136. Where("is_delete = ?", pasturePb.IsShow_Ok).
  137. Where("is_show =? ", pasturePb.IsShow_Ok)
  138. if depId != -1 && depId > 0 {
  139. pref = pref.Where("dept_id = ?", depId)
  140. }
  141. if err = pref.Find(&systemUserList).Error; err != nil {
  142. return nil, xerr.WithStack(err)
  143. }
  144. return &pasturePb.ConfigOptionsListResponse{
  145. Code: http.StatusOK,
  146. Msg: "ok",
  147. Data: model.SystemUserSlice(systemUserList).ToPB2(),
  148. }, nil
  149. }
  150. func (s *StoreEntry) BullOptions(ctx context.Context) (*pasturePb.BullOptionsListResponse, error) {
  151. userModel, err := s.GetUserModel(ctx)
  152. if err != nil {
  153. return nil, xerr.WithStack(err)
  154. }
  155. return &pasturePb.BullOptionsListResponse{
  156. Code: http.StatusOK,
  157. Msg: "ok",
  158. Data: s.BullNumberEnumList("", userModel.AppPasture.Id),
  159. }, nil
  160. }
  161. func (s *StoreEntry) SystemBaseConfigOptions(ctx context.Context, optionsName, isAll string) (*pasturePb.ConfigOptionsListResponse, error) {
  162. if optionsName == "" {
  163. return nil, xerr.New("optionsName is empty")
  164. }
  165. getConfigFuncMap := map[string]func(isAll string) []*pasturePb.ConfigOptionsList{
  166. "childNumber": s.ChildNumberEnumList,
  167. "calvingLevel": s.CalvingLevelEnumList,
  168. "dystociaReason": s.DystociaReasonEnumList,
  169. "drugCategory": s.DrugCategoryEnumList,
  170. "drugUsage": s.DrugUsageEnumList,
  171. "unit": s.UnitEnumList,
  172. "pregnantCheckResult": s.PregnantCheckResultEnumList,
  173. "pregnantCheckMethod": s.PregnantCheckMethodEnumList,
  174. "exposeEstrusType": s.ExposeEstrusTypeEnumList,
  175. "frozenSemenType": s.FrozenSemenTypeEnumList,
  176. "week": s.WeekEnumList,
  177. "month": s.MonthEnumList,
  178. "sameTimeCowType": s.SameTimeCowTypeEnumList,
  179. "sameTimeType": s.SameTimeTypeEnumList,
  180. "immunizationCowType": s.ImmunizationCowTypeEnumList,
  181. "workOrderFrequency": s.WorkOrderFrequencyEnumList,
  182. "workOrderSubUnit": s.WorkOrderSubUnitEnumList,
  183. "workOrderPriority": s.WorkOrderPriorityEnumList,
  184. "workOrderCategory": s.WorkOrderCategoryEnumList,
  185. "immunizationConditions": s.ImmunizationConditionsEnumList,
  186. "abortionReasons": s.AbortionReasonsEnumList,
  187. "healthStatus": s.HealthStatusEnumList,
  188. "calendarType": CalendarTypeEnumList,
  189. "calvingAnalysisMethod": s.CalvingAnalysisMethodEnumList,
  190. "lact": s.LactEnumList,
  191. "diseaseAnalysisMethod": s.DiseaseAnalysisMethodEnumList,
  192. "singleFactorAnalysisMethod": s.SingleFactorAnalysisMethodEnumList,
  193. "lactIntervalSymbol": s.LactIntervalSymbolEnumList,
  194. "multiFactorAnalysisMethod": s.MultiFactorAnalysisMethodEnumList,
  195. "saleCowAnalysisMethod": s.SaleCowAnalysisMethodEnumList,
  196. "neckRingIsBind": s.NeckRingIsBindEnumList,
  197. "outType": s.OutTypeEnumList,
  198. "auditStatus": s.AuditStatusEnumList,
  199. "pregnantCheckName": s.PregnantCheckNameEnumList,
  200. "unMatingReasons": s.UnMatingReasonsEnumList,
  201. "evenType": s.EventTypeEnumList,
  202. "outReason": s.OutReasonEnumList,
  203. "deathReason": s.DeathReasonEnumList,
  204. "categoryKind": s.EventCategoryEnumList,
  205. "indicatorsDetails": s.IndicatorsDetailsList,
  206. "purposeKind": s.CowPurposeList,
  207. "forbiddenMatingReasons": s.ForbiddenMatingReasonsEnumList,
  208. "cowOutReason": s.CowOutReasonList,
  209. "deathDestination": s.CowDeathDestinationList,
  210. "warningHealthLevel": s.WarningHealthLevel,
  211. "behavior": s.Behavior,
  212. "matingWindowPeriod": s.MatingWindowPeriodEnumList,
  213. "neckRingError": s.NeckRingErrorEnumList,
  214. }
  215. getConfigFunc, ok := getConfigFuncMap[optionsName]
  216. if !ok {
  217. return nil, fmt.Errorf("invalid optionsName: %s", optionsName)
  218. }
  219. configOptions := getConfigFunc(isAll)
  220. if configOptions == nil {
  221. return nil, fmt.Errorf("failed to retrieve configOptions for %s", optionsName)
  222. }
  223. return &pasturePb.ConfigOptionsListResponse{
  224. Code: http.StatusOK,
  225. Msg: "ok",
  226. Data: configOptions,
  227. }, nil
  228. }