event_breed_more.go 9.0 KB

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