calendar.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. `
  135. if err = s.DB.Raw(historyCountSql).Find(&historyCount).Error; err != nil {
  136. return nil, xerr.WithStack(err)
  137. }
  138. dMap := make(map[pasturePb.CalendarType_Kind]int32)
  139. for _, v := range historyCount {
  140. dMap[v.CalendarTypeKind] = v.Count
  141. }
  142. list, total := Paginate(calendarToDoList, req, pagination)
  143. return &pasturePb.CalendarToDoResponse{
  144. Code: http.StatusOK,
  145. Msg: "ok",
  146. Data: &pasturePb.CalendarToDoData{
  147. List: list,
  148. Progress: ProgressList(dMap, toDayCompletedCountMap),
  149. Total: total,
  150. PageSize: pagination.PageSize,
  151. Page: pagination.Page,
  152. },
  153. }, nil
  154. }
  155. func (s *StoreEntry) CalendarList(ctx context.Context, req *pasturePb.CalendarRequest) (*pasturePb.CalendarResponse, error) {
  156. userModel, err := s.GetUserModel(ctx)
  157. if err != nil {
  158. return nil, xerr.WithStack(err)
  159. }
  160. calendarList := make([]*model.Calendar, 0)
  161. if err = s.DB.Model(new(model.Calendar)).
  162. Where("start_day BETWEEN ? AND ?", req.ShowStartDay, req.ShowEndDay).
  163. Where("pasture_id = ?", userModel.AppPasture.Id).
  164. Where("is_show = ?", pasturePb.IsShow_Ok).
  165. Find(&calendarList).Error; err != nil {
  166. return nil, xerr.WithStack(err)
  167. }
  168. return &pasturePb.CalendarResponse{
  169. Code: http.StatusOK,
  170. Msg: "ok",
  171. Data: model.CalendarSlice(calendarList).ToPB(),
  172. }, nil
  173. }
  174. func (s *StoreEntry) CalendarTableDetail(ctx context.Context, req *pasturePb.CalendarTableRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  175. userModel, err := s.GetUserModel(ctx)
  176. if err != nil {
  177. return nil, xerr.WithStack(err)
  178. }
  179. newCalendar := &model.Calendar{}
  180. if err = s.DB.Model(&model.Calendar{}).
  181. Where("calendar_type = ?", req.CalendarType).
  182. Where("start_day = ?", req.Start).
  183. Where("is_show = ?", pasturePb.IsShow_Ok).
  184. Where("pasture_id = ?", userModel.AppPasture.Id).
  185. First(newCalendar).Error; err != nil {
  186. return nil, xerr.WithStack(err)
  187. }
  188. if newCalendar.Id <= 0 {
  189. return nil, xerr.New("不存在该日历数据")
  190. }
  191. return s.getCalendarCowList(ctx, req.CalendarType, req.Start, pagination, userModel.AppPasture.Id)
  192. }
  193. func (s *StoreEntry) getCalendarCowList(
  194. ctx context.Context,
  195. calendarType pasturePb.CalendarType_Kind,
  196. showDay string,
  197. pagination *pasturePb.PaginationModel,
  198. pastureId int64,
  199. ) (interface{}, error) {
  200. req := &pasturePb.ItemsRequest{EndDay: showDay, CalendarType: calendarType, PastureId: int32(pastureId)}
  201. switch calendarType {
  202. case pasturePb.CalendarType_Immunisation: // 免疫
  203. return s.ImmunisationCowList(ctx, req, pagination)
  204. case pasturePb.CalendarType_PG, pasturePb.CalendarType_RnGH: // 同期
  205. return s.SameTimeCowList(ctx, req, pagination)
  206. case pasturePb.CalendarType_Pregnancy_Check: // 孕检
  207. return s.PregnancyCheckCowList(ctx, req, pagination)
  208. case pasturePb.CalendarType_WorkOrder: // 工作单
  209. return s.WorkOrderCowList(ctx, req, pagination)
  210. case pasturePb.CalendarType_Weaning: // 断奶
  211. return s.WeaningCowList(ctx, req, pagination)
  212. case pasturePb.CalendarType_Disease: // 治疗
  213. return s.TreatmentCowList(ctx, req, pagination)
  214. case pasturePb.CalendarType_Mating: // 配种
  215. return s.MatingCowList(ctx, req, pagination)
  216. case pasturePb.CalendarType_Calving: // 产犊
  217. return s.CalvingCowList(ctx, req, pagination)
  218. case pasturePb.CalendarType_DryMilk: // 干奶
  219. return s.DryMilkCowList(ctx, req, pagination)
  220. default:
  221. return nil, xerr.New("不支持的日历类型")
  222. }
  223. }
  224. func (s *StoreEntry) ImmunisationCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.ImmunizationItemsResponse, error) {
  225. userModel, err := s.GetUserModel(ctx)
  226. if err != nil {
  227. return nil, xerr.WithStack(err)
  228. }
  229. eventImmunizationPlanList := make([]*model.EventImmunizationPlan, 0)
  230. count := int64(0)
  231. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventImmunizationPlan).TableName())).
  232. Select("a.id,a.cow_id,a.plan_day,a.plan_name,b.pen_name,b.day_age,b.ear_number,a.plan_id").
  233. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  234. Where("a.status = ?", pasturePb.IsShow_No).
  235. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  236. if req.StartDay != "" && req.EndDay != "" {
  237. startTime := util.TimeParseLocalUnix(req.StartDay)
  238. endTime := util.TimeParseLocalEndUnix(req.EndDay)
  239. pref.Where("a.plan_day between ? and ?", startTime, endTime)
  240. }
  241. if req.CowId > 0 {
  242. pref.Where("a.cow_id = ?", req.CowId)
  243. }
  244. if req.PlanId > 0 {
  245. pref.Where("a.plan_id = ?", req.PlanId)
  246. }
  247. if req.PenId > 0 {
  248. pref.Where("b.pen_id = ?", req.PenId)
  249. }
  250. if err = pref.Count(&count).
  251. Limit(int(pagination.PageSize)).
  252. Offset(int(pagination.PageOffset)).
  253. Order("a.plan_day DESC").
  254. Find(&eventImmunizationPlanList).Error; err != nil {
  255. return nil, xerr.WithStack(err)
  256. }
  257. return &pasturePb.ImmunizationItemsResponse{
  258. Code: http.StatusOK,
  259. Msg: "ok",
  260. Data: &pasturePb.ImmunizationItemsData{
  261. Total: int32(count),
  262. Page: pagination.Page,
  263. PageSize: pagination.PageSize,
  264. HeaderSort: []string{"id", "cowId", "planDay", "planName", "penName", "dayAge", "earNumber", "planId"},
  265. Header: map[string]string{
  266. "id": "编号",
  267. "cowId": "牛号",
  268. "earNumber": "耳标号",
  269. "penName": "栏舍",
  270. "dayAge": "日龄",
  271. "planDay": "免疫时间",
  272. "planName": "免疫名称",
  273. "planId": "免疫Id",
  274. },
  275. List: model.EventImmunizationPlanSlice(eventImmunizationPlanList).ToPB(),
  276. },
  277. }, nil
  278. }
  279. func (s *StoreEntry) SameTimeCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SameTimeItemResponse, error) {
  280. userModel, err := s.GetUserModel(ctx)
  281. if err != nil {
  282. return nil, xerr.WithStack(err)
  283. }
  284. sameTimeBodyList := make([]*model.SameTimeItemBody, 0)
  285. count := int64(0)
  286. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowSameTime).TableName())).
  287. 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,
  288. 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`).
  289. Joins("left join cow as b on a.cow_id = b.id").
  290. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  291. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  292. Where("a.status = ?", pasturePb.IsShow_No).
  293. Where("a.plan_day <= ?", time.Now().Local().Unix())
  294. if req.EndDay != "" {
  295. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  296. pref.Where("a.plan_day <= ?", dateTime)
  297. }
  298. if req.CowType > 0 {
  299. pref.Where("b.cow_type = ?", req.CowType)
  300. }
  301. if req.SameTimeId > 0 {
  302. pref.Where("a.same_time_id = ?", req.SameTimeId)
  303. }
  304. if req.SameTimeType > 0 {
  305. pref.Where("a.same_time_type = ?", req.SameTimeType)
  306. }
  307. if err = pref.Order("a.plan_day DESC").Count(&count).
  308. Limit(int(pagination.PageSize)).
  309. Offset(int(pagination.PageOffset)).
  310. Find(&sameTimeBodyList).Error; err != nil {
  311. return nil, xerr.WithStack(err)
  312. }
  313. breedStatusMap := s.CowBreedStatusMap()
  314. sameTimeTypeMap := s.SameTimeTypeMap()
  315. return &pasturePb.SameTimeItemResponse{
  316. Code: http.StatusOK,
  317. Msg: "ok",
  318. Data: &pasturePb.SameTimeItemsData{
  319. Total: int32(count),
  320. Page: pagination.Page,
  321. PageSize: pagination.PageSize,
  322. HeaderSort: []string{"id", "cowId", "earNumber", "breedStatusName", "cowTypeName", "planDayAtFormat", "penName",
  323. "lact", "calvingAge", "abortionAge", "dayAge", "status", "sameTimeTypeName", "matingTimes", "calvingAtFormat",
  324. "abortionAtFormat", "sameTimeName"},
  325. Header: map[string]string{
  326. "id": "编号",
  327. "cowId": "牛号",
  328. "earNumber": "耳标号",
  329. "breedStatusName": "繁殖状态",
  330. "cowTypeName": "牛只类型",
  331. "planDayAtFormat": "执行日期",
  332. "penName": "栏舍",
  333. "lact": "胎次",
  334. "calvingAge": "产后天数",
  335. "abortionAge": "流产天数",
  336. "dayAge": "日龄",
  337. "status": "状态",
  338. "sameTimeTypeName": "处理方式",
  339. "matingTimes": "本胎次配次",
  340. "calvingAtFormat": "产犊日期",
  341. "abortionAtFormat": "流产日期",
  342. "sameTimeName": "同期名称",
  343. },
  344. List: model.SameTimeBodySlice(sameTimeBodyList).ToPB(breedStatusMap, sameTimeTypeMap),
  345. },
  346. }, nil
  347. }
  348. func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnancyCheckItemsResponse, error) {
  349. userModel, err := s.GetUserModel(ctx)
  350. if err != nil {
  351. return nil, xerr.WithStack(err)
  352. }
  353. newPregnancyCheckItems := make([]*pasturePb.PregnancyCheckItems, 0)
  354. var count int64
  355. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventPregnantCheck).TableName())).
  356. Select(`a.id,a.cow_id,a.ear_number,a.pen_id,a.status,b.pen_name,b.cow_type,
  357. DATE_FORMAT(FROM_UNIXTIME(b.last_mating_at),'%Y-%m-%d')as mating_at_format,pregnancy_age,DATEDIFF(CURDATE(),
  358. FROM_UNIXTIME(last_mating_at)) AS mating_age,
  359. CASE b.breed_status
  360. WHEN 1 THEN '未配'
  361. WHEN 2 THEN '已配'
  362. WHEN 3 THEN '已孕'
  363. WHEN 4 THEN '空怀'
  364. WHEN 5 THEN '流产'
  365. WHEN 6 THEN '产犊'
  366. WHEN 7 THEN '禁配'
  367. ELSE '其他'
  368. END AS breed_status_name,
  369. CASE a.pregnant_check_name
  370. WHEN 'pregnant_check_for_first' THEN '初检'
  371. WHEN 'pregnant_check_for_second' THEN '复检'
  372. ELSE '其他'
  373. END AS check_type_name,b.day_age,b.calving_age,b.abortion_age,a.bull_id`).
  374. Joins("left join cow as b on a.cow_id = b.id").
  375. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  376. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  377. Where("a.status = ?", pasturePb.IsShow_No)
  378. if req.EndDay != "" {
  379. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  380. pref.Where("a.plan_day <= ?", dateTime)
  381. }
  382. if req.PenId > 0 {
  383. pref.Where("b.pen_id = ?", req.PenId)
  384. }
  385. if req.CowType > 0 {
  386. pref.Where("a.cow_type = ?", req.CowType)
  387. }
  388. if req.PregnantCheckType > 0 {
  389. pref.Where("a.pregnant_check_name = ?", model.PregnantCheckNameValueMap[req.PregnantCheckType])
  390. }
  391. if err = pref.Order("a.plan_day DESC").
  392. Count(&count).
  393. Limit(int(pagination.PageSize)).
  394. Offset(int(pagination.PageOffset)).
  395. Find(&newPregnancyCheckItems).Error; err != nil {
  396. return nil, xerr.WithStack(err)
  397. }
  398. return &pasturePb.PregnancyCheckItemsResponse{
  399. Code: http.StatusOK,
  400. Msg: "ok",
  401. Data: &pasturePb.PregnancyCheckItemsData{
  402. Total: int32(count),
  403. Page: pagination.Page,
  404. PageSize: pagination.PageSize,
  405. HeaderSort: []string{"id", "cowId", "earNumber", "cowTypeName", "penName", "lact", "dayAge", "breedStatusName", "planDay",
  406. "checkTypeName", "status", "matingTimes", "calvingAtFormat", "matingAtFormat", "matingAge", "bullId", "pregnancyAge"},
  407. Header: map[string]string{
  408. "id": "编号",
  409. "cowId": "牛号",
  410. "earNumber": "耳标号",
  411. "cowTypeName": "牛只类型",
  412. "penName": "栏舍",
  413. "lact": "胎次",
  414. "dayAge": "日龄",
  415. "planDay": "孕检日期",
  416. "checkTypeName": "孕检名称",
  417. "status": "状态",
  418. "matingTimes": "配次",
  419. "calvingAtFormat": "产检日期",
  420. "matingAtFormat": "配种日期",
  421. "matingAge": "配后天数",
  422. "bullId": "配种公牛",
  423. "pregnancyAge": "怀孕天数",
  424. "breedStatusName": "繁殖状态",
  425. },
  426. List: newPregnancyCheckItems,
  427. },
  428. }, nil
  429. }
  430. func (s *StoreEntry) WeaningCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.WeaningItemsResponse, error) {
  431. userModel, err := s.GetUserModel(ctx)
  432. if err != nil {
  433. return nil, xerr.WithStack(err)
  434. }
  435. weaningItems := make([]*pasturePb.WeaningItems, 0)
  436. count := int64(0)
  437. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeaning).TableName())).
  438. Select(`a.id,a.cow_id,ROUND(b.current_weight / 1000,2) as current_weight,
  439. DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day_format,b.day_age,b.pen_name,
  440. b.ear_number,DATE_FORMAT(FROM_UNIXTIME(b.birth_at), '%Y-%m-%d') AS birth_at_format`).
  441. Joins("left join cow as b on a.cow_id = b.id").
  442. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  443. Where("a.status = ?", pasturePb.IsShow_No).
  444. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  445. if req.EndDay != "" {
  446. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  447. pref.Where("a.plan_day <= ?", dateTime)
  448. }
  449. if err = pref.Order("a.plan_day DESC").Count(&count).
  450. Limit(int(pagination.PageSize)).
  451. Offset(int(pagination.PageOffset)).
  452. Find(&weaningItems).Error; err != nil {
  453. return nil, xerr.WithStack(err)
  454. }
  455. return &pasturePb.WeaningItemsResponse{
  456. Code: http.StatusOK,
  457. Msg: "ok",
  458. Data: &pasturePb.WeaningItemsData{
  459. Total: int32(count),
  460. Page: pagination.Page,
  461. PageSize: pagination.PageSize,
  462. HeaderSort: []string{"id", "cowId", "earNumber", "penName", "dayAge", "planDayFormat", "birthAtFormat", "currentWeight"},
  463. Header: map[string]string{
  464. "id": "编号",
  465. "cowId": "牛号",
  466. "earNumber": "耳标号",
  467. "penName": "栏舍",
  468. "dayAge": "日龄",
  469. "planDayFormat": "断奶日期",
  470. "birthAtFormat": "出生日期",
  471. "currentWeight": "体重",
  472. },
  473. List: weaningItems,
  474. },
  475. }, nil
  476. }
  477. func (s *StoreEntry) MatingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingItemsResponse, error) {
  478. userModel, err := s.GetUserModel(ctx)
  479. if err != nil {
  480. return nil, xerr.WithStack(err)
  481. }
  482. matingItems := make([]*pasturePb.MatingItems, 0)
  483. count := int64(0)
  484. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventMating).TableName())).
  485. Select(`a.id,a.cow_id,a.status,a.ear_number,DATE_FORMAT(FROM_UNIXTIME(plan_day), '%Y-%m-%d') as plan_day,
  486. CASE a.expose_estrus_type
  487. WHEN 1 THEN '脖环揭发'
  488. WHEN 2 THEN '脚环/计步器'
  489. WHEN 3 THEN '自然发情'
  490. WHEN 4 THEN '同期'
  491. ELSE '其他'
  492. END AS expose_estrus_type_name,
  493. CASE
  494. WHEN last_calving_at = 0 THEN ""
  495. ELSE DATE_FORMAT(FROM_UNIXTIME(last_calving_at), '%Y-%m-%d %H:%i:%s')
  496. END AS last_calving_at_format,
  497. b.breed_status,b.cow_type,b.pen_id,b.day_age,b.calving_age,b.abortion_age,b.pen_name`).
  498. Joins("left join cow as b on a.cow_id = b.id").
  499. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  500. Where("a.status = ?", pasturePb.IsShow_No)
  501. if req.EndDay != "" {
  502. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  503. pref.Where("a.plan_day <= ?", dateTime)
  504. }
  505. if req.PenId > 0 {
  506. pref.Where("b.pen_id = ?", req.PenId)
  507. }
  508. if req.EarNumber != "" {
  509. pref.Where("a.ear_number = ?", req.EarNumber)
  510. }
  511. if err = pref.Order("a.plan_day DESC").
  512. Count(&count).
  513. Limit(int(pagination.PageSize)).
  514. Offset(int(pagination.PageOffset)).
  515. Find(&matingItems).Error; err != nil {
  516. return nil, xerr.WithStack(err)
  517. }
  518. return &pasturePb.MatingItemsResponse{
  519. Code: http.StatusOK,
  520. Msg: "ok",
  521. Data: &pasturePb.MatingItemsData{
  522. Total: int32(count),
  523. Page: pagination.Page,
  524. PageSize: pagination.PageSize,
  525. HeaderSort: []string{"id", "cowId", "earNumber", "dayAge", "lact", "penName", "planDay", "breedStatusName",
  526. "cowTypeName", "calvingAge", "abortionAge", "exposeEstrusTypeName", "lastCalvingAtFormat"},
  527. Header: map[string]string{
  528. "id": "编号",
  529. "cowId": "牛号",
  530. "earNumber": "耳标号",
  531. "breedStatusName": "繁殖状态",
  532. "cowTypeName": "牛只类型",
  533. "penName": "栏舍",
  534. "lact": "胎次",
  535. "calvingAge": "产后天数",
  536. "abortionAge": "流产天数",
  537. "dayAge": "日龄",
  538. "status": "状态",
  539. "exposeEstrusTypeName": "发情揭发方式",
  540. "lastCalvingAtFormat": "产犊日期",
  541. "planDay": "计划配种时间",
  542. },
  543. List: matingItems,
  544. },
  545. }, nil
  546. }