ops_service.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "kpt-tmr-group/model"
  7. "kpt-tmr-group/pkg/xerr"
  8. operationPb "kpt-tmr-group/proto/go/backend/operation"
  9. "gorm.io/gorm"
  10. )
  11. var CattleCategoryMap = map[operationPb.CattleCategoryParent_Kind]string{
  12. operationPb.CattleCategoryParent_LACTATION_CAW: "泌乳牛",
  13. operationPb.CattleCategoryParent_FATTEN_CAW: "育肥牛",
  14. operationPb.CattleCategoryParent_RESERVE_CAW: "后备牛",
  15. operationPb.CattleCategoryParent_DRY_CAW: "干奶牛",
  16. operationPb.CattleCategoryParent_PERINATAL_CAW: "围产牛",
  17. operationPb.CattleCategoryParent_OTHER_CAW: "其他",
  18. }
  19. // CreateGroupPasture 创建集团牧场
  20. func (s *StoreEntry) CreateGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
  21. pastureList := model.NewGroupPasture(req)
  22. if err := s.DB.Create(pastureList).Error; err != nil {
  23. return xerr.WithStack(err)
  24. }
  25. return nil
  26. }
  27. // EditGroupPasture 创建集团牧场
  28. func (s *StoreEntry) EditGroupPasture(ctx context.Context, req *operationPb.AddPastureRequest) error {
  29. groupPasture := &model.GroupPasture{Id: req.Id}
  30. if err := s.DB.First(groupPasture).Error; err != nil {
  31. if errors.Is(err, gorm.ErrRecordNotFound) {
  32. return xerr.Custom("该数据不存在!")
  33. }
  34. return xerr.WithStack(err)
  35. }
  36. updateData := &model.GroupPasture{
  37. Name: req.Name,
  38. Account: req.Account,
  39. ManagerUser: req.ManagerUser,
  40. ManagerPhone: req.ManagerPhone,
  41. Address: req.Address,
  42. }
  43. if err := s.DB.Model(new(model.GroupPasture)).Omit("is_show", "password").
  44. Where("id = ?", req.Id).
  45. Updates(updateData).Error; err != nil {
  46. return xerr.WithStack(err)
  47. }
  48. return nil
  49. }
  50. // SearchGroupPastureList 查询牧场列表
  51. func (s *StoreEntry) SearchGroupPastureList(ctx context.Context, req *operationPb.SearchPastureRequest) (*model.GroupPastureResponse, error) {
  52. groupPasture := make([]*model.GroupPasture, 0)
  53. var count int64 = 0
  54. pref := s.DB.Model(new(model.GroupPasture)).Where("is_delete = ? ", operationPb.IsShow_OK)
  55. if req.Name != "" {
  56. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  57. }
  58. if req.ManagerPhone != "" {
  59. pref.Where("manager_phone like ?", fmt.Sprintf("%s%s%s", "%", req.ManagerPhone, "%"))
  60. }
  61. if req.ManagerUser != "" {
  62. pref.Where("manager_user like ?", fmt.Sprintf("%s%s%s", "%", req.ManagerUser, "%"))
  63. }
  64. if req.StartTime > 0 && req.EndTime > 0 && req.EndTime >= req.StartTime {
  65. pref.Where("created_at BETWEEN ? AND ? ", req.StartTime, req.EndTime)
  66. }
  67. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  68. Find(&groupPasture).Debug().Error; err != nil {
  69. return nil, xerr.WithStack(err)
  70. }
  71. return &model.GroupPastureResponse{
  72. Page: req.Pagination.Page,
  73. Total: int32(count),
  74. List: model.GroupPastureSlice(groupPasture).ToPB(),
  75. }, nil
  76. }
  77. func (s *StoreEntry) DeleteGroupPasture(ctx context.Context, pastureId int64) error {
  78. groupPasture := &model.GroupPasture{
  79. Id: pastureId,
  80. }
  81. if err := s.DB.First(groupPasture).Error; err != nil {
  82. if errors.Is(err, gorm.ErrRecordNotFound) {
  83. return xerr.Custom("该数据不存在")
  84. }
  85. return xerr.WithStack(err)
  86. }
  87. if err := s.DB.Model(groupPasture).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  88. return xerr.WithStack(err)
  89. }
  90. return nil
  91. }
  92. func (s *StoreEntry) ResetPasswordGroupPasture(ctx context.Context, req *operationPb.RestPasswordGroupPasture) error {
  93. groupPasture := &model.GroupPasture{
  94. Id: req.PastureId,
  95. }
  96. if err := s.DB.First(groupPasture).Error; err != nil {
  97. if errors.Is(err, gorm.ErrRecordNotFound) {
  98. return xerr.Custom("该数据不存在")
  99. }
  100. return xerr.WithStack(err)
  101. }
  102. if err := s.DB.Model(groupPasture).Update("password", req.Password).Error; err != nil {
  103. return xerr.WithStack(err)
  104. }
  105. return nil
  106. }
  107. func (s *StoreEntry) IsShowGroupPasture(ctx context.Context, req *operationPb.IsShowGroupPasture) error {
  108. groupPasture := &model.GroupPasture{
  109. Id: req.PastureId,
  110. }
  111. if err := s.DB.First(groupPasture).Error; err != nil {
  112. if errors.Is(err, gorm.ErrRecordNotFound) {
  113. return xerr.Custom("该数据不存在")
  114. }
  115. return xerr.WithStack(err)
  116. }
  117. if err := s.DB.Model(groupPasture).Update("is_show", req.IsShow).Error; err != nil {
  118. return xerr.WithStack(err)
  119. }
  120. return nil
  121. }
  122. // ParentCattleCategoryList 畜牧类别父类列表
  123. func (s *StoreEntry) ParentCattleCategoryList(ctx context.Context) map[operationPb.CattleCategoryParent_Kind]string {
  124. return CattleCategoryMap
  125. }
  126. // AddCattleCategory 添加畜牧分类
  127. func (s *StoreEntry) AddCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
  128. cattleCategory := model.NewCattleCategory(req)
  129. if err := s.DB.Create(cattleCategory).Error; err != nil {
  130. return xerr.WithStack(err)
  131. }
  132. return nil
  133. }
  134. // EditCattleCategory 编辑畜牧分类
  135. func (s *StoreEntry) EditCattleCategory(ctx context.Context, req *operationPb.AddCattleCategoryRequest) error {
  136. cattleCategory := &model.CattleCategory{Id: req.Id}
  137. if err := s.DB.First(cattleCategory).Error; err != nil {
  138. if errors.Is(err, gorm.ErrRecordNotFound) {
  139. return xerr.Custom("该数据不存在")
  140. }
  141. return xerr.WithStack(err)
  142. }
  143. updateData := &model.CattleCategory{
  144. ParentName: req.ParentName,
  145. Name: req.Name,
  146. Number: req.Number,
  147. ParentId: req.ParentId,
  148. }
  149. if err := s.DB.Model(new(model.CattleCategory)).Omit("is_show", "is_delete").
  150. Where("id = ?", req.Id).
  151. Updates(updateData).Error; err != nil {
  152. return xerr.WithStack(err)
  153. }
  154. return nil
  155. }
  156. // IsShowCattleCategory 是否启用
  157. func (s *StoreEntry) IsShowCattleCategory(ctx context.Context, req *operationPb.IsShowCattleCategory) error {
  158. cattleCategory := &model.CattleCategory{Id: req.CattleCategoryId}
  159. if err := s.DB.First(cattleCategory).Error; err != nil {
  160. if errors.Is(err, gorm.ErrRecordNotFound) {
  161. return xerr.Custom("该数据不存在")
  162. }
  163. return xerr.WithStack(err)
  164. }
  165. if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", req.CattleCategoryId).Update("is_show", req.IsShow).Error; err != nil {
  166. return xerr.WithStack(err)
  167. }
  168. return nil
  169. }
  170. // DeleteCattleCategory 是否删除
  171. func (s *StoreEntry) DeleteCattleCategory(ctx context.Context, cattleCategoryId int64) error {
  172. cattleCategory := &model.CattleCategory{Id: cattleCategoryId}
  173. if err := s.DB.First(cattleCategory).Error; err != nil {
  174. if errors.Is(err, gorm.ErrRecordNotFound) {
  175. return xerr.Custom("该数据不存在")
  176. }
  177. return xerr.WithStack(err)
  178. }
  179. if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", cattleCategoryId).Update("is_delete", operationPb.IsShow_NO).Error; err != nil {
  180. return xerr.WithStack(err)
  181. }
  182. return nil
  183. }
  184. // SearchCattleCategoryList 牧畜分类类别列表
  185. func (s *StoreEntry) SearchCattleCategoryList(ctx context.Context, req *operationPb.SearchCattleCategoryRequest) (*model.CattleCategoryResponse, error) {
  186. cattleCategory := make([]*model.CattleCategory, 0)
  187. var count int64 = 0
  188. pref := s.DB.Model(new(model.CattleCategory)).Where("is_delete = ?", operationPb.IsShow_OK)
  189. if req.Name != "" {
  190. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  191. }
  192. if req.ParentName != "" {
  193. pref.Where("parent_name like ?", fmt.Sprintf("%s%s%s", "%", req.ParentName, "%"))
  194. }
  195. if req.IsShow > 0 {
  196. pref.Where("is_show = ?", req.IsShow)
  197. }
  198. if err := pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  199. Find(&cattleCategory).Debug().Error; err != nil {
  200. return nil, xerr.WithStack(err)
  201. }
  202. return &model.CattleCategoryResponse{
  203. Page: req.Pagination.Page,
  204. Total: int32(count),
  205. List: model.CattleCategorySlice(cattleCategory).ToPB(),
  206. }, nil
  207. }