event_breed_more.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. userModel, err := s.GetUserModel(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 = ?", userModel.AppPasture.Id)
  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. Msg: "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(
  85. int64(item.PregnantCheckAt),
  86. item.PregnantCheckResult,
  87. item.PregnantCheckMethod,
  88. item.OperationUser,
  89. item.UserModel.SystemUser,
  90. item.Remarks,
  91. )
  92. if err = tx.Model(new(model.EventPregnantCheck)).
  93. Select("reality_day,pregnant_check_result,pregnant_check_method,operation_id,operation_name,message_id,message_name,remarks,status").
  94. Where("id = ?", item.EventPregnancyCheck.Id).
  95. Updates(item.EventPregnancyCheck).Error; err != nil {
  96. return xerr.WithStack(err)
  97. }
  98. // 记录日志
  99. cowLogs := s.SubmitEventLog(ctx, item.UserModel.AppPasture.Id, item.Cow, pasturePb.EventType_Pregnancy_Check, pasturePb.ExposeEstrusType_Invalid, req)
  100. tx.Table(cowLogs.TableName()).Create(cowLogs)
  101. // 更新上次配种结果
  102. if err = s.UpdateMatingResultEventCowLogByCowId(ctx, item.Cow.Id, s.MatingResultMap()[matingResult]); err != nil {
  103. zaplog.Error("PregnantCheckCreateBatch", zap.Any("UpdateMatingResultEventCowLogByCowId", err))
  104. }
  105. // 如果确定是流产
  106. if matingResult == pasturePb.MatingResult_Abort {
  107. newEventAbortion := model.NewEventAbortion(item.UserModel.AppPasture.Id, item.Cow, &pasturePb.EventAbortionRequest{
  108. CowId: int32(item.Cow.Id),
  109. AbortionAt: item.PregnantCheckAt,
  110. IsAfterbirth: pasturePb.IsShow_No,
  111. Photos: []string{},
  112. AbortionReasons: pasturePb.AbortionReasons_Unknown,
  113. AbortionReasonsName: abortionReasonsMap[pasturePb.AbortionReasons_Unknown],
  114. Remarks: "追加流产数据",
  115. OperationId: int32(item.OperationUser.Id),
  116. OperationName: item.OperationUser.Name,
  117. }, pasturePb.IsShow_Ok)
  118. if err = tx.Create(newEventAbortion).Error; err != nil {
  119. return xerr.WithStack(err)
  120. }
  121. }
  122. }
  123. return nil
  124. }); err != nil {
  125. return xerr.WithStack(err)
  126. }
  127. return nil
  128. }
  129. func (s *StoreEntry) AbortionCreate(ctx context.Context, req *pasturePb.EventAbortionRequest) error {
  130. userModel, err := s.GetUserModel(ctx)
  131. if err != nil {
  132. return xerr.WithStack(err)
  133. }
  134. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
  135. if err != nil {
  136. return xerr.WithStack(err)
  137. }
  138. systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  139. if err != nil {
  140. return xerr.WithStack(err)
  141. }
  142. req.OperationName = systemUser.Name
  143. req.AbortionReasonsName = s.AbortionReasonsMap()[req.AbortionReasons]
  144. newEventAbortion := model.NewEventAbortion(userModel.AppPasture.Id, cow, req, pasturePb.IsShow_No)
  145. lastCowMating := &model.EventMating{}
  146. if err = s.DB.Model(new(model.EventMating)).Where("cow_id = ?", cow.Id).
  147. Where("mating_result = ?", pasturePb.MatingResult_Pregnant).
  148. Order("id desc").First(lastCowMating).Error; err != nil {
  149. return xerr.WithStack(err)
  150. }
  151. // 更新最近一次配种结果为流产
  152. lastCowMating.EventMatingResultUpdate(pasturePb.MatingResult_Abort, int64(req.AbortionAt))
  153. // 更新流产数据
  154. cow.EventAbortionUpdate(int64(req.AbortionAt))
  155. defer func() {
  156. if err != nil {
  157. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Abort, pasturePb.ExposeEstrusType_Invalid, req)
  158. s.DB.Table(cowLogs.TableName()).Create(cowLogs)
  159. }
  160. }()
  161. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  162. // 创建牛只流产事件数据
  163. if err = tx.Create(newEventAbortion).Error; err != nil {
  164. return xerr.WithStack(err)
  165. }
  166. // 更新牛只状态
  167. if err = tx.Model(new(model.Cow)).
  168. Select("is_pregnant", "last_abortion_at", "breed_status").
  169. Where("id = ?", req.CowId).
  170. Updates(cow).Error; err != nil {
  171. return xerr.WithStack(err)
  172. }
  173. // 更新最近一次配种结果为流产
  174. if err = tx.Model(new(model.EventMating)).
  175. Select("mating_result", "mating_result_at").
  176. Where("id = ?", lastCowMating.Id).
  177. Updates(lastCowMating).Error; err != nil {
  178. }
  179. return nil
  180. }); err != nil {
  181. return xerr.WithStack(err)
  182. }
  183. return nil
  184. }
  185. func (s *StoreEntry) AbortionCreateBatch(ctx context.Context, req *pasturePb.EventAbortionBatch) error {
  186. if len(req.Item) <= 0 {
  187. return xerr.WithStack(xerr.New("流产数据不能为空"))
  188. }
  189. errors := make(map[int32]error)
  190. for _, v := range req.Item {
  191. if err := s.AbortionCreate(ctx, v); err != nil {
  192. errors[v.CowId] = err
  193. }
  194. }
  195. if len(errors) > 0 {
  196. b, _ := json.Marshal(errors)
  197. return xerr.WithStack(xerr.Customf("部分流产数据创建失败: %s", string(b)))
  198. }
  199. return nil
  200. }
  201. func (s *StoreEntry) AbortionList(
  202. ctx context.Context,
  203. req *pasturePb.SearchEventRequest,
  204. pagination *pasturePb.PaginationModel,
  205. ) (*pasturePb.EventAbortionResponse, error) {
  206. userModel, err := s.GetUserModel(ctx)
  207. if err != nil {
  208. return nil, xerr.WithStack(err)
  209. }
  210. abortionList := make([]*model.EventAbortion, 0)
  211. var count int64 = 0
  212. pref := s.DB.Model(new(model.EventAbortion)).
  213. Where("pasture_id = ?", userModel.AppPasture.Id)
  214. if len(req.CowId) > 0 {
  215. cowIds := strings.Split(req.CowId, ",")
  216. pref.Where("cow_id IN ?", cowIds)
  217. }
  218. if err = pref.Order("id desc").
  219. Count(&count).Limit(int(pagination.PageSize)).
  220. Offset(int(pagination.PageOffset)).
  221. Find(&abortionList).Error; err != nil {
  222. return nil, xerr.WithStack(err)
  223. }
  224. return &pasturePb.EventAbortionResponse{
  225. Code: http.StatusOK,
  226. Msg: "ok",
  227. Data: &pasturePb.EventAbortionData{
  228. List: model.AbortionSlice(abortionList).ToPB(),
  229. Total: int32(count),
  230. PageSize: pagination.PageSize,
  231. Page: pagination.Page,
  232. },
  233. }, nil
  234. }