calendar.go 24 KB

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