event_breed_more.go 8.0 KB

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