event_mating.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. 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(cowInfo *Cow, 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. e.DayAge = cowInfo.GetEventDayAge(matingAt)
  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).Local()
  68. currentMatingAt := time.Unix(matingAt, 0).Local()
  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).Local()
  85. currentMatingAt := time.Unix(matingAt, 0).Local()
  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 + 86399,
  106. MatingResult: pasturePb.MatingResult_Unknown,
  107. ExposeEstrusType: exposeEstrusType,
  108. Status: pasturePb.IsShow_No,
  109. }
  110. }
  111. // NewEventMatingNaturalEstrus 自然发情的牛只
  112. func NewEventMatingNaturalEstrus(pastureId int64, item *EventMatingCheckBatchModel, currentUser *SystemUser) *EventMating {
  113. return &EventMating{
  114. PastureId: pastureId,
  115. CowId: item.Cow.Id,
  116. EarNumber: item.Cow.EarNumber,
  117. Lact: item.Cow.Lact,
  118. DayAge: item.Cow.GetDayAge(),
  119. CowType: item.Cow.CowType,
  120. CowKind: item.Cow.CowKind,
  121. CalvingAt: item.Cow.LastMatingAt,
  122. PlanDay: item.MatingAt,
  123. RealityDay: item.MatingAt,
  124. EndDay: item.MatingAt,
  125. MatingResult: pasturePb.MatingResult_Unknown,
  126. ExposeEstrusType: pasturePb.ExposeEstrusType_Natural_Estrus,
  127. Status: pasturePb.IsShow_Ok,
  128. OperationId: item.OperationUser.Id,
  129. OperationName: item.OperationUser.Name,
  130. MessageId: currentUser.Id,
  131. MessageName: currentUser.Name,
  132. FrozenSemenNumber: item.FrozenSemen.BullId,
  133. Remarks: item.Remarks,
  134. MatingTimes: item.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).Local().Format(LayoutDate2),
  157. RealityDay: time.Unix(v.RealityDay, 0).Local().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. func (e EventMatingSlice) ToPB2() []*pasturePb.CowList {
  171. res := make([]*pasturePb.CowList, len(e))
  172. for i, v := range e {
  173. calvingAt, matingAtFormat := "", ""
  174. if v.CalvingAt > 0 {
  175. calvingAt = time.Unix(v.CalvingAt, 0).Local().Format(LayoutDate2)
  176. }
  177. if v.RealityDay > 0 {
  178. matingAtFormat = time.Unix(v.RealityDay, 0).Local().Format(LayoutDate2)
  179. }
  180. res[i] = &pasturePb.CowList{
  181. CowId: int32(v.CowId),
  182. DayAge: int32(v.DayAge),
  183. CalvingAge: v.CalvingAge,
  184. MatingAtFormat: matingAtFormat,
  185. CalvingAtFormat: calvingAt,
  186. Lact: int32(v.Lact),
  187. }
  188. }
  189. return res
  190. }
  191. func (e EventMatingSlice) ToPB3(isMating bool, cowTypeMap map[pasturePb.CowType_Kind]string) []*pasturePb.TwentyOnePregnantItems {
  192. res := make([]*pasturePb.TwentyOnePregnantItems, 0)
  193. for _, v := range e {
  194. if !isMating {
  195. continue
  196. }
  197. res = append(res, &pasturePb.TwentyOnePregnantItems{
  198. CowId: int32(v.CowId),
  199. EarNumber: v.EarNumber,
  200. CowTypeName: cowTypeMap[v.CowType],
  201. Lact: v.Lact,
  202. })
  203. }
  204. return res
  205. }
  206. func (e EventMatingSlice) ToPB4(cowTypeMap map[pasturePb.CowType_Kind]string) []*pasturePb.TwentyOnePregnantItems {
  207. res := make([]*pasturePb.TwentyOnePregnantItems, 0)
  208. for _, v := range e {
  209. if v.MatingResult != pasturePb.MatingResult_Abort {
  210. continue
  211. }
  212. res = append(res, &pasturePb.TwentyOnePregnantItems{
  213. CowId: int32(v.CowId),
  214. EarNumber: v.EarNumber,
  215. CowTypeName: cowTypeMap[v.CowType],
  216. Lact: v.Lact,
  217. })
  218. }
  219. return res
  220. }
  221. type CowMatingBody struct {
  222. Id int64 `json:"id"`
  223. CowId int64 `json:"cowId"`
  224. BreedStatus pasturePb.BreedStatus_Kind `json:"breedStatus"`
  225. BreedStatusName string `json:"breedStatusName"`
  226. CowType pasturePb.CowType_Kind `json:"cowType"`
  227. CowTypeName string `json:"cowTypeName"`
  228. PenId int32 `json:"penId"`
  229. PenName string `json:"penName"`
  230. Lact int32 `json:"lact"`
  231. CalvingAge int32 `json:"calvingAge"`
  232. AbortionAge int32 `json:"abortionAge"`
  233. DayAge int32 `json:"dayAge"`
  234. Status pasturePb.IsShow_Kind `json:"status"`
  235. }
  236. type CowMatingBodySlice []*CowMatingBody
  237. func (s CowMatingBodySlice) ToPB(
  238. cowTypeMap map[pasturePb.CowType_Kind]string,
  239. breedStatusMap map[pasturePb.BreedStatus_Kind]string,
  240. penMap map[int32]*Pen,
  241. ) []*CowMatingBody {
  242. res := make([]*CowMatingBody, len(s))
  243. for i, v := range s {
  244. res[i] = &CowMatingBody{
  245. Id: v.Id,
  246. CowId: v.CowId,
  247. CowType: v.CowType,
  248. CowTypeName: cowTypeMap[v.CowType],
  249. BreedStatus: v.BreedStatus,
  250. BreedStatusName: breedStatusMap[v.BreedStatus],
  251. PenName: penMap[v.PenId].Name,
  252. PenId: v.PenId,
  253. Lact: v.Lact,
  254. CalvingAge: v.CalvingAge,
  255. AbortionAge: v.AbortionAge,
  256. DayAge: v.DayAge,
  257. Status: v.Status,
  258. }
  259. }
  260. return res
  261. }
  262. type MatingTimelyChart struct {
  263. CalvingAge int32 `json:"calvingAge"`
  264. RealityDay string `json:"realityDay"`
  265. LactGroup string `json:"lactGroup"`
  266. }
  267. type MatingTimelyResponse struct {
  268. Code int32 `json:"code"`
  269. Msg string `json:"msg"`
  270. Data *MatingTimelyData `json:"data"`
  271. }
  272. type MatingTimelyData struct {
  273. CowList []*pasturePb.CowList `json:"cowList"`
  274. Chart *CowMatingChart `json:"chart"`
  275. }
  276. type CowMatingChart struct {
  277. Lact0 [][]string `json:"lact0"`
  278. Lact1 [][]string `json:"lact1"`
  279. Lact2 [][]string `json:"lact2"`
  280. Lact3 [][]string `json:"lact3"`
  281. }
  282. // MultiFactorPregnancyRateResponse 多维度受胎率
  283. type MultiFactorPregnancyRateResponse struct {
  284. Code int32 `json:"code"`
  285. Msg string `json:"msg"`
  286. Data *MultiFactorPregnancyRateData `json:"data"`
  287. }
  288. // MultiFactorPregnancyRateList 多维度受胎率分析
  289. type MultiFactorPregnancyRateList struct {
  290. StatisticMethod1 string `json:"statisticMethod1"` // 统计方式名称1 (月度、品种)
  291. StatisticMethod2 string `json:"statisticMethod2"` // 统计方式名称2 (月度、品种)
  292. PregnantRate float32 `json:"pregnantRate"` // 受胎率%(怀孕数 / 怀孕数 + 空怀数)
  293. PregnantCount int32 `json:"pregnantCount"` // 怀孕总数
  294. EmptyPregnantCount int32 `json:"emptyPregnantCount"` // 空怀数
  295. OtherCount int32 `json:"otherCount"` // 其他数 (配种后结果未知的个数,小于等于三次配种后,尚未孕检已经淘汰的个数)
  296. AbortionCount int32 `json:"abortionCount"` // 流产数 (已经怀孕后流产的个数)
  297. TotalCount int32 `json:"totalCount"` // 合计( 怀孕总数+空怀数+其他数)
  298. SpcRate float32 `json:"spcRate"` // spc(1 / 受胎率)
  299. Months string `json:"months"` // 月份
  300. OperationName string `json:"operationName"` // 配种员名称
  301. Bull string `json:"bull"` // 公牛
  302. Lact string `json:"lact"` // 胎次
  303. MatingTimes string `json:"matingTimes"` // 配次
  304. ExposeEstrusType string `json:"exposeEstrusType"` // 发情揭发方式
  305. Week string `json:"week"` // 周
  306. }
  307. type MultiFactorPregnancyRateData struct {
  308. Total int32 `json:"total"`
  309. PageSize int32 `json:"pageSize"`
  310. Page int32 `json:"page"`
  311. List []*MultiFactorPregnancyRateList `json:"list"`
  312. Chart *MultiFactorPregnancyRateChart `json:"chart"`
  313. }
  314. type MultiFactorPregnancyRateChart struct {
  315. Header []string `json:"header"` // 标题头
  316. PregnantRateMap map[string]map[string]string `json:"pregnantRateMap"`
  317. KepMap []string `json:"kepMap"`
  318. }
  319. // EventMatingCheckBatchModel 批量配种
  320. type EventMatingCheckBatchModel struct {
  321. Cow *Cow
  322. FrozenSemen *FrozenSemen
  323. OperationUser *SystemUser
  324. MatingAt int64
  325. FrozenSemenCount int32
  326. Remarks string
  327. ExposeEstrusType pasturePb.ExposeEstrusType_Kind
  328. }