calendar.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. // CalendarToDoList 获取日历待办列表
  13. func (s *StoreEntry) CalendarToDoList(ctx context.Context, req *pasturePb.CalendarToDoRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CalendarToDoResponse, error) {
  14. currentUser, err := s.GetCurrentSystemUser(ctx)
  15. if err != nil || currentUser.Id <= 0 {
  16. return nil, xerr.WithStack(err)
  17. }
  18. var count int64 = 0
  19. eventItem := make([]*model.EventItem, 0)
  20. pref := s.DB.Model(&model.EventItem{})
  21. if req.StartAt > 0 && req.EndAt > 0 && req.StartAt <= req.EndAt {
  22. pref = pref.Where("plan_day >= ?", req.StartAt).
  23. Where("plan_day <= ?", req.EndAt)
  24. }
  25. if len(req.CowIds) > 0 {
  26. cowList, _ := s.ParseCowIds(ctx, req.CowIds)
  27. cowIds := make([]int64, 0)
  28. for _, cow := range cowList {
  29. cowIds = append(cowIds, cow.Id)
  30. }
  31. if len(cowIds) > 0 {
  32. pref.Where("cow_id IN (?)", cowIds)
  33. }
  34. }
  35. if req.CalendarType > 0 {
  36. pref.Where("calendar_type = ?", req.CalendarType)
  37. }
  38. if req.IsFinish > 0 {
  39. pref.Where("is_finish = ?", req.IsFinish)
  40. }
  41. if req.IsExpire > 0 {
  42. pref.Where("is_expire = ?", req.IsExpire)
  43. }
  44. if err = pref.Order("id desc").
  45. Count(&count).
  46. Limit(int(pagination.PageSize)).
  47. Offset(int(pagination.PageOffset)).
  48. Find(&eventItem).Error; err != nil {
  49. return nil, xerr.WithStack(err)
  50. }
  51. penMap := s.PenMap(ctx)
  52. calendarMap := CalendarTypeMap()
  53. return &pasturePb.CalendarToDoResponse{
  54. Code: http.StatusOK,
  55. Message: "ok",
  56. Data: &pasturePb.CalendarToDoData{
  57. List: model.EventItemSlice(eventItem).ToPB(penMap, calendarMap),
  58. Total: int32(count),
  59. PageSize: pagination.PageSize,
  60. Page: pagination.Page,
  61. },
  62. }, nil
  63. }
  64. func (s *StoreEntry) CalendarList(ctx context.Context, req *pasturePb.CalendarRequest) (*pasturePb.CalendarResponse, error) {
  65. calendarList := make([]*model.Calendar, 0)
  66. if err := s.DB.Model(&model.Calendar{}).
  67. Where("show_day >= ?", req.ShowStartDay).
  68. Where("show_day <= ?", req.ShowEndDay).
  69. Where("is_show = ?", pasturePb.IsShow_Ok).
  70. Find(&calendarList).Error; err != nil {
  71. return nil, xerr.WithStack(err)
  72. }
  73. return &pasturePb.CalendarResponse{
  74. Code: http.StatusOK,
  75. Message: "ok",
  76. Data: model.CalendarSlice(calendarList).ToPB(),
  77. }, nil
  78. }
  79. func (s *StoreEntry) CalendarTableDetail(
  80. ctx context.Context,
  81. req *pasturePb.CalendarTableRequest,
  82. pagination *pasturePb.PaginationModel,
  83. ) (interface{}, error) {
  84. if req.Start != time.Now().Format(model.LayoutDate2) {
  85. return nil, xerr.New("参数错误")
  86. }
  87. newCalendar := &model.Calendar{}
  88. if err := s.DB.Model(&model.Calendar{}).
  89. Where("calendar_type = ?", req.CalendarType).
  90. Where("show_day = ?", req.Start).
  91. Where("is_show = ?", pasturePb.IsShow_Ok).
  92. First(newCalendar).Error; err != nil {
  93. return nil, xerr.WithStack(err)
  94. }
  95. if newCalendar.Id <= 0 {
  96. return nil, xerr.New("不存在该日历数据")
  97. }
  98. return s.getCalendarCowList(ctx, req.CalendarType, req.Start, pagination)
  99. }
  100. func (s *StoreEntry) getCalendarCowList(
  101. ctx context.Context,
  102. calendarType pasturePb.CalendarType_Kind,
  103. showDay string,
  104. pagination *pasturePb.PaginationModel) (interface{}, error) {
  105. req := &pasturePb.ItemsRequest{EndDay: showDay, CalendarType: calendarType, Status: pasturePb.IsShow_No}
  106. switch calendarType {
  107. case pasturePb.CalendarType_Immunisation: // 免疫
  108. return s.ImmunisationCowList(ctx, req, pagination)
  109. case pasturePb.CalendarType_PG, pasturePb.CalendarType_RnGH: // 同期
  110. return s.SameTimeCowList(ctx, req, pagination)
  111. case pasturePb.CalendarType_Pregnancy_Check: // 孕检
  112. return s.PregnancyCheckCowList(ctx, req, pagination)
  113. case pasturePb.CalendarType_WorkOrder: // 工作单
  114. return s.WorkOrderCowList(ctx, req, pagination)
  115. case pasturePb.CalendarType_Weaning: // 断奶
  116. return s.WeaningCowList(ctx, req, pagination)
  117. case pasturePb.CalendarType_Treatment: // 治疗
  118. return s.TreatmentCowList(ctx, req, pagination)
  119. case pasturePb.CalendarType_Mating: // 配种
  120. return s.MatingCowList(ctx, req, pagination)
  121. default:
  122. return nil, xerr.New("不支持的日历类型")
  123. }
  124. }
  125. func (s *StoreEntry) ImmunisationCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.ImmunizationItemsResponse, error) {
  126. eventImmunizationPlanList := make([]*model.EventImmunizationPlan, 0)
  127. count := int64(0)
  128. pref := s.DB.Model(&model.EventImmunizationPlan{})
  129. if req.StartDay != "" {
  130. dateTime := util.TimeParseLocalUnix(req.StartDay)
  131. pref.Where("plan_day >= ?", dateTime)
  132. }
  133. if req.EndDay != "" {
  134. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  135. pref.Where("plan_day <= ?", dateTime)
  136. }
  137. if req.CowId > 0 {
  138. pref.Where("cow_id = ?", req.CowId)
  139. }
  140. if req.ImmunizationId > 0 {
  141. pref.Where("plan_id = ?", req.ImmunizationId)
  142. }
  143. if req.Lact >= 0 {
  144. pref.Where("lact = ?", req.Lact)
  145. }
  146. if req.PenId > 0 {
  147. pref.Where("pen_id = ?", req.PenId)
  148. }
  149. if req.Status > 0 {
  150. pref.Where("status = ?", req.Status)
  151. }
  152. if err := pref.Order("id desc").
  153. Count(&count).
  154. Limit(int(pagination.PageSize)).
  155. Offset(int(pagination.PageOffset)).
  156. Order("id desc").
  157. Find(&eventImmunizationPlanList).Error; err != nil {
  158. return nil, xerr.WithStack(err)
  159. }
  160. return &pasturePb.ImmunizationItemsResponse{
  161. Code: http.StatusOK,
  162. Message: "ok",
  163. Data: &pasturePb.ImmunizationItemsData{
  164. Total: int32(count),
  165. Page: pagination.Page,
  166. PageSize: pagination.PageSize,
  167. Header: map[string]string{
  168. "id": "编号",
  169. "cowId": "牛号",
  170. "penName": "栏舍",
  171. "dayAge": "日龄",
  172. "planStartTime": "免疫开始时间",
  173. "immunizationPlanName": "免疫名称",
  174. "status": "状态",
  175. },
  176. List: model.EventImmunizationPlanSlice(eventImmunizationPlanList).ToPB(),
  177. },
  178. }, nil
  179. }
  180. func (s *StoreEntry) SameTimeCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SameTimeItemResponse, error) {
  181. sameTimeBodyList := make([]*model.SameTimeBody, 0)
  182. count := int64(0)
  183. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowSameTime).TableName())).
  184. Select("a.id,a.cow_id,a.pen_name,a.status,b.breed_status,b.cow_type,b.day_age,b.calving_age,b.abortion_age").
  185. Joins("left join cow as b on a.cow_id = b.id").
  186. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  187. if req.EndDay != "" {
  188. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  189. pref.Where("a.plan_day <= ?", dateTime)
  190. }
  191. if req.CalendarType > 0 {
  192. pref.Where("a.same_time_type = ?", req.CalendarType)
  193. }
  194. if req.CowType >= 0 {
  195. pref.Where("b.cow_type = ?", req.CowType)
  196. }
  197. if req.Status > 0 {
  198. pref.Where("a.status = ?", pasturePb.IsShow_No)
  199. }
  200. if err := pref.Order("a.id desc").Count(&count).
  201. Limit(int(pagination.PageSize)).
  202. Offset(int(pagination.PageOffset)).
  203. Order("id desc").
  204. Find(&sameTimeBodyList).Error; err != nil {
  205. return nil, xerr.WithStack(err)
  206. }
  207. breedStatusMap := s.CowBreedStatusMap()
  208. penMap := s.PenMap(ctx)
  209. return &pasturePb.SameTimeItemResponse{
  210. Code: http.StatusOK,
  211. Message: "ok",
  212. Data: &pasturePb.SameTimeItemsData{
  213. Total: int32(count),
  214. Page: pagination.Page,
  215. PageSize: pagination.PageSize,
  216. Header: map[string]string{
  217. "id": "编号",
  218. "cowId": "牛号",
  219. "breedStatusName": "繁殖状态",
  220. "cowTypeName": "牛只类型",
  221. "penName": "栏舍",
  222. "lact": "胎次",
  223. "calvingAge": "产后天数",
  224. "abortionAge": "流产天数",
  225. "dayAge": "日龄",
  226. "status": "状态",
  227. },
  228. List: model.SameTimeBodySlice(sameTimeBodyList).ToPB(breedStatusMap, penMap),
  229. },
  230. }, nil
  231. }
  232. func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnancyCheckItemsResponse, error) {
  233. newPregnancyCheckItems := make([]*pasturePb.PregnancyCheckItems, 0)
  234. var count int64
  235. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventPregnantCheck).TableName())).
  236. Select("a.id,a.cow_id,a.pen_id,a.status,b.breed_status,b.cow_type,b.day_age,b.calving_age,b.abortion_age,a.bull_id").
  237. Joins("left join cow as b on a.cow_id = b.id").
  238. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  239. if req.EndDay != "" {
  240. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  241. pref.Where("plan_day <= ?", dateTime)
  242. }
  243. if req.CowType > 0 {
  244. pref.Where("cow_type = ?", req.CowType)
  245. }
  246. if req.Status > 0 {
  247. pref.Where("a.status = ?", req.Status)
  248. }
  249. if err := pref.Order("id desc").
  250. Count(&count).
  251. Limit(int(pagination.PageSize)).
  252. Offset(int(pagination.PageOffset)).
  253. Order("id desc").
  254. Find(&newPregnancyCheckItems).Error; err != nil {
  255. return nil, xerr.WithStack(err)
  256. }
  257. return &pasturePb.PregnancyCheckItemsResponse{
  258. Code: http.StatusOK,
  259. Message: "ok",
  260. Data: &pasturePb.PregnancyCheckItemsData{
  261. Total: int32(count),
  262. Page: pagination.Page,
  263. PageSize: pagination.PageSize,
  264. Header: map[string]string{
  265. "id": "编号",
  266. "cowId": "牛号",
  267. "cowTypeName": "牛只类型",
  268. "penName": "栏舍",
  269. "lact": "胎次",
  270. "dayAge": "日龄",
  271. "planDay": "孕检日期",
  272. "checkTypeName": "孕检名称",
  273. "status": "状态",
  274. "matingTimes": "配次",
  275. "calvingAtFormat": "产检日期",
  276. "matingAtFormat": "配种日期",
  277. "matingAge": "配后天数",
  278. "bullId": "配种公牛",
  279. },
  280. List: newPregnancyCheckItems,
  281. },
  282. }, nil
  283. }
  284. func (s *StoreEntry) WeaningCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.WeaningItemsResponse, error) {
  285. weaningItems := make([]*pasturePb.WeaningItems, 0)
  286. count := int64(0)
  287. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeaning).TableName())).
  288. Select("a.*,b.day_age").
  289. Joins("left join cow as b on a.cow_id = b.id").
  290. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  291. if req.EndDay != "" {
  292. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  293. pref.Where("a.plan_day <= ?", dateTime)
  294. }
  295. if req.Status > 0 {
  296. pref.Where("a.status = ?", req.Status)
  297. }
  298. if err := pref.Order("a.id desc").Count(&count).
  299. Limit(int(pagination.PageSize)).
  300. Offset(int(pagination.PageOffset)).
  301. Find(&weaningItems).Error; err != nil {
  302. return nil, xerr.WithStack(err)
  303. }
  304. return &pasturePb.WeaningItemsResponse{
  305. Code: http.StatusOK,
  306. Message: "ok",
  307. Data: &pasturePb.WeaningItemsData{
  308. Total: int32(count),
  309. Page: pagination.Page,
  310. PageSize: pagination.PageSize,
  311. Header: map[string]string{
  312. "id": "编号",
  313. "cowId": "牛号",
  314. "penName": "栏舍",
  315. "planDay": "断奶日期",
  316. "dayAge": "日龄",
  317. "status": "状态",
  318. },
  319. List: weaningItems,
  320. },
  321. }, nil
  322. }
  323. func (s *StoreEntry) MatingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingItemsResponse, error) {
  324. matingItems := make([]*pasturePb.MatingItems, 0)
  325. count := int64(0)
  326. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventMating).TableName())).
  327. 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").
  328. Joins("left join cow as b on a.cow_id = b.id").
  329. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  330. if req.EndDay != "" {
  331. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  332. pref.Where("a.plan_day <= ?", dateTime)
  333. }
  334. if req.Status > 0 {
  335. pref.Where("a.status = ?", req.Status)
  336. }
  337. if err := pref.Order("a.id desc").Count(&count).
  338. Limit(int(pagination.PageSize)).
  339. Offset(int(pagination.PageOffset)).
  340. Find(&matingItems).Error; err != nil {
  341. return nil, xerr.WithStack(err)
  342. }
  343. return &pasturePb.MatingItemsResponse{
  344. Code: http.StatusOK,
  345. Message: "ok",
  346. Data: &pasturePb.MatingItemsData{
  347. Total: int32(count),
  348. Page: pagination.Page,
  349. PageSize: pagination.PageSize,
  350. Header: map[string]string{
  351. "id": "编号",
  352. "cowId": "牛号",
  353. "breedStatusName": "繁殖状态",
  354. "cowTypeName": "牛只类型",
  355. "penName": "栏舍",
  356. "lact": "胎次",
  357. "calvingAge": "产后天数",
  358. "abortionAge": "流产天数",
  359. "dayAge": "日龄",
  360. "status": "状态",
  361. },
  362. List: matingItems,
  363. },
  364. }, nil
  365. }
  366. func (s *StoreEntry) WorkOrderCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  367. return nil, nil
  368. }
  369. func (s *StoreEntry) TreatmentCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  370. return nil, nil
  371. }