goods.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "net/http"
  7. "time"
  8. "gorm.io/gorm"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  11. )
  12. func (s *StoreEntry) DrugsList(ctx context.Context, req *pasturePb.SearchDrugsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDrugsResponse, error) {
  13. drugsList := make([]*model.Drugs, 0)
  14. var count int64 = 0
  15. pref := s.DB.Model(new(model.Drugs))
  16. if req.Name != "" {
  17. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  18. }
  19. if req.Usage > 0 {
  20. pref.Where("usage_method = ?", req.Usage)
  21. }
  22. if req.CategoryId > 0 {
  23. pref.Where("category_id = ?", req.CategoryId)
  24. }
  25. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  26. Find(&drugsList).Error; err != nil {
  27. return nil, xerr.WithStack(err)
  28. }
  29. drugsCategoryMap := s.DrugCategoryMaps()
  30. unitMap := s.UnitMap()
  31. drugUsageMap := s.DrugUsageMaps()
  32. return &pasturePb.SearchDrugsResponse{
  33. Code: http.StatusOK,
  34. Message: "ok",
  35. Data: &pasturePb.SearchDrugsData{
  36. List: model.DrugsSlice(drugsList).ToPB(drugsCategoryMap, unitMap, drugUsageMap),
  37. Total: int32(count),
  38. PageSize: pagination.PageSize,
  39. Page: pagination.Page,
  40. },
  41. }, nil
  42. }
  43. func (s *StoreEntry) DrugsCreateOrUpdate(ctx context.Context, req *pasturePb.SearchDrugsList) error {
  44. currentUser, _ := s.GetCurrentSystemUser(ctx)
  45. newDrugs := model.NewDrugs(req, currentUser)
  46. if req.Id <= 0 {
  47. if err := s.DB.Create(newDrugs).Error; err != nil {
  48. return xerr.WithStack(err)
  49. }
  50. } else {
  51. if err := s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
  52. return xerr.WithStack(err)
  53. }
  54. }
  55. return nil
  56. }
  57. func (s *StoreEntry) MedicalEquipmentList(ctx context.Context, req *pasturePb.SearchMedicalEquipmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchMedicalEquipmentResponse, error) {
  58. medicalEquipmentList := make([]*model.MedicalEquipment, 0)
  59. var count int64 = 0
  60. pref := s.DB.Model(new(model.MedicalEquipment))
  61. if req.Name != "" {
  62. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  63. }
  64. if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  65. Find(&medicalEquipmentList).Error; err != nil {
  66. return nil, xerr.WithStack(err)
  67. }
  68. unitMap := s.UnitMap()
  69. return &pasturePb.SearchMedicalEquipmentResponse{
  70. Code: http.StatusOK,
  71. Message: "ok",
  72. Data: &pasturePb.SearchMedicalEquipmentData{
  73. List: model.MedicalEquipmentSlice(medicalEquipmentList).ToPB(unitMap),
  74. Total: int32(count),
  75. PageSize: pagination.PageSize,
  76. Page: pagination.Page,
  77. },
  78. }, nil
  79. }
  80. func (s *StoreEntry) MedicalEquipmentCreateOrUpdate(ctx context.Context, req *pasturePb.SearchMedicalEquipmentList) error {
  81. currentUser, err := s.GetCurrentSystemUser(ctx)
  82. if err != nil {
  83. return xerr.Custom("登录人信息失效")
  84. }
  85. newDrugs := model.NewMedicalEquipment(req, currentUser)
  86. if req.Id <= 0 {
  87. if err = s.DB.Create(newDrugs).Error; err != nil {
  88. return xerr.WithStack(err)
  89. }
  90. } else {
  91. if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil {
  92. return xerr.WithStack(err)
  93. }
  94. }
  95. return nil
  96. }
  97. func (s *StoreEntry) NeckRingLogCreateOrUpdate(ctx context.Context, req *pasturePb.NeckRingCreateRequest) error {
  98. currentUser, err := s.GetCurrentSystemUser(ctx)
  99. if err != nil {
  100. return xerr.Custom("登录人信息失效")
  101. }
  102. if req.Items == nil || len(req.Items) == 0 {
  103. return xerr.Custom("请选择要脖环数据")
  104. }
  105. newNeckRingLogList := model.NewNeckRingLogList(req.Items, currentUser)
  106. cowIds := make([]int64, 0)
  107. for _, v := range newNeckRingLogList {
  108. cowIds = append(cowIds, v.CowId)
  109. }
  110. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  111. // 解绑脖环号
  112. if err = tx.Model(new(model.NeckRingLog)).
  113. Where("cowId IN ?", cowIds).
  114. Updates(map[string]interface{}{
  115. "unbind_at": time.Now().Unix(),
  116. "status": pasturePb.NeckRingStatus_Unbind,
  117. "operation_id": currentUser.Id,
  118. "operation_name": currentUser.Name,
  119. }).Error; err != nil {
  120. return xerr.WithStack(err)
  121. }
  122. // 绑定新脖环
  123. if err = tx.Create(newNeckRingLogList).Error; err != nil {
  124. return xerr.WithStack(err)
  125. }
  126. for _, v := range req.Items {
  127. if v.CowId > 0 {
  128. if err = tx.Model(new(model.Cow)).
  129. Where("id = ?", v.CowId).
  130. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  131. Updates(map[string]interface{}{
  132. "neck_ring_number": v.Number,
  133. }).Error; err != nil {
  134. return xerr.WithStack(err)
  135. }
  136. }
  137. }
  138. return nil
  139. }); err != nil {
  140. return xerr.WithStack(err)
  141. }
  142. return nil
  143. }
  144. func (s *StoreEntry) NeckRingLogList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) {
  145. neckRingLogList := make([]*model.NeckRingLog, 0)
  146. var count int64 = 0
  147. pref := s.DB.Model(new(model.NeckRingLog)).Where("status > ?", pasturePb.NeckRingStatus_Unbind)
  148. if req.Status > 0 {
  149. pref.Where("status = ?", req.Status)
  150. }
  151. if req.CowId > 0 {
  152. pref.Where("cow_id = ?", req.CowId)
  153. }
  154. if req.Number != "" {
  155. pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%"))
  156. }
  157. if err := pref.Order("id desc").
  158. Count(&count).
  159. Limit(int(pagination.PageSize)).
  160. Offset(int(pagination.PageOffset)).
  161. Find(&neckRingLogList).Error; err != nil {
  162. return nil, xerr.WithStack(err)
  163. }
  164. neckRingStatusMap := s.NeckRingStatusMap()
  165. return &pasturePb.SearchNeckRingResponse{
  166. Code: http.StatusOK,
  167. Message: "ok",
  168. Data: &pasturePb.SearchNeckRingData{
  169. List: model.NeckRingLogSlice(neckRingLogList).ToPB(neckRingStatusMap),
  170. Total: int32(count),
  171. PageSize: pagination.PageSize,
  172. Page: pagination.Page,
  173. },
  174. }, nil
  175. }