event_breed_more.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. abortionReasonsMap := s.AbortionReasonsMap()
  52. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  53. for _, item := range pregnantCheckBatchModelList {
  54. breedStatus := pasturePb.BreedStatus_Pregnant
  55. isPregnant := pasturePb.IsShow_Ok
  56. matingResult := pasturePb.MatingResult_Pregnant
  57. if item.PregnantCheckResult == pasturePb.PregnantCheckResult_UnPregnant {
  58. breedStatus = pasturePb.BreedStatus_Empty
  59. isPregnant = pasturePb.IsShow_No
  60. matingResult = pasturePb.MatingResult_Empty
  61. }
  62. // 更新上一次配种结果数据,如何是复检无胎则更新为流产
  63. if item.EventPregnancyCheck.PregnantCheckName == model.PregnantCheckForSecond {
  64. breedStatus = pasturePb.BreedStatus_Abort
  65. matingResult = pasturePb.MatingResult_Abort
  66. }
  67. item.Cow.EventPregnantCheckUpdate(breedStatus, int64(item.PregnantCheckAt), isPregnant)
  68. // 更新牛只基本信息
  69. if err = tx.Model(item.Cow).
  70. Select("breed_status", "last_pregnant_check_at", "is_pregnant").
  71. Where("id = ?", item.Cow.Id).
  72. Updates(item.Cow).Error; err != nil {
  73. return xerr.WithStack(err)
  74. }
  75. // 更新最近一次配种结果数据
  76. item.LastMating.EventMatingResultUpdate(matingResult, int64(item.PregnantCheckAt))
  77. if err = tx.Model(item.LastMating).
  78. Where("id = ?", item.LastMating.Id).
  79. Select("mating_result", "mating_result_at").
  80. Updates(item.LastMating).Error; err != nil {
  81. return xerr.WithStack(err)
  82. }
  83. // 更新孕检事件表
  84. item.EventPregnancyCheck.EventUpdate(int64(item.PregnantCheckAt), item.PregnantCheckResult, item.PregnantCheckMethod, item.OperationUser, item.CurrentUser, item.Remarks)
  85. if err = tx.Model(new(model.EventPregnantCheck)).
  86. Select("reality_day,pregnant_check_result,pregnant_check_method,operation_id,operation_name,message_id,message_name,remarks,status").
  87. Where("id = ?", item.EventPregnancyCheck.Id).
  88. Updates(item.EventPregnancyCheck).Error; err != nil {
  89. return xerr.WithStack(err)
  90. }
  91. // 记录日志
  92. cowLogs := s.SubmitEventLog(ctx, item.CurrentUser, item.Cow, pasturePb.EventType_Pregnancy_Check, pasturePb.ExposeEstrusType_Invalid, req)
  93. tx.Table(cowLogs.TableName()).Create(cowLogs)
  94. // 更新上次配种结果
  95. if err = s.UpdateMatingResultEventCowLogByCowId(ctx, item.Cow.Id, s.MatingResultMap()[matingResult]); err != nil {
  96. zaplog.Error("PregnantCheckCreateBatch", zap.Any("UpdateMatingResultEventCowLogByCowId", err))
  97. }
  98. // 如果确定是流产
  99. if matingResult == pasturePb.MatingResult_Abort {
  100. newEventAbortion := model.NewEventAbortion(item.CurrentUser.PastureId, item.Cow, &pasturePb.EventAbortionRequest{
  101. CowId: int32(item.Cow.Id),
  102. AbortionAt: item.PregnantCheckAt,
  103. IsAfterbirth: pasturePb.IsShow_No,
  104. Photos: []string{},
  105. AbortionReasons: pasturePb.AbortionReasons_Unknown,
  106. AbortionReasonsName: abortionReasonsMap[pasturePb.AbortionReasons_Unknown],
  107. Remarks: "追加流产数据",
  108. OperationId: int32(item.OperationUser.Id),
  109. OperationName: item.OperationUser.Name,
  110. }, pasturePb.IsShow_Ok)
  111. if err = tx.Create(newEventAbortion).Error; err != nil {
  112. return xerr.WithStack(err)
  113. }
  114. }
  115. }
  116. return nil
  117. }); err != nil {
  118. return xerr.WithStack(err)
  119. }
  120. return nil
  121. }
  122. func (s *StoreEntry) AbortionCreate(ctx context.Context, req *pasturePb.EventAbortionRequest) error {
  123. currentUser, err := s.GetCurrentSystemUser(ctx)
  124. if err != nil {
  125. return xerr.Custom("当前用户信息错误,请退出重新登录")
  126. }
  127. cow, err := s.GetCowInfoByCowId(ctx, currentUser.PastureId, int64(req.CowId))
  128. if err != nil {
  129. return xerr.WithStack(err)
  130. }
  131. systemUser, err := s.GetSystemUserById(ctx, currentUser.PastureId, int64(req.OperationId))
  132. if err != nil {
  133. return xerr.WithStack(err)
  134. }
  135. req.OperationName = systemUser.Name
  136. req.AbortionReasonsName = s.AbortionReasonsMap()[req.AbortionReasons]
  137. newEventAbortion := model.NewEventAbortion(currentUser.PastureId, cow, req, pasturePb.IsShow_No)
  138. lastCowMating := &model.EventMating{}
  139. if err = s.DB.Model(new(model.EventMating)).Where("cow_id = ?", cow.Id).
  140. Where("mating_result = ?", pasturePb.MatingResult_Pregnant).
  141. Order("id desc").First(lastCowMating).Error; err != nil {
  142. return xerr.WithStack(err)
  143. }
  144. // 更新最近一次配种结果为流产
  145. lastCowMating.EventMatingResultUpdate(pasturePb.MatingResult_Abort, int64(req.AbortionAt))
  146. // 更新流产数据
  147. cow.EventAbortionUpdate(int64(req.AbortionAt))
  148. defer func() {
  149. if err != nil {
  150. cowLogs := s.SubmitEventLog(ctx, currentUser, cow, pasturePb.EventType_Abort, pasturePb.ExposeEstrusType_Invalid, req)
  151. s.DB.Table(cowLogs.TableName()).Create(cowLogs)
  152. }
  153. }()
  154. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  155. // 创建牛只流产事件数据
  156. if err = tx.Create(newEventAbortion).Error; err != nil {
  157. return xerr.WithStack(err)
  158. }
  159. // 更新牛只状态
  160. if err = tx.Model(new(model.Cow)).
  161. Select("is_pregnant", "last_abortion_at", "breed_status").
  162. Where("id = ?", req.CowId).
  163. Updates(cow).Error; err != nil {
  164. return xerr.WithStack(err)
  165. }
  166. // 更新最近一次配种结果为流产
  167. if err = tx.Model(new(model.EventMating)).
  168. Select("mating_result", "mating_result_at").
  169. Where("id = ?", lastCowMating.Id).
  170. Updates(lastCowMating).Error; err != nil {
  171. }
  172. return nil
  173. }); err != nil {
  174. return xerr.WithStack(err)
  175. }
  176. return nil
  177. }
  178. func (s *StoreEntry) AbortionCreateBatch(ctx context.Context, req *pasturePb.EventAbortionBatch) error {
  179. if len(req.Item) <= 0 {
  180. return xerr.WithStack(xerr.New("流产数据不能为空"))
  181. }
  182. errors := make(map[int32]error)
  183. for _, v := range req.Item {
  184. if err := s.AbortionCreate(ctx, v); err != nil {
  185. errors[v.CowId] = err
  186. }
  187. }
  188. if len(errors) > 0 {
  189. b, _ := json.Marshal(errors)
  190. return xerr.WithStack(xerr.Customf("部分流产数据创建失败: %s", string(b)))
  191. }
  192. return nil
  193. }
  194. func (s *StoreEntry) AbortionList(
  195. ctx context.Context,
  196. req *pasturePb.SearchEventRequest,
  197. pagination *pasturePb.PaginationModel,
  198. ) (*pasturePb.EventAbortionResponse, error) {
  199. currentUser, err := s.GetCurrentSystemUser(ctx)
  200. if err != nil {
  201. return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
  202. }
  203. abortionList := make([]*model.EventAbortion, 0)
  204. var count int64 = 0
  205. pref := s.DB.Model(new(model.EventAbortion)).
  206. Where("pasture_id = ?", currentUser.PastureId)
  207. if len(req.CowId) > 0 {
  208. cowIds := strings.Split(req.CowId, ",")
  209. pref.Where("cow_id IN ?", cowIds)
  210. }
  211. if err = pref.Order("id desc").
  212. Count(&count).Limit(int(pagination.PageSize)).
  213. Offset(int(pagination.PageOffset)).
  214. Find(&abortionList).Error; err != nil {
  215. return nil, xerr.WithStack(err)
  216. }
  217. return &pasturePb.EventAbortionResponse{
  218. Code: http.StatusOK,
  219. Message: "ok",
  220. Data: &pasturePb.EventAbortionData{
  221. List: model.AbortionSlice(abortionList).ToPB(),
  222. Total: int32(count),
  223. PageSize: pagination.PageSize,
  224. Page: pagination.Page,
  225. },
  226. }, nil
  227. }