calendar.go 23 KB

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