cow.go 13 KB

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