calendar.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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.breed_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(), FROM_UNIXTIME(last_mating_at)) AS mating_age,
  358. CASE a.pregnant_check_name
  359. WHEN 'pregnant_check_for_first' THEN '初检'
  360. WHEN 'pregnant_check_for_second' THEN '复检'
  361. ELSE '其他'
  362. END AS check_type_name,b.day_age,b.calving_age,b.abortion_age,a.bull_id`).
  363. Joins("left join cow as b on a.cow_id = b.id").
  364. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  365. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  366. Where("a.status = ?", pasturePb.IsShow_No)
  367. if req.EndDay != "" {
  368. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  369. pref.Where("a.plan_day <= ?", dateTime)
  370. }
  371. if req.PenId > 0 {
  372. pref.Where("b.pen_id = ?", req.PenId)
  373. }
  374. if req.CowType > 0 {
  375. pref.Where("a.cow_type = ?", req.CowType)
  376. }
  377. if req.PregnantCheckType > 0 {
  378. pref.Where("a.pregnant_check_name = ?", model.PregnantCheckNameValueMap[req.PregnantCheckType])
  379. }
  380. if err = pref.Order("a.plan_day DESC").
  381. Count(&count).
  382. Limit(int(pagination.PageSize)).
  383. Offset(int(pagination.PageOffset)).
  384. Find(&newPregnancyCheckItems).Error; err != nil {
  385. return nil, xerr.WithStack(err)
  386. }
  387. return &pasturePb.PregnancyCheckItemsResponse{
  388. Code: http.StatusOK,
  389. Msg: "ok",
  390. Data: &pasturePb.PregnancyCheckItemsData{
  391. Total: int32(count),
  392. Page: pagination.Page,
  393. PageSize: pagination.PageSize,
  394. HeaderSort: []string{"id", "cowId", "earNumber", "cowTypeName", "penName", "lact", "dayAge", "planDay",
  395. "checkTypeName", "status", "matingTimes", "calvingAtFormat", "matingAtFormat", "matingAge", "bullId", "pregnancyAge"},
  396. Header: map[string]string{
  397. "id": "编号",
  398. "cowId": "牛号",
  399. "earNumber": "耳标号",
  400. "cowTypeName": "牛只类型",
  401. "penName": "栏舍",
  402. "lact": "胎次",
  403. "dayAge": "日龄",
  404. "planDay": "孕检日期",
  405. "checkTypeName": "孕检名称",
  406. "status": "状态",
  407. "matingTimes": "配次",
  408. "calvingAtFormat": "产检日期",
  409. "matingAtFormat": "配种日期",
  410. "matingAge": "配后天数",
  411. "bullId": "配种公牛",
  412. "pregnancyAge": "怀孕天数",
  413. },
  414. List: newPregnancyCheckItems,
  415. },
  416. }, nil
  417. }
  418. func (s *StoreEntry) WeaningCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.WeaningItemsResponse, error) {
  419. userModel, err := s.GetUserModel(ctx)
  420. if err != nil {
  421. return nil, xerr.WithStack(err)
  422. }
  423. weaningItems := make([]*pasturePb.WeaningItems, 0)
  424. count := int64(0)
  425. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeaning).TableName())).
  426. Select(`a.id,a.cow_id,ROUND(b.current_weight / 1000,2) as current_weight,
  427. DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day_format,b.day_age,b.pen_name,
  428. b.ear_number,DATE_FORMAT(FROM_UNIXTIME(b.birth_at), '%Y-%m-%d') AS birth_at_format`).
  429. Joins("left join cow as b on a.cow_id = b.id").
  430. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  431. Where("a.status = ?", pasturePb.IsShow_No).
  432. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  433. if req.EndDay != "" {
  434. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  435. pref.Where("a.plan_day <= ?", dateTime)
  436. }
  437. if err = pref.Order("a.plan_day DESC").Count(&count).
  438. Limit(int(pagination.PageSize)).
  439. Offset(int(pagination.PageOffset)).
  440. Find(&weaningItems).Error; err != nil {
  441. return nil, xerr.WithStack(err)
  442. }
  443. return &pasturePb.WeaningItemsResponse{
  444. Code: http.StatusOK,
  445. Msg: "ok",
  446. Data: &pasturePb.WeaningItemsData{
  447. Total: int32(count),
  448. Page: pagination.Page,
  449. PageSize: pagination.PageSize,
  450. HeaderSort: []string{"id", "cowId", "earNumber", "penName", "dayAge", "planDayFormat", "birthAtFormat", "currentWeight"},
  451. Header: map[string]string{
  452. "id": "编号",
  453. "cowId": "牛号",
  454. "earNumber": "耳标号",
  455. "penName": "栏舍",
  456. "dayAge": "日龄",
  457. "planDayFormat": "断奶日期",
  458. "birthAtFormat": "出生日期",
  459. "currentWeight": "体重",
  460. },
  461. List: weaningItems,
  462. },
  463. }, nil
  464. }
  465. func (s *StoreEntry) MatingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingItemsResponse, error) {
  466. userModel, err := s.GetUserModel(ctx)
  467. if err != nil {
  468. return nil, xerr.WithStack(err)
  469. }
  470. matingItems := make([]*pasturePb.MatingItems, 0)
  471. count := int64(0)
  472. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventMating).TableName())).
  473. Select(`a.id,a.cow_id,a.status,a.ear_number,DATE_FORMAT(FROM_UNIXTIME(plan_day), '%Y-%m-%d') as plan_day,
  474. CASE a.expose_estrus_type
  475. WHEN 1 THEN '脖环揭发'
  476. WHEN 2 THEN '脚环/计步器'
  477. WHEN 3 THEN '自然发情'
  478. WHEN 4 THEN '同期'
  479. ELSE '其他'
  480. END AS expose_estrus_type_name,
  481. CASE
  482. WHEN last_calving_at = 0 THEN ""
  483. ELSE DATE_FORMAT(FROM_UNIXTIME(last_calving_at), '%Y-%m-%d %H:%i:%s')
  484. END AS last_calving_at_format,
  485. b.breed_status,b.cow_type,b.pen_id,b.day_age,b.calving_age,b.abortion_age,b.pen_name`).
  486. Joins("left join cow as b on a.cow_id = b.id").
  487. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  488. Where("a.status = ?", pasturePb.IsShow_No)
  489. if req.EndDay != "" {
  490. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  491. pref.Where("a.plan_day <= ?", dateTime)
  492. }
  493. if req.PenId > 0 {
  494. pref.Where("b.pen_id = ?", req.PenId)
  495. }
  496. if req.EarNumber != "" {
  497. pref.Where("a.ear_number = ?", req.EarNumber)
  498. }
  499. if err = pref.Order("a.plan_day DESC").
  500. Count(&count).
  501. Limit(int(pagination.PageSize)).
  502. Offset(int(pagination.PageOffset)).
  503. Find(&matingItems).Error; err != nil {
  504. return nil, xerr.WithStack(err)
  505. }
  506. return &pasturePb.MatingItemsResponse{
  507. Code: http.StatusOK,
  508. Msg: "ok",
  509. Data: &pasturePb.MatingItemsData{
  510. Total: int32(count),
  511. Page: pagination.Page,
  512. PageSize: pagination.PageSize,
  513. HeaderSort: []string{"id", "cowId", "earNumber", "dayAge", "lact", "penName", "planDay", "breedStatusName",
  514. "cowTypeName", "calvingAge", "abortionAge", "exposeEstrusTypeName", "lastCalvingAtFormat"},
  515. Header: map[string]string{
  516. "id": "编号",
  517. "cowId": "牛号",
  518. "earNumber": "耳标号",
  519. "breedStatusName": "繁殖状态",
  520. "cowTypeName": "牛只类型",
  521. "penName": "栏舍",
  522. "lact": "胎次",
  523. "calvingAge": "产后天数",
  524. "abortionAge": "流产天数",
  525. "dayAge": "日龄",
  526. "status": "状态",
  527. "exposeEstrusTypeName": "发情揭发方式",
  528. "lastCalvingAtFormat": "产犊日期",
  529. "planDay": "计划配种时间",
  530. },
  531. List: matingItems,
  532. },
  533. }, nil
  534. }