cow.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. package model
  2. import (
  3. "fmt"
  4. "kpt-pasture/util"
  5. "math"
  6. "time"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. )
  9. type Cow struct {
  10. Id int64 `json:"id"`
  11. Sex pasturePb.Genders_Kind `json:"sex"`
  12. NeckRingNumber string `json:"neckRingNumber"`
  13. EarNumber string `json:"earNumber"`
  14. EarOldNumber string `json:"earOldNumber"`
  15. PenId int32 `json:"penId"`
  16. PenName string `json:"penName"`
  17. Lact int32 `json:"lact"`
  18. DayAge int32 `json:"dayAge"`
  19. CalvingAge int64 `json:"calvingAge"`
  20. PregnancyAge int32 `json:"pregnancyAge"` // 怀孕天数 孕检结果有阳性更新,产犊后至0
  21. AdmissionAge int32 `json:"admissionAge"`
  22. AbortionAge int64 `json:"abortionAge"` // 流产天数
  23. CowType pasturePb.CowType_Kind `json:"cowType"`
  24. BreedStatus pasturePb.BreedStatus_Kind `json:"breedStatus"`
  25. CowKind pasturePb.CowKind_Kind `json:"cowKind"`
  26. BirthWeight int64 `json:"birthWeight"`
  27. CurrentWeight int64 `json:"currentWeight"`
  28. AdmissionWeight int64 `json:"admissionWeight"`
  29. SourceId pasturePb.CowSource_Kind `json:"sourceId"`
  30. FatherNumber string `json:"fatherNumber"`
  31. MotherNumber string `json:"motherNumber"`
  32. AdmissionStatus pasturePb.AdmissionStatus_Kind `json:"admissionStatus"`
  33. IsPregnant pasturePb.IsShow_Kind `json:"isPregnant"`
  34. HealthStatus pasturePb.HealthStatus_Kind `json:"healthStatus"`
  35. WeaningAt int64 `json:"weaningAt"`
  36. BirthAt int64 `json:"birthAt"`
  37. AdmissionAt int64 `json:"admissionAt"`
  38. FirstMatingAt int64 `json:"firstMatingAt"`
  39. MatingTimes int32 `json:"matingTimes"`
  40. WeeklyActive int32 `json:"weeklyActive"`
  41. LastEstrusAt int64 `json:"lastEstrusAt"`
  42. LastCalvingAt int64 `json:"lastCalvingAt"`
  43. LastMatingAt int64 `json:"lastMatingAt"`
  44. LastBullNumber string `json:"lastBullNumber"`
  45. LastPregnantCheckAt int64 `json:"lastPregnantCheckAt"`
  46. LastDryMilkAt int64 `json:"lastDryMilkAt"`
  47. LastSecondWeight int64 `json:"lastSecondWeight"`
  48. LastSecondWeightAt int64 `json:"lastSecondWeightAt"`
  49. LastAbortionAt int64 `json:"lastAbortionAt"`
  50. LastWeightAt int64 `json:"lastWeightAt"`
  51. CreatedAt int64 `json:"createdAt"`
  52. UpdatedAt int64 `json:"updatedAt"`
  53. }
  54. func (c *Cow) TableName() string {
  55. return "cow"
  56. }
  57. type CowSlice []*Cow
  58. func (c CowSlice) ToPB(
  59. penMap map[int32]*Pen,
  60. cowTypeMap map[pasturePb.CowType_Kind]string,
  61. breedStatusMap map[pasturePb.BreedStatus_Kind]string,
  62. cowKindMap map[pasturePb.CowKind_Kind]string,
  63. cowSourceMap map[pasturePb.CowSource_Kind]string,
  64. admissionStatusMap map[pasturePb.AdmissionStatus_Kind]string,
  65. healthStatusMap map[pasturePb.HealthStatus_Kind]string,
  66. ) []*pasturePb.CowDetails {
  67. res := make([]*pasturePb.CowDetails, len(c))
  68. for i, v := range c {
  69. penName := ""
  70. if pen, ok := penMap[v.PenId]; ok {
  71. penName = pen.Name
  72. }
  73. sex := "公"
  74. if v.Sex == pasturePb.Genders_Female {
  75. sex = "母"
  76. }
  77. lastWeightAtFormat := ""
  78. if v.LastWeightAt > 0 {
  79. lastWeightAtFormat = time.Unix(v.LastWeightAt, 0).Format(LayoutDate2)
  80. }
  81. isPregnantName := ""
  82. if v.IsPregnant == pasturePb.IsShow_Ok {
  83. isPregnantName = "已孕"
  84. } else {
  85. isPregnantName = "未孕"
  86. }
  87. admissionAtFormat := ""
  88. if v.AdmissionAt > 0 {
  89. admissionAtFormat = time.Unix(v.AdmissionAt, 0).Format(LayoutDate2)
  90. }
  91. birthAtFormat := ""
  92. if v.BirthAt > 0 {
  93. birthAtFormat = time.Unix(v.BirthAt, 0).Format(LayoutDate2)
  94. }
  95. weaningAtFormat := ""
  96. if v.WeaningAt > 0 {
  97. weaningAtFormat = time.Unix(v.WeaningAt, 0).Format(LayoutDate2)
  98. }
  99. firstMatingAtFormat := ""
  100. if v.FirstMatingAt > 0 {
  101. firstMatingAtFormat = time.Unix(v.FirstMatingAt, 0).Format(LayoutDate2)
  102. }
  103. lastMatingAtFormat := ""
  104. if v.LastMatingAt > 0 {
  105. lastMatingAtFormat = time.Unix(v.LastMatingAt, 0).Format(LayoutDate2)
  106. }
  107. lastPregnantCheckAtFormat := ""
  108. if v.LastPregnantCheckAt > 0 {
  109. lastPregnantCheckAtFormat = time.Unix(v.LastPregnantCheckAt, 0).Format(LayoutDate2)
  110. }
  111. lastCalvingAtFormat := ""
  112. if v.LastCalvingAt > 0 {
  113. lastCalvingAtFormat = time.Unix(v.LastCalvingAt, 0).Format(LayoutDate2)
  114. }
  115. res[i] = &pasturePb.CowDetails{
  116. CowId: int32(v.Id),
  117. Sex: sex,
  118. NeckRingNumber: v.NeckRingNumber,
  119. PenName: penName,
  120. Lact: v.Lact,
  121. CowTypeName: cowTypeMap[v.CowType],
  122. BreedStatusName: breedStatusMap[v.BreedStatus],
  123. CowKindName: cowKindMap[v.CowKind],
  124. EarNumber: v.EarNumber,
  125. BirthWeight: float32(v.BirthWeight) / 1000,
  126. CurrentWeight: float32(v.CurrentWeight) / 1000,
  127. DayAge: v.DayAge,
  128. SourceName: cowSourceMap[v.SourceId],
  129. MontherNumber: v.MotherNumber,
  130. FatherNumber: v.FatherNumber,
  131. AdmissionStatusName: admissionStatusMap[v.AdmissionStatus],
  132. HealthStatusName: healthStatusMap[v.HealthStatus],
  133. IsPregnantName: isPregnantName,
  134. AdmissionAtFormat: admissionAtFormat,
  135. BirthAtFormat: birthAtFormat,
  136. WeaningAtFormat: weaningAtFormat,
  137. CalvingAge: int32(v.GetCalvingAge()),
  138. AbortionAge: int32(v.AbortionAge),
  139. MatingTimes: v.MatingTimes,
  140. FirstMatingAtFormat: firstMatingAtFormat,
  141. LastMatingAtFormat: lastMatingAtFormat,
  142. LastBullNumber: v.LastBullNumber,
  143. LastPregnantCheckAtFormat: lastPregnantCheckAtFormat,
  144. LastWeightAtFormat: lastWeightAtFormat,
  145. LastCalvingAtFormat: lastCalvingAtFormat,
  146. //PregnancyAge: v.PregnancyAge,
  147. }
  148. }
  149. return res
  150. }
  151. func (c CowSlice) ToPB2(penMap map[int32]*Pen, penWeightSlice PenWeightSlice) []*pasturePb.CowList {
  152. res := make([]*pasturePb.CowList, len(c))
  153. for i, v := range c {
  154. penName := ""
  155. if pen, ok := penMap[v.PenId]; ok {
  156. penName = pen.Name
  157. }
  158. penWeight := penWeightSlice.GetPenWeight(v.PenId)
  159. lastWeightDay := util.Ceil(float64(v.LastWeightAt-v.LastSecondWeightAt) / 86400)
  160. penAvgWeight := float32(0)
  161. previousStageDailyWeight := float32(0)
  162. cowPenAvgWeightDiffValue := float32(0)
  163. if penWeight != nil {
  164. penAvgWeight = float32(penWeight.AvgWeight) / 1000
  165. cowPenAvgWeightDiffValue = float32(v.CurrentWeight-int64(penWeight.AvgWeight)) / 1000
  166. if lastWeightDay > 0 {
  167. previousStageDailyWeight = float32(v.CurrentWeight-v.LastSecondWeight) / 1000 / float32(lastWeightDay)
  168. }
  169. }
  170. res[i] = &pasturePb.CowList{
  171. CowId: int32(v.Id),
  172. DayAge: v.DayAge,
  173. DailyWeightGain: float32(v.GetDayWeight()),
  174. AverageDailyWeightGain: float32(v.GetAverageDailyWeight()),
  175. EarNumber: v.EarNumber,
  176. PenName: penName,
  177. BirthAt: int32(v.BirthAt),
  178. BirthWeight: float32(v.BirthWeight) / 1000,
  179. CurrentWeight: float32(v.CurrentWeight) / 1000,
  180. LastWeightAt: int32(v.LastWeightAt),
  181. AdmissionAge: int32(v.AdmissionAge),
  182. AdmissionWeight: float32(v.AbortionAge) / 1000,
  183. PreviousStageDailyWeight: previousStageDailyWeight,
  184. PenAvgWeight: penAvgWeight,
  185. CowPenAvgWeightDiffValue: cowPenAvgWeightDiffValue,
  186. }
  187. }
  188. return res
  189. }
  190. func NewCow(req *pasturePb.EventEnterRequest) *Cow {
  191. var isPregnant = pasturePb.IsShow_No
  192. if req.BreedStatus == pasturePb.BreedStatus_Pregnant {
  193. isPregnant = pasturePb.IsShow_Ok
  194. }
  195. return &Cow{
  196. Sex: req.Sex,
  197. EarNumber: req.EarNumber,
  198. PenId: req.PenId,
  199. Lact: req.Lact,
  200. CowType: req.CowType,
  201. BreedStatus: req.BreedStatus,
  202. CowKind: req.CowKind,
  203. SourceId: req.CowSource,
  204. FatherNumber: req.FatherNumber,
  205. MotherNumber: req.MotherNumber,
  206. AdmissionStatus: pasturePb.AdmissionStatus_Admission,
  207. HealthStatus: pasturePb.HealthStatus_Health,
  208. IsPregnant: isPregnant,
  209. WeaningAt: int64(req.WeaningAt),
  210. BirthAt: int64(req.BirthAt),
  211. AdmissionWeight: int64(req.Weight * 1000),
  212. FirstMatingAt: int64(req.MatingAt),
  213. LastMatingAt: int64(req.MatingAt),
  214. LastPregnantCheckAt: int64(req.PregnancyCheckAt),
  215. AdmissionAt: time.Now().Unix(),
  216. }
  217. }
  218. func NewCalfCow(motherId int64, fatherNumber string, calf *CalvingCalf) *Cow {
  219. return &Cow{
  220. Sex: calf.Sex,
  221. EarNumber: calf.EarNumber,
  222. PenId: calf.PenId,
  223. CowType: pasturePb.CowType_Lactating_Calf, // 哺乳犊牛
  224. BreedStatus: pasturePb.BreedStatus_UnBreed, // 未配
  225. CowKind: calf.CowKind, // 牛只品种
  226. BirthWeight: calf.BirthWeight,
  227. BirthAt: calf.BirthAt,
  228. SourceId: pasturePb.CowSource_Calving, // 产犊方式
  229. FatherNumber: fatherNumber,
  230. MotherNumber: fmt.Sprintf("%d", motherId),
  231. AdmissionStatus: pasturePb.AdmissionStatus_Admission,
  232. IsPregnant: pasturePb.IsShow_No,
  233. AdmissionAt: calf.BirthAt,
  234. }
  235. }
  236. type BarCowStruct struct {
  237. Number int32 `json:"number"`
  238. TypeId pasturePb.CowType_Kind `json:"type_id"`
  239. }
  240. // BarCowStructSlice 首页牛群结构
  241. type BarCowStructSlice []*BarCowStruct
  242. func (b BarCowStructSlice) ToPB(cowTypeMap map[pasturePb.CowType_Kind]string, count int32) []*pasturePb.BarCowStruct {
  243. var pb []*pasturePb.BarCowStruct
  244. for _, v := range b {
  245. name := fmt.Sprintf("%s", cowTypeMap[v.TypeId])
  246. pb = append(pb, &pasturePb.BarCowStruct{Name: name, Value: v.Number})
  247. }
  248. return pb
  249. }
  250. // GetDayAge 日龄
  251. func (c *Cow) GetDayAge() int32 {
  252. if c.BirthAt <= 0 {
  253. return 0
  254. }
  255. return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400))
  256. }
  257. // GetCalvingAge 产后天数
  258. func (c *Cow) GetCalvingAge() int64 {
  259. if c.LastMatingAt <= 0 {
  260. return 0
  261. }
  262. return int64(math.Floor(float64(time.Now().Unix()-c.LastMatingAt) / 86400))
  263. }
  264. // GetDaysPregnant 怀孕天数
  265. func (c *Cow) GetDaysPregnant() int32 {
  266. if c.BreedStatus == pasturePb.BreedStatus_Pregnant &&
  267. c.AdmissionStatus == pasturePb.AdmissionStatus_Admission &&
  268. c.IsPregnant == pasturePb.IsShow_Ok {
  269. return int32(math.Floor(float64(time.Now().Unix()-c.LastMatingAt) / 86400))
  270. }
  271. return 0
  272. }
  273. // GetLactationDays 泌乳天数
  274. func (c *Cow) GetLactationDays() int32 {
  275. if c.BreedStatus == pasturePb.BreedStatus_Calving && c.AdmissionStatus == pasturePb.AdmissionStatus_Admission {
  276. return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400))
  277. }
  278. return 0
  279. }
  280. // GetAdmissionAge 入场天数
  281. func (c *Cow) GetAdmissionAge() int32 {
  282. if c.AdmissionAt > 0 && c.AdmissionStatus == pasturePb.AdmissionStatus_Admission {
  283. return int32(math.Floor(float64(time.Now().Unix()-c.AdmissionAt) / 86400))
  284. }
  285. return 0
  286. }
  287. // GetDayWeight 日增重
  288. func (c *Cow) GetDayWeight() float64 {
  289. if c.CurrentWeight-c.LastSecondWeight > 0 && c.LastWeightAt > c.LastSecondWeightAt {
  290. days := int32(math.Floor(float64(c.LastWeightAt-c.LastSecondWeightAt) / 86400))
  291. if days <= 0 {
  292. return 0
  293. }
  294. dayWeight := math.Round(1.0 * float64(c.CurrentWeight-c.LastSecondWeight) / float64(days))
  295. return dayWeight / 1000
  296. }
  297. return 0
  298. }
  299. // GetAverageDailyWeight 平均日增重
  300. func (c *Cow) GetAverageDailyWeight() float64 {
  301. if c.CurrentWeight-c.BirthWeight > 0 && c.LastWeightAt > c.BirthAt {
  302. days := int32(math.Floor(float64(c.LastWeightAt-c.BirthAt) / 86400))
  303. if days <= 0 {
  304. return 0
  305. }
  306. dailyWeight := math.Round(1.0 * float64(c.CurrentWeight-c.BirthWeight) / float64(days))
  307. return dailyWeight / 1000
  308. }
  309. return 0
  310. }
  311. func (c *Cow) GetAbortionAge() int32 {
  312. if c.LastAbortionAt > 0 && c.AdmissionStatus == pasturePb.AdmissionStatus_Admission {
  313. return int32(math.Floor(float64(time.Now().Unix()-c.LastAbortionAt) / 86400))
  314. }
  315. return 0
  316. }
  317. type CowWeightRange struct {
  318. WeightRange string `json:"weight_range"`
  319. Count int32 `json:"count"`
  320. }
  321. func (c CowSlice) WeightRangeToPB(penMap map[int32]*Pen) []*pasturePb.CowList {
  322. res := make([]*pasturePb.CowList, len(c))
  323. for i, v := range c {
  324. penName := ""
  325. if pen, ok := penMap[v.PenId]; ok {
  326. penName = pen.Name
  327. }
  328. res[i] = &pasturePb.CowList{
  329. CowId: int32(v.Id),
  330. DayAge: v.DayAge,
  331. DailyWeightGain: float32(v.GetDayWeight()),
  332. AverageDailyWeightGain: float32(v.GetAverageDailyWeight()),
  333. EarNumber: v.EarNumber,
  334. PenName: penName,
  335. BirthAt: int32(v.BirthAt),
  336. BirthWeight: float32(v.BirthWeight) / 1000,
  337. CurrentWeight: float32(v.CurrentWeight) / 1000,
  338. LastWeightAt: int32(v.LastWeightAt),
  339. }
  340. }
  341. return res
  342. }