calendar.go 8.8 KB

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