calendar.go 19 KB

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