analysis.go 18 KB

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