calendar.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 WHERE b.admission_status = %d ORDER BY a.plan_day DESC", sql, pasturePb.AdmissionStatus_Admission)
  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. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  243. if req.StartDay != "" && req.EndDay != "" {
  244. startTime := util.TimeParseLocalUnix(req.StartDay)
  245. endTime := util.TimeParseLocalEndUnix(req.EndDay)
  246. pref.Where("a.plan_day between ? and ?", startTime, endTime)
  247. }
  248. if req.CowId > 0 {
  249. pref.Where("a.cow_id = ?", req.CowId)
  250. }
  251. if req.PlanId > 0 {
  252. pref.Where("a.plan_id = ?", req.PlanId)
  253. }
  254. if req.PenId > 0 {
  255. pref.Where("b.pen_id = ?", req.PenId)
  256. }
  257. if err = pref.Count(&count).
  258. Limit(int(pagination.PageSize)).
  259. Offset(int(pagination.PageOffset)).
  260. Order("a.plan_day DESC").
  261. Find(&eventImmunizationPlanList).Error; err != nil {
  262. return nil, xerr.WithStack(err)
  263. }
  264. return &pasturePb.ImmunizationItemsResponse{
  265. Code: http.StatusOK,
  266. Msg: "ok",
  267. Data: &pasturePb.ImmunizationItemsData{
  268. Total: int32(count),
  269. Page: pagination.Page,
  270. PageSize: pagination.PageSize,
  271. HeaderSort: []string{"planDay", "planName", "penName", "dayAge", "earNumber", "planId"},
  272. Header: map[string]string{
  273. "earNumber": "耳标号",
  274. "penName": "栏舍",
  275. "dayAge": "日龄",
  276. "planDay": "免疫时间",
  277. "planName": "免疫名称",
  278. "planId": "免疫Id",
  279. },
  280. List: model.EventImmunizationPlanSlice(eventImmunizationPlanList).ToPB(),
  281. },
  282. }, nil
  283. }
  284. func (s *StoreEntry) SameTimeCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SameTimeItemResponse, error) {
  285. userModel, err := s.GetUserModel(ctx)
  286. if err != nil {
  287. return nil, xerr.WithStack(err)
  288. }
  289. sameTimeBodyList := make([]*model.SameTimeItemBody, 0)
  290. count := int64(0)
  291. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowSameTime).TableName())).
  292. 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,
  293. 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`).
  294. Joins("left join cow as b on a.cow_id = b.id").
  295. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  296. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  297. Where("a.status = ?", pasturePb.IsShow_No).
  298. Where("a.plan_day <= ?", time.Now().Local().Unix())
  299. if req.EndDay != "" {
  300. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  301. pref.Where("a.plan_day <= ?", dateTime)
  302. }
  303. if req.CowType > 0 {
  304. pref.Where("b.cow_type = ?", req.CowType)
  305. }
  306. if req.SameTimeId > 0 {
  307. pref.Where("a.same_time_id = ?", req.SameTimeId)
  308. }
  309. if req.SameTimeType > 0 {
  310. pref.Where("a.same_time_type = ?", req.SameTimeType)
  311. }
  312. if err = pref.Order("a.plan_day DESC").Count(&count).
  313. Limit(int(pagination.PageSize)).
  314. Offset(int(pagination.PageOffset)).
  315. Find(&sameTimeBodyList).Error; err != nil {
  316. return nil, xerr.WithStack(err)
  317. }
  318. breedStatusMap := s.CowBreedStatusMap()
  319. sameTimeTypeMap := s.SameTimeTypeMap()
  320. return &pasturePb.SameTimeItemResponse{
  321. Code: http.StatusOK,
  322. Msg: "ok",
  323. Data: &pasturePb.SameTimeItemsData{
  324. Total: int32(count),
  325. Page: pagination.Page,
  326. PageSize: pagination.PageSize,
  327. HeaderSort: []string{"earNumber", "breedStatusName", "cowTypeName", "planDayAtFormat", "penName",
  328. "lact", "calvingAge", "abortionAge", "dayAge", "status", "sameTimeTypeName", "matingTimes", "calvingAtFormat",
  329. "abortionAtFormat", "sameTimeName"},
  330. Header: map[string]string{
  331. "earNumber": "耳标号",
  332. "breedStatusName": "繁殖状态",
  333. "cowTypeName": "牛只类型",
  334. "planDayAtFormat": "执行日期",
  335. "penName": "栏舍",
  336. "lact": "胎次",
  337. "calvingAge": "产后天数",
  338. "abortionAge": "流产天数",
  339. "dayAge": "日龄",
  340. "status": "状态",
  341. "sameTimeTypeName": "处理方式",
  342. "matingTimes": "本胎次配次",
  343. "calvingAtFormat": "产犊日期",
  344. "abortionAtFormat": "流产日期",
  345. "sameTimeName": "同期名称",
  346. },
  347. List: model.SameTimeBodySlice(sameTimeBodyList).ToPB(breedStatusMap, sameTimeTypeMap),
  348. },
  349. }, nil
  350. }
  351. func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnancyCheckItemsResponse, error) {
  352. userModel, err := s.GetUserModel(ctx)
  353. if err != nil {
  354. return nil, xerr.WithStack(err)
  355. }
  356. newPregnancyCheckItems := make([]*pasturePb.PregnancyCheckItems, 0)
  357. var count int64
  358. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventPregnantCheck).TableName())).
  359. Select(`a.id,a.cow_id,a.ear_number,a.pen_id,a.status,b.pen_name,b.cow_type,
  360. DATE_FORMAT(FROM_UNIXTIME(b.last_mating_at),'%Y-%m-%d')as mating_at_format,pregnancy_age,DATEDIFF(CURDATE(),
  361. FROM_UNIXTIME(last_mating_at)) AS mating_age,
  362. b.breed_status,
  363. CASE b.breed_status
  364. WHEN 1 THEN '未配'
  365. WHEN 2 THEN '已配'
  366. WHEN 3 THEN '已孕'
  367. WHEN 4 THEN '空怀'
  368. WHEN 5 THEN '流产'
  369. WHEN 6 THEN '产犊'
  370. WHEN 7 THEN '禁配'
  371. ELSE '未知'
  372. END AS breed_status_name,
  373. CASE a.pregnant_check_name
  374. WHEN 'pregnant_check_for_first' THEN '初检'
  375. WHEN 'pregnant_check_for_second' THEN '复检'
  376. ELSE '其他'
  377. END AS check_type_name,b.day_age,b.calving_age,b.abortion_age,a.bull_id`).
  378. Joins("left join cow as b on a.cow_id = b.id").
  379. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  380. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  381. Where("a.status = ?", pasturePb.IsShow_No)
  382. if req.EarNumber != "" {
  383. pref.Where("a.ear_number = ?", req.EarNumber)
  384. }
  385. if req.EndDay != "" {
  386. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  387. pref.Where("a.plan_day <= ?", dateTime)
  388. }
  389. if req.PenId > 0 {
  390. pref.Where("b.pen_id = ?", req.PenId)
  391. }
  392. if req.CowType > 0 {
  393. pref.Where("a.cow_type = ?", req.CowType)
  394. }
  395. if req.PregnantCheckType > 0 {
  396. pref.Where("a.pregnant_check_name = ?", model.PregnantCheckNameValueMap[req.PregnantCheckType])
  397. }
  398. if err = pref.Order("a.plan_day DESC").
  399. Count(&count).
  400. Limit(int(pagination.PageSize)).
  401. Offset(int(pagination.PageOffset)).
  402. Find(&newPregnancyCheckItems).Error; err != nil {
  403. return nil, xerr.WithStack(err)
  404. }
  405. return &pasturePb.PregnancyCheckItemsResponse{
  406. Code: http.StatusOK,
  407. Msg: "ok",
  408. Data: &pasturePb.PregnancyCheckItemsData{
  409. Total: int32(count),
  410. Page: pagination.Page,
  411. PageSize: pagination.PageSize,
  412. HeaderSort: []string{"earNumber", "cowTypeName", "penName", "lact", "dayAge", "breedStatus", "planDay",
  413. "checkTypeName", "status", "matingTimes", "calvingAtFormat", "matingAtFormat", "matingAge", "bullId", "pregnancyAge"},
  414. Header: map[string]string{
  415. "earNumber": "耳标号",
  416. "cowTypeName": "牛只类型",
  417. "penName": "栏舍",
  418. "lact": "胎次",
  419. "dayAge": "日龄",
  420. "planDay": "孕检日期",
  421. "checkTypeName": "孕检名称",
  422. "status": "状态",
  423. "matingTimes": "配次",
  424. "calvingAtFormat": "产检日期",
  425. "matingAtFormat": "配种日期",
  426. "matingAge": "配后天数",
  427. "bullId": "配种公牛",
  428. "pregnancyAge": "怀孕天数",
  429. "breedStatus": "繁殖状态",
  430. },
  431. List: newPregnancyCheckItems,
  432. },
  433. }, nil
  434. }
  435. func (s *StoreEntry) WeaningCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.WeaningItemsResponse, error) {
  436. userModel, err := s.GetUserModel(ctx)
  437. if err != nil {
  438. return nil, xerr.WithStack(err)
  439. }
  440. weaningItems := make([]*pasturePb.WeaningItems, 0)
  441. count := int64(0)
  442. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeaning).TableName())).
  443. Select(`a.id,a.cow_id,ROUND(b.current_weight / 1000,2) as current_weight,
  444. DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day_format,b.day_age,b.pen_name,
  445. b.ear_number,DATE_FORMAT(FROM_UNIXTIME(b.birth_at), '%Y-%m-%d') AS birth_at_format`).
  446. Joins("left join cow as b on a.cow_id = b.id").
  447. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  448. Where("a.status = ?", pasturePb.IsShow_No).
  449. Where("a.pasture_id = ?", userModel.AppPasture.Id)
  450. if req.EndDay != "" {
  451. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  452. pref.Where("a.plan_day <= ?", dateTime)
  453. }
  454. if err = pref.Order("a.plan_day DESC").Count(&count).
  455. Limit(int(pagination.PageSize)).
  456. Offset(int(pagination.PageOffset)).
  457. Find(&weaningItems).Error; err != nil {
  458. return nil, xerr.WithStack(err)
  459. }
  460. return &pasturePb.WeaningItemsResponse{
  461. Code: http.StatusOK,
  462. Msg: "ok",
  463. Data: &pasturePb.WeaningItemsData{
  464. Total: int32(count),
  465. Page: pagination.Page,
  466. PageSize: pagination.PageSize,
  467. HeaderSort: []string{"earNumber", "penName", "dayAge", "planDayFormat", "birthAtFormat", "currentWeight"},
  468. Header: map[string]string{
  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. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  505. if req.EndDay != "" {
  506. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  507. pref.Where("a.plan_day <= ?", dateTime)
  508. }
  509. if req.PenId > 0 {
  510. pref.Where("b.pen_id = ?", req.PenId)
  511. }
  512. if req.EarNumber != "" {
  513. pref.Where("a.ear_number = ?", req.EarNumber)
  514. }
  515. if err = pref.Order("a.plan_day DESC").
  516. Count(&count).
  517. Limit(int(pagination.PageSize)).
  518. Offset(int(pagination.PageOffset)).
  519. Find(&matingItems).Error; err != nil {
  520. return nil, xerr.WithStack(err)
  521. }
  522. return &pasturePb.MatingItemsResponse{
  523. Code: http.StatusOK,
  524. Msg: "ok",
  525. Data: &pasturePb.MatingItemsData{
  526. Total: int32(count),
  527. Page: pagination.Page,
  528. PageSize: pagination.PageSize,
  529. HeaderSort: []string{"earNumber", "dayAge", "lact", "penName", "planDay", "breedStatusName",
  530. "cowTypeName", "calvingAge", "abortionAge", "exposeEstrusTypeName", "lastCalvingAtFormat"},
  531. Header: map[string]string{
  532. "earNumber": "耳标号",
  533. "breedStatusName": "繁殖状态",
  534. "cowTypeName": "牛只类型",
  535. "penName": "栏舍",
  536. "lact": "胎次",
  537. "calvingAge": "产后天数",
  538. "abortionAge": "流产天数",
  539. "dayAge": "日龄",
  540. "status": "状态",
  541. "exposeEstrusTypeName": "发情揭发方式",
  542. "lastCalvingAtFormat": "产犊日期",
  543. "planDay": "计划配种时间",
  544. },
  545. List: matingItems,
  546. },
  547. }, nil
  548. }