event_mating.go 12 KB

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