enum_options.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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(userModel, 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(userModel, 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. Order("kind ASC").
  60. Find(&diseaseTypeList).Error; err != nil {
  61. return nil, xerr.WithStack(err)
  62. }
  63. diseaseList := make([]*model.Disease, 0)
  64. if isChildren == model.IsAllYes {
  65. if err = s.DB.Model(new(model.Disease)).
  66. Where("pasture_id =? ", userModel.AppPasture.Id).
  67. Where("is_show =? ", pasturePb.IsShow_Ok).
  68. Find(&diseaseList).Error; err != nil {
  69. return nil, xerr.WithStack(err)
  70. }
  71. }
  72. return &pasturePb.ConfigOptionsListResponse{
  73. Code: http.StatusOK,
  74. Msg: "ok",
  75. Data: model.ConfigDiseaseTypeSlice(diseaseTypeList).ToPB2(userModel, diseaseList),
  76. }, nil
  77. }
  78. func (s *StoreEntry) DiseaseOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  79. userModel, err := s.GetUserModel(ctx)
  80. if err != nil {
  81. return nil, xerr.WithStack(err)
  82. }
  83. diseaseList := make([]*model.Disease, 0)
  84. if err = s.DB.Model(new(model.Disease)).
  85. Where("is_show =? ", pasturePb.IsShow_Ok).
  86. Where("pasture_id =? ", userModel.AppPasture.Id).
  87. Find(&diseaseList).Error; err != nil {
  88. return nil, xerr.WithStack(err)
  89. }
  90. return &pasturePb.ConfigOptionsListResponse{
  91. Code: http.StatusOK,
  92. Msg: "ok",
  93. Data: model.DiseaseSlice(diseaseList).ToPB2(),
  94. }, nil
  95. }
  96. func (s *StoreEntry) PrescriptionOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  97. userModel, err := s.GetUserModel(ctx)
  98. if err != nil {
  99. return nil, xerr.WithStack(err)
  100. }
  101. prescriptionList := make([]*model.Prescription, 0)
  102. if err = s.DB.Model(new(model.Prescription)).
  103. Where("pasture_id = ? ", userModel.AppPasture.Id).
  104. Where("is_show = ? ", pasturePb.IsShow_Ok).
  105. Find(&prescriptionList).Error; err != nil {
  106. return nil, xerr.WithStack(err)
  107. }
  108. return &pasturePb.ConfigOptionsListResponse{
  109. Code: http.StatusOK,
  110. Msg: "ok",
  111. Data: model.PrescriptionSlice(prescriptionList).ToPB2(),
  112. }, nil
  113. }
  114. func (s *StoreEntry) BreedStatusOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  115. userModel, err := s.GetUserModel(ctx)
  116. if err != nil {
  117. return nil, xerr.WithStack(err)
  118. }
  119. return &pasturePb.ConfigOptionsListResponse{
  120. Code: http.StatusOK,
  121. Msg: "ok",
  122. Data: s.BreedStatusEnumList(userModel, ""),
  123. }, nil
  124. }
  125. func (s *StoreEntry) CowKindOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  126. userModel, err := s.GetUserModel(ctx)
  127. if err != nil {
  128. return nil, xerr.WithStack(err)
  129. }
  130. return &pasturePb.ConfigOptionsListResponse{
  131. Code: http.StatusOK,
  132. Msg: "ok",
  133. Data: s.CowKindEnumList(userModel, ""),
  134. }, nil
  135. }
  136. func (s *StoreEntry) CowSourceOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  137. userModel, err := s.GetUserModel(ctx)
  138. if err != nil {
  139. return nil, xerr.WithStack(err)
  140. }
  141. return &pasturePb.ConfigOptionsListResponse{
  142. Code: http.StatusOK,
  143. Msg: "ok",
  144. Data: s.CowSourceEnumList(userModel, ""),
  145. }, nil
  146. }
  147. func (s *StoreEntry) CowTypeOptions(ctx context.Context, optionName, isAll string) (*pasturePb.ConfigOptionsListResponse, error) {
  148. userModel, err := s.GetUserModel(ctx)
  149. if err != nil {
  150. return nil, xerr.WithStack(err)
  151. }
  152. return &pasturePb.ConfigOptionsListResponse{
  153. Code: http.StatusOK,
  154. Msg: "ok",
  155. Data: s.CowTypeEnumList(userModel, optionName, isAll),
  156. }, nil
  157. }
  158. func (s *StoreEntry) CowTransferPenReasonOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
  159. userModel, err := s.GetUserModel(ctx)
  160. if err != nil {
  161. return nil, xerr.WithStack(err)
  162. }
  163. return &pasturePb.ConfigOptionsListResponse{
  164. Code: http.StatusOK,
  165. Msg: "ok",
  166. Data: s.TransferPenEnumList(userModel, ""),
  167. }, nil
  168. }
  169. // SystemUserOptions 系统用户下拉框
  170. func (s *StoreEntry) SystemUserOptions(ctx context.Context, depName string) (*pasturePb.ConfigOptionsListResponse, error) {
  171. userModel, err := s.GetUserModel(ctx)
  172. if err != nil {
  173. return nil, xerr.WithStack(err)
  174. }
  175. systemDepth := &model.SystemDept{}
  176. if err = s.DB.Model(new(model.SystemDept)).
  177. Where("name = ?", depName).
  178. Where("pasture_id = ?", userModel.AppPasture.Id).
  179. First(systemDepth).Error; err != nil {
  180. return nil, xerr.WithStack(err)
  181. }
  182. if systemDepth.IsDelete == pasturePb.IsShow_No {
  183. return nil, xerr.Custom("部门已经删除")
  184. }
  185. if systemDepth.IsShow == pasturePb.IsShow_No {
  186. return nil, xerr.Custom("部门已经禁用")
  187. }
  188. systemUserDepthRoleList := make([]*model.SystemUserDepthRole, 0)
  189. if err = s.DB.Model(new(model.SystemUserDepthRole)).
  190. Where("FIND_IN_SET(?,depth_ids) > 0", systemDepth.Id).
  191. Where("pasture_id = ?", userModel.AppPasture.Id).
  192. Find(&systemUserDepthRoleList).Error; err != nil {
  193. return nil, xerr.WithStack(err)
  194. }
  195. if len(systemUserDepthRoleList) <= 0 {
  196. return &pasturePb.ConfigOptionsListResponse{
  197. Code: http.StatusOK,
  198. Msg: "ok",
  199. Data: make([]*pasturePb.ConfigOptionsList, 0),
  200. }, nil
  201. }
  202. userIds := make([]int64, 0)
  203. for _, v := range systemUserDepthRoleList {
  204. userIds = append(userIds, v.UserId)
  205. }
  206. systemUserList := make([]*model.SystemUser, 0)
  207. if err = s.DB.Table(new(model.SystemUser).TableName()).
  208. Where("id IN ?", userIds).
  209. Where("is_delete = ?", pasturePb.IsShow_Ok).
  210. Where("is_show = ? ", pasturePb.IsShow_Ok).
  211. Find(&systemUserList).Error; err != nil {
  212. return nil, xerr.WithStack(err)
  213. }
  214. return &pasturePb.ConfigOptionsListResponse{
  215. Code: http.StatusOK,
  216. Msg: "ok",
  217. Data: model.SystemUserSlice(systemUserList).ToPB2(),
  218. }, nil
  219. }
  220. func (s *StoreEntry) BullOptions(ctx context.Context) (*pasturePb.BullOptionsListResponse, error) {
  221. userModel, err := s.GetUserModel(ctx)
  222. if err != nil {
  223. return nil, xerr.WithStack(err)
  224. }
  225. return &pasturePb.BullOptionsListResponse{
  226. Code: http.StatusOK,
  227. Msg: "ok",
  228. Data: s.BullNumberEnumList(userModel, ""),
  229. }, nil
  230. }
  231. func (s *StoreEntry) SystemBaseConfigOptions(ctx context.Context, optionsName, isAll string) (*pasturePb.ConfigOptionsListResponse, error) {
  232. userModel, err := s.GetUserModel(ctx)
  233. if err != nil {
  234. return nil, xerr.WithStack(err)
  235. }
  236. if optionsName == "" {
  237. return nil, xerr.New("optionsName is empty")
  238. }
  239. getConfigFuncMap := map[string]func(userModel *model.UserModel, isAll string) []*pasturePb.ConfigOptionsList{
  240. "childNumber": s.ChildNumberEnumList,
  241. "calvingLevel": s.CalvingLevelEnumList,
  242. "dystociaReason": s.DystociaReasonEnumList,
  243. "drugCategory": s.DrugCategoryEnumList,
  244. "drugUsage": s.DrugUsageEnumList,
  245. "unit": s.UnitEnumList,
  246. "pregnantCheckResult": s.PregnantCheckResultEnumList,
  247. "pregnantCheckMethod": s.PregnantCheckMethodEnumList,
  248. "exposeEstrusType": s.ExposeEstrusTypeEnumList,
  249. "frozenSemenType": s.FrozenSemenTypeEnumList,
  250. "week": s.WeekEnumList,
  251. "month": s.MonthEnumList,
  252. "sameTimeCowType": s.SameTimeCowTypeEnumList,
  253. "sameTimeType": s.SameTimeTypeEnumList,
  254. "immunizationCowType": s.ImmunizationCowTypeEnumList,
  255. "workOrderFrequency": s.WorkOrderFrequencyEnumList,
  256. "workOrderSubUnit": s.WorkOrderSubUnitEnumList,
  257. "workOrderPriority": s.WorkOrderPriorityEnumList,
  258. "workOrderCategory": s.WorkOrderCategoryEnumList,
  259. "immunizationConditions": s.ImmunizationConditionsEnumList,
  260. "abortionReasons": s.AbortionReasonsEnumList,
  261. "healthStatus": s.HealthStatusEnumList,
  262. "calendarType": s.CalendarTypeEnumList,
  263. "calvingAnalysisMethod": s.CalvingAnalysisMethodEnumList,
  264. "lact": s.LactEnumList,
  265. "diseaseAnalysisMethod": s.DiseaseAnalysisMethodEnumList,
  266. "singleFactorAnalysisMethod": s.SingleFactorAnalysisMethodEnumList,
  267. "lactIntervalSymbol": s.LactIntervalSymbolEnumList,
  268. "multiFactorAnalysisMethod": s.MultiFactorAnalysisMethodEnumList,
  269. "saleCowAnalysisMethod": s.SaleCowAnalysisMethodEnumList,
  270. "neckRingIsBind": s.NeckRingIsBindEnumList,
  271. "outType": s.OutTypeEnumList,
  272. "auditStatus": s.AuditStatusEnumList,
  273. "pregnantCheckName": s.PregnantCheckNameEnumList,
  274. "unMatingReasons": s.UnMatingReasonsEnumList,
  275. "evenType": s.EventTypeEnumList,
  276. "outReason": s.CowOutReasonEnumList,
  277. "deathReason": s.DeathReasonEnumList,
  278. "categoryKind": s.EventCategoryEnumList,
  279. "indicatorsDetails": s.IndicatorsDetailsList,
  280. "purposeKind": s.CowPurposeList,
  281. "forbiddenMatingReasons": s.ForbiddenMatingReasonsEnumList,
  282. "cowOutReason": s.CowOutReasonEnumList,
  283. "deathDestination": s.CowDeathDestinationList,
  284. "warningHealthLevel": s.WarningHealthLevel,
  285. "behavior": s.Behavior,
  286. "matingWindowPeriod": s.MatingWindowPeriodEnumList,
  287. "neckRingError": s.NeckRingErrorEnumList,
  288. "indicatorType": s.IndicatorsCategoryEnumList,
  289. }
  290. getConfigFunc, ok := getConfigFuncMap[optionsName]
  291. if !ok {
  292. return nil, fmt.Errorf("invalid optionsName: %s", optionsName)
  293. }
  294. configOptions := getConfigFunc(userModel, isAll)
  295. if configOptions == nil {
  296. return nil, fmt.Errorf("failed to retrieve configOptions for %s", optionsName)
  297. }
  298. return &pasturePb.ConfigOptionsListResponse{
  299. Code: http.StatusOK,
  300. Msg: "ok",
  301. Data: configOptions,
  302. }, nil
  303. }