event_base_more.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. "kpt-pasture/util"
  6. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  7. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  8. "gitee.com/xuyiping_admin/pkg/xerr"
  9. "go.uber.org/zap"
  10. "gorm.io/gorm"
  11. )
  12. func (s *StoreEntry) DepartureBatch(ctx context.Context, req *pasturePb.EventDepartureBatch) (err error) {
  13. if len(req.Item) <= 0 {
  14. return xerr.Custom("请选择相关牛只")
  15. }
  16. userModel, err := s.GetUserModel(ctx)
  17. if err != nil {
  18. return xerr.WithStack(err)
  19. }
  20. newEventDepartureModelList := make([]*model.EventDepartureModel, 0)
  21. cow := &model.Cow{}
  22. for _, item := range req.Item {
  23. cow, err = s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(item.CowId))
  24. if err != nil {
  25. zaplog.Error("DepartureBatch", zap.Any("item", item), zap.Any("err", err))
  26. return xerr.Customf("获取牛只信息失败: %d", item.CowId)
  27. }
  28. operationUser, err := s.GetSystemUserById(ctx, int64(item.OperationId))
  29. if err != nil {
  30. zaplog.Error("DepartureBatch", zap.Any("item", item), zap.Any("err", err))
  31. return xerr.Customf("获取操作人员信息失败: %d", item.OperationId)
  32. }
  33. reasonName := ""
  34. switch item.DepartureType {
  35. case pasturePb.DepartureType_Death:
  36. reasonName = s.DeadReasonMap()[pasturePb.DeadReason_Kind(item.DepartureReasonKind)]
  37. case pasturePb.DepartureType_Out:
  38. reasonName = s.OutReasonMap()[pasturePb.OutReason_Kind(item.DepartureReasonKind)]
  39. default:
  40. return xerr.Custom("未知的离场类型")
  41. }
  42. newEventDeparture := model.NewEventDeparture(userModel.AppPasture.Id, cow, item, reasonName, userModel.SystemUser, operationUser)
  43. newEventDepartureModelList = append(newEventDepartureModelList, &model.EventDepartureModel{
  44. EventDeparture: newEventDeparture,
  45. Cow: cow,
  46. DepartureAt: int64(item.DepartureAt),
  47. })
  48. }
  49. if len(newEventDepartureModelList) <= 0 {
  50. return nil
  51. }
  52. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  53. // 记录事件日志
  54. for _, item := range newEventDepartureModelList {
  55. eventType := pasturePb.EventType_Death
  56. if item.EventDeparture.DepartureType == pasturePb.DepartureType_Out {
  57. eventType = pasturePb.EventType_Out
  58. }
  59. if err = tx.Create(item.EventDeparture).Error; err != nil {
  60. return xerr.WithStack(err)
  61. }
  62. cow.EventDepartureUpdate(item.DepartureAt, item.EventDeparture.DepartureType)
  63. if err = tx.Model(cow).
  64. Select("admission_status", "health_status", "departure_at").
  65. Where("id = ?", cow.Id).
  66. Updates(cow).Error; err != nil {
  67. return xerr.WithStack(err)
  68. }
  69. // 记录事件日志
  70. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, item.Cow, eventType, pasturePb.ExposeEstrusType_Invalid, item)
  71. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  72. return xerr.WithStack(err)
  73. }
  74. }
  75. return nil
  76. }); err != nil {
  77. return xerr.WithStack(err)
  78. }
  79. return nil
  80. }
  81. func (s *StoreEntry) CowEarNumberUpdate(ctx context.Context, req *pasturePb.EventReplaceEarNumber) (err error) {
  82. userModel, err := s.GetUserModel(ctx)
  83. if err != nil {
  84. return xerr.WithStack(err)
  85. }
  86. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
  87. if err != nil {
  88. return xerr.Custom("未找到该牛只信息")
  89. }
  90. cow.EventEarNumberUpdate(req.EarNumber)
  91. if err = s.DB.Model(cow).
  92. Select("ear_number", "ear_old_number").
  93. Where("id = ?", cow.Id).
  94. Updates(cow).Error; err != nil {
  95. return xerr.WithStack(err)
  96. }
  97. return nil
  98. }
  99. func (s *StoreEntry) CowSaleCreate(ctx context.Context, req *pasturePb.EventCowSale) error {
  100. userModel, err := s.GetUserModel(ctx)
  101. if err != nil {
  102. return xerr.WithStack(err)
  103. }
  104. cowIds := make([]int64, 0)
  105. if len(req.CowIds) <= 0 {
  106. return xerr.Custom("请选择牛只")
  107. }
  108. for _, v := range req.CowIds {
  109. cowIds = append(cowIds, int64(v))
  110. }
  111. cowList, err := s.GetCowInfoByCowIds(ctx, userModel.AppPasture.Id, cowIds)
  112. if err != nil {
  113. return xerr.WithStack(err)
  114. }
  115. if len(cowList) != len(req.CowIds) {
  116. return xerr.Custom("数据异常")
  117. }
  118. if len(cowList) <= 0 {
  119. return nil
  120. }
  121. dealerInfo := &model.SaleDealer{}
  122. if err = s.DB.Model(new(model.SaleDealer)).Where("id = ?", req.DealerId).First(dealerInfo).Error; err != nil {
  123. return xerr.Custom("经销商数据异常")
  124. }
  125. req.DealerName = dealerInfo.Name
  126. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  127. if err != nil {
  128. return xerr.Custom("获取操作人员信息失败")
  129. }
  130. req.OperationName = operationUser.Name
  131. cowIdsStr := util.ArrayInt32ToStrings(req.CowIds, ",")
  132. newEventSale := model.NewEventSale(userModel.AppPasture.Id, dealerInfo, cowIdsStr, req, operationUser, userModel.SystemUser)
  133. eventCowLogList := make([]*model.EventCowLog, 0)
  134. for _, cow := range cowList {
  135. newEventCowLog := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Sale, pasturePb.ExposeEstrusType_Invalid, req)
  136. eventCowLogList = append(eventCowLogList, newEventCowLog)
  137. }
  138. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  139. // 创建销售数据
  140. if err = tx.Model(new(model.EventSale)).Create(newEventSale).Error; err != nil {
  141. return xerr.WithStack(err)
  142. }
  143. newEventSaleCarList := model.NewEventSaleCarList(newEventSale.Id, newEventSale.SaleAt, req.SaleVehicleItems)
  144. if err = tx.Model(new(model.EventSaleCar)).Create(&newEventSaleCarList).Error; err != nil {
  145. return xerr.WithStack(err)
  146. }
  147. // 更新牛只状态
  148. if err = tx.Model(new(model.Cow)).Where("id IN (?)", cowIds).Update("admission_status", pasturePb.AdmissionStatus_Sale).Error; err != nil {
  149. return xerr.WithStack(err)
  150. }
  151. // 记录日志
  152. if len(eventCowLogList) > 0 {
  153. if err = tx.Model(new(model.EventCowLog)).Create(&eventCowLogList).Error; err != nil {
  154. return xerr.WithStack(err)
  155. }
  156. }
  157. return nil
  158. }); err != nil {
  159. return xerr.WithStack(err)
  160. }
  161. return nil
  162. }