calendar.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. HeaderSort: []string{"id", "cowId", "planDay", "planName", "penName", "dayAge", "earNumber", "planId"},
  232. Header: map[string]string{
  233. "id": "编号",
  234. "cowId": "牛号",
  235. "earNumber": "耳标号",
  236. "penName": "栏舍",
  237. "dayAge": "日龄",
  238. "planDay": "免疫时间",
  239. "planName": "免疫名称",
  240. "planId": "免疫Id",
  241. },
  242. List: model.EventImmunizationPlanSlice(eventImmunizationPlanList).ToPB(),
  243. },
  244. }, nil
  245. }
  246. func (s *StoreEntry) SameTimeCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SameTimeItemResponse, error) {
  247. userModel, err := s.GetUserModel(ctx)
  248. if err != nil {
  249. return nil, xerr.WithStack(err)
  250. }
  251. sameTimeBodyList := make([]*model.SameTimeItemBody, 0)
  252. count := int64(0)
  253. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowSameTime).TableName())).
  254. 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,
  255. 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`).
  256. Joins("left join cow as b on a.cow_id = b.id").
  257. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  258. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  259. Where("a.status = ?", pasturePb.IsShow_No)
  260. if req.EndDay != "" {
  261. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  262. pref.Where("a.plan_day <= ?", dateTime)
  263. }
  264. if req.CowType > 0 {
  265. pref.Where("b.cow_type = ?", req.CowType)
  266. }
  267. if req.SameTimeId > 0 {
  268. pref.Where("a.same_time_id = ?", req.SameTimeId)
  269. }
  270. if req.SameTimeType > 0 {
  271. pref.Where("a.same_time_type = ?", req.SameTimeType)
  272. }
  273. if err = pref.Order("a.plan_day DESC").Count(&count).
  274. Limit(int(pagination.PageSize)).
  275. Offset(int(pagination.PageOffset)).
  276. Find(&sameTimeBodyList).Error; err != nil {
  277. return nil, xerr.WithStack(err)
  278. }
  279. breedStatusMap := s.CowBreedStatusMap()
  280. penMap := s.PenMap(ctx, int64(req.PastureId))
  281. sameTimeTypeMap := s.SameTimeTypeMap()
  282. return &pasturePb.SameTimeItemResponse{
  283. Code: http.StatusOK,
  284. Msg: "ok",
  285. Data: &pasturePb.SameTimeItemsData{
  286. Total: int32(count),
  287. Page: pagination.Page,
  288. PageSize: pagination.PageSize,
  289. HeaderSort: []string{"id", "cowId", "earNumber", "breedStatusName", "cowTypeName", "planDayAtFormat", "penName",
  290. "lact", "calvingAge", "abortionAge", "dayAge", "status", "sameTimeTypeName", "matingTimes", "calvingAtFormat",
  291. "abortionAtFormat", "sameTimeName"},
  292. Header: map[string]string{
  293. "id": "编号",
  294. "cowId": "牛号",
  295. "earNumber": "耳标号",
  296. "breedStatusName": "繁殖状态",
  297. "cowTypeName": "牛只类型",
  298. "planDayAtFormat": "执行日期",
  299. "penName": "栏舍",
  300. "lact": "胎次",
  301. "calvingAge": "产后天数",
  302. "abortionAge": "流产天数",
  303. "dayAge": "日龄",
  304. "status": "状态",
  305. "sameTimeTypeName": "处理方式",
  306. "matingTimes": "本胎次配次",
  307. "calvingAtFormat": "产犊日期",
  308. "abortionAtFormat": "流产日期",
  309. "sameTimeName": "同期名称",
  310. },
  311. List: model.SameTimeBodySlice(sameTimeBodyList).ToPB(breedStatusMap, penMap, sameTimeTypeMap),
  312. },
  313. }, nil
  314. }
  315. func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnancyCheckItemsResponse, error) {
  316. userModel, err := s.GetUserModel(ctx)
  317. if err != nil {
  318. return nil, xerr.WithStack(err)
  319. }
  320. newPregnancyCheckItems := make([]*pasturePb.PregnancyCheckItems, 0)
  321. var count int64
  322. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventPregnantCheck).TableName())).
  323. Select(`a.id,a.cow_id,a.ear_number,a.pen_id,a.status,b.breed_status,b.pen_name,b.cow_type,
  324. 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,
  325. CASE a.pregnant_check_name
  326. WHEN 'pregnant_check_for_first' THEN '初检'
  327. WHEN 'pregnant_check_for_second' THEN '复检'
  328. ELSE '其他'
  329. END AS check_type_name,b.day_age,b.calving_age,b.abortion_age,a.bull_id`).
  330. Joins("left join cow as b on a.cow_id = b.id").
  331. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  332. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  333. Where("a.status = ?", pasturePb.IsShow_No)
  334. if req.EndDay != "" {
  335. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  336. pref.Where("a.plan_day <= ?", dateTime)
  337. }
  338. if req.PenId > 0 {
  339. pref.Where("b.pen_id = ?", req.PenId)
  340. }
  341. if req.CowType > 0 {
  342. pref.Where("a.cow_type = ?", req.CowType)
  343. }
  344. if req.PregnantCheckType > 0 {
  345. pref.Where("a.pregnant_check_name = ?", model.PregnantCheckNameValueMap[req.PregnantCheckType])
  346. }
  347. if err = pref.Order("a.plan_day DESC").
  348. Count(&count).
  349. Limit(int(pagination.PageSize)).
  350. Offset(int(pagination.PageOffset)).
  351. Find(&newPregnancyCheckItems).Error; err != nil {
  352. return nil, xerr.WithStack(err)
  353. }
  354. return &pasturePb.PregnancyCheckItemsResponse{
  355. Code: http.StatusOK,
  356. Msg: "ok",
  357. Data: &pasturePb.PregnancyCheckItemsData{
  358. Total: int32(count),
  359. Page: pagination.Page,
  360. PageSize: pagination.PageSize,
  361. HeaderSort: []string{"id", "cowId", "earNumber", "cowTypeName", "penName", "lact", "dayAge", "planDay",
  362. "checkTypeName", "status", "matingTimes", "calvingAtFormat", "matingAtFormat", "matingAge", "bullId", "pregnancyAge"},
  363. Header: map[string]string{
  364. "id": "编号",
  365. "cowId": "牛号",
  366. "earNumber": "耳标号",
  367. "cowTypeName": "牛只类型",
  368. "penName": "栏舍",
  369. "lact": "胎次",
  370. "dayAge": "日龄",
  371. "planDay": "孕检日期",
  372. "checkTypeName": "孕检名称",
  373. "status": "状态",
  374. "matingTimes": "配次",
  375. "calvingAtFormat": "产检日期",
  376. "matingAtFormat": "配种日期",
  377. "matingAge": "配后天数",
  378. "bullId": "配种公牛",
  379. "pregnancyAge": "怀孕天数",
  380. },
  381. List: newPregnancyCheckItems,
  382. },
  383. }, nil
  384. }
  385. func (s *StoreEntry) WeaningCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.WeaningItemsResponse, error) {
  386. userModel, err := s.GetUserModel(ctx)
  387. if err != nil {
  388. return nil, xerr.WithStack(err)
  389. }
  390. weaningItems := make([]*pasturePb.WeaningItems, 0)
  391. count := int64(0)
  392. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeaning).TableName())).
  393. Select(`a.id,a.cow_id,ROUND(b.current_weight / 1000,2) as current_weight,
  394. DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day_format,b.day_age,b.pen_name,
  395. b.ear_number,DATE_FORMAT(FROM_UNIXTIME(b.birth_at), '%Y-%m-%d') AS birth_at_format`).
  396. Joins("left join cow as b on a.cow_id = b.id").
  397. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  398. Where("a.status = ?", pasturePb.IsShow_No).
  399. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  400. if req.EndDay != "" {
  401. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  402. pref.Where("a.plan_day <= ?", dateTime)
  403. }
  404. if err = pref.Order("a.plan_day DESC").Count(&count).
  405. Limit(int(pagination.PageSize)).
  406. Offset(int(pagination.PageOffset)).
  407. Find(&weaningItems).Error; err != nil {
  408. return nil, xerr.WithStack(err)
  409. }
  410. return &pasturePb.WeaningItemsResponse{
  411. Code: http.StatusOK,
  412. Msg: "ok",
  413. Data: &pasturePb.WeaningItemsData{
  414. Total: int32(count),
  415. Page: pagination.Page,
  416. PageSize: pagination.PageSize,
  417. HeaderSort: []string{"id", "cowId", "earNumber", "penName", "dayAge", "planDayFormat", "birthAtFormat", "currentWeight"},
  418. Header: map[string]string{
  419. "id": "编号",
  420. "cowId": "牛号",
  421. "earNumber": "耳标号",
  422. "penName": "栏舍",
  423. "dayAge": "日龄",
  424. "planDayFormat": "断奶日期",
  425. "birthAtFormat": "出生日期",
  426. "currentWeight": "体重",
  427. },
  428. List: weaningItems,
  429. },
  430. }, nil
  431. }
  432. func (s *StoreEntry) MatingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingItemsResponse, error) {
  433. userModel, err := s.GetUserModel(ctx)
  434. if err != nil {
  435. return nil, xerr.WithStack(err)
  436. }
  437. matingItems := make([]*pasturePb.MatingItems, 0)
  438. count := int64(0)
  439. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventMating).TableName())).
  440. Select(`a.id,a.cow_id,a.status,a.ear_number,
  441. CASE a.expose_estrus_type
  442. WHEN 1 THEN '脖环揭发'
  443. WHEN 2 THEN '脚环/计步器'
  444. WHEN 3 THEN '自然发情'
  445. WHEN 4 THEN '同期'
  446. ELSE '其他'
  447. END AS expose_estrus_type_name,
  448. CASE
  449. WHEN last_calving_at = 0 THEN ""
  450. ELSE DATE_FORMAT(FROM_UNIXTIME(last_calving_at), '%Y-%m-%d %H:%i:%s')
  451. END AS last_calving_at_format,
  452. b.breed_status,b.cow_type,b.pen_id,b.day_age,b.calving_age,b.abortion_age,b.pen_name`).
  453. Joins("left join cow as b on a.cow_id = b.id").
  454. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  455. Where("a.status = ?", pasturePb.IsShow_No)
  456. if req.EndDay != "" {
  457. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  458. pref.Where("a.plan_day <= ?", dateTime)
  459. }
  460. if req.PenId > 0 {
  461. pref.Where("b.pen_id = ?", req.PenId)
  462. }
  463. if req.EarNumber != "" {
  464. pref.Where("a.ear_number = ?", req.EarNumber)
  465. }
  466. if err = pref.Order("a.plan_day DESC").
  467. Count(&count).
  468. Limit(int(pagination.PageSize)).
  469. Offset(int(pagination.PageOffset)).
  470. Find(&matingItems).Error; err != nil {
  471. return nil, xerr.WithStack(err)
  472. }
  473. return &pasturePb.MatingItemsResponse{
  474. Code: http.StatusOK,
  475. Msg: "ok",
  476. Data: &pasturePb.MatingItemsData{
  477. Total: int32(count),
  478. Page: pagination.Page,
  479. PageSize: pagination.PageSize,
  480. HeaderSort: []string{"id", "cowId", "earNumber", "dayAge", "lact", "penName", "breedStatusName", "cowTypeName", "calvingAge", "abortionAge", "exposeEstrusTypeName", "lastCalvingAtFormat"},
  481. Header: map[string]string{
  482. "id": "编号",
  483. "cowId": "牛号",
  484. "earNumber": "耳标号",
  485. "breedStatusName": "繁殖状态",
  486. "cowTypeName": "牛只类型",
  487. "penName": "栏舍",
  488. "lact": "胎次",
  489. "calvingAge": "产后天数",
  490. "abortionAge": "流产天数",
  491. "dayAge": "日龄",
  492. "status": "状态",
  493. "exposeEstrusTypeName": "发情揭发方式",
  494. "lastCalvingAtFormat": "产犊日期",
  495. },
  496. List: matingItems,
  497. },
  498. }, nil
  499. }