event_mating.go 13 KB

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