calendar.go 23 KB

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