enum_options.go 11 KB

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