event_mating.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package model
  2. import (
  3. "kpt-pasture/util"
  4. "time"
  5. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  6. )
  7. type EventMating struct {
  8. Id int64 `json:"id"`
  9. PastureId int64 `json:"pastureId"`
  10. CowId int64 `json:"cowId"`
  11. DayAge int32 `json:"dayAge"`
  12. Lact int32 `json:"lact"`
  13. PenId int32 `json:"penId"`
  14. PenName string `json:"penName"`
  15. CowType pasturePb.CowType_Kind `json:"cowType"`
  16. CowKind pasturePb.CowKind_Kind `json:"cowKind"`
  17. CalvingAge int32 `json:"calvingAge"`
  18. PlanDay int64 `json:"planDay"`
  19. EndDay int64 `json:"endDay"`
  20. CalvingAt int64 `json:"calvingAt"`
  21. RealityDay int64 `json:"realityDay"`
  22. Status pasturePb.IsShow_Kind `json:"status"`
  23. MatingTimes int32 `json:"matingTimes"`
  24. MatingResult pasturePb.MatingResult_Kind `json:"matingResult"`
  25. MatingResultAt int64 `json:"matingResultAt"`
  26. ExposeEstrusType pasturePb.ExposeEstrusType_Kind `json:"exposeEstrusType"`
  27. FrozenSemenNumber string `json:"frozenSemenNumber"`
  28. OperationId int64 `json:"operationId"`
  29. OperationName string `json:"operationName"`
  30. MessageId int64 `json:"messageId"`
  31. MessageName string `json:"messageName"`
  32. Remarks string `json:"remarks"`
  33. EventEstrusId int64 `json:"eventEstrusId"`
  34. CreatedAt int64 `json:"createdAt"`
  35. UpdatedAt int64 `json:"updatedAt"`
  36. }
  37. func (e *EventMating) TableName() string {
  38. return "event_mating"
  39. }
  40. func (e *EventMating) EventUpdate(matingAt int64, bullNumber string, isReMating bool, operationUser, currentUser *SystemUser) {
  41. e.MatingResult = pasturePb.MatingResult_Unknown
  42. e.Status = pasturePb.IsShow_Ok
  43. e.RealityDay = matingAt
  44. e.FrozenSemenNumber = bullNumber
  45. e.OperationName = operationUser.Name
  46. e.OperationId = operationUser.Id
  47. e.MessageName = currentUser.Name
  48. e.MessageId = currentUser.Id
  49. if !isReMating {
  50. e.MatingTimes += 1
  51. }
  52. }
  53. // EventMatingResultUpdate 配种结果更新
  54. func (e *EventMating) EventMatingResultUpdate(matingResult pasturePb.MatingResult_Kind, resultAt int64) {
  55. e.MatingResult = matingResult
  56. e.MatingResultAt = resultAt
  57. }
  58. // IsReMating 判断是不是复配
  59. func (e *EventMating) IsReMating(cow *Cow, matingAt int64) bool {
  60. lastMatingAt := time.Unix(cow.LastMatingAt, 0)
  61. currentMatingAt := time.Unix(matingAt, 0)
  62. daysBetween := util.DaysBetween(currentMatingAt.Unix(), lastMatingAt.Unix())
  63. if (currentMatingAt.Format(LayoutDate2) == lastMatingAt.Format(LayoutDate2) || daysBetween == 1) && e.Status == pasturePb.IsShow_Ok && e.MatingResult == pasturePb.MatingResult_Unknown {
  64. return true
  65. }
  66. return false
  67. }
  68. // IsEmptyMating 判断上次配种结果是不是空怀
  69. func (e *EventMating) IsEmptyMating(cow *Cow, matingAt int64) bool {
  70. lastMatingAt := time.Unix(cow.LastMatingAt, 0)
  71. currentMatingAt := time.Unix(matingAt, 0)
  72. daysBetween := util.DaysBetween(currentMatingAt.Unix(), lastMatingAt.Unix())
  73. if (e.MatingResult == pasturePb.MatingResult_Unknown || e.MatingResult == pasturePb.MatingResult_ReMatch) && daysBetween >= 2 {
  74. return true
  75. }
  76. return false
  77. }
  78. func NewEventMating(pastureId int64, cow *Cow, planDay int64, exposeEstrusType pasturePb.ExposeEstrusType_Kind) *EventMating {
  79. return &EventMating{
  80. PastureId: pastureId,
  81. CowId: cow.Id,
  82. Lact: cow.Lact,
  83. PenId: cow.PenId,
  84. PenName: cow.PenName,
  85. CowType: cow.CowType,
  86. CowKind: cow.CowKind,
  87. CalvingAt: cow.LastMatingAt,
  88. PlanDay: planDay,
  89. EndDay: planDay,
  90. MatingResult: pasturePb.MatingResult_Unknown,
  91. ExposeEstrusType: exposeEstrusType,
  92. Status: pasturePb.IsShow_No,
  93. }
  94. }
  95. // NewEventMatingNaturalEstrus 自然发情的牛只
  96. func NewEventMatingNaturalEstrus(cow *Cow, req *pasturePb.EventMating, currentUser *SystemUser) *EventMating {
  97. return &EventMating{
  98. PastureId: currentUser.PastureId,
  99. CowId: cow.Id,
  100. Lact: cow.Lact,
  101. DayAge: cow.GetDayAge(),
  102. CowType: cow.CowType,
  103. CowKind: cow.CowKind,
  104. CalvingAt: cow.LastMatingAt,
  105. PlanDay: int64(req.MatingAt),
  106. RealityDay: int64(req.MatingAt),
  107. EndDay: int64(req.MatingAt),
  108. MatingResult: pasturePb.MatingResult_Unknown,
  109. ExposeEstrusType: pasturePb.ExposeEstrusType_Natural_Estrus,
  110. Status: pasturePb.IsShow_Ok,
  111. OperationId: int64(req.OperationId),
  112. OperationName: req.OperationName,
  113. MessageId: currentUser.Id,
  114. MessageName: currentUser.Name,
  115. FrozenSemenNumber: req.FrozenSemenNumber,
  116. Remarks: req.Remarks,
  117. MatingTimes: cow.MatingTimes + 1,
  118. }
  119. }
  120. // NewEventMatingList 同情配种
  121. func NewEventMatingList(pastureId int64, cowList []*Cow, planDay int64, exposeEstrusType pasturePb.ExposeEstrusType_Kind) []*EventMating {
  122. var matingList []*EventMating
  123. for _, cow := range cowList {
  124. matingList = append(matingList, NewEventMating(pastureId, cow, planDay, exposeEstrusType))
  125. }
  126. return matingList
  127. }
  128. type EventMatingSlice []*EventMating
  129. func (e EventMatingSlice) ToPB(exposeEstrusTypeMap map[pasturePb.ExposeEstrusType_Kind]string) []*pasturePb.SearchMatingList {
  130. res := make([]*pasturePb.SearchMatingList, len(e))
  131. for i, v := range e {
  132. res[i] = &pasturePb.SearchMatingList{
  133. Id: int32(v.Id),
  134. CowId: int32(v.CowId),
  135. DayAge: int32(v.DayAge),
  136. Lact: int32(v.Lact),
  137. CalvingAge: v.CalvingAge,
  138. PlanDay: time.Unix(v.PlanDay, 0).Format(LayoutDate2),
  139. RealityDay: time.Unix(v.RealityDay, 0).Format(LayoutDate2),
  140. ExposeEstrusType: v.ExposeEstrusType,
  141. ExposeEstrusTypeName: exposeEstrusTypeMap[v.ExposeEstrusType],
  142. FrozenSemenNumber: v.FrozenSemenNumber,
  143. Remarks: v.Remarks,
  144. OperationId: int32(v.OperationId),
  145. OperationName: v.OperationName,
  146. CreatedAt: int32(v.CreatedAt),
  147. UpdatedAt: int32(v.UpdatedAt),
  148. }
  149. }
  150. return res
  151. }
  152. type CowMatingBody struct {
  153. Id int64 `json:"id"`
  154. CowId int64 `json:"cowId"`
  155. BreedStatus pasturePb.BreedStatus_Kind `json:"breedStatus"`
  156. BreedStatusName string `json:"breedStatusName"`
  157. CowType pasturePb.CowType_Kind `json:"cowType"`
  158. CowTypeName string `json:"cowTypeName"`
  159. PenId int32 `json:"penId"`
  160. PenName string `json:"penName"`
  161. Lact int32 `json:"lact"`
  162. CalvingAge int32 `json:"calvingAge"`
  163. AbortionAge int32 `json:"abortionAge"`
  164. DayAge int32 `json:"dayAge"`
  165. Status pasturePb.IsShow_Kind `json:"status"`
  166. }
  167. type CowMatingBodySlice []*CowMatingBody
  168. func (s CowMatingBodySlice) ToPB(
  169. cowTypeMap map[pasturePb.CowType_Kind]string,
  170. breedStatusMap map[pasturePb.BreedStatus_Kind]string,
  171. penMap map[int32]*Pen,
  172. ) []*CowMatingBody {
  173. res := make([]*CowMatingBody, len(s))
  174. for i, v := range s {
  175. res[i] = &CowMatingBody{
  176. Id: v.Id,
  177. CowId: v.CowId,
  178. CowType: v.CowType,
  179. CowTypeName: cowTypeMap[v.CowType],
  180. BreedStatus: v.BreedStatus,
  181. BreedStatusName: breedStatusMap[v.BreedStatus],
  182. PenName: penMap[v.PenId].Name,
  183. PenId: v.PenId,
  184. Lact: v.Lact,
  185. CalvingAge: v.CalvingAge,
  186. AbortionAge: v.AbortionAge,
  187. DayAge: v.DayAge,
  188. Status: v.Status,
  189. }
  190. }
  191. return res
  192. }
  193. type MatingTimelyChart struct {
  194. CalvingAge int32 `json:"calvingAge"`
  195. RealityDay string `json:"realityDay"`
  196. LactGroup string `json:"lactGroup"`
  197. }
  198. func (e EventMatingSlice) ToPB2() []*pasturePb.CowList {
  199. res := make([]*pasturePb.CowList, len(e))
  200. for i, v := range e {
  201. calvingAt, matingAtFormat := "", ""
  202. if v.CalvingAt > 0 {
  203. calvingAt = time.Unix(v.CalvingAt, 0).Format(LayoutDate2)
  204. }
  205. if v.RealityDay > 0 {
  206. matingAtFormat = time.Unix(v.RealityDay, 0).Format(LayoutDate2)
  207. }
  208. res[i] = &pasturePb.CowList{
  209. CowId: int32(v.CowId),
  210. DayAge: int32(v.DayAge),
  211. CalvingAge: v.CalvingAge,
  212. MatingAtFormat: matingAtFormat,
  213. CalvingAtFormat: calvingAt,
  214. Lact: int32(v.Lact),
  215. }
  216. }
  217. return res
  218. }
  219. type MatingTimelyResponse struct {
  220. Code int32 `json:"code"`
  221. Message string `json:"message"`
  222. Data *MatingTimelyData `json:"data"`
  223. }
  224. type MatingTimelyData struct {
  225. CowList []*pasturePb.CowList `json:"cowList"`
  226. Chart *CowMatingChart `json:"chart"`
  227. }
  228. type CowMatingChart struct {
  229. Lact0 [][]string `json:"lact0"`
  230. Lact1 [][]string `json:"lact1"`
  231. Lact2 [][]string `json:"lact2"`
  232. Lact3 [][]string `json:"lact3"`
  233. }
  234. // MultiFactorPregnancyRateResponse 多维度受胎率
  235. type MultiFactorPregnancyRateResponse struct {
  236. Code int32 `json:"code"`
  237. Message string `json:"message"`
  238. Data *MultiFactorPregnancyRateData `json:"data"`
  239. }
  240. // MultiFactorPregnancyRateList 多维度受胎率分析
  241. type MultiFactorPregnancyRateList struct {
  242. StatisticMethod1 string `json:"statisticMethod1"` // 统计方式名称1 (月度、品种)
  243. StatisticMethod2 string `json:"statisticMethod2"` // 统计方式名称2 (月度、品种)
  244. PregnantRate float32 `json:"pregnantRate"` // 受胎率%(怀孕数 / 怀孕数 + 空怀数)
  245. PregnantCount int32 `json:"pregnantCount"` // 怀孕总数
  246. EmptyPregnantCount int32 `json:"emptyPregnantCount"` // 空怀数
  247. OtherCount int32 `json:"otherCount"` // 其他数 (配种后结果未知的个数,小于等于三次配种后,尚未孕检已经淘汰的个数)
  248. AbortionCount int32 `json:"abortionCount"` // 流产数 (已经怀孕后流产的个数)
  249. TotalCount int32 `json:"totalCount"` // 合计( 怀孕总数+空怀数+其他数)
  250. SpcRate float32 `json:"spcRate"` // spc(1 / 受胎率)
  251. Months string `json:"months"` // 月份
  252. OperationName string `json:"operationName"` // 配种员名称
  253. Bull string `json:"bull"` // 公牛
  254. Lact string `json:"lact"` // 胎次
  255. MatingTimes string `json:"matingTimes"` // 配次
  256. ExposeEstrusType string `json:"exposeEstrusType"` // 发情揭发方式
  257. Week string `json:"week"` // 周
  258. }
  259. type MultiFactorPregnancyRateData struct {
  260. Total int32 `json:"total"`
  261. PageSize int32 `json:"pageSize"`
  262. Page int32 `json:"page"`
  263. List []*MultiFactorPregnancyRateList `json:"list"`
  264. Chart *MultiFactorPregnancyRateChart `json:"chart"`
  265. }
  266. type MultiFactorPregnancyRateChart struct {
  267. Header []string `json:"header"` // 标题头
  268. PregnantRateMap map[string]map[string]string `json:"pregnantRateMap"`
  269. KepMap []string `json:"kepMap"`
  270. }