calendar.go 20 KB

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