event_cow_log.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  11. )
  12. func (s *StoreEntry) SubmitEventLog(
  13. ctx context.Context,
  14. pastureId int64,
  15. cow *model.Cow,
  16. eventType pasturePb.EventType_Kind,
  17. exposeEstrusType pasturePb.ExposeEstrusType_Kind,
  18. req interface{},
  19. ) *model.EventCowLog {
  20. var (
  21. desc, remarks = "", ""
  22. eventTypeName = s.EventTypeMap()[eventType]
  23. eventAt = int64(0)
  24. eventCategoryMap = s.eventCategoryMap()
  25. penMap = s.PenMap(ctx, pastureId)
  26. cowTypeMap = s.CowTypeMap()
  27. operationUser = &model.SystemUser{}
  28. )
  29. switch eventType {
  30. case pasturePb.EventType_Enter:
  31. data := req.(*pasturePb.EventEnterRequest)
  32. eventAt = int64(data.EnterAt)
  33. remarks = data.Remarks
  34. sourceMap := s.CowSourceMap()
  35. sex := "公"
  36. if data.Sex == pasturePb.Genders_Female {
  37. sex = "母"
  38. }
  39. desc = fmt.Sprintf("性别: %s; 栏舍: %s; 体重: %s kg; price: %s; 来源: %s;", sex, penMap[cow.PenId].Name, strconv.FormatFloat(float64(data.Weight), 'f', 2, 64),
  40. strconv.FormatFloat(float64(data.Price), 'f', 2, 64), sourceMap[cow.SourceId])
  41. case pasturePb.EventType_Transfer_Ben:
  42. data := req.(*model.EventTransferGroup)
  43. transferAt, _ := time.Parse(model.LayoutDate2, data.TransferDate)
  44. eventAt = transferAt.Unix()
  45. remarks = data.Remarks
  46. operationUser.Id = data.OperationId
  47. operationUser.Name = data.OperationName
  48. desc = fmt.Sprintf("转出栏舍: %s; 转入栏舍: %s", penMap[data.PenOutId].Name, penMap[data.PenInId].Name)
  49. case pasturePb.EventType_Body_Score:
  50. case pasturePb.EventType_Pregnancy_Check:
  51. data := req.(*PregnantCheckBatchModel)
  52. eventAt = int64(data.PregnantCheckAt)
  53. if data.PregnantCheckResult == pasturePb.PregnantCheckResult_Pregnant {
  54. desc += fmt.Sprintf("孕检方式: %s; 孕检结果: %s", s.PregnantCheckMethodMap()[data.PregnantCheckMethod], s.MatingResultMap()[pasturePb.MatingResult_Pregnant])
  55. }
  56. if data.PregnantCheckResult == pasturePb.PregnantCheckResult_UnPregnant {
  57. desc += fmt.Sprintf("孕检方式: %s", s.PregnantCheckMethodMap()[data.PregnantCheckMethod])
  58. if cow.BreedStatus == pasturePb.BreedStatus_Abort {
  59. desc += fmt.Sprintf("; 复检结果: %s", s.MatingResultMap()[pasturePb.MatingResult_Abort])
  60. }
  61. if cow.BreedStatus == pasturePb.BreedStatus_Empty {
  62. desc += fmt.Sprintf("; 孕检结果: %s", s.MatingResultMap()[pasturePb.MatingResult_Empty])
  63. }
  64. }
  65. operationUser.Id = data.OperationUser.Id
  66. operationUser.Name = data.OperationUser.Name
  67. remarks = data.Remarks
  68. case pasturePb.EventType_Estrus:
  69. data := req.(*model.EventEstrus)
  70. eventAt = data.PlanDay
  71. isMating := "否"
  72. if data.IsMating == pasturePb.IsShow_Ok {
  73. isMating = "是"
  74. }
  75. desc = fmt.Sprintf("发情揭发方式:%s;是否配种:%s;", s.ExposeEstrusTypeMap()[exposeEstrusType], isMating)
  76. operationUser.Id = int64(data.OperationId)
  77. operationUser.Name = data.OperationName
  78. remarks = data.Remarks
  79. case pasturePb.EventType_Calving:
  80. data := req.(*pasturePb.EventCalving)
  81. eventAt = int64(data.CalvingAt)
  82. desc = fmt.Sprintf("怀孕天数:%d;难产等级: %s;产子数量: %d;", cow.PregnancyAge, s.CalvingLevelMap()[data.CalvingLevel], data.ChildNumber)
  83. for _, v := range data.CalfItemList {
  84. if v.CowId > 0 {
  85. desc += fmt.Sprintf("犊牛耳号: %s; 出生体重: %s KG;", v.EarNumber, strconv.FormatFloat(float64(v.Weight), 'f', 2, 64))
  86. }
  87. }
  88. if data.IsInducingChildbirth == pasturePb.IsShow_Ok {
  89. desc += fmt.Sprintf("难产原因: %s", s.DystociaReasonMap()[data.DystociaReason])
  90. }
  91. operationUser.Id = int64(data.OperationId)
  92. operationUser.Name = data.OperationName
  93. remarks = data.Remarks
  94. case pasturePb.EventType_Seme_Time:
  95. data := req.(*pasturePb.EventSameTime)
  96. eventAt = int64(data.SameTimeAt)
  97. desc = fmt.Sprintf("同期名称: %s;激素名称: %s;剂量:%f;操作人: %s", data.SameTimeTypeName, data.DrugsName, data.Usage, data.OperationName)
  98. operationUser.Id = int64(data.OperationId)
  99. operationUser.Name = data.OperationName
  100. remarks = data.Remarks
  101. case pasturePb.EventType_Mating:
  102. data := req.(*pasturePb.EventMating)
  103. eventAt = int64(data.MatingAt)
  104. desc = fmt.Sprintf("配种公牛: %s;配种结果:未知", data.FrozenSemenNumber)
  105. operationUser.Id = int64(data.OperationId)
  106. operationUser.Name = data.OperationName
  107. remarks = data.Remarks
  108. case pasturePb.EventType_Birth:
  109. eventAt = cow.BirthAt
  110. desc = fmt.Sprintf("出生体重: %s KG;母号:%s;父号: %s", strconv.FormatFloat(float64(cow.BirthWeight)/1000, 'f', 2, 64), cow.MotherNumber, cow.LastBullNumber)
  111. case pasturePb.EventType_Death:
  112. data := req.(*model.EventDeparture)
  113. eventAt = data.DepartureAt
  114. desc = fmt.Sprintf("死亡原因: %s", data.ReasonName)
  115. operationUser.Id = data.OperationId
  116. operationUser.Name = data.OperationName
  117. case pasturePb.EventType_Transfer_Out:
  118. case pasturePb.EventType_Transfer_In:
  119. case pasturePb.EventType_Out:
  120. data := req.(*model.EventDeparture)
  121. eventAt = data.DepartureAt
  122. desc = fmt.Sprintf("淘汰原因: %s", data.ReasonName)
  123. operationUser.Id = data.OperationId
  124. operationUser.Name = data.OperationName
  125. case pasturePb.EventType_Immunication:
  126. case pasturePb.EventType_Weaning:
  127. data := req.(*pasturePb.EventWeaningBatchRequest)
  128. eventAt = int64(data.WeaningAt)
  129. operationUser.Id = int64(data.OperationId)
  130. operationUser.Name = data.OperationName
  131. remarks = data.Remarks
  132. for _, v := range data.Item {
  133. if int64(v.CowId) == cow.Id {
  134. desc = fmt.Sprintf("具体体重: %f", v.Weight)
  135. break
  136. }
  137. }
  138. case pasturePb.EventType_Sale:
  139. case pasturePb.EventType_Abort:
  140. data := req.(*pasturePb.EventAbortionItem)
  141. eventAt = int64(data.AbortionAt)
  142. operationUser.Id = int64(data.OperationId)
  143. operationUser.Name = data.OperationName
  144. remarks = data.Remarks
  145. desc = fmt.Sprintf("流产原因: %s", s.AbortionReasonsMap()[data.AbortionReasons])
  146. case pasturePb.EventType_Weight:
  147. data := req.(*pasturePb.EventWeight)
  148. eventAt = int64(data.WeightAt)
  149. operationUser.Id = int64(data.OperationId)
  150. operationUser.Name = data.OperationName
  151. remarks = data.Remarks
  152. desc = fmt.Sprintf("日龄: %d;体重: %s kg", cow.DayAge, strconv.FormatFloat(float64(data.Weight), 'f', 2, 64))
  153. case pasturePb.EventType_Castrated:
  154. case pasturePb.EventType_Insect_Repellent:
  155. }
  156. newEventCowLogModel := &model.EventCowLogModel{
  157. Cow: cow,
  158. CowTypeName: cowTypeMap[cow.CowType],
  159. OperationUser: operationUser,
  160. EventCategoryKind: eventCategoryMap[eventType],
  161. EventAt: eventAt,
  162. EventType: eventType,
  163. EventTypeName: eventTypeName,
  164. Description: desc,
  165. Remarks: remarks,
  166. PenName: penMap[cow.PenId].Name,
  167. }
  168. return model.NewEventCowLog(newEventCowLogModel)
  169. }
  170. func (s *StoreEntry) UpdateMatingResultEventCowLogByCowId(ctx context.Context, cowId int64, newResult string) error {
  171. newEventCowLog := &model.EventCowLog{CowId: cowId}
  172. if err := s.DB.Table(newEventCowLog.TableName()).
  173. Where("cow_id = ?", cowId).
  174. Where("event_category_kind = ?", pasturePb.EventCategory_Breed).
  175. Where("event_type = ?", pasturePb.EventType_Mating).
  176. Order("id desc").
  177. First(newEventCowLog).Error; err != nil {
  178. return xerr.WithStack(err)
  179. } else {
  180. desc := strings.ReplaceAll(newEventCowLog.EventDescription, "未知", newResult)
  181. if err = s.DB.Table(newEventCowLog.TableName()).
  182. Where("id = ?", newEventCowLog.Id).
  183. Update("event_description", desc).Error; err != nil {
  184. return xerr.WithStack(err)
  185. }
  186. }
  187. return nil
  188. }