event_cow_log.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(ctx context.Context, pastureId int64, cow *model.Cow, eventType pasturePb.EventType_Kind, req interface{}) *model.EventCowLog {
  13. var (
  14. desc, remarks = "", ""
  15. eventTypeName = s.EventTypeMap()[eventType]
  16. eventAt = int64(0)
  17. eventCategoryMap = s.eventCategoryMap()
  18. penMap = s.PenMap(ctx, pastureId)
  19. cowTypeMap = s.CowTypeMap()
  20. operationUser = &model.SystemUser{}
  21. )
  22. switch eventType {
  23. case pasturePb.EventType_Enter:
  24. data := req.(*pasturePb.EventEnterRequest)
  25. eventAt = int64(data.EnterAt)
  26. remarks = data.Remarks
  27. sourceMap := s.CowSourceMap()
  28. sex := "公"
  29. if data.Sex == pasturePb.Genders_Female {
  30. sex = "母"
  31. }
  32. desc = fmt.Sprintf("性别: %s; 栏舍: %s; 体重: %s kg; price: %s; 来源: %s;", sex, penMap[cow.PenId].Name, strconv.FormatFloat(float64(data.Weight), 'f', 2, 64),
  33. strconv.FormatFloat(float64(data.Price), 'f', 2, 64), sourceMap[cow.SourceId])
  34. case pasturePb.EventType_Transfer_Ben:
  35. data := req.(*model.EventTransferGroup)
  36. transferAt, _ := time.Parse(model.LayoutTime, data.TransferDate)
  37. eventAt = transferAt.Unix()
  38. remarks = data.Remarks
  39. operationUser.Id = data.OperationId
  40. operationUser.Name = data.OperationName
  41. desc = fmt.Sprintf("转出栏舍: %s; 转入栏舍: %s", penMap[data.PenOutId].Name, penMap[data.PenInId].Name)
  42. case pasturePb.EventType_Body_Score:
  43. case pasturePb.EventType_Disease:
  44. data := req.(*model.EventCowDisease)
  45. eventAt = data.DiseaseAt
  46. remarks = data.Remarks
  47. operationUser.Id = int64(data.OperationId)
  48. operationUser.Name = data.OperationName
  49. desc = fmt.Sprintf("疾病名称: %s; 状态: 治疗中;", data.DiseaseName)
  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()[data.ExposeEstrusType], isMating)
  76. operationUser.Id = 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. if data.CalvingLevel > pasturePb.CalvingLevel_Natural_Childbirth {
  84. desc += fmt.Sprintf("难产原因: %s", s.DystociaReasonMap()[data.DystociaReason])
  85. }
  86. ly, qy, die := 0, 0, 0
  87. earNumber := ""
  88. if len(data.CalfItemList) > 0 {
  89. for _, v := range data.CalfItemList {
  90. if v.IsLive == pasturePb.IsShow_Ok && v.IsAdoption == pasturePb.IsShow_No {
  91. qy++
  92. }
  93. if v.IsLive == pasturePb.IsShow_No {
  94. die++
  95. }
  96. if v.IsAdoption == pasturePb.IsShow_Ok {
  97. ly++
  98. earNumber += fmt.Sprintf("犊牛耳号: %s,出生体重: %s KG;", v.EarNumber, strconv.FormatFloat(float64(v.Weight), 'f', 2, 64))
  99. }
  100. }
  101. }
  102. desc += fmt.Sprintf("留养: %d个 %s;弃养: %d; 死亡: %d个", ly, earNumber, qy, die)
  103. operationUser.Id = int64(data.OperationId)
  104. operationUser.Name = data.OperationName
  105. remarks = data.Remarks
  106. case pasturePb.EventType_Seme_Time:
  107. data := req.(*pasturePb.EventSameTime)
  108. eventAt = int64(data.SameTimeAt)
  109. desc = fmt.Sprintf("同期名称: %s;激素名称: %s;剂量:%f;操作人: %s", data.SameTimeTypeName, data.DrugsName, data.Usage, data.OperationName)
  110. operationUser.Id = int64(data.OperationId)
  111. operationUser.Name = data.OperationName
  112. remarks = data.Remarks
  113. case pasturePb.EventType_Mating:
  114. data := req.(*model.EventMatingCheckBatchModel)
  115. eventAt = data.MatingAt
  116. desc = fmt.Sprintf("配种公牛: %s;配种结果:未知", data.FrozenSemen.BullId)
  117. operationUser.Id = data.OperationUser.Id
  118. operationUser.Name = data.OperationUser.Name
  119. remarks = data.Remarks
  120. case pasturePb.EventType_Birth:
  121. eventAt = cow.BirthAt
  122. desc = fmt.Sprintf("出生体重: %s KG;母号:%s;父号: %s", strconv.FormatFloat(float64(cow.BirthWeight)/1000, 'f', 2, 64), cow.MotherNumber, cow.FatherNumber)
  123. case pasturePb.EventType_Death:
  124. data := req.(*model.EventDeparture)
  125. eventAt = data.DepartureAt
  126. desc = fmt.Sprintf("死亡原因: %s", data.ReasonName)
  127. operationUser.Id = data.OperationId
  128. operationUser.Name = data.OperationName
  129. case pasturePb.EventType_Transfer_Out:
  130. case pasturePb.EventType_Transfer_In:
  131. case pasturePb.EventType_Out:
  132. data := req.(*model.EventDeparture)
  133. eventAt = data.DepartureAt
  134. desc = fmt.Sprintf("淘汰原因: %s", data.ReasonName)
  135. operationUser.Id = data.OperationId
  136. operationUser.Name = data.OperationName
  137. case pasturePb.EventType_Immunication:
  138. data := req.(*model.EventImmunizationPlan)
  139. eventAt = data.RealityDay
  140. desc = fmt.Sprintf("免疫名称: %s;药物名称: %s;剂量: %d %s", data.PlanName, data.DrugsName, data.Usage, data.UnitName)
  141. operationUser.Id = data.OperationId
  142. operationUser.Name = data.OperationName
  143. case pasturePb.EventType_Weaning:
  144. data := req.(*pasturePb.EventWeaningBatch)
  145. eventAt = int64(data.WeaningAt)
  146. operationUser.Id = int64(data.OperationId)
  147. operationUser.Name = data.OperationName
  148. remarks = data.Remarks
  149. for _, v := range data.Items {
  150. if int64(v.CowId) == cow.Id {
  151. desc = fmt.Sprintf("具体体重: %s", strconv.FormatFloat(float64(v.Weight), 'f', 2, 64))
  152. break
  153. }
  154. }
  155. case pasturePb.EventType_Sale:
  156. data := req.(*pasturePb.EventCowSale)
  157. eventAt = int64(data.SaleAt)
  158. for _, v := range data.CowIds {
  159. if int64(v) == cow.Id {
  160. desc = fmt.Sprintf("经销商: %s,单价: %s", data.DealerName, strconv.FormatFloat(float64(data.SalePrice), 'f', 2, 64))
  161. break
  162. }
  163. }
  164. case pasturePb.EventType_Abort:
  165. data := req.(*AbortionCheckBatchModel)
  166. eventAt = data.EventAbortion.AbortionAt
  167. operationUser.Id = data.OperationUser.Id
  168. operationUser.Name = data.OperationUser.Name
  169. remarks = data.EventAbortion.Remarks
  170. desc = fmt.Sprintf("流产原因: %s", data.EventAbortion.AbortionReasonsName)
  171. case pasturePb.EventType_Weight:
  172. data := req.(*pasturePb.EventWeight)
  173. eventAt = int64(data.WeightAt)
  174. operationUser.Id = int64(data.OperationId)
  175. operationUser.Name = data.OperationName
  176. remarks = data.Remarks
  177. desc = fmt.Sprintf("日龄: %d;体重: %s kg", cow.DayAge, strconv.FormatFloat(float64(data.Weight), 'f', 2, 64))
  178. case pasturePb.EventType_Castrated:
  179. case pasturePb.EventType_Insect_Repellent:
  180. }
  181. newEventCowLogModel := &model.EventCowLogModel{
  182. Cow: cow,
  183. CowTypeName: cowTypeMap[cow.CowType],
  184. OperationUser: operationUser,
  185. EventCategoryKind: eventCategoryMap[eventType],
  186. EventAt: eventAt,
  187. EventType: eventType,
  188. EventTypeName: eventTypeName,
  189. Description: desc,
  190. Remarks: remarks,
  191. PenName: penMap[cow.PenId].Name,
  192. }
  193. return model.NewEventCowLog(newEventCowLogModel)
  194. }
  195. func (s *StoreEntry) UpdateMatingResultEventCowLogByCowId(ctx context.Context, cowId int64, newResult string) error {
  196. newEventCowLog := &model.EventCowLog{CowId: cowId}
  197. if err := s.DB.Table(newEventCowLog.TableName()).
  198. Where("cow_id = ?", cowId).
  199. Where("event_category_kind = ?", pasturePb.EventCategory_Breed).
  200. Where("event_type = ?", pasturePb.EventType_Mating).
  201. Order("id desc").
  202. First(newEventCowLog).Error; err != nil {
  203. return xerr.WithStack(err)
  204. } else {
  205. desc := strings.ReplaceAll(newEventCowLog.EventDescription, "未知", newResult)
  206. if err = s.DB.Table(newEventCowLog.TableName()).
  207. Where("id = ?", newEventCowLog.Id).
  208. Update("event_description", desc).Error; err != nil {
  209. return xerr.WithStack(err)
  210. }
  211. }
  212. return nil
  213. }