calendar.go 20 KB

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