cow.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. package model
  2. import (
  3. "fmt"
  4. "math"
  5. "time"
  6. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  7. )
  8. type Cow struct {
  9. Id int64 `json:"id"`
  10. PastureId int64 `json:"pastureId"` // 牧场id
  11. Sex pasturePb.Genders_Kind `json:"sex"` // 性别
  12. NeckRingNumber string `json:"neckRingNumber"` // 脖环号
  13. EarNumber string `json:"earNumber"` // 耳标号
  14. EleEarNumber string `json:"eleEarNumber"` // 电子耳标号
  15. EarOldNumber string `json:"earOldNumber"` // 旧耳标号
  16. PenId int32 `json:"penId"` // 栏舍id
  17. PenName string `json:"penName"` // 栏舍名称
  18. Lact int32 `json:"lact"` // 胎次
  19. DayAge int32 `json:"dayAge"` // 日龄
  20. CalvingAge int32 `json:"calvingAge"` // 产后天使
  21. PregnancyAge int32 `json:"pregnancyAge"` // 怀孕天数 孕检结果有阳性更新,产犊后至0
  22. AdmissionAge int32 `json:"admissionAge"` // 入场日龄
  23. AbortionAge int32 `json:"abortionAge"` // 流产天数
  24. LactationAge int32 `json:"lactationAge"` // 泌乳天数
  25. DryMilkAge int32 `json:"dryMilkAge"` // 干奶天数
  26. CowType pasturePb.CowType_Kind `json:"cowType"` // 牛只类型
  27. MilkKind pasturePb.CowMilk_Kind `json:"milkKind"` // 牛只奶属性
  28. BreedStatus pasturePb.BreedStatus_Kind `json:"breedStatus"` // 繁殖状态
  29. CowKind pasturePb.CowKind_Kind `json:"cowKind"` // 牛只品种
  30. BirthWeight int64 `json:"birthWeight"` // 出生体重
  31. CurrentWeight int64 `json:"currentWeight"` // 当前体重
  32. CurrentHeight int64 `json:"currentHeight"` // 当前身高
  33. AdmissionWeight int64 `json:"admissionWeight"` // 入场体重
  34. SourceKind pasturePb.CowSource_Kind `json:"sourceKind"` // 来源哪里
  35. PurposeKind pasturePb.Purpose_Kind `json:"purposeKind"` // 用途
  36. FatherNumber string `json:"fatherNumber"` // 父号
  37. MotherNumber string `json:"motherNumber"` // 母号
  38. AdmissionStatus pasturePb.AdmissionStatus_Kind `json:"admissionStatus"` // 在场状态
  39. IsPregnant pasturePb.IsShow_Kind `json:"isPregnant"` // 是否怀孕
  40. IsForbiddenMating pasturePb.IsShow_Kind `json:"isForbiddenMating"` // 是否禁配 1 是 2 否
  41. HealthStatus pasturePb.HealthStatus_Kind `json:"healthStatus"` // 健康状态
  42. WeaningAt int64 `json:"weaningAt"` // 断奶时间
  43. BirthAt int64 `json:"birthAt"` // 出生时间
  44. AdmissionAt int64 `json:"admissionAt"` // 入场时间
  45. DepartureAt int64 `json:"departureAt"` // 离场时间
  46. FirstMatingAt int64 `json:"firstMatingAt"` // 首次配种时间
  47. MatingTimes int32 `json:"matingTimes"` // 配种次数
  48. AbortionTimes int32 `json:"abortionTimes"` // 流产次数
  49. WeeklyActive int32 `json:"weeklyActive"` // 每周活跃度
  50. LastEstrusAt int64 `json:"lastEstrusAt"` // 最后一次发情时间
  51. LastCalvingAt int64 `json:"lastCalvingAt"` // 最后一次产犊时间
  52. LastMatingAt int64 `json:"lastMatingAt"` // 最后一次配种时间
  53. LastBullNumber string `json:"lastBullNumber"` // 最后一次配种牛号
  54. LastPregnantCheckAt int64 `json:"lastPregnantCheckAt"` // 最后一次孕检时间
  55. LastDryMilkAt int64 `json:"lastDryMilkAt"` // 最近一次干奶日期
  56. LastForbiddenMatingAt int64 `json:"lastForbiddenMatingAt"` // 最近一次禁配时间
  57. LastSecondWeight int64 `json:"lastSecondWeight"` // 最后第二次称重
  58. LastSecondWeightAt int64 `json:"lastSecondWeightAt"` // 最后第二次称重时间
  59. LastAbortionAt int64 `json:"lastAbortionAt"` // 最近一次流产时间
  60. LastWeightAt int64 `json:"lastWeightAt"` // 最近一次称重时间
  61. CreatedAt int64 `json:"createdAt"`
  62. UpdatedAt int64 `json:"updatedAt"`
  63. }
  64. func (c *Cow) TableName() string {
  65. return "cow"
  66. }
  67. // EventUpdate 牛只基本信息维护
  68. func (c *Cow) EventUpdate(weeklyActive int32) {
  69. c.DayAge = c.GetDayAge()
  70. c.CalvingAge = c.GetCalvingAge()
  71. c.PregnancyAge = c.GetDaysPregnant()
  72. c.AdmissionAge = c.GetAdmissionAge()
  73. c.AbortionAge = c.GetAbortionAge()
  74. if c.DayAge == 60 {
  75. c.CowType = pasturePb.CowType_Weaned_Calf
  76. }
  77. c.WeeklyActive = weeklyActive
  78. c.LactationAge = c.GetLactationAge()
  79. c.DryMilkAge = c.GetDryMilkAge()
  80. }
  81. // EventCalvingUpdate 产犊更新
  82. func (c *Cow) EventCalvingUpdate(calvingAt int64) {
  83. c.Lact += 1
  84. c.MatingTimes = 0
  85. c.PregnancyAge = 0
  86. c.AbortionTimes = 0
  87. c.BreedStatus = pasturePb.BreedStatus_Calving
  88. c.IsPregnant = pasturePb.IsShow_No
  89. c.LastCalvingAt = calvingAt
  90. c.CalvingAge = c.GetCalvingAge()
  91. c.CowType = pasturePb.CowType_Breeding_Calf
  92. c.MilkKind = pasturePb.CowMilk_Lactation
  93. }
  94. // EventWeaningUpdate 断奶更新
  95. func (c *Cow) EventWeaningUpdate(weaningAt int64, penId int32, currentWeight int64) {
  96. c.PenId = penId
  97. c.WeaningAt = weaningAt
  98. c.CurrentWeight = currentWeight
  99. c.LastWeightAt = weaningAt
  100. }
  101. // EventPregnantCheckUpdate 孕检更新
  102. func (c *Cow) EventPregnantCheckUpdate(breedStatus pasturePb.BreedStatus_Kind, pregnantCheckAt int64, isPregnant pasturePb.IsShow_Kind) {
  103. c.BreedStatus = breedStatus
  104. c.LastPregnantCheckAt = pregnantCheckAt
  105. c.IsPregnant = isPregnant
  106. }
  107. // EventAbortionUpdate 流产更新
  108. func (c *Cow) EventAbortionUpdate(abortionAt int64, isLact pasturePb.IsShow_Kind) {
  109. c.IsPregnant = pasturePb.IsShow_No
  110. c.LastAbortionAt = abortionAt
  111. c.BreedStatus = pasturePb.BreedStatus_Abort
  112. c.AbortionTimes += 1
  113. if isLact == pasturePb.IsShow_Ok {
  114. c.Lact += 1
  115. }
  116. }
  117. // EventWeightUpdate 称重更新
  118. func (c *Cow) EventWeightUpdate(weight, height, weightAt int64) {
  119. c.LastSecondWeight = c.CurrentWeight
  120. c.LastSecondWeightAt = c.LastWeightAt
  121. c.LastWeightAt = weightAt
  122. c.CurrentWeight = weight
  123. c.CurrentHeight = height
  124. }
  125. // EventHealthStatusUpdate 健康状态更新
  126. func (c *Cow) EventHealthStatusUpdate(healthStatus pasturePb.HealthStatus_Kind) {
  127. c.HealthStatus = healthStatus
  128. }
  129. // EventPenUpdate 更新栏舍
  130. func (c *Cow) EventPenUpdate(pen *Pen) {
  131. c.PenId = pen.Id
  132. c.PenName = pen.Name
  133. }
  134. // EventEarNumberUpdate 更新耳标号
  135. func (c *Cow) EventEarNumberUpdate(newEarNumber string) {
  136. c.EarOldNumber = c.EarNumber
  137. c.EarNumber = newEarNumber
  138. }
  139. // EventDeathUpdate 更新牛只死亡信息
  140. func (c *Cow) EventDeathUpdate(eventAt int64) {
  141. c.AdmissionStatus = pasturePb.AdmissionStatus_Die
  142. c.HealthStatus = pasturePb.HealthStatus_Dead
  143. c.DepartureAt = eventAt
  144. }
  145. // EventMatingUpdate 配种更新
  146. func (c *Cow) EventMatingUpdate(matingAt int64, bullNumber string, isReMating bool) {
  147. c.LastMatingAt = matingAt
  148. if c.FirstMatingAt <= 0 {
  149. c.FirstMatingAt = matingAt
  150. }
  151. c.LastBullNumber = bullNumber
  152. c.IsPregnant = pasturePb.IsShow_No
  153. c.BreedStatus = pasturePb.BreedStatus_Breeding
  154. if !isReMating {
  155. c.MatingTimes += 1
  156. }
  157. if c.Lact == 0 {
  158. c.CowType = pasturePb.CowType_Reserve_Calf
  159. }
  160. }
  161. // EstrusUpdate 发情更新
  162. func (c *Cow) EstrusUpdate(estrusAt int64) {
  163. c.LastEstrusAt = estrusAt
  164. }
  165. // EventDryMilkUpdate 干奶更新
  166. func (c *Cow) EventDryMilkUpdate(dryMilkAt int64, pen *Pen) {
  167. c.LastDryMilkAt = dryMilkAt
  168. c.MilkKind = pasturePb.CowMilk_Dry_Milk
  169. c.PenId = pen.Id
  170. c.PenName = pen.Name
  171. }
  172. // ForbiddenMatingUpdate 禁配更新
  173. func (c *Cow) ForbiddenMatingUpdate(forbiddenMatingAt int64) {
  174. c.IsForbiddenMating = pasturePb.IsShow_Ok
  175. c.LastForbiddenMatingAt = forbiddenMatingAt
  176. c.BreedStatus = pasturePb.BreedStatus_No_Mating
  177. }
  178. // UnForbiddenMatingUpdate 解禁配更新
  179. func (c *Cow) UnForbiddenMatingUpdate() {
  180. c.IsForbiddenMating = pasturePb.IsShow_No
  181. c.LastForbiddenMatingAt = 0
  182. c.BreedStatus = pasturePb.BreedStatus_UnBreed
  183. }
  184. type CowSlice []*Cow
  185. func (c CowSlice) ToPB(
  186. penMap map[int32]*Pen,
  187. cowTypeMap map[pasturePb.CowType_Kind]string,
  188. breedStatusMap map[pasturePb.BreedStatus_Kind]string,
  189. cowKindMap map[pasturePb.CowKind_Kind]string,
  190. cowSourceMap map[pasturePb.CowSource_Kind]string,
  191. admissionStatusMap map[pasturePb.AdmissionStatus_Kind]string,
  192. healthStatusMap map[pasturePb.HealthStatus_Kind]string,
  193. ) []*pasturePb.CowDetails {
  194. res := make([]*pasturePb.CowDetails, len(c))
  195. for i, v := range c {
  196. penName := ""
  197. if pen, ok := penMap[v.PenId]; ok {
  198. penName = pen.Name
  199. }
  200. sex := "公"
  201. if v.Sex == pasturePb.Genders_Female {
  202. sex = "母"
  203. }
  204. lastWeightAtFormat := ""
  205. if v.LastWeightAt > 0 {
  206. lastWeightAtFormat = time.Unix(v.LastWeightAt, 0).Format(LayoutDate2)
  207. }
  208. isPregnantName := ""
  209. if v.IsPregnant == pasturePb.IsShow_Ok {
  210. isPregnantName = "已孕"
  211. } else {
  212. isPregnantName = "未孕"
  213. }
  214. admissionAtFormat := ""
  215. if v.AdmissionAt > 0 {
  216. admissionAtFormat = time.Unix(v.AdmissionAt, 0).Format(LayoutDate2)
  217. }
  218. birthAtFormat := ""
  219. if v.BirthAt > 0 {
  220. birthAtFormat = time.Unix(v.BirthAt, 0).Format(LayoutDate2)
  221. }
  222. weaningAtFormat := ""
  223. if v.WeaningAt > 0 {
  224. weaningAtFormat = time.Unix(v.WeaningAt, 0).Format(LayoutDate2)
  225. }
  226. firstMatingAtFormat := ""
  227. if v.FirstMatingAt > 0 {
  228. firstMatingAtFormat = time.Unix(v.FirstMatingAt, 0).Format(LayoutDate2)
  229. }
  230. lastMatingAtFormat := ""
  231. if v.LastMatingAt > 0 {
  232. lastMatingAtFormat = time.Unix(v.LastMatingAt, 0).Format(LayoutDate2)
  233. }
  234. lastPregnantCheckAtFormat := ""
  235. if v.LastPregnantCheckAt > 0 {
  236. lastPregnantCheckAtFormat = time.Unix(v.LastPregnantCheckAt, 0).Format(LayoutDate2)
  237. }
  238. lastCalvingAtFormat := ""
  239. if v.LastCalvingAt > 0 {
  240. lastCalvingAtFormat = time.Unix(v.LastCalvingAt, 0).Format(LayoutDate2)
  241. }
  242. lastAbortionAtFormat := ""
  243. if v.LastAbortionAt > 0 {
  244. lastAbortionAtFormat = time.Unix(v.LastAbortionAt, 0).Format(LayoutDate2)
  245. }
  246. lastSecondWeightAtFormat := ""
  247. if v.LastSecondWeightAt > 0 {
  248. lastSecondWeightAtFormat = time.Unix(v.LastSecondWeightAt, 0).Format(LayoutDate2)
  249. }
  250. res[i] = &pasturePb.CowDetails{
  251. CowId: int32(v.Id),
  252. Sex: sex,
  253. NeckRingNumber: v.NeckRingNumber,
  254. PenName: penName,
  255. Lact: v.Lact,
  256. CowTypeName: cowTypeMap[v.CowType],
  257. CowType: v.CowType,
  258. BreedStatusName: breedStatusMap[v.BreedStatus],
  259. BreedStatus: v.BreedStatus,
  260. CowKindName: cowKindMap[v.CowKind],
  261. EarNumber: v.EarNumber,
  262. BirthWeight: float32(v.BirthWeight) / 1000,
  263. CurrentWeight: float32(v.CurrentWeight) / 1000,
  264. CurrentHeight: int32(v.CurrentHeight),
  265. DayAge: v.DayAge,
  266. SourceName: cowSourceMap[v.SourceKind],
  267. MotherNumber: v.MotherNumber,
  268. FatherNumber: v.FatherNumber,
  269. AdmissionStatusName: admissionStatusMap[v.AdmissionStatus],
  270. HealthStatusName: healthStatusMap[v.HealthStatus],
  271. IsPregnantName: isPregnantName,
  272. AdmissionAtFormat: admissionAtFormat,
  273. BirthAtFormat: birthAtFormat,
  274. WeaningAtFormat: weaningAtFormat,
  275. CalvingAge: v.GetCalvingAge(),
  276. AbortionAge: v.AbortionAge,
  277. MatingTimes: v.MatingTimes,
  278. FirstMatingAtFormat: firstMatingAtFormat,
  279. LastMatingAtFormat: lastMatingAtFormat,
  280. LastBullNumber: v.LastBullNumber,
  281. LastPregnantCheckAtFormat: lastPregnantCheckAtFormat,
  282. LastWeightAtFormat: lastWeightAtFormat,
  283. LastCalvingAtFormat: lastCalvingAtFormat,
  284. LastAbortionAtFormat: lastAbortionAtFormat,
  285. LastSecondWeight: float32(v.LastSecondWeight) / 1000,
  286. LastSecondWeightAtFormat: lastSecondWeightAtFormat,
  287. }
  288. }
  289. return res
  290. }
  291. func (c CowSlice) ToPB2(penWeightSlice PenWeightSlice) []*pasturePb.CowList {
  292. res := make([]*pasturePb.CowList, len(c))
  293. for i, v := range c {
  294. penWeight := penWeightSlice.GetPenWeight(v.PenId)
  295. penAvgWeight := float32(0)
  296. cowPenAvgWeightDiffValue := float32(0)
  297. if penWeight != nil {
  298. penAvgWeight = float32(penWeight.AvgWeight) / 1000
  299. cowPenAvgWeightDiffValue = float32(v.CurrentWeight-int64(penWeight.AvgWeight)) / 1000
  300. }
  301. res[i] = &pasturePb.CowList{
  302. CowId: int32(v.Id),
  303. DayAge: v.DayAge,
  304. AverageDailyWeightGain: float32(v.GetAverageDailyWeight()),
  305. EarNumber: v.EarNumber,
  306. PenName: v.PenName,
  307. BirthAt: int32(v.BirthAt),
  308. BirthWeight: float32(v.BirthWeight) / 1000,
  309. CurrentWeight: float32(v.CurrentWeight) / 1000,
  310. LastWeightAt: int32(v.LastWeightAt),
  311. AdmissionAge: v.AdmissionAge,
  312. AdmissionWeight: float32(v.AbortionAge) / 1000,
  313. PreviousStageDailyWeight: float32(v.GetPreviousStageDailyWeight()),
  314. PenAvgWeight: penAvgWeight,
  315. CowPenAvgWeightDiffValue: cowPenAvgWeightDiffValue,
  316. }
  317. }
  318. return res
  319. }
  320. // NewEnterCow 入场新增牛只
  321. func NewEnterCow(pastureId int64, req *pasturePb.EventEnterRequest, penMap map[int32]*Pen) *Cow {
  322. var isPregnant = pasturePb.IsShow_No
  323. if req.BreedStatus == pasturePb.BreedStatus_Pregnant {
  324. isPregnant = pasturePb.IsShow_Ok
  325. }
  326. admissionAt := int64(0)
  327. switch req.CowSource {
  328. case pasturePb.CowSource_Calving:
  329. admissionAt = int64(req.BirthAt)
  330. case pasturePb.CowSource_Transfer_In:
  331. admissionAt = int64(req.EnterAt)
  332. case pasturePb.CowSource_Buy:
  333. admissionAt = int64(req.EnterAt)
  334. }
  335. isForbiddenMating := pasturePb.IsShow_No
  336. if req.BreedStatus == pasturePb.BreedStatus_No_Mating {
  337. isForbiddenMating = pasturePb.IsShow_Ok
  338. }
  339. return &Cow{
  340. PastureId: pastureId,
  341. Sex: req.Sex,
  342. EarNumber: req.EarNumber,
  343. PenId: req.PenId,
  344. PenName: penMap[req.PenId].Name,
  345. Lact: req.Lact,
  346. CowType: req.CowType,
  347. BreedStatus: req.BreedStatus,
  348. CowKind: req.CowKind,
  349. SourceKind: req.CowSource,
  350. FatherNumber: req.FatherNumber,
  351. MotherNumber: req.MotherNumber,
  352. AdmissionStatus: pasturePb.AdmissionStatus_Admission,
  353. HealthStatus: pasturePb.HealthStatus_Health,
  354. PurposeKind: req.PurposeKind,
  355. EleEarNumber: req.EleEarNumber,
  356. IsPregnant: isPregnant,
  357. IsForbiddenMating: isForbiddenMating,
  358. WeaningAt: int64(req.WeaningAt),
  359. BirthAt: int64(req.BirthAt),
  360. AdmissionWeight: int64(req.Weight * 1000),
  361. FirstMatingAt: int64(req.MatingAt),
  362. LastMatingAt: int64(req.MatingAt),
  363. LastPregnantCheckAt: int64(req.PregnancyCheckAt),
  364. AdmissionAt: admissionAt,
  365. BirthWeight: int64(req.Weight * 1000),
  366. LastWeightAt: int64(req.EstrusAt),
  367. CurrentWeight: int64(req.Weight * 1000),
  368. LastDryMilkAt: int64(req.DryMilkAt),
  369. }
  370. }
  371. // NewCalfCow 产犊新增
  372. func NewCalfCow(matherInfo *Cow, calf *CalvingCalf) *Cow {
  373. return &Cow{
  374. PastureId: calf.PastureId,
  375. Sex: calf.Sex,
  376. EarNumber: calf.EarNumber,
  377. PenId: calf.PenId,
  378. PenName: calf.PenName,
  379. CowType: pasturePb.CowType_Lactating_Calf, // 哺乳犊牛
  380. BreedStatus: pasturePb.BreedStatus_UnBreed, // 未配
  381. CowKind: matherInfo.CowKind, // 牛只品种
  382. BirthWeight: calf.BirthWeight,
  383. BirthAt: calf.BirthAt,
  384. SourceKind: pasturePb.CowSource_Calving, // 产犊方式
  385. FatherNumber: matherInfo.EarNumber,
  386. MotherNumber: matherInfo.LastBullNumber,
  387. AdmissionStatus: pasturePb.AdmissionStatus_Admission,
  388. IsPregnant: pasturePb.IsShow_No,
  389. AdmissionAt: calf.BirthAt,
  390. }
  391. }
  392. type BarCowStruct struct {
  393. Number int32 `json:"number"`
  394. TypeId pasturePb.CowType_Kind `json:"type_id"`
  395. }
  396. // BarCowStructSlice 首页牛群结构
  397. type BarCowStructSlice []*BarCowStruct
  398. func (b BarCowStructSlice) ToPB(cowTypeMap map[pasturePb.CowType_Kind]string, count int32) []*pasturePb.BarCowStruct {
  399. var pb []*pasturePb.BarCowStruct
  400. for _, v := range b {
  401. name := fmt.Sprintf("%s", cowTypeMap[v.TypeId])
  402. pb = append(pb, &pasturePb.BarCowStruct{Name: name, Value: v.Number})
  403. }
  404. return pb
  405. }
  406. // GetDayAge 日龄
  407. func (c *Cow) GetDayAge() int32 {
  408. if c.BirthAt <= 0 {
  409. return 0
  410. }
  411. return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400))
  412. }
  413. // GetCalvingAge 产后天数
  414. func (c *Cow) GetCalvingAge() int32 {
  415. if c.LastCalvingAt <= 0 {
  416. return 0
  417. }
  418. return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400))
  419. }
  420. // GetDaysPregnant 怀孕天数
  421. func (c *Cow) GetDaysPregnant() int32 {
  422. if c.BreedStatus == pasturePb.BreedStatus_Pregnant &&
  423. c.AdmissionStatus == pasturePb.AdmissionStatus_Admission &&
  424. c.IsPregnant == pasturePb.IsShow_Ok {
  425. return int32(math.Floor(float64(time.Now().Unix()-c.LastMatingAt) / 86400))
  426. }
  427. return 0
  428. }
  429. // GetLactationDays 泌乳天数
  430. func (c *Cow) GetLactationDays() int32 {
  431. if c.BreedStatus == pasturePb.BreedStatus_Calving && c.AdmissionStatus == pasturePb.AdmissionStatus_Admission {
  432. return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400))
  433. }
  434. return 0
  435. }
  436. // GetAdmissionAge 入场天数
  437. func (c *Cow) GetAdmissionAge() int32 {
  438. if c.AdmissionAt > 0 && c.AdmissionStatus == pasturePb.AdmissionStatus_Admission {
  439. return int32(math.Floor(float64(time.Now().Unix()-c.AdmissionAt) / 86400))
  440. }
  441. return 0
  442. }
  443. // GetAverageDailyWeight 平均日增重 (最后一次称重 - 第一次称重 ) ÷ 在群天数
  444. func (c *Cow) GetAverageDailyWeight() float64 {
  445. if c.CurrentWeight <= 0 || c.AdmissionAge <= 0 {
  446. return 0
  447. }
  448. firstWeight := c.BirthWeight
  449. if c.SourceKind == pasturePb.CowSource_Buy {
  450. firstWeight = c.AdmissionWeight
  451. }
  452. res := math.Round(1.0 * float64(c.CurrentWeight-firstWeight) / float64(c.AdmissionAge))
  453. return res / 1000
  454. }
  455. // GetPreviousStageDailyWeight 上一个阶段日增重
  456. func (c *Cow) GetPreviousStageDailyWeight() float64 {
  457. if c.CurrentWeight-c.LastSecondWeight > 0 && c.LastWeightAt-c.LastSecondWeightAt > 0 {
  458. days := int32(math.Floor(float64(c.LastWeightAt-c.LastSecondWeightAt) / 86400))
  459. if days <= 0 {
  460. return float64(c.CurrentWeight - c.LastSecondWeight)
  461. }
  462. dayWeight := math.Round(1.0 * float64(c.CurrentWeight-c.LastSecondWeight) / float64(days))
  463. return dayWeight / 1000
  464. }
  465. return 0
  466. }
  467. // GetAbortionAge 流产天数
  468. func (c *Cow) GetAbortionAge() int32 {
  469. if c.LastAbortionAt > 0 && c.AdmissionStatus == pasturePb.AdmissionStatus_Admission {
  470. return int32(math.Floor(float64(time.Now().Unix()-c.LastAbortionAt) / 86400))
  471. }
  472. return 0
  473. }
  474. // GetLactationAge 泌乳天数
  475. func (c *Cow) GetLactationAge() int32 {
  476. if c.MilkKind == pasturePb.CowMilk_Lactation {
  477. return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400))
  478. }
  479. return c.LactationAge
  480. }
  481. // GetDryMilkAge 干奶天数
  482. func (c *Cow) GetDryMilkAge() int32 {
  483. if c.MilkKind == pasturePb.CowMilk_Dry_Milk {
  484. return int32(math.Floor(float64(time.Now().Unix()-c.LastDryMilkAt) / 86400))
  485. }
  486. return c.DryMilkAge
  487. }
  488. type CowWeightRange struct {
  489. WeightRange string `json:"weight_range"`
  490. Count int32 `json:"count"`
  491. }
  492. func (c CowSlice) WeightRangeToPB(penMap map[int32]*Pen) []*pasturePb.CowList {
  493. res := make([]*pasturePb.CowList, len(c))
  494. for i, v := range c {
  495. penName := ""
  496. if pen, ok := penMap[v.PenId]; ok {
  497. penName = pen.Name
  498. }
  499. res[i] = &pasturePb.CowList{
  500. CowId: int32(v.Id),
  501. DayAge: v.DayAge,
  502. AverageDailyWeightGain: float32(v.GetAverageDailyWeight()),
  503. PreviousStageDailyWeight: float32(v.GetPreviousStageDailyWeight()),
  504. EarNumber: v.EarNumber,
  505. PenName: penName,
  506. BirthAt: int32(v.BirthAt),
  507. BirthWeight: float32(v.BirthWeight) / 1000,
  508. CurrentWeight: float32(v.CurrentWeight) / 1000,
  509. LastWeightAt: int32(v.LastWeightAt),
  510. }
  511. }
  512. return res
  513. }
  514. func (c CowSlice) LongTermInfertilityToPB(breedStatusMap map[pasturePb.BreedStatus_Kind]string) []*pasturePb.LongTermInfertility {
  515. res := make([]*pasturePb.LongTermInfertility, len(c))
  516. for i, v := range c {
  517. breedStatusName := ""
  518. if breedStatus, ok := breedStatusMap[v.BreedStatus]; ok {
  519. breedStatusName = breedStatus
  520. }
  521. lastCalvingAtFormat := ""
  522. if v.LastCalvingAt > 0 {
  523. lastCalvingAtFormat = time.Unix(v.LastCalvingAt, 0).Format(LayoutDate2)
  524. }
  525. lastAbortionAtFormat := ""
  526. if v.LastAbortionAt > 0 {
  527. lastAbortionAtFormat = time.Unix(v.LastAbortionAt, 0).Format(LayoutDate2)
  528. }
  529. lastMatingAtFormat := ""
  530. if v.LastMatingAt > 0 {
  531. lastMatingAtFormat = time.Unix(v.LastMatingAt, 0).Format(LayoutDate2)
  532. }
  533. res[i] = &pasturePb.LongTermInfertility{
  534. CowId: int32(v.Id),
  535. EarNumber: v.EarNumber,
  536. Lact: v.Lact,
  537. PenId: v.PenId,
  538. CalvingAge: v.CalvingAge,
  539. PenName: v.PenName,
  540. BreedStatusName: breedStatusName,
  541. LastCalvingAtFormat: lastCalvingAtFormat,
  542. LastAbortionAtFormat: lastAbortionAtFormat,
  543. LastMatingAtFormat: lastMatingAtFormat,
  544. MatingTimes: v.MatingTimes,
  545. LastBullNumber: v.LastBullNumber,
  546. AbortionTimes: v.AbortionTimes,
  547. }
  548. }
  549. return res
  550. }
  551. // CowBehaviorCurveResponse 脖环行为数据
  552. type CowBehaviorCurveResponse struct {
  553. Code int32 `json:"code"`
  554. Msg string `json:"msg"`
  555. Data *CowBehaviorCurveData `json:"data"`
  556. }
  557. type CowBehaviorCurveData struct {
  558. OriginalDateList []int32 `json:"originalDateList"` // 原始行为数据
  559. ChangeDateList []int32 `json:"changeDateList"` // 变化数据
  560. SumDateList []int32 `json:"sumDateList"` // 累计24小时数据
  561. SumChewList []int32 `json:"sumChewList"` // 累计24小时咀嚼
  562. DateTimeList []string `json:"dateTimeList"` // 时间数据
  563. EstrusList map[pasturePb.EstrusLevel_Kind][]string `json:"estrusList"` // 发情预警
  564. EventList map[string][]string `json:"eventList"` // 事件数据
  565. EventMap map[pasturePb.EventType_Kind]string `json:"eventMap"` // 所有事件
  566. RuminaChange []int32 `json:"ruminaChange"` // 反刍变化
  567. LowActivity int32 `json:"lowActivity"` // 低活动量参数
  568. MiddleActivity int32 `json:"middleActivity"` // 中活动量行数
  569. }