enum_options.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. userModel, err := s.GetUserModel(ctx)
  12. if err != nil {
  13. return nil, xerr.WithStack(err)
  14. }
  15. excludeTypes := make([]pasturePb.PenType_Kind, 0)
  16. if userModel.AppPasture.Category == pasturePb.PastureCategory_Beef {
  17. excludeTypes = append(excludeTypes, pasturePb.PenType_Dry_Milking, pasturePb.PenType_Peripartum)
  18. }
  19. return &pasturePb.ConfigOptionsListResponse{
  20. Code: http.StatusOK,
  21. Msg: "ok",
  22. Data: s.BarnTypeEnumList(excludeTypes),
  23. }, nil
  24. }
  25. func (s *StoreEntry) BarnListOptions(ctx context.Context, penType int, isAll string) (*pasturePb.ConfigOptionsListResponse, error) {
  26. userModel, err := s.GetUserModel(ctx)
  27. if err != nil {
  28. return nil, xerr.WithStack(err)
  29. }
  30. penList := make([]*model.Pen, 0)
  31. pref := s.DB.Table(new(model.Pen).TableName()).
  32. Where("pasture_id = ?", userModel.AppPasture.Id).
  33. Where("is_delete = ?", pasturePb.IsShow_Ok)
  34. if penType != -1 {
  35. pref.Where("pen_type = ?", penType)
  36. }
  37. if err = pref.Find(&penList).Error; err != nil {
  38. return nil, err
  39. }
  40. excludeTypes := make([]pasturePb.PenType_Kind, 0)
  41. if userModel.AppPasture.Category == pasturePb.PastureCategory_Beef {
  42. excludeTypes = append(excludeTypes, pasturePb.PenType_Dry_Milking, pasturePb.PenType_Peripartum)
  43. }
  44. barnTypeList := s.BarnTypeEnumList(excludeTypes)
  45. return &pasturePb.ConfigOptionsListResponse{
  46. Code: http.StatusOK,
  47. Msg: "ok",
  48. Data: model.PenSlice(penList).ToPB2(barnTypeList, isAll),
  49. }, nil
  50. }
  51. func (s *StoreEntry) DiseaseTypeOptions(ctx context.Context, isChildren string) (*pasturePb.ConfigOptionsListResponse, error) {
  52. userModel, err := s.GetUserModel(ctx)
  53. if err != nil {
  54. return nil, xerr.WithStack(err)
  55. }
  56. diseaseTypeList := make([]*model.ConfigDiseaseType, 0)
  57. if err = s.DB.Table(new(model.ConfigDiseaseType).TableName()).
  58. Where("is_show =? ", pasturePb.IsShow_Ok).
  59. Find(&diseaseTypeList).Error; err != nil {
  60. return nil, xerr.WithStack(err)
  61. }
  62. diseaseList := make([]*model.Disease, 0)
  63. if isChildren == model.IsAllYes {
  64. if err = s.DB.Model(new(model.Disease)).
  65. Where("pasture_id =? ", userModel.AppPasture.Id).
  66. Where("is_show =? ", pasturePb.IsShow_Ok).
  67. Find(&diseaseList).Error; err != nil {
  68. return nil, xerr.WithStack(err)
  69. }
  70. }
  71. return &pasturePb.ConfigOptionsListResponse{
  72. Code: http.StatusOK,
  73. Msg: "ok",
  74. Data: model.ConfigDiseaseTypeSlice(diseaseTypeList).ToPB2(diseaseList),
  75. }, nil
  76. }
  77. func (s *StoreEntry) DiseaseOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  78. userModel, err := s.GetUserModel(ctx)
  79. if err != nil {
  80. return nil, xerr.WithStack(err)
  81. }
  82. diseaseList := make([]*model.Disease, 0)
  83. if err = s.DB.Model(new(model.Disease)).
  84. Where("is_show =? ", pasturePb.IsShow_Ok).
  85. Where("pasture_id =? ", userModel.AppPasture.Id).
  86. Find(&diseaseList).Error; err != nil {
  87. return nil, xerr.WithStack(err)
  88. }
  89. return &pasturePb.ConfigOptionsListResponse{
  90. Code: http.StatusOK,
  91. Msg: "ok",
  92. Data: model.DiseaseSlice(diseaseList).ToPB2(),
  93. }, nil
  94. }
  95. func (s *StoreEntry) PrescriptionOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  96. userModel, err := s.GetUserModel(ctx)
  97. if err != nil {
  98. return nil, xerr.WithStack(err)
  99. }
  100. prescriptionList := make([]*model.Prescription, 0)
  101. if err = s.DB.Model(new(model.Prescription)).
  102. Where("pasture_id = ? ", userModel.AppPasture.Id).
  103. Where("is_show = ? ", pasturePb.IsShow_Ok).
  104. Find(&prescriptionList).Error; err != nil {
  105. return nil, xerr.WithStack(err)
  106. }
  107. return &pasturePb.ConfigOptionsListResponse{
  108. Code: http.StatusOK,
  109. Msg: "ok",
  110. Data: model.PrescriptionSlice(prescriptionList).ToPB2(),
  111. }, nil
  112. }
  113. func (s *StoreEntry) BreedStatusOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  114. return &pasturePb.ConfigOptionsListResponse{
  115. Code: http.StatusOK,
  116. Msg: "ok",
  117. Data: s.BreedStatusEnumList(),
  118. }, nil
  119. }
  120. func (s *StoreEntry) CowKindOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  121. return &pasturePb.ConfigOptionsListResponse{
  122. Code: http.StatusOK,
  123. Msg: "ok",
  124. Data: s.CowKindEnumList(),
  125. }, nil
  126. }
  127. func (s *StoreEntry) CowSourceOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  128. return &pasturePb.ConfigOptionsListResponse{
  129. Code: http.StatusOK,
  130. Msg: "ok",
  131. Data: s.CowSourceEnumList(),
  132. }, nil
  133. }
  134. func (s *StoreEntry) CowTypeOptions(ctx context.Context, optionName, isAll string) (*pasturePb.ConfigOptionsListResponse, error) {
  135. return &pasturePb.ConfigOptionsListResponse{
  136. Code: http.StatusOK,
  137. Msg: "ok",
  138. Data: s.CowTypeEnumList(optionName, isAll),
  139. }, nil
  140. }
  141. func (s *StoreEntry) CowTransferPenReasonOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  142. return &pasturePb.ConfigOptionsListResponse{
  143. Code: http.StatusOK,
  144. Msg: "ok",
  145. Data: s.TransferPenEnumList(""),
  146. }, nil
  147. }
  148. // SystemUserOptions 系统用户下拉框
  149. func (s *StoreEntry) SystemUserOptions(ctx context.Context, depName string) (*pasturePb.ConfigOptionsListResponse, error) {
  150. userModel, err := s.GetUserModel(ctx)
  151. if err != nil {
  152. return nil, xerr.WithStack(err)
  153. }
  154. systemDepth := &model.SystemDept{}
  155. if err = s.DB.Model(new(model.SystemDept)).
  156. Where("name = ?", depName).
  157. Where("pasture_id = ?", userModel.AppPasture.Id).
  158. First(systemDepth).Error; err != nil {
  159. return nil, xerr.WithStack(err)
  160. }
  161. if systemDepth.IsDelete == pasturePb.IsShow_No {
  162. return nil, xerr.Custom("部门已经删除")
  163. }
  164. if systemDepth.IsShow == pasturePb.IsShow_No {
  165. return nil, xerr.Custom("部门已经禁用")
  166. }
  167. systemUserList := make([]*model.SystemUser, 0)
  168. if err = s.DB.Table(new(model.SystemUser).TableName()).
  169. Where("FIND_IN_SET(?,dept_ids) > 0", systemDepth.Id).
  170. Where("is_delete = ?", pasturePb.IsShow_Ok).
  171. Where("is_show = ? ", pasturePb.IsShow_Ok).
  172. Find(&systemUserList).Error; err != nil {
  173. return nil, xerr.WithStack(err)
  174. }
  175. return &pasturePb.ConfigOptionsListResponse{
  176. Code: http.StatusOK,
  177. Msg: "ok",
  178. Data: model.SystemUserSlice(systemUserList).ToPB2(),
  179. }, nil
  180. }
  181. func (s *StoreEntry) BullOptions(ctx context.Context) (*pasturePb.BullOptionsListResponse, error) {
  182. userModel, err := s.GetUserModel(ctx)
  183. if err != nil {
  184. return nil, xerr.WithStack(err)
  185. }
  186. return &pasturePb.BullOptionsListResponse{
  187. Code: http.StatusOK,
  188. Msg: "ok",
  189. Data: s.BullNumberEnumList("", userModel.AppPasture.Id),
  190. }, nil
  191. }
  192. func (s *StoreEntry) SystemBaseConfigOptions(ctx context.Context, optionsName, isAll string) (*pasturePb.ConfigOptionsListResponse, error) {
  193. if optionsName == "" {
  194. return nil, xerr.New("optionsName is empty")
  195. }
  196. getConfigFuncMap := map[string]func(isAll string) []*pasturePb.ConfigOptionsList{
  197. "childNumber": s.ChildNumberEnumList,
  198. "calvingLevel": s.CalvingLevelEnumList,
  199. "dystociaReason": s.DystociaReasonEnumList,
  200. "drugCategory": s.DrugCategoryEnumList,
  201. "drugUsage": s.DrugUsageEnumList,
  202. "unit": s.UnitEnumList,
  203. "pregnantCheckResult": s.PregnantCheckResultEnumList,
  204. "pregnantCheckMethod": s.PregnantCheckMethodEnumList,
  205. "exposeEstrusType": s.ExposeEstrusTypeEnumList,
  206. "frozenSemenType": s.FrozenSemenTypeEnumList,
  207. "week": s.WeekEnumList,
  208. "month": s.MonthEnumList,
  209. "sameTimeCowType": s.SameTimeCowTypeEnumList,
  210. "sameTimeType": s.SameTimeTypeEnumList,
  211. "immunizationCowType": s.ImmunizationCowTypeEnumList,
  212. "workOrderFrequency": s.WorkOrderFrequencyEnumList,
  213. "workOrderSubUnit": s.WorkOrderSubUnitEnumList,
  214. "workOrderPriority": s.WorkOrderPriorityEnumList,
  215. "workOrderCategory": s.WorkOrderCategoryEnumList,
  216. "immunizationConditions": s.ImmunizationConditionsEnumList,
  217. "abortionReasons": s.AbortionReasonsEnumList,
  218. "healthStatus": s.HealthStatusEnumList,
  219. "calendarType": CalendarTypeEnumList,
  220. "calvingAnalysisMethod": s.CalvingAnalysisMethodEnumList,
  221. "lact": s.LactEnumList,
  222. "diseaseAnalysisMethod": s.DiseaseAnalysisMethodEnumList,
  223. "singleFactorAnalysisMethod": s.SingleFactorAnalysisMethodEnumList,
  224. "lactIntervalSymbol": s.LactIntervalSymbolEnumList,
  225. "multiFactorAnalysisMethod": s.MultiFactorAnalysisMethodEnumList,
  226. "saleCowAnalysisMethod": s.SaleCowAnalysisMethodEnumList,
  227. "neckRingIsBind": s.NeckRingIsBindEnumList,
  228. "outType": s.OutTypeEnumList,
  229. "auditStatus": s.AuditStatusEnumList,
  230. "pregnantCheckName": s.PregnantCheckNameEnumList,
  231. "unMatingReasons": s.UnMatingReasonsEnumList,
  232. "evenType": s.EventTypeEnumList,
  233. "outReason": s.OutReasonEnumList,
  234. "deathReason": s.DeathReasonEnumList,
  235. "categoryKind": s.EventCategoryEnumList,
  236. "indicatorsDetails": s.IndicatorsDetailsList,
  237. "purposeKind": s.CowPurposeList,
  238. "forbiddenMatingReasons": s.ForbiddenMatingReasonsEnumList,
  239. "cowOutReason": s.CowOutReasonList,
  240. "deathDestination": s.CowDeathDestinationList,
  241. "warningHealthLevel": s.WarningHealthLevel,
  242. "behavior": s.Behavior,
  243. "matingWindowPeriod": s.MatingWindowPeriodEnumList,
  244. "neckRingError": s.NeckRingErrorEnumList,
  245. "indicatorType": s.IndicatorsCategoryEnumList,
  246. }
  247. getConfigFunc, ok := getConfigFuncMap[optionsName]
  248. if !ok {
  249. return nil, fmt.Errorf("invalid optionsName: %s", optionsName)
  250. }
  251. configOptions := getConfigFunc(isAll)
  252. if configOptions == nil {
  253. return nil, fmt.Errorf("failed to retrieve configOptions for %s", optionsName)
  254. }
  255. return &pasturePb.ConfigOptionsListResponse{
  256. Code: http.StatusOK,
  257. Msg: "ok",
  258. Data: configOptions,
  259. }, nil
  260. }