calendar.go 15 KB

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