event_cow_log.go 8.2 KB

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