event_breed_more.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package backend
  2. import (
  3. "context"
  4. "encoding/json"
  5. "kpt-pasture/model"
  6. "net/http"
  7. "strings"
  8. "go.uber.org/zap"
  9. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  10. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  11. "gitee.com/xuyiping_admin/pkg/xerr"
  12. "gorm.io/gorm"
  13. )
  14. func (s *StoreEntry) PregnantCheckList(ctx context.Context, req *pasturePb.SearchEventRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnantCheckEventResponse, error) {
  15. currentUser, err := s.GetCurrentSystemUser(ctx)
  16. if err != nil {
  17. return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
  18. }
  19. pregnantCheckList := make([]*model.EventPregnantCheck, 0)
  20. var count int64 = 0
  21. pref := s.DB.Table(new(model.EventPregnantCheck).TableName()).
  22. Where("pasture_id = ?", currentUser.PastureId)
  23. if len(req.CowId) > 0 {
  24. cowIds := strings.Split(req.CowId, ",")
  25. pref.Where("cow_id IN ?", cowIds)
  26. }
  27. if err = pref.Order("id desc").
  28. Count(&count).Limit(int(pagination.PageSize)).
  29. Offset(int(pagination.PageOffset)).
  30. Find(&pregnantCheckList).Error; err != nil {
  31. return nil, xerr.WithStack(err)
  32. }
  33. pregnantCheckResultMap := s.PregnantCheckResultMap()
  34. pregnantCheckMethodMap := s.PregnantCheckMethodMap()
  35. return &pasturePb.PregnantCheckEventResponse{
  36. Code: http.StatusOK,
  37. Message: "ok",
  38. Data: &pasturePb.SearchPregnantCheckData{
  39. List: model.EventPregnantCheckSlice(pregnantCheckList).ToPB(pregnantCheckResultMap, pregnantCheckMethodMap),
  40. Total: int32(count),
  41. PageSize: pagination.PageSize,
  42. Page: pagination.Page,
  43. },
  44. }, nil
  45. }
  46. func (s *StoreEntry) PregnantCheckCreateBatch(ctx context.Context, req *pasturePb.EventPregnantCheckBatch) (err error) {
  47. pregnantCheckBatchModelList, err := s.PregnantCheckDataCheck(ctx, req)
  48. if err != nil {
  49. return xerr.WithStack(err)
  50. }
  51. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  52. for _, item := range pregnantCheckBatchModelList {
  53. breedStatus := pasturePb.BreedStatus_Pregnant
  54. isPregnant := pasturePb.IsShow_Ok
  55. matingResult := pasturePb.MatingResult_Pregnant
  56. if item.PregnantCheckResult == pasturePb.PregnantCheckResult_UnPregnant {
  57. breedStatus = pasturePb.BreedStatus_Empty
  58. isPregnant = pasturePb.IsShow_No
  59. matingResult = pasturePb.MatingResult_Empty
  60. }
  61. // 更新上一次配种结果数据,如何是复检无胎则更新为流产
  62. if item.EventPregnancyCheck.PregnantCheckName == model.PregnantCheckForSecond {
  63. breedStatus = pasturePb.BreedStatus_Abort
  64. matingResult = pasturePb.MatingResult_Abort
  65. }
  66. item.Cow.EventPregnantCheckUpdate(breedStatus, int64(item.PregnantCheckAt), isPregnant)
  67. // 更新牛只基本信息
  68. if err = tx.Model(item.Cow).
  69. Select("breed_status", "last_pregnant_check_at", "is_pregnant").
  70. Where("id = ?", item.Cow.Id).
  71. Updates(item.Cow).Error; err != nil {
  72. return xerr.WithStack(err)
  73. }
  74. // 更新最近一次配种结果数据
  75. item.LastMating.EventMatingResultUpdate(matingResult, int64(item.PregnantCheckAt))
  76. if err = tx.Model(item.LastMating).
  77. Where("id = ?", item.LastMating.Id).
  78. Select("mating_result", "mating_result_at").
  79. Updates(item.LastMating).Error; err != nil {
  80. return xerr.WithStack(err)
  81. }
  82. // 更新孕检事件表
  83. item.EventPregnancyCheck.EventUpdate(int64(item.PregnantCheckAt), item.PregnantCheckResult, item.PregnantCheckMethod, item.OperationUser, item.CurrentUser, item.Remarks)
  84. if err = tx.Model(new(model.EventPregnantCheck)).
  85. Select("reality_day,pregnant_check_result,pregnant_check_method,operation_id,operation_name,message_id,message_name,remarks,status").
  86. Where("id = ?", item.EventPregnancyCheck.Id).
  87. Updates(item.EventPregnancyCheck).Error; err != nil {
  88. return xerr.WithStack(err)
  89. }
  90. // 记录日志
  91. cowLogs := s.SubmitEventLog(ctx, item.CurrentUser, item.Cow, pasturePb.EventType_Pregnancy_Check, pasturePb.ExposeEstrusType_Invalid, req)
  92. tx.Table(cowLogs.TableName()).Create(cowLogs)
  93. // 更新上次配种结果
  94. if err = s.UpdateMatingResultEventCowLogByCowId(ctx, item.Cow.Id, s.MatingResultMap()[matingResult]); err != nil {
  95. zaplog.Error("PregnantCheckCreateBatch", zap.Any("UpdateMatingResultEventCowLogByCowId", err))
  96. }
  97. }
  98. return nil
  99. }); err != nil {
  100. return xerr.WithStack(err)
  101. }
  102. return nil
  103. }
  104. func (s *StoreEntry) AbortionCreate(ctx context.Context, req *pasturePb.EventAbortionRequest) error {
  105. currentUser, err := s.GetCurrentSystemUser(ctx)
  106. if err != nil {
  107. return xerr.Custom("当前用户信息错误,请退出重新登录")
  108. }
  109. cow, err := s.GetCowInfoByCowId(ctx, currentUser.PastureId, int64(req.CowId))
  110. if err != nil {
  111. return xerr.WithStack(err)
  112. }
  113. systemUser, err := s.GetSystemUserById(ctx, currentUser.PastureId, int64(req.OperationId))
  114. if err != nil {
  115. return xerr.WithStack(err)
  116. }
  117. req.OperationName = systemUser.Name
  118. abortionReasonsMap := s.AbortionReasonsMap()
  119. newEventAbortion := model.NewEventAbortion(currentUser.PastureId, cow, req, abortionReasonsMap)
  120. lastCowMating := &model.EventMating{}
  121. if err = s.DB.Model(new(model.EventMating)).Where("cow_id = ?", cow.Id).
  122. Where("mating_result = ?", pasturePb.MatingResult_Pregnant).
  123. Order("id desc").First(lastCowMating).Error; err != nil {
  124. return xerr.WithStack(err)
  125. }
  126. // 更新最近一次配种结果为流产
  127. lastCowMating.EventMatingResultUpdate(pasturePb.MatingResult_Abort, int64(req.AbortionAt))
  128. // 更新流产数据
  129. cow.EventAbortionUpdate(int64(req.AbortionAt))
  130. defer func() {
  131. if err != nil {
  132. cowLogs := s.SubmitEventLog(ctx, currentUser, cow, pasturePb.EventType_Abort, pasturePb.ExposeEstrusType_Invalid, req)
  133. s.DB.Table(cowLogs.TableName()).Create(cowLogs)
  134. }
  135. }()
  136. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  137. // 创建牛只流产事件数据
  138. if err = tx.Create(newEventAbortion).Error; err != nil {
  139. return xerr.WithStack(err)
  140. }
  141. // 更新牛只状态
  142. if err = tx.Model(new(model.Cow)).
  143. Select("is_pregnant", "last_abortion_at", "breed_status").
  144. Where("id = ?", req.CowId).
  145. Updates(cow).Error; err != nil {
  146. return xerr.WithStack(err)
  147. }
  148. // 更新最近一次配种结果为流产
  149. if err = tx.Model(new(model.EventMating)).
  150. Select("mating_result", "mating_result_at").
  151. Where("id = ?", lastCowMating.Id).
  152. Updates(lastCowMating).Error; err != nil {
  153. }
  154. return nil
  155. }); err != nil {
  156. return xerr.WithStack(err)
  157. }
  158. return nil
  159. }
  160. func (s *StoreEntry) AbortionCreateBatch(ctx context.Context, req *pasturePb.EventAbortionBatch) error {
  161. if len(req.Item) <= 0 {
  162. return xerr.WithStack(xerr.New("流产数据不能为空"))
  163. }
  164. errors := make(map[int32]error)
  165. for _, v := range req.Item {
  166. if err := s.AbortionCreate(ctx, v); err != nil {
  167. errors[v.CowId] = err
  168. }
  169. }
  170. if len(errors) > 0 {
  171. b, _ := json.Marshal(errors)
  172. return xerr.WithStack(xerr.Customf("部分流产数据创建失败: %s", string(b)))
  173. }
  174. return nil
  175. }
  176. func (s *StoreEntry) AbortionList(
  177. ctx context.Context,
  178. req *pasturePb.SearchEventRequest,
  179. pagination *pasturePb.PaginationModel,
  180. ) (*pasturePb.EventAbortionResponse, error) {
  181. currentUser, err := s.GetCurrentSystemUser(ctx)
  182. if err != nil {
  183. return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
  184. }
  185. abortionList := make([]*model.EventAbortion, 0)
  186. var count int64 = 0
  187. pref := s.DB.Model(new(model.EventAbortion)).
  188. Where("pasture_id = ?", currentUser.PastureId)
  189. if len(req.CowId) > 0 {
  190. cowIds := strings.Split(req.CowId, ",")
  191. pref.Where("cow_id IN ?", cowIds)
  192. }
  193. if err = pref.Order("id desc").
  194. Count(&count).Limit(int(pagination.PageSize)).
  195. Offset(int(pagination.PageOffset)).
  196. Find(&abortionList).Error; err != nil {
  197. return nil, xerr.WithStack(err)
  198. }
  199. return &pasturePb.EventAbortionResponse{
  200. Code: http.StatusOK,
  201. Message: "ok",
  202. Data: &pasturePb.EventAbortionData{
  203. List: model.AbortionSlice(abortionList).ToPB(),
  204. Total: int32(count),
  205. PageSize: pagination.PageSize,
  206. Page: pagination.Page,
  207. },
  208. }, nil
  209. }