event_cow_log.go 8.6 KB

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