calendar.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. case pasturePb.CalendarType_Calving: // 产犊
  122. return s.CalvingCowList(ctx, req, pagination)
  123. default:
  124. return nil, xerr.New("不支持的日历类型")
  125. }
  126. }
  127. func (s *StoreEntry) ImmunisationCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.ImmunizationItemsResponse, error) {
  128. eventImmunizationPlanList := make([]*model.EventImmunizationPlan, 0)
  129. count := int64(0)
  130. pref := s.DB.Model(&model.EventImmunizationPlan{})
  131. if req.StartDay != "" {
  132. dateTime := util.TimeParseLocalUnix(req.StartDay)
  133. pref.Where("plan_day >= ?", dateTime)
  134. }
  135. if req.EndDay != "" {
  136. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  137. pref.Where("plan_day <= ?", dateTime)
  138. }
  139. if req.CowId > 0 {
  140. pref.Where("cow_id = ?", req.CowId)
  141. }
  142. if req.ImmunizationId > 0 {
  143. pref.Where("plan_id = ?", req.ImmunizationId)
  144. }
  145. if req.Lact >= 0 {
  146. pref.Where("lact = ?", req.Lact)
  147. }
  148. if req.PenId > 0 {
  149. pref.Where("pen_id = ?", req.PenId)
  150. }
  151. if req.Status > 0 {
  152. pref.Where("status = ?", req.Status)
  153. }
  154. if err := pref.Order("id desc").
  155. Count(&count).
  156. Limit(int(pagination.PageSize)).
  157. Offset(int(pagination.PageOffset)).
  158. Order("id desc").
  159. Find(&eventImmunizationPlanList).Error; err != nil {
  160. return nil, xerr.WithStack(err)
  161. }
  162. return &pasturePb.ImmunizationItemsResponse{
  163. Code: http.StatusOK,
  164. Message: "ok",
  165. Data: &pasturePb.ImmunizationItemsData{
  166. Total: int32(count),
  167. Page: pagination.Page,
  168. PageSize: pagination.PageSize,
  169. Header: map[string]string{
  170. "id": "编号",
  171. "cowId": "牛号",
  172. "penName": "栏舍",
  173. "dayAge": "日龄",
  174. "planStartTime": "免疫开始时间",
  175. "immunizationPlanName": "免疫名称",
  176. "status": "状态",
  177. },
  178. List: model.EventImmunizationPlanSlice(eventImmunizationPlanList).ToPB(),
  179. },
  180. }, nil
  181. }
  182. func (s *StoreEntry) SameTimeCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SameTimeItemResponse, error) {
  183. sameTimeBodyList := make([]*model.SameTimeBody, 0)
  184. count := int64(0)
  185. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowSameTime).TableName())).
  186. 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").
  187. Joins("left join cow as b on a.cow_id = b.id").
  188. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  189. if req.EndDay != "" {
  190. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  191. pref.Where("a.plan_day <= ?", dateTime)
  192. }
  193. if req.CalendarType > 0 {
  194. pref.Where("a.same_time_type = ?", req.CalendarType)
  195. }
  196. if req.CowType >= 0 {
  197. pref.Where("b.cow_type = ?", req.CowType)
  198. }
  199. if req.Status > 0 {
  200. pref.Where("a.status = ?", pasturePb.IsShow_No)
  201. }
  202. if err := pref.Order("a.id desc").Count(&count).
  203. Limit(int(pagination.PageSize)).
  204. Offset(int(pagination.PageOffset)).
  205. Order("id desc").
  206. Find(&sameTimeBodyList).Error; err != nil {
  207. return nil, xerr.WithStack(err)
  208. }
  209. breedStatusMap := s.CowBreedStatusMap()
  210. penMap := s.PenMap(ctx)
  211. return &pasturePb.SameTimeItemResponse{
  212. Code: http.StatusOK,
  213. Message: "ok",
  214. Data: &pasturePb.SameTimeItemsData{
  215. Total: int32(count),
  216. Page: pagination.Page,
  217. PageSize: pagination.PageSize,
  218. Header: map[string]string{
  219. "id": "编号",
  220. "cowId": "牛号",
  221. "breedStatusName": "繁殖状态",
  222. "cowTypeName": "牛只类型",
  223. "penName": "栏舍",
  224. "lact": "胎次",
  225. "calvingAge": "产后天数",
  226. "abortionAge": "流产天数",
  227. "dayAge": "日龄",
  228. "status": "状态",
  229. },
  230. List: model.SameTimeBodySlice(sameTimeBodyList).ToPB(breedStatusMap, penMap),
  231. },
  232. }, nil
  233. }
  234. func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnancyCheckItemsResponse, error) {
  235. newPregnancyCheckItems := make([]*pasturePb.PregnancyCheckItems, 0)
  236. var count int64
  237. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventPregnantCheck).TableName())).
  238. 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").
  239. Joins("left join cow as b on a.cow_id = b.id").
  240. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  241. if req.EndDay != "" {
  242. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  243. pref.Where("plan_day <= ?", dateTime)
  244. }
  245. if req.CowType > 0 {
  246. pref.Where("cow_type = ?", req.CowType)
  247. }
  248. if req.Status > 0 {
  249. pref.Where("a.status = ?", req.Status)
  250. }
  251. if err := pref.Order("id desc").
  252. Count(&count).
  253. Limit(int(pagination.PageSize)).
  254. Offset(int(pagination.PageOffset)).
  255. Order("id desc").
  256. Find(&newPregnancyCheckItems).Error; err != nil {
  257. return nil, xerr.WithStack(err)
  258. }
  259. return &pasturePb.PregnancyCheckItemsResponse{
  260. Code: http.StatusOK,
  261. Message: "ok",
  262. Data: &pasturePb.PregnancyCheckItemsData{
  263. Total: int32(count),
  264. Page: pagination.Page,
  265. PageSize: pagination.PageSize,
  266. Header: map[string]string{
  267. "id": "编号",
  268. "cowId": "牛号",
  269. "cowTypeName": "牛只类型",
  270. "penName": "栏舍",
  271. "lact": "胎次",
  272. "dayAge": "日龄",
  273. "planDay": "孕检日期",
  274. "checkTypeName": "孕检名称",
  275. "status": "状态",
  276. "matingTimes": "配次",
  277. "calvingAtFormat": "产检日期",
  278. "matingAtFormat": "配种日期",
  279. "matingAge": "配后天数",
  280. "bullId": "配种公牛",
  281. },
  282. List: newPregnancyCheckItems,
  283. },
  284. }, nil
  285. }
  286. func (s *StoreEntry) WeaningCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.WeaningItemsResponse, error) {
  287. weaningItems := make([]*pasturePb.WeaningItems, 0)
  288. count := int64(0)
  289. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeaning).TableName())).
  290. Select("a.*,b.day_age").
  291. Joins("left join cow as b on a.cow_id = b.id").
  292. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  293. if req.EndDay != "" {
  294. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  295. pref.Where("a.plan_day <= ?", dateTime)
  296. }
  297. if req.Status > 0 {
  298. pref.Where("a.status = ?", req.Status)
  299. }
  300. if err := pref.Order("a.id desc").Count(&count).
  301. Limit(int(pagination.PageSize)).
  302. Offset(int(pagination.PageOffset)).
  303. Find(&weaningItems).Error; err != nil {
  304. return nil, xerr.WithStack(err)
  305. }
  306. return &pasturePb.WeaningItemsResponse{
  307. Code: http.StatusOK,
  308. Message: "ok",
  309. Data: &pasturePb.WeaningItemsData{
  310. Total: int32(count),
  311. Page: pagination.Page,
  312. PageSize: pagination.PageSize,
  313. Header: map[string]string{
  314. "id": "编号",
  315. "cowId": "牛号",
  316. "penName": "栏舍",
  317. "planDay": "断奶日期",
  318. "dayAge": "日龄",
  319. "status": "状态",
  320. },
  321. List: weaningItems,
  322. },
  323. }, nil
  324. }
  325. func (s *StoreEntry) MatingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingItemsResponse, error) {
  326. matingItems := make([]*pasturePb.MatingItems, 0)
  327. count := int64(0)
  328. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventMating).TableName())).
  329. 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").
  330. Joins("left join cow as b on a.cow_id = b.id").
  331. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  332. if req.EndDay != "" {
  333. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  334. pref.Where("a.plan_day <= ?", dateTime)
  335. }
  336. if req.Status > 0 {
  337. pref.Where("a.status = ?", req.Status)
  338. }
  339. if err := pref.Order("a.id desc").
  340. Count(&count).
  341. Limit(int(pagination.PageSize)).
  342. Offset(int(pagination.PageOffset)).
  343. Find(&matingItems).Error; err != nil {
  344. return nil, xerr.WithStack(err)
  345. }
  346. return &pasturePb.MatingItemsResponse{
  347. Code: http.StatusOK,
  348. Message: "ok",
  349. Data: &pasturePb.MatingItemsData{
  350. Total: int32(count),
  351. Page: pagination.Page,
  352. PageSize: pagination.PageSize,
  353. Header: map[string]string{
  354. "id": "编号",
  355. "cowId": "牛号",
  356. "breedStatusName": "繁殖状态",
  357. "cowTypeName": "牛只类型",
  358. "penName": "栏舍",
  359. "lact": "胎次",
  360. "calvingAge": "产后天数",
  361. "abortionAge": "流产天数",
  362. "dayAge": "日龄",
  363. "status": "状态",
  364. },
  365. List: matingItems,
  366. },
  367. }, nil
  368. }
  369. func (s *StoreEntry) CalvingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CalvingItemsResponse, error) {
  370. calvingItems := make([]*pasturePb.CalvingItems, 0)
  371. count := int64(0)
  372. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCalving).TableName())).
  373. Select(`a.id,a.cow_id,a.status,b.breed_status,b.pen_id,DATE_FORMAT(FROM_UNIXTIME(last_mating_at), '%Y-%m-%d') AS mating_at_format,
  374. b.day_age,b.last_bull_number as bull_id,DATEDIFF(NOW(),FROM_UNIXTIME(last_mating_at)) AS mating_age,DATE_FORMAT(FROM_UNIXTIME(a.plan_day), '%Y-%m-%d') AS plan_day`).
  375. Joins("left join cow as b on a.cow_id = b.id").
  376. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  377. if req.EndDay != "" {
  378. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  379. pref.Where("a.plan_day <= ?", dateTime)
  380. }
  381. if req.Status > 0 {
  382. pref.Where("a.status = ?", req.Status)
  383. }
  384. if err := pref.Order("a.id desc").
  385. Count(&count).
  386. Limit(int(pagination.PageSize)).
  387. Offset(int(pagination.PageOffset)).
  388. Find(&calvingItems).Error; err != nil {
  389. return nil, xerr.WithStack(err)
  390. }
  391. breedStatusMap := s.CowBreedStatusMap()
  392. for _, v := range calvingItems {
  393. breedStatusName := ""
  394. if breedStatus, ok := breedStatusMap[v.BreedStatus]; ok {
  395. breedStatusName = breedStatus
  396. }
  397. v.BreedStatusName = breedStatusName
  398. }
  399. return &pasturePb.CalvingItemsResponse{
  400. Code: http.StatusOK,
  401. Message: "ok",
  402. Data: &pasturePb.CalvingItemsData{
  403. Total: int32(count),
  404. Page: pagination.Page,
  405. PageSize: pagination.PageSize,
  406. Header: map[string]string{
  407. "id": "编号",
  408. "cowId": "牛号",
  409. "breedStatusName": "繁殖状态",
  410. "penName": "栏舍",
  411. "lact": "胎次",
  412. "matingAge": "配后天数",
  413. "dayAge": "日龄",
  414. "status": "是否完成",
  415. "bullId": "配种公牛号",
  416. "planDay": "预产时间",
  417. },
  418. List: calvingItems,
  419. },
  420. }, nil
  421. }
  422. // WorkOrderCowList 暂时不处理工单业务
  423. func (s *StoreEntry) WorkOrderCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  424. return nil, nil
  425. }
  426. // TreatmentCowList 治疗清单
  427. func (s *StoreEntry) TreatmentCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  428. return nil, nil
  429. }