analysis.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  7. "net/http"
  8. "time"
  9. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  10. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  11. "gitee.com/xuyiping_admin/pkg/xerr"
  12. "go.uber.org/zap"
  13. )
  14. // WeightScatterPlot 体重散点图 获取图表数据
  15. func (s *StoreEntry) WeightScatterPlot(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest, pagination *pasturePb.PaginationModel) (*pasturePb.GrowthCurvesResponse, error) {
  16. userModel, err := s.GetUserModel(ctx)
  17. if err != nil {
  18. return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
  19. }
  20. // 查询数据
  21. cowList := make([]*model.Cow, 0)
  22. pref := s.DB.Model(new(model.Cow)).
  23. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  24. Where("pasture_id = ?", userModel.AppPasture.Id)
  25. if req.EarNumber != "" {
  26. pref.Where("ear_number = ?", req.EarNumber)
  27. }
  28. if len(req.PenIds) > 0 {
  29. pref.Where("pen_id IN (?)", req.PenIds)
  30. }
  31. if len(req.AdmissionDate) == 2 {
  32. t0, _ := util.TimeParseLocal(model.LayoutDate2, req.AdmissionDate[0])
  33. t1, _ := util.TimeParseLocal(model.LayoutDate2, req.AdmissionDate[1])
  34. pref.Where("admission_at BETWEEN ? AND ?", t0.Unix(), t1.Unix()+86399)
  35. }
  36. var count int64
  37. if err = pref.Count(&count).
  38. Limit(int(pagination.PageSize)).
  39. Offset(int(pagination.PageOffset)).
  40. Find(&cowList).Error; err != nil {
  41. return nil, err
  42. }
  43. if err != nil {
  44. return nil, xerr.WithStack(err)
  45. }
  46. // 计算图表数据
  47. chartsList := &pasturePb.Charts{
  48. CowId: make([]int32, 0),
  49. Weight: make([]float32, 0),
  50. AdmissionAge: make([]int32, 0),
  51. }
  52. cowData := make([]*pasturePb.CowList, 0)
  53. for _, cow := range cowList {
  54. currentWeight := float32(cow.CurrentWeight) / 1000
  55. admissionAtFormat := ""
  56. if cow.AdmissionAt > 0 {
  57. admissionAtFormat = time.Unix(cow.AdmissionAt, 0).Format(model.LayoutDate2)
  58. }
  59. cowData = append(cowData, &pasturePb.CowList{
  60. CowId: int32(cow.Id),
  61. EarNumber: cow.EarNumber,
  62. DayAge: cow.GetDayAge(),
  63. PenName: cow.PenName,
  64. CurrentWeight: currentWeight,
  65. BirthAt: int32(cow.BirthAt),
  66. BirthWeight: float32(cow.BirthWeight) / 1000,
  67. LastWeightAt: int32(cow.LastWeightAt),
  68. AverageDailyWeightGain: float32(cow.GetAverageDailyWeight()),
  69. PreviousStageDailyWeight: float32(cow.GetPreviousStageDailyWeight()),
  70. AdmissionAge: cow.GetAdmissionAge(),
  71. AdmissionAtFormat: admissionAtFormat,
  72. })
  73. chartsList.CowId = append(chartsList.CowId, int32(cow.Id))
  74. chartsList.Weight = append(chartsList.Weight, currentWeight)
  75. chartsList.AdmissionAge = append(chartsList.AdmissionAge, cow.GetAdmissionAge())
  76. }
  77. // 返回数据
  78. return &pasturePb.GrowthCurvesResponse{
  79. Code: http.StatusOK,
  80. Msg: "success",
  81. Data: &pasturePb.GrowthCurveData{
  82. Table: cowData,
  83. Charts: chartsList,
  84. },
  85. }, nil
  86. }
  87. func (s *StoreEntry) WeightRange(ctx context.Context, req *pasturePb.WeightRangeRequest) (*pasturePb.WeightRangeResponse, error) {
  88. userModel, err := s.GetUserModel(ctx)
  89. if err != nil {
  90. return nil, xerr.WithStack(err)
  91. }
  92. cowWeightRange := make([]*model.CowWeightRange, 0)
  93. prefix := s.DB.Model(new(model.Cow)).
  94. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  95. Where("pasture_id = ?", userModel.AppPasture.Id)
  96. if req.CowKind > 0 {
  97. prefix.Where("cow_kind = ?", req.CowKind)
  98. }
  99. if err = prefix.Select(`
  100. CASE
  101. WHEN current_weight BETWEEN 0 AND 50000 THEN '0-50'
  102. WHEN current_weight BETWEEN 50001 AND 100000 THEN '51-100'
  103. WHEN current_weight BETWEEN 100001 AND 150000 THEN '101-150'
  104. WHEN current_weight BETWEEN 150001 AND 200000 THEN '151-200'
  105. WHEN current_weight BETWEEN 200001 AND 250000 THEN '201-250'
  106. WHEN current_weight BETWEEN 250001 AND 300000 THEN '251-300'
  107. WHEN current_weight BETWEEN 300001 AND 350000 THEN '301-350'
  108. WHEN current_weight BETWEEN 350001 AND 400000 THEN '351-400'
  109. WHEN current_weight BETWEEN 400001 AND 450000 THEN '401-450'
  110. WHEN current_weight BETWEEN 450001 AND 500000 THEN '451-500'
  111. WHEN current_weight BETWEEN 500001 AND 550000 THEN '500-550'
  112. WHEN current_weight BETWEEN 550001 AND 600000 THEN '551-600'
  113. WHEN current_weight BETWEEN 600001 AND 650000 THEN '601-650'
  114. WHEN current_weight BETWEEN 650001 AND 700000 THEN '651-700'
  115. WHEN current_weight BETWEEN 700001 AND 750000 THEN '701-750'
  116. ELSE '750+'
  117. END AS weight_range,
  118. COUNT(*) AS count `,
  119. ).Group("weight_range").Order("MIN(current_weight)").Find(&cowWeightRange).Error; err != nil {
  120. return nil, err
  121. }
  122. if len(cowWeightRange) == 0 {
  123. return &pasturePb.WeightRangeResponse{
  124. Code: http.StatusOK,
  125. Msg: "ok",
  126. Data: &pasturePb.WeightRangeData{
  127. CowList: make([]*pasturePb.CowList, 0),
  128. WeightBarChart: &pasturePb.WeightBarChart{
  129. Header: make([]string, 0),
  130. Data: make([]int32, 0),
  131. },
  132. },
  133. }, nil
  134. }
  135. header := make([]string, 0)
  136. data := make([]int32, 0)
  137. for _, v := range cowWeightRange {
  138. header = append(header, v.WeightRange)
  139. data = append(data, v.Count)
  140. }
  141. // 牛只详情列表
  142. pref := s.DB.Model(new(model.Cow)).
  143. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  144. Where("pasture_id = ?", userModel.AppPasture.Id)
  145. if req.CowKind > 0 {
  146. pref.Where("cow_kind = ?", req.CowKind)
  147. }
  148. cowList := make([]*model.Cow, 0)
  149. if req.MinWeight >= 0 && req.MaxWeight >= 0 && req.MinWeight < req.MaxWeight {
  150. pref.Where("current_weight BETWEEN ? AND ? ", req.MinWeight*1000, req.MaxWeight*1000)
  151. }
  152. if err = pref.Find(&cowList).Error; err != nil {
  153. return nil, err
  154. }
  155. penMap := s.PenMap(ctx, userModel.AppPasture.Id)
  156. return &pasturePb.WeightRangeResponse{
  157. Code: http.StatusOK,
  158. Msg: "ok",
  159. Data: &pasturePb.WeightRangeData{
  160. CowList: model.CowSlice(cowList).WeightRangeToPB(penMap),
  161. WeightBarChart: &pasturePb.WeightBarChart{
  162. Header: header,
  163. Data: data,
  164. },
  165. },
  166. }, nil
  167. }
  168. func (s *StoreEntry) MatingTimely(ctx context.Context, req *pasturePb.MatingTimelyRequest) (*model.MatingTimelyResponse, error) {
  169. userModel, err := s.GetUserModel(ctx)
  170. if err != nil {
  171. return nil, xerr.WithStack(err)
  172. }
  173. matingTimelyChart := make([]*model.MatingTimelyChart, 0)
  174. pastureWhereSql := fmt.Sprintf(" AND pasture_id = %d", userModel.AppPasture.Id)
  175. sql := `SELECT calving_age,cow_type, DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') AS reality_day, lact_group
  176. FROM (
  177. SELECT calving_age, cow_type,reality_day, '0' AS lact_group
  178. FROM event_mating
  179. WHERE lact = 0 AND status = 1 ` + pastureWhereSql + `
  180. UNION ALL
  181. SELECT calving_age,cow_type, reality_day, '1' AS lact_group
  182. FROM event_mating
  183. WHERE lact = 1 AND status = 1 ` + pastureWhereSql + `
  184. UNION ALL
  185. SELECT calving_age,cow_type, reality_day, '2' AS lact_group
  186. FROM event_mating
  187. WHERE lact = 2 AND status = 1 ` + pastureWhereSql + `
  188. UNION ALL
  189. SELECT calving_age, cow_type, reality_day, '3+' AS lact_group
  190. FROM event_mating
  191. WHERE lact >= 3 AND status = 1 ` + pastureWhereSql + `
  192. ) AS subquery WHERE 1 = 1 `
  193. whereSql := ""
  194. if req.CowType > 0 {
  195. whereSql += fmt.Sprintf("AND cow_type = %d ", req.CowType)
  196. }
  197. if req.StartDayAt > 0 && req.EndDayAt > 0 {
  198. whereSql += fmt.Sprintf("AND reality_day BETWEEN %d AND %d", req.StartDayAt, req.EndDayAt)
  199. }
  200. if err = s.DB.Raw(fmt.Sprintf("%s %s", sql, whereSql)).Find(&matingTimelyChart).Error; err != nil {
  201. return nil, err
  202. }
  203. chart := &model.CowMatingChart{
  204. Lact0: make([][]string, 0),
  205. Lact1: make([][]string, 0),
  206. Lact2: make([][]string, 0),
  207. Lact3: make([][]string, 0),
  208. }
  209. if len(matingTimelyChart) == 0 {
  210. return &model.MatingTimelyResponse{
  211. Code: http.StatusOK,
  212. Msg: "ok",
  213. Data: &model.MatingTimelyData{
  214. CowList: make([]*pasturePb.CowList, 0),
  215. Chart: chart,
  216. },
  217. }, nil
  218. }
  219. for _, v := range matingTimelyChart {
  220. t, _ := util.TimeParseLocal(model.LayoutDate2, v.RealityDay)
  221. switch v.LactGroup {
  222. case "0":
  223. chart.Lact0 = append(chart.Lact0, []string{fmt.Sprintf("%d", t.Day()), fmt.Sprintf("%d", v.CalvingAge), v.RealityDay})
  224. case "1":
  225. chart.Lact1 = append(chart.Lact1, []string{fmt.Sprintf("%d", t.Day()), fmt.Sprintf("%d", v.CalvingAge), v.RealityDay})
  226. case "2":
  227. chart.Lact2 = append(chart.Lact2, []string{fmt.Sprintf("%d", t.Day()), fmt.Sprintf("%d", v.CalvingAge), v.RealityDay})
  228. case "3+":
  229. chart.Lact3 = append(chart.Lact3, []string{fmt.Sprintf("%d", t.Day()), fmt.Sprintf("%d", v.CalvingAge), v.RealityDay})
  230. }
  231. }
  232. // 牛只详情列表
  233. eventMatingList := make([]*model.EventMating, 0)
  234. pref := s.DB.Model(new(model.EventMating)).
  235. Where("status = ?", pasturePb.IsShow_Ok)
  236. if req.CowType > 0 {
  237. pref.Where("cow_type = ?", req.CowType)
  238. }
  239. if req.StartDayAt > 0 && req.EndDayAt > 0 {
  240. pref.Where("reality_day BETWEEN ? AND ?", req.StartDayAt, req.EndDayAt)
  241. }
  242. if err = pref.Find(&eventMatingList).Error; err != nil {
  243. return nil, err
  244. }
  245. return &model.MatingTimelyResponse{
  246. Code: http.StatusOK,
  247. Msg: "ok",
  248. Data: &model.MatingTimelyData{
  249. CowList: model.EventMatingSlice(eventMatingList).ToPB2(),
  250. Chart: chart,
  251. },
  252. }, nil
  253. }
  254. func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PenWeightResponse, error) {
  255. userModel, err := s.GetUserModel(ctx)
  256. if err != nil {
  257. return nil, xerr.WithStack(err)
  258. }
  259. penWeightList := make([]*model.PenWeight, 0)
  260. pref := s.DB.Model(new(model.Cow)).
  261. Select(`
  262. pen_id,
  263. CEILING(AVG(current_weight) / 1000 ) AS avg_weight,
  264. CEILING(SUM(current_weight) / 1000 ) AS all_weight,
  265. COUNT(*) AS cow_count`,
  266. ).
  267. Where("pasture_id = ?", userModel.AppPasture.Id).
  268. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission)
  269. if len(req.PenId) > 0 && req.BarId <= 0 {
  270. pref.Where("pen_id IN ?", req.PenId)
  271. }
  272. if err = pref.Group("pen_id").
  273. Order("pen_id").
  274. Find(&penWeightList).Error; err != nil {
  275. return nil, err
  276. }
  277. chart := &pasturePb.PenWeightChart{
  278. Header: make([]string, 0),
  279. AllWeight: make([]int32, 0),
  280. AvgWeight: make([]int32, 0),
  281. CowCount: make([]int32, 0),
  282. }
  283. if len(penWeightList) <= 0 {
  284. return &pasturePb.PenWeightResponse{
  285. Code: http.StatusOK,
  286. Msg: "ok",
  287. Data: &pasturePb.PenWeightData{
  288. CowList: make([]*pasturePb.CowList, 0),
  289. Chart: chart,
  290. },
  291. }, nil
  292. }
  293. cowList := make([]*model.Cow, 0)
  294. var count int64 = 0
  295. prefList := s.DB.Model(new(model.Cow)).
  296. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission)
  297. if len(req.PenId) > 0 {
  298. prefList.Where("pen_id IN (?)", req.PenId)
  299. } else if req.BarId > 0 {
  300. prefList.Where("pen_id = ?", []int32{req.BarId})
  301. }
  302. if err = prefList.Count(&count).Limit(int(pagination.PageSize)).
  303. Offset(int(pagination.PageOffset)).Order("pen_id").
  304. Find(&cowList).Error; err != nil {
  305. return nil, xerr.WithStack(err)
  306. }
  307. penMap := s.PenMap(ctx, userModel.AppPasture.Id)
  308. return &pasturePb.PenWeightResponse{
  309. Code: http.StatusOK,
  310. Msg: "ok",
  311. Data: &pasturePb.PenWeightData{
  312. CowList: model.CowSlice(cowList).ToPB2(penWeightList),
  313. Total: int32(count),
  314. Page: pagination.Page,
  315. PageSize: pagination.PageSize,
  316. Chart: model.PenWeightSlice(penWeightList).ToPB(penMap),
  317. },
  318. }, nil
  319. }
  320. func (s *StoreEntry) AbortionRate(ctx context.Context, req *pasturePb.AbortionRateRequest) (*pasturePb.AbortionRateResponse, error) {
  321. userModel, err := s.GetUserModel(ctx)
  322. if err != nil {
  323. return nil, xerr.WithStack(err)
  324. }
  325. dayTimeList, err := util.GetMonthsInRange(req.StartDayTime, req.EndDayTime)
  326. if err != nil {
  327. return nil, xerr.WithStack(err)
  328. }
  329. // 历史每月怀孕牛头数量
  330. cowPregnantMonthList := make([]*model.CowPregnantMonth, 0)
  331. pref := s.DB.Model(new(model.EventMating)).
  332. Select(`COUNT(cow_id) AS cow_count,DATE_FORMAT(FROM_UNIXTIME(reality_day),'%Y-%m') as month`).
  333. Where("cow_type = ?", req.CowType).
  334. Where("pasture_id = ?", userModel.AppPasture.Id).
  335. Where("status = ?", pasturePb.IsShow_Ok).
  336. Where("mating_result = ?", pasturePb.MatingResult_Pregnant).
  337. Where("DATE_FORMAT(FROM_UNIXTIME(reality_day),'%Y-%m') IN (?)", dayTimeList)
  338. if req.Lact >= 0 && req.Lact <= 3 {
  339. pref.Where("lact = ?", req.Lact)
  340. } else {
  341. pref.Where("lact > ?", req.Lact)
  342. }
  343. if err = pref.Group("month").
  344. Find(&cowPregnantMonthList).Error; err != nil {
  345. return nil, xerr.WithStack(err)
  346. }
  347. // 历史每月流产牛头数量
  348. cowAbortionMonthList := make([]*model.CowPregnantMonth, 0)
  349. pref2 := s.DB.Model(new(model.EventAbortion)).
  350. Select(`COUNT(cow_id) AS cow_count,DATE_FORMAT(FROM_UNIXTIME(abortion_at),'%Y-%m') as month`).
  351. Where("cow_type = ?", req.CowType).
  352. Where("DATE_FORMAT(FROM_UNIXTIME(abortion_at),'%Y-%m') IN ?", dayTimeList)
  353. if req.Lact >= 0 {
  354. pref2.Where("lact = ?", req.Lact)
  355. }
  356. if err = pref2.Group("month").Find(&cowAbortionMonthList).Error; err != nil {
  357. return nil, xerr.WithStack(err)
  358. }
  359. chart := &pasturePb.AbortionRateChart{
  360. Header: make([]string, 0),
  361. AbortionCountMonth: make([]int32, 0),
  362. PregnantCountMonth: make([]int32, 0),
  363. AbortionRateMonth: make([]float32, 0),
  364. }
  365. table := make([]*pasturePb.AbortionRateTable, 0)
  366. for _, v2 := range cowAbortionMonthList {
  367. pregnantCountMonth := int32(0)
  368. for _, v := range cowPregnantMonthList {
  369. if v.Month == v2.Month {
  370. pregnantCountMonth = v.CowCount
  371. }
  372. }
  373. abortionRateMonth := float64(0)
  374. if pregnantCountMonth > 0 && v2.CowCount > 0 {
  375. abortionRateMonth = util.RoundToTwoDecimals(float64(v2.CowCount) / float64(pregnantCountMonth) * 100)
  376. }
  377. chart.Header = append(chart.Header, v2.Month)
  378. chart.AbortionCountMonth = append(chart.AbortionCountMonth, v2.CowCount)
  379. chart.PregnantCountMonth = append(chart.PregnantCountMonth, pregnantCountMonth)
  380. chart.AbortionRateMonth = append(chart.AbortionRateMonth, float32(abortionRateMonth))
  381. table = append(table, &pasturePb.AbortionRateTable{
  382. AbortionCount: v2.CowCount,
  383. MonthName: v2.Month,
  384. PregnantCount: pregnantCountMonth,
  385. AbortionRate: float32(abortionRateMonth),
  386. })
  387. }
  388. return &pasturePb.AbortionRateResponse{
  389. Code: http.StatusOK,
  390. Msg: "ok",
  391. Data: &pasturePb.AbortionRateData{
  392. Chart: chart,
  393. Table: table,
  394. },
  395. }, nil
  396. }
  397. func (s *StoreEntry) TwentyOnePregnantRate(ctx context.Context, req *pasturePb.TwentyOnePregnantRateRequest) (*pasturePb.TwentyOnePregnantRateResponse, error) {
  398. userModel, err := s.GetUserModel(ctx)
  399. if err != nil {
  400. return nil, xerr.WithStack(err)
  401. }
  402. startUnix := util.TimeParseLocalUnix(req.StartDate)
  403. endUnix := util.TimeParseLocalUnix(req.EndDate)
  404. if startUnix > endUnix {
  405. return nil, xerr.Customf("开始时间不能大于结束时间: %s ~ %d", req.StartDate, req.EndDate)
  406. }
  407. nowDateTime := time.Now()
  408. if endUnix > nowDateTime.Unix() {
  409. return nil, xerr.Customf("结束时间不能大于当前时间: %s ~ %s", req.EndDate, nowDateTime.Format(model.LayoutDate2))
  410. }
  411. dataRange, err := util.Get21DayPeriods(req.StartDate, req.EndDate)
  412. if err != nil {
  413. return nil, xerr.WithStack(err)
  414. }
  415. chart := &pasturePb.TwentyOnePregnantRateChart{
  416. Header: make([]string, 0),
  417. PregnantRate: make([]float32, 0),
  418. MatingRate: make([]float32, 0),
  419. }
  420. // 牛只主动停配期
  421. systemBasicName := ""
  422. switch req.CowType {
  423. case pasturePb.CowType_Breeding_Calf:
  424. systemBasicName = model.ProactivelyStopBreedingForAdult
  425. case pasturePb.CowType_Reserve_Calf:
  426. systemBasicName = model.ProactivelyStopBreedingForBackup
  427. default:
  428. return nil, xerr.Customf("不支持的牛只类型: %d", req.CowType)
  429. }
  430. systemBasic, err := s.GetSystemBasicByName(ctx, userModel.AppPasture.Id, systemBasicName)
  431. if err != nil {
  432. return nil, xerr.WithStack(err)
  433. }
  434. stopBreedingDay := systemBasic.MinValue * 86400
  435. dateCowList := make([][]*model.Cow, len(dataRange))
  436. twentyOnePregnantRateList := make([]*pasturePb.TwentyOnePregnantRateList, 0)
  437. for i, v := range dataRange {
  438. middleDay, err := util.GetRangeDayMiddleDay(v, 11)
  439. if err != nil {
  440. return nil, xerr.WithStack(err)
  441. }
  442. middleDayUnix := util.TimeParseLocalEndUnix(middleDay)
  443. chart.Header = append(chart.Header, fmt.Sprintf("%s ~ %s", v[0], v[1]))
  444. cowList := s.TwentyOnePregnantCowList(userModel.AppPasture.Id, req.CowType, stopBreedingDay, middleDayUnix, []int64{})
  445. twentyOnePregnantRateList = append(twentyOnePregnantRateList, &pasturePb.TwentyOnePregnantRateList{
  446. StartDay: v[0],
  447. EndDay: v[1],
  448. ShouldBreedCount: int32(len(cowList)),
  449. RealityBreedCount: 0,
  450. BreedRate: 0,
  451. ShouldPregnantCount: 0,
  452. RealityPregnantCount: 0,
  453. PregnantRate: 0,
  454. RealityAbortionCount: 0,
  455. AbortionRate: 0,
  456. })
  457. dateCowList[i] = cowList
  458. }
  459. return &pasturePb.TwentyOnePregnantRateResponse{
  460. Code: http.StatusOK,
  461. Msg: "ok",
  462. Data: &pasturePb.TwentyOnePregnantRateData{
  463. Chart: chart,
  464. Table: &pasturePb.TwentyOnePregnantRateTable{
  465. List: twentyOnePregnantRateList,
  466. Total: int32(len(dataRange)),
  467. },
  468. },
  469. }, nil
  470. }
  471. // TwentyOnePregnantCowList 21天牛只停配期牛只列表
  472. func (s *StoreEntry) TwentyOnePregnantCowList(
  473. pastureId int64,
  474. cowType pasturePb.CowType_Kind,
  475. stopBreedingDay int32,
  476. middleDay int64,
  477. notInCow []int64,
  478. ) []*model.Cow {
  479. cowList := make([]*model.Cow, 0)
  480. switch cowType {
  481. case pasturePb.CowType_Reserve_Calf:
  482. pref := s.DB.Model(new(model.Cow)).
  483. Where("pasture_id = ?", pastureId).
  484. Where("cow_type = ?", cowType).
  485. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  486. Where("is_pregnant = ?", pasturePb.IsShow_No).
  487. Where("lact = ?", 0).
  488. Where("birth_at + ? < ?", stopBreedingDay, middleDay)
  489. if len(notInCow) > 0 {
  490. pref = pref.Where("id NOT IN ?", notInCow)
  491. }
  492. if err := pref.Find(&cowList).Error; err != nil {
  493. zaplog.Error("TwentyOnePregnantCowList",
  494. zap.Any("cowType", cowType),
  495. zap.Any("stopBreedingDay", stopBreedingDay),
  496. zap.Any("middleDay", middleDay),
  497. zap.Any("notInCow", notInCow),
  498. )
  499. }
  500. case pasturePb.CowType_Breeding_Calf:
  501. }
  502. return cowList
  503. }