cow.go 21 KB

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