calendar.go 24 KB

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