calendar.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "net/http"
  7. "time"
  8. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. )
  11. // CalendarList
  12. // /*func (s StoreEntry) CalendarToDoList(ctx context.Context) (*pasturePb.CalendarToDoListResponse, error) {
  13. func (s *StoreEntry) CalendarList(ctx context.Context, req *pasturePb.CalendarRequest) (*pasturePb.CalendarResponse, error) {
  14. calendarList := make([]*model.Calendar, 0)
  15. if err := s.DB.Model(&model.Calendar{}).
  16. Where("show_day >= ?", req.ShowStartDay).
  17. Where("show_day <= ?", req.ShowEndDay).
  18. Where("is_show = ?", pasturePb.IsShow_Ok).
  19. Find(&calendarList).Error; err != nil {
  20. return nil, xerr.WithStack(err)
  21. }
  22. return &pasturePb.CalendarResponse{
  23. Code: http.StatusOK,
  24. Message: "ok",
  25. Data: model.CalendarSlice(calendarList).ToPB(),
  26. }, nil
  27. }
  28. func (s *StoreEntry) CalendarTableDetail(
  29. ctx context.Context,
  30. req *pasturePb.CalendarTableRequest,
  31. pagination *pasturePb.PaginationModel,
  32. ) (interface{}, error) {
  33. if req.Start != time.Now().Format(model.LayoutDate2) {
  34. return nil, xerr.New("参数错误")
  35. }
  36. newCalendar := &model.Calendar{}
  37. if err := s.DB.Model(&model.Calendar{}).
  38. Where("calendar_type = ?", req.CalendarType).
  39. Where("show_day = ?", req.Start).
  40. Where("is_show = ?", pasturePb.IsShow_Ok).
  41. First(newCalendar).Error; err != nil {
  42. return nil, xerr.WithStack(err)
  43. }
  44. if newCalendar.Id == 0 {
  45. return nil, xerr.New("不存在该日历数据")
  46. }
  47. return s.getCalendarCowList(ctx, req.CalendarType, req.Start, pagination)
  48. }
  49. func (s *StoreEntry) getCalendarCowList(
  50. ctx context.Context,
  51. calendarType pasturePb.CalendarType_Kind,
  52. startDate string,
  53. pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
  54. switch calendarType {
  55. case pasturePb.CalendarType_Immunisation: // 免疫
  56. return s.ImmunisationCowList(ctx, startDate, pagination)
  57. case pasturePb.CalendarType_PG, pasturePb.CalendarType_RnGH: // 同期
  58. return s.SameTimeCowList(ctx, startDate, pagination, calendarType)
  59. case pasturePb.CalendarType_Pregnancy_Check: // 孕检
  60. return s.PregnancyCheckCowList(ctx, startDate, pagination)
  61. case pasturePb.CalendarType_WorkOrder: // 工作单
  62. return s.WorkOrderCowList(ctx, startDate, pagination)
  63. case pasturePb.CalendarType_Weaning: // 断奶
  64. return s.WeaningCowList(ctx, startDate, pagination)
  65. case pasturePb.CalendarType_Treatment: // 治疗
  66. return s.TreatmentCowList(ctx, startDate, pagination)
  67. case pasturePb.CalendarType_Mating: // 配种
  68. return s.MatingCowList(ctx, startDate, pagination)
  69. default:
  70. return nil, xerr.New("不支持的日历类型")
  71. }
  72. }
  73. func (s *StoreEntry) ImmunisationCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
  74. immunizationPlanCowList := make([]*model.ImmunizationPlanCow, 0)
  75. count := int64(0)
  76. if err := s.DB.Model(&model.ImmunizationPlanCow{}).
  77. Where("plan_start_time <= ?", dateTime).
  78. Where("status = ?", pasturePb.IsShow_No).
  79. Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)).
  80. Find(&immunizationPlanCowList).Error; err != nil {
  81. return nil, xerr.WithStack(err)
  82. }
  83. return &model.CalendarResponse{
  84. Code: http.StatusOK,
  85. Message: "ok",
  86. Data: &model.CalendarData{
  87. Total: int32(count),
  88. Page: pagination.Page,
  89. PageSize: pagination.PageSize,
  90. Header: model.ImmunizationCalendarHeader{
  91. Id: "编号",
  92. CowId: "牛号",
  93. PlanStartTime: "免疫开始时间",
  94. ImmunizationPlanName: "免疫名称",
  95. Status: "状态",
  96. },
  97. List: model.ImmunizationPlanCowSlice(immunizationPlanCowList).ToPB(),
  98. },
  99. }, nil
  100. }
  101. func (s *StoreEntry) SameTimeCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel, calendarType pasturePb.CalendarType_Kind) (*model.CalendarResponse, error) {
  102. sameTimeBodyList := make([]*model.SameTimeBody, 0)
  103. count := int64(0)
  104. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.SameTimeCowDetail).TableName())).
  105. Select("a.id,a.cow_id,a.status,b.breed_status,b.cow_type,b.pen_id,b.day_age,b.calving_age,b.abortion_age").
  106. Joins("left join cow as b on a.cow_id = b.id").
  107. Where("b.is_remove = ?", pasturePb.IsShow_No).
  108. Where("a.plan_day <= ?", dateTime).
  109. Where("a.status = ?", pasturePb.IsShow_No)
  110. if calendarType == pasturePb.CalendarType_PG {
  111. pref.Where("a.same_time_type = ?", calendarType)
  112. }
  113. if calendarType == pasturePb.CalendarType_RnGH {
  114. pref.Where("a.same_time_type = ?", calendarType)
  115. }
  116. if err := pref.Order("a.id desc").Count(&count).
  117. Limit(int(pagination.PageSize)).
  118. Offset(int(pagination.PageOffset)).
  119. Find(&sameTimeBodyList).Error; err != nil {
  120. return nil, xerr.WithStack(err)
  121. }
  122. cowTypeMap := s.CowTypeMap()
  123. breedStatusMap := s.CowBreedStatusMap()
  124. penMap := s.PenMap(ctx)
  125. return &model.CalendarResponse{
  126. Code: http.StatusOK,
  127. Message: "ok",
  128. Data: &model.CalendarData{
  129. Total: int32(count),
  130. Page: pagination.Page,
  131. PageSize: pagination.PageSize,
  132. Header: model.SameTimeHeader{
  133. Id: "编号",
  134. CowId: "牛号",
  135. BreedStatusName: "繁殖状态",
  136. CowTypeName: "牛只类型",
  137. PenName: "栏舍",
  138. Lact: "胎次",
  139. CalvingAge: "产后天数",
  140. AbortionAge: "流产天数",
  141. DayAge: "日龄",
  142. Status: "状态",
  143. },
  144. List: model.SameTimeBodySlice(sameTimeBodyList).ToPB(cowTypeMap, breedStatusMap, penMap),
  145. },
  146. }, nil
  147. }
  148. func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
  149. return nil, nil
  150. }
  151. func (s *StoreEntry) WorkOrderCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
  152. return nil, nil
  153. }
  154. func (s *StoreEntry) WeaningCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
  155. weaningBodyList := make([]*model.WeaningBody, 0)
  156. count := int64(0)
  157. dateTime = dateTime + " 23:59:59" // todo 转换成时间戳
  158. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.SameTimeCowDetail).TableName())).
  159. Select("a.*,b.day_age").
  160. Joins("left join cow as b on a.cow_id = b.id").
  161. Where("b.is_remove = ?", pasturePb.IsShow_No).
  162. Where("a.plan_day <= ?", dateTime).
  163. Where("a.status = ?", pasturePb.IsShow_No)
  164. if err := pref.Order("a.id desc").Count(&count).
  165. Limit(int(pagination.PageSize)).
  166. Offset(int(pagination.PageOffset)).
  167. Find(&weaningBodyList).Error; err != nil {
  168. return nil, xerr.WithStack(err)
  169. }
  170. return &model.CalendarResponse{
  171. Code: http.StatusOK,
  172. Message: "ok",
  173. Data: &model.CalendarData{
  174. Total: int32(count),
  175. Page: pagination.Page,
  176. PageSize: pagination.PageSize,
  177. Header: model.WeaningHeader{
  178. Id: "编号",
  179. CowId: "牛号",
  180. PenName: "栏舍",
  181. PlanDay: "断奶日期",
  182. DayAge: "日龄",
  183. Status: "状态",
  184. },
  185. List: weaningBodyList,
  186. },
  187. }, nil
  188. }
  189. func (s *StoreEntry) TreatmentCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
  190. return nil, nil
  191. }
  192. func (s *StoreEntry) MatingCowList(ctx context.Context, dateTime string, pagination *pasturePb.PaginationModel) (*model.CalendarResponse, error) {
  193. return nil, nil
  194. }