calendar.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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/xerr"
  11. )
  12. func (s *StoreEntry) CalendarToDoCount(ctx context.Context) (*pasturePb.TodoCountResponse, error) {
  13. userModel, err := s.GetUserModel(ctx)
  14. if err != nil {
  15. return nil, xerr.WithStack(err)
  16. }
  17. todoList, err := s.CalendarToDoHistoryList(ctx, userModel.AppPasture.Id, "")
  18. if err != nil {
  19. return nil, xerr.WithStack(err)
  20. }
  21. return &pasturePb.TodoCountResponse{
  22. Code: http.StatusOK,
  23. Msg: "ok",
  24. Data: &pasturePb.TodoCountData{Count: int32(len(todoList))},
  25. }, nil
  26. }
  27. func (s *StoreEntry) CalendarToDoHistoryList(ctx context.Context, pastureId int64, earNumber string) ([]*pasturePb.CalendarToDoList, error) {
  28. whereSql := ""
  29. if earNumber != "" {
  30. whereSql += fmt.Sprintf(` AND ear_number = '%s' `, earNumber)
  31. }
  32. whereSql += fmt.Sprintf(" AND pasture_id = %d ", pastureId)
  33. whereSql1 := whereSql + fmt.Sprintf(` AND end_day >= %d `, util.TimeParseLocalEndUnix(time.Now().Local().Format(model.LayoutDate2)))
  34. calendarToDoList := make([]*pasturePb.CalendarToDoList, 0)
  35. sql := `SELECT a.cow_id,b.pen_name,a.calendar_type_name,a.calendar_type_kind as calendar_type,DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day,
  36. a.remaining_days,b.lact,b.ear_number FROM (
  37. SELECT cow_id,plan_day,'免疫' as calendar_type_name,1 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days
  38. FROM event_immunization_plan WHERE status = 2` + whereSql1 + `
  39. UNION ALL
  40. SELECT cow_id,plan_day,'同期' as calendar_type_name,2 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days
  41. FROM event_cow_same_time WHERE status = 2` + whereSql1 + `
  42. UNION ALL
  43. SELECT cow_id,plan_day,'孕检' as calendar_type_name,4 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days
  44. FROM event_pregnant_check WHERE status = 2` + whereSql1 + `
  45. UNION ALL
  46. SELECT cow_id,plan_day,'断奶' as calendar_type_name,6 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days
  47. FROM event_weaning WHERE status = 2` + whereSql1 + `
  48. UNION ALL
  49. SELECT cow_id,plan_day,'配种' as calendar_type_name,8 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days
  50. FROM event_mating WHERE status = 2` + whereSql1 + `
  51. UNION ALL
  52. SELECT cow_id,plan_day,'产犊' as calendar_type_name,9 as calendar_type_kind,TIMESTAMPDIFF(DAY, NOW(), FROM_UNIXTIME(end_day)) AS remaining_days
  53. FROM event_calving WHERE status = 2` + whereSql1 + `
  54. UNION ALL
  55. SELECT cow_id,disease_at as plan_day,'疾病' as calendar_type_name,7 as calendar_type_kind,0 AS remaining_days
  56. FROM event_cow_disease WHERE health_status IN (2,3) ` + whereSql + `
  57. ) as a JOIN cow b ON a.cow_id = b.id `
  58. completeSql := fmt.Sprintf("%s ORDER BY a.plan_day DESC", sql)
  59. if err := s.DB.Raw(completeSql).Find(&calendarToDoList).Error; err != nil {
  60. return nil, err
  61. }
  62. return calendarToDoList, nil
  63. }
  64. // CalendarToDoList 获取日历待办列表
  65. func (s *StoreEntry) CalendarToDoList(ctx context.Context, req *pasturePb.CalendarToDoRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CalendarToDoResponse, error) {
  66. userModel, err := s.GetUserModel(ctx)
  67. if err != nil {
  68. return nil, xerr.WithStack(err)
  69. }
  70. pastureId := userModel.AppPasture.Id
  71. calendarToDoList, err := s.CalendarToDoHistoryList(ctx, pastureId, req.EarNumber)
  72. if err != nil {
  73. return nil, xerr.WithStack(err)
  74. }
  75. nowTime := time.Now().Local().Format(model.LayoutDate2)
  76. todayCompletedSql := `SELECT a.count as count,a.calendar_type_name as calendar_type_name,a.calendar_type_kind as calendar_type_kind FROM (
  77. SELECT count('cow_id') as count,'免疫' as calendar_type_name,1 as calendar_type_kind FROM event_immunization_plan
  78. WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ? AND pasture_id = ?
  79. UNION ALL
  80. SELECT count('cow_id') as count,'同期' as calendar_type_name,2 as calendar_type_kind FROM event_cow_same_time
  81. WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ? AND pasture_id = ?
  82. UNION ALL
  83. SELECT count('cow_id') as count,'孕检' as calendar_type_name,4 as calendar_type_kind FROM event_pregnant_check
  84. WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ? AND pasture_id = ?
  85. UNION ALL
  86. SELECT count('cow_id') as count,'断奶' as calendar_type_name,6 as calendar_type_kind FROM event_weaning
  87. WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ? AND pasture_id = ?
  88. UNION ALL
  89. SELECT count('cow_id') as count,'配种' as calendar_type_name,8 as calendar_type_kind FROM event_mating
  90. WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ? AND pasture_id = ?
  91. UNION ALL
  92. SELECT count('cow_id') as count,'产犊' as calendar_type_name,9 as calendar_type_kind FROM event_calving
  93. WHERE status = 1 AND DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') = ? AND pasture_id = ?
  94. UNION ALL
  95. SELECT count('cow_id') as count,'疾病' as calendar_type_name,7 as calendar_type_kind FROM event_cow_disease
  96. WHERE health_status = 4 AND DATE_FORMAT(FROM_UNIXTIME(curable_at), '%Y-%m-%d') = ? AND pasture_id = ?
  97. ) as a `
  98. toDayCompletedList := make([]*model.CompletedData, 0)
  99. if err = s.DB.Raw(todayCompletedSql, nowTime, pastureId, nowTime, pastureId, nowTime, pastureId, nowTime,
  100. pastureId, nowTime, pastureId, nowTime, pastureId, nowTime, pastureId).
  101. Find(&toDayCompletedList).Error; err != nil {
  102. return nil, xerr.WithStack(err)
  103. }
  104. toDayCompletedCountMap := make(map[pasturePb.CalendarType_Kind]*pasturePb.ProgressList)
  105. for _, v := range toDayCompletedList {
  106. toDayCompletedCountMap[v.CalendarTypeKind] = &pasturePb.ProgressList{
  107. CalendarTypeKind: v.CalendarTypeKind,
  108. CalendarName: v.CalendarTypeName,
  109. CompletedCount: v.Count,
  110. }
  111. }
  112. historyCount := make([]*model.CompletedData, 0)
  113. todayStartTime := util.TimeParseLocalUnix(nowTime)
  114. todayEndTime := util.TimeParseLocalEndUnix(nowTime)
  115. whereSql := fmt.Sprintf(` WHERE pasture_id = %d AND end_day >= %d AND (status = %d OR (status = %d AND reality_day BETWEEN %d AND %d ))`,
  116. pastureId, todayEndTime, pasturePb.IsShow_No, pasturePb.IsShow_Ok, todayStartTime, todayEndTime)
  117. historyCountSql := `SELECT a.count as count,a.calendar_type_kind as calendar_type_kind FROM (
  118. SELECT count(cow_id) as count,1 as calendar_type_kind FROM event_immunization_plan ` + whereSql + `
  119. UNION ALL
  120. SELECT count(cow_id) as count,2 as calendar_type_kind FROM event_cow_same_time ` + whereSql + `
  121. UNION ALL
  122. SELECT count(cow_id) as count,4 as calendar_type_kind FROM event_pregnant_check ` + whereSql + `
  123. UNION ALL
  124. SELECT count(cow_id) as count,6 as calendar_type_kind FROM event_weaning ` + whereSql + `
  125. UNION ALL
  126. SELECT count(cow_id) as count,8 as calendar_type_kind FROM event_mating ` + whereSql + `
  127. UNION ALL
  128. SELECT count(cow_id) as count,9 as calendar_type_kind FROM event_calving ` + whereSql + `
  129. UNION ALL
  130. SELECT count(cow_id) as count,7 as calendar_type_kind FROM event_cow_disease WHERE ` +
  131. fmt.Sprintf("pasture_id = %d AND (health_status IN (%d,%d) OR (health_status = %d AND curable_at BETWEEN %d AND %d))",
  132. pastureId, pasturePb.HealthStatus_Disease, pasturePb.HealthStatus_Treatment, pasturePb.HealthStatus_Curable, todayStartTime, todayEndTime) + `
  133. ) as a`
  134. if err = s.DB.Raw(historyCountSql).Find(&historyCount).Error; err != nil {
  135. return nil, xerr.WithStack(err)
  136. }
  137. dMap := make(map[pasturePb.CalendarType_Kind]int32)
  138. for _, v := range historyCount {
  139. dMap[v.CalendarTypeKind] = v.Count
  140. }
  141. list, total := Paginate(calendarToDoList, req, pagination)
  142. return &pasturePb.CalendarToDoResponse{
  143. Code: http.StatusOK,
  144. Msg: "ok",
  145. Data: &pasturePb.CalendarToDoData{
  146. List: list,
  147. Progress: ProgressList(dMap, toDayCompletedCountMap),
  148. Total: total,
  149. PageSize: pagination.PageSize,
  150. Page: pagination.Page,
  151. },
  152. }, nil
  153. }
  154. func (s *StoreEntry) CalendarList(ctx context.Context, req *pasturePb.CalendarRequest) (*pasturePb.CalendarResponse, error) {
  155. userModel, err := s.GetUserModel(ctx)
  156. if err != nil {
  157. return nil, xerr.WithStack(err)
  158. }
  159. calendarList := make([]*model.Calendar, 0)
  160. if err = s.DB.Model(new(model.Calendar)).
  161. Where("start_day BETWEEN ? AND ?", req.ShowStartDay, req.ShowEndDay).
  162. Where("pasture_id = ?", userModel.AppPasture.Id).
  163. Where("is_show = ?", pasturePb.IsShow_Ok).
  164. Find(&calendarList).Error; err != nil {
  165. return nil, xerr.WithStack(err)
  166. }
  167. return &pasturePb.CalendarResponse{
  168. Code: http.StatusOK,
  169. Msg: "ok",
  170. Data: model.CalendarSlice(calendarList).ToPB(),
  171. }, nil
  172. }
  173. func (s *StoreEntry) CalendarTableDetail(ctx context.Context, req *pasturePb.CalendarTableRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  174. userModel, err := s.GetUserModel(ctx)
  175. if err != nil {
  176. return nil, xerr.WithStack(err)
  177. }
  178. newCalendar := &model.Calendar{}
  179. if err = s.DB.Model(&model.Calendar{}).
  180. Where("calendar_type = ?", req.CalendarType).
  181. Where("start_day = ?", req.Start).
  182. Where("is_show = ?", pasturePb.IsShow_Ok).
  183. Where("pasture_id = ?", userModel.AppPasture.Id).
  184. First(newCalendar).Error; err != nil {
  185. return nil, xerr.WithStack(err)
  186. }
  187. if newCalendar.Id <= 0 {
  188. return nil, xerr.New("不存在该日历数据")
  189. }
  190. return s.getCalendarCowList(ctx, req.CalendarType, req.Start, pagination, userModel.AppPasture.Id)
  191. }
  192. func (s *StoreEntry) getCalendarCowList(
  193. ctx context.Context,
  194. calendarType pasturePb.CalendarType_Kind,
  195. showDay string,
  196. pagination *pasturePb.PaginationModel,
  197. pastureId int64,
  198. ) (interface{}, error) {
  199. req := &pasturePb.ItemsRequest{EndDay: showDay, CalendarType: calendarType, PastureId: int32(pastureId)}
  200. switch calendarType {
  201. case pasturePb.CalendarType_Immunisation: // 免疫
  202. return s.ImmunisationCowList(ctx, req, pagination)
  203. case pasturePb.CalendarType_PG, pasturePb.CalendarType_RnGH: // 同期
  204. return s.SameTimeCowList(ctx, req, pagination)
  205. case pasturePb.CalendarType_Pregnancy_Check: // 孕检
  206. return s.PregnancyCheckCowList(ctx, req, pagination)
  207. case pasturePb.CalendarType_WorkOrder: // 工作单
  208. return s.WorkOrderCowList(ctx, req, pagination)
  209. case pasturePb.CalendarType_Weaning: // 断奶
  210. return s.WeaningCowList(ctx, req, pagination)
  211. case pasturePb.CalendarType_Disease: // 治疗
  212. return s.TreatmentCowList(ctx, req, pagination)
  213. case pasturePb.CalendarType_Mating: // 配种
  214. return s.MatingCowList(ctx, req, pagination)
  215. case pasturePb.CalendarType_Calving: // 产犊
  216. return s.CalvingCowList(ctx, req, pagination)
  217. case pasturePb.CalendarType_DryMilk: // 干奶
  218. return s.DryMilkCowList(ctx, req, pagination)
  219. default:
  220. return nil, xerr.New("不支持的日历类型")
  221. }
  222. }
  223. func (s *StoreEntry) ImmunisationCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.ImmunizationItemsResponse, error) {
  224. userModel, err := s.GetUserModel(ctx)
  225. if err != nil {
  226. return nil, xerr.WithStack(err)
  227. }
  228. eventImmunizationPlanList := make([]*model.EventImmunizationPlan, 0)
  229. count := int64(0)
  230. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventImmunizationPlan).TableName())).
  231. Select("a.id,a.cow_id,a.plan_day,a.plan_name,b.pen_name,b.day_age,b.ear_number,a.plan_id").
  232. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  233. Where("a.status = ?", pasturePb.IsShow_No).
  234. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  235. if req.StartDay != "" && req.EndDay != "" {
  236. startTime := util.TimeParseLocalUnix(req.StartDay)
  237. endTime := util.TimeParseLocalEndUnix(req.EndDay)
  238. pref.Where("a.plan_day between ? and ?", startTime, endTime)
  239. }
  240. if req.CowId > 0 {
  241. pref.Where("a.cow_id = ?", req.CowId)
  242. }
  243. if req.PlanId > 0 {
  244. pref.Where("a.plan_id = ?", req.PlanId)
  245. }
  246. if req.PenId > 0 {
  247. pref.Where("b.pen_id = ?", req.PenId)
  248. }
  249. if err = pref.Count(&count).
  250. Limit(int(pagination.PageSize)).
  251. Offset(int(pagination.PageOffset)).
  252. Order("a.plan_day DESC").
  253. Find(&eventImmunizationPlanList).Error; err != nil {
  254. return nil, xerr.WithStack(err)
  255. }
  256. return &pasturePb.ImmunizationItemsResponse{
  257. Code: http.StatusOK,
  258. Msg: "ok",
  259. Data: &pasturePb.ImmunizationItemsData{
  260. Total: int32(count),
  261. Page: pagination.Page,
  262. PageSize: pagination.PageSize,
  263. HeaderSort: []string{"planDay", "planName", "penName", "dayAge", "earNumber", "planId"},
  264. Header: map[string]string{
  265. "earNumber": "耳标号",
  266. "penName": "栏舍",
  267. "dayAge": "日龄",
  268. "planDay": "免疫时间",
  269. "planName": "免疫名称",
  270. "planId": "免疫Id",
  271. },
  272. List: model.EventImmunizationPlanSlice(eventImmunizationPlanList).ToPB(),
  273. },
  274. }, nil
  275. }
  276. func (s *StoreEntry) SameTimeCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SameTimeItemResponse, error) {
  277. userModel, err := s.GetUserModel(ctx)
  278. if err != nil {
  279. return nil, xerr.WithStack(err)
  280. }
  281. sameTimeBodyList := make([]*model.SameTimeItemBody, 0)
  282. count := int64(0)
  283. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowSameTime).TableName())).
  284. Select(`a.id,a.cow_id,a.ear_number,a.pen_name,a.status,a.same_time_type,b.breed_status,a.same_time_name,a.plan_day,
  285. b.cow_type,b.day_age,b.calving_age,b.abortion_age,b.last_calving_at,b.last_abortion_at,b.lact,b.pen_name,b.mating_times`).
  286. Joins("left join cow as b on a.cow_id = b.id").
  287. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  288. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  289. Where("a.status = ?", pasturePb.IsShow_No).
  290. Where("a.plan_day <= ?", time.Now().Local().Unix())
  291. if req.EndDay != "" {
  292. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  293. pref.Where("a.plan_day <= ?", dateTime)
  294. }
  295. if req.CowType > 0 {
  296. pref.Where("b.cow_type = ?", req.CowType)
  297. }
  298. if req.SameTimeId > 0 {
  299. pref.Where("a.same_time_id = ?", req.SameTimeId)
  300. }
  301. if req.SameTimeType > 0 {
  302. pref.Where("a.same_time_type = ?", req.SameTimeType)
  303. }
  304. if err = pref.Order("a.plan_day DESC").Count(&count).
  305. Limit(int(pagination.PageSize)).
  306. Offset(int(pagination.PageOffset)).
  307. Find(&sameTimeBodyList).Error; err != nil {
  308. return nil, xerr.WithStack(err)
  309. }
  310. breedStatusMap := s.CowBreedStatusMap()
  311. sameTimeTypeMap := s.SameTimeTypeMap()
  312. return &pasturePb.SameTimeItemResponse{
  313. Code: http.StatusOK,
  314. Msg: "ok",
  315. Data: &pasturePb.SameTimeItemsData{
  316. Total: int32(count),
  317. Page: pagination.Page,
  318. PageSize: pagination.PageSize,
  319. HeaderSort: []string{"earNumber", "breedStatusName", "cowTypeName", "planDayAtFormat", "penName",
  320. "lact", "calvingAge", "abortionAge", "dayAge", "status", "sameTimeTypeName", "matingTimes", "calvingAtFormat",
  321. "abortionAtFormat", "sameTimeName"},
  322. Header: map[string]string{
  323. "earNumber": "耳标号",
  324. "breedStatusName": "繁殖状态",
  325. "cowTypeName": "牛只类型",
  326. "planDayAtFormat": "执行日期",
  327. "penName": "栏舍",
  328. "lact": "胎次",
  329. "calvingAge": "产后天数",
  330. "abortionAge": "流产天数",
  331. "dayAge": "日龄",
  332. "status": "状态",
  333. "sameTimeTypeName": "处理方式",
  334. "matingTimes": "本胎次配次",
  335. "calvingAtFormat": "产犊日期",
  336. "abortionAtFormat": "流产日期",
  337. "sameTimeName": "同期名称",
  338. },
  339. List: model.SameTimeBodySlice(sameTimeBodyList).ToPB(breedStatusMap, sameTimeTypeMap),
  340. },
  341. }, nil
  342. }
  343. func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnancyCheckItemsResponse, error) {
  344. userModel, err := s.GetUserModel(ctx)
  345. if err != nil {
  346. return nil, xerr.WithStack(err)
  347. }
  348. newPregnancyCheckItems := make([]*pasturePb.PregnancyCheckItems, 0)
  349. var count int64
  350. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventPregnantCheck).TableName())).
  351. Select(`a.id,a.cow_id,a.ear_number,a.pen_id,a.status,b.pen_name,b.cow_type,
  352. DATE_FORMAT(FROM_UNIXTIME(b.last_mating_at),'%Y-%m-%d')as mating_at_format,pregnancy_age,DATEDIFF(CURDATE(),
  353. FROM_UNIXTIME(last_mating_at)) AS mating_age,
  354. b.breed_status,
  355. CASE b.breed_status
  356. WHEN 1 THEN '未配'
  357. WHEN 2 THEN '已配'
  358. WHEN 3 THEN '已孕'
  359. WHEN 4 THEN '空怀'
  360. WHEN 5 THEN '流产'
  361. WHEN 6 THEN '产犊'
  362. WHEN 7 THEN '禁配'
  363. ELSE '未知'
  364. END AS breed_status_name,
  365. CASE a.pregnant_check_name
  366. WHEN 'pregnant_check_for_first' THEN '初检'
  367. WHEN 'pregnant_check_for_second' THEN '复检'
  368. ELSE '其他'
  369. END AS check_type_name,b.day_age,b.calving_age,b.abortion_age,a.bull_id`).
  370. Joins("left join cow as b on a.cow_id = b.id").
  371. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  372. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  373. Where("a.status = ?", pasturePb.IsShow_No)
  374. if req.EarNumber != "" {
  375. pref.Where("a.ear_number = ?", req.EarNumber)
  376. }
  377. if req.EndDay != "" {
  378. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  379. pref.Where("a.plan_day <= ?", dateTime)
  380. }
  381. if req.PenId > 0 {
  382. pref.Where("b.pen_id = ?", req.PenId)
  383. }
  384. if req.CowType > 0 {
  385. pref.Where("a.cow_type = ?", req.CowType)
  386. }
  387. if req.PregnantCheckType > 0 {
  388. pref.Where("a.pregnant_check_name = ?", model.PregnantCheckNameValueMap[req.PregnantCheckType])
  389. }
  390. if err = pref.Order("a.plan_day DESC").
  391. Count(&count).
  392. Limit(int(pagination.PageSize)).
  393. Offset(int(pagination.PageOffset)).
  394. Find(&newPregnancyCheckItems).Error; err != nil {
  395. return nil, xerr.WithStack(err)
  396. }
  397. return &pasturePb.PregnancyCheckItemsResponse{
  398. Code: http.StatusOK,
  399. Msg: "ok",
  400. Data: &pasturePb.PregnancyCheckItemsData{
  401. Total: int32(count),
  402. Page: pagination.Page,
  403. PageSize: pagination.PageSize,
  404. HeaderSort: []string{"earNumber", "cowTypeName", "penName", "lact", "dayAge", "breedStatus", "planDay",
  405. "checkTypeName", "status", "matingTimes", "calvingAtFormat", "matingAtFormat", "matingAge", "bullId", "pregnancyAge"},
  406. Header: map[string]string{
  407. "earNumber": "耳标号",
  408. "cowTypeName": "牛只类型",
  409. "penName": "栏舍",
  410. "lact": "胎次",
  411. "dayAge": "日龄",
  412. "planDay": "孕检日期",
  413. "checkTypeName": "孕检名称",
  414. "status": "状态",
  415. "matingTimes": "配次",
  416. "calvingAtFormat": "产检日期",
  417. "matingAtFormat": "配种日期",
  418. "matingAge": "配后天数",
  419. "bullId": "配种公牛",
  420. "pregnancyAge": "怀孕天数",
  421. "breedStatus": "繁殖状态",
  422. },
  423. List: newPregnancyCheckItems,
  424. },
  425. }, nil
  426. }
  427. func (s *StoreEntry) WeaningCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.WeaningItemsResponse, error) {
  428. userModel, err := s.GetUserModel(ctx)
  429. if err != nil {
  430. return nil, xerr.WithStack(err)
  431. }
  432. weaningItems := make([]*pasturePb.WeaningItems, 0)
  433. count := int64(0)
  434. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeaning).TableName())).
  435. Select(`a.id,a.cow_id,ROUND(b.current_weight / 1000,2) as current_weight,
  436. DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day_format,b.day_age,b.pen_name,
  437. b.ear_number,DATE_FORMAT(FROM_UNIXTIME(b.birth_at), '%Y-%m-%d') AS birth_at_format`).
  438. Joins("left join cow as b on a.cow_id = b.id").
  439. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  440. Where("a.status = ?", pasturePb.IsShow_No).
  441. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  442. if req.EndDay != "" {
  443. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  444. pref.Where("a.plan_day <= ?", dateTime)
  445. }
  446. if err = pref.Order("a.plan_day DESC").Count(&count).
  447. Limit(int(pagination.PageSize)).
  448. Offset(int(pagination.PageOffset)).
  449. Find(&weaningItems).Error; err != nil {
  450. return nil, xerr.WithStack(err)
  451. }
  452. return &pasturePb.WeaningItemsResponse{
  453. Code: http.StatusOK,
  454. Msg: "ok",
  455. Data: &pasturePb.WeaningItemsData{
  456. Total: int32(count),
  457. Page: pagination.Page,
  458. PageSize: pagination.PageSize,
  459. HeaderSort: []string{"earNumber", "penName", "dayAge", "planDayFormat", "birthAtFormat", "currentWeight"},
  460. Header: map[string]string{
  461. "earNumber": "耳标号",
  462. "penName": "栏舍",
  463. "dayAge": "日龄",
  464. "planDayFormat": "断奶日期",
  465. "birthAtFormat": "出生日期",
  466. "currentWeight": "体重",
  467. },
  468. List: weaningItems,
  469. },
  470. }, nil
  471. }
  472. func (s *StoreEntry) MatingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingItemsResponse, error) {
  473. userModel, err := s.GetUserModel(ctx)
  474. if err != nil {
  475. return nil, xerr.WithStack(err)
  476. }
  477. matingItems := make([]*pasturePb.MatingItems, 0)
  478. count := int64(0)
  479. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventMating).TableName())).
  480. Select(`a.id,a.cow_id,a.status,a.ear_number,DATE_FORMAT(FROM_UNIXTIME(plan_day), '%Y-%m-%d') as plan_day,
  481. CASE a.expose_estrus_type
  482. WHEN 1 THEN '脖环揭发'
  483. WHEN 2 THEN '脚环/计步器'
  484. WHEN 3 THEN '自然发情'
  485. WHEN 4 THEN '同期'
  486. ELSE '其他'
  487. END AS expose_estrus_type_name,
  488. CASE
  489. WHEN last_calving_at = 0 THEN ""
  490. ELSE DATE_FORMAT(FROM_UNIXTIME(last_calving_at), '%Y-%m-%d')
  491. END AS last_calving_at_format,
  492. b.breed_status,b.cow_type,b.pen_id,b.day_age,b.calving_age,b.abortion_age,b.pen_name`).
  493. Joins("left join cow as b on a.cow_id = b.id").
  494. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  495. Where("a.status = ?", pasturePb.IsShow_No)
  496. if req.EndDay != "" {
  497. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  498. pref.Where("a.plan_day <= ?", dateTime)
  499. }
  500. if req.PenId > 0 {
  501. pref.Where("b.pen_id = ?", req.PenId)
  502. }
  503. if req.EarNumber != "" {
  504. pref.Where("a.ear_number = ?", req.EarNumber)
  505. }
  506. if err = pref.Order("a.plan_day DESC").
  507. Count(&count).
  508. Limit(int(pagination.PageSize)).
  509. Offset(int(pagination.PageOffset)).
  510. Find(&matingItems).Error; err != nil {
  511. return nil, xerr.WithStack(err)
  512. }
  513. return &pasturePb.MatingItemsResponse{
  514. Code: http.StatusOK,
  515. Msg: "ok",
  516. Data: &pasturePb.MatingItemsData{
  517. Total: int32(count),
  518. Page: pagination.Page,
  519. PageSize: pagination.PageSize,
  520. HeaderSort: []string{"earNumber", "dayAge", "lact", "penName", "planDay", "breedStatusName",
  521. "cowTypeName", "calvingAge", "abortionAge", "exposeEstrusTypeName", "lastCalvingAtFormat"},
  522. Header: map[string]string{
  523. "earNumber": "耳标号",
  524. "breedStatusName": "繁殖状态",
  525. "cowTypeName": "牛只类型",
  526. "penName": "栏舍",
  527. "lact": "胎次",
  528. "calvingAge": "产后天数",
  529. "abortionAge": "流产天数",
  530. "dayAge": "日龄",
  531. "status": "状态",
  532. "exposeEstrusTypeName": "发情揭发方式",
  533. "lastCalvingAtFormat": "产犊日期",
  534. "planDay": "计划配种时间",
  535. },
  536. List: matingItems,
  537. },
  538. }, nil
  539. }