calendar.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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.SameTimeItemBody, 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,a.same_time_type,b.breed_status,a.same_time_name,b.cow_type,b.day_age,b.calving_age,b.abortion_age,b.last_calving_at,b.last_abortion_at").
  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. sameTimeTypeMap := s.SameTimeTypeMap()
  214. return &pasturePb.SameTimeItemResponse{
  215. Code: http.StatusOK,
  216. Message: "ok",
  217. Data: &pasturePb.SameTimeItemsData{
  218. Total: int32(count),
  219. Page: pagination.Page,
  220. PageSize: pagination.PageSize,
  221. Header: map[string]string{
  222. "id": "编号",
  223. "cowId": "牛号",
  224. "breedStatusName": "繁殖状态",
  225. "cowTypeName": "牛只类型",
  226. "penName": "栏舍",
  227. "lact": "胎次",
  228. "calvingAge": "产后天数",
  229. "abortionAge": "流产天数",
  230. "dayAge": "日龄",
  231. "status": "状态",
  232. "sameTimeTypeName": "处理方式",
  233. "matingTimes": "本胎次配次",
  234. "calvingAtFormat": "产犊日期",
  235. "abortionAtFormat": "流产日期",
  236. "sameTimeName": "同情名称",
  237. },
  238. List: model.SameTimeBodySlice(sameTimeBodyList).ToPB(breedStatusMap, penMap, sameTimeTypeMap),
  239. },
  240. }, nil
  241. }
  242. func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnancyCheckItemsResponse, error) {
  243. newPregnancyCheckItems := make([]*pasturePb.PregnancyCheckItems, 0)
  244. var count int64
  245. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventPregnantCheck).TableName())).
  246. 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").
  247. Joins("left join cow as b on a.cow_id = b.id").
  248. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  249. Where("a.status = ?", pasturePb.IsShow_No)
  250. if req.EndDay != "" {
  251. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  252. pref.Where("plan_day <= ?", dateTime)
  253. }
  254. if req.CowType > 0 {
  255. pref.Where("cow_type = ?", req.CowType)
  256. }
  257. if req.Status > 0 {
  258. pref.Where("a.status = ?", req.Status)
  259. }
  260. if req.PregnantCheckType > 0 {
  261. pref.Where("pregnant_check_name = ?", model.PregnantCheckNameValueMap[req.PregnantCheckType])
  262. }
  263. if err := pref.Order("id desc").
  264. Count(&count).
  265. Limit(int(pagination.PageSize)).
  266. Offset(int(pagination.PageOffset)).
  267. Order("id desc").
  268. Find(&newPregnancyCheckItems).Error; err != nil {
  269. return nil, xerr.WithStack(err)
  270. }
  271. return &pasturePb.PregnancyCheckItemsResponse{
  272. Code: http.StatusOK,
  273. Message: "ok",
  274. Data: &pasturePb.PregnancyCheckItemsData{
  275. Total: int32(count),
  276. Page: pagination.Page,
  277. PageSize: pagination.PageSize,
  278. Header: map[string]string{
  279. "id": "编号",
  280. "cowId": "牛号",
  281. "cowTypeName": "牛只类型",
  282. "penName": "栏舍",
  283. "lact": "胎次",
  284. "dayAge": "日龄",
  285. "planDay": "孕检日期",
  286. "checkTypeName": "孕检名称",
  287. "status": "状态",
  288. "matingTimes": "配次",
  289. "calvingAtFormat": "产检日期",
  290. "matingAtFormat": "配种日期",
  291. "matingAge": "配后天数",
  292. "bullId": "配种公牛",
  293. },
  294. List: newPregnancyCheckItems,
  295. },
  296. }, nil
  297. }
  298. func (s *StoreEntry) WeaningCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.WeaningItemsResponse, error) {
  299. weaningItems := make([]*pasturePb.WeaningItems, 0)
  300. count := int64(0)
  301. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeaning).TableName())).
  302. Select("a.*,b.day_age,c.name as pen_name").
  303. Joins("left join cow as b on a.cow_id = b.id").
  304. Joins("left join pen as c on a.before_pen_id = c.id").
  305. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
  306. Where("a.status = ?", pasturePb.IsShow_No)
  307. if req.EndDay != "" {
  308. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  309. pref.Where("a.plan_day <= ?", dateTime)
  310. }
  311. if req.Status > 0 {
  312. pref.Where("a.status = ?", req.Status)
  313. }
  314. if err := pref.Order("a.id desc").Count(&count).
  315. Limit(int(pagination.PageSize)).
  316. Offset(int(pagination.PageOffset)).
  317. Find(&weaningItems).Error; err != nil {
  318. return nil, xerr.WithStack(err)
  319. }
  320. return &pasturePb.WeaningItemsResponse{
  321. Code: http.StatusOK,
  322. Message: "ok",
  323. Data: &pasturePb.WeaningItemsData{
  324. Total: int32(count),
  325. Page: pagination.Page,
  326. PageSize: pagination.PageSize,
  327. Header: map[string]string{
  328. "id": "编号",
  329. "cowId": "牛号",
  330. "penName": "栏舍",
  331. "planDay": "断奶日期",
  332. "dayAge": "日龄",
  333. "status": "状态",
  334. },
  335. List: weaningItems,
  336. },
  337. }, nil
  338. }
  339. func (s *StoreEntry) MatingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingItemsResponse, error) {
  340. matingItems := make([]*pasturePb.MatingItems, 0)
  341. count := int64(0)
  342. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventMating).TableName())).
  343. 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").
  344. Joins("left join cow as b on a.cow_id = b.id").
  345. Joins("left join pen as c on a.pen_id = c.id").
  346. Where("a.status = ?", pasturePb.IsShow_No)
  347. if req.EndDay != "" {
  348. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  349. pref.Where("a.plan_day <= ?", dateTime)
  350. }
  351. if req.PenId > 0 {
  352. pref.Where("a.pen_id = ?", req.PenId)
  353. }
  354. if req.Status > 0 {
  355. pref.Where("a.status = ?", req.Status)
  356. }
  357. if err := pref.Order("a.id desc").
  358. Count(&count).
  359. Limit(int(pagination.PageSize)).
  360. Offset(int(pagination.PageOffset)).
  361. Find(&matingItems).Error; err != nil {
  362. return nil, xerr.WithStack(err)
  363. }
  364. return &pasturePb.MatingItemsResponse{
  365. Code: http.StatusOK,
  366. Message: "ok",
  367. Data: &pasturePb.MatingItemsData{
  368. Total: int32(count),
  369. Page: pagination.Page,
  370. PageSize: pagination.PageSize,
  371. Header: map[string]string{
  372. "id": "编号",
  373. "cowId": "牛号",
  374. "breedStatusName": "繁殖状态",
  375. "cowTypeName": "牛只类型",
  376. "penName": "栏舍",
  377. "lact": "胎次",
  378. "calvingAge": "产后天数",
  379. "abortionAge": "流产天数",
  380. "dayAge": "日龄",
  381. "status": "状态",
  382. },
  383. List: matingItems,
  384. },
  385. }, nil
  386. }
  387. func (s *StoreEntry) CalvingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CalvingItemsResponse, error) {
  388. calvingItems := make([]*pasturePb.CalvingItems, 0)
  389. count := int64(0)
  390. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCalving).TableName())).
  391. 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,
  392. 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`).
  393. Joins("left join cow as b on a.cow_id = b.id").
  394. Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission)
  395. if req.EndDay != "" {
  396. dateTime := util.TimeParseLocalEndUnix(req.EndDay)
  397. pref.Where("a.plan_day <= ?", dateTime)
  398. }
  399. if req.Status > 0 {
  400. pref.Where("a.status = ?", req.Status)
  401. }
  402. if err := pref.Order("a.id desc").
  403. Count(&count).
  404. Limit(int(pagination.PageSize)).
  405. Offset(int(pagination.PageOffset)).
  406. Find(&calvingItems).Error; err != nil {
  407. return nil, xerr.WithStack(err)
  408. }
  409. breedStatusMap := s.CowBreedStatusMap()
  410. for _, v := range calvingItems {
  411. breedStatusName := ""
  412. if breedStatus, ok := breedStatusMap[v.BreedStatus]; ok {
  413. breedStatusName = breedStatus
  414. }
  415. v.BreedStatusName = breedStatusName
  416. }
  417. return &pasturePb.CalvingItemsResponse{
  418. Code: http.StatusOK,
  419. Message: "ok",
  420. Data: &pasturePb.CalvingItemsData{
  421. Total: int32(count),
  422. Page: pagination.Page,
  423. PageSize: pagination.PageSize,
  424. Header: map[string]string{
  425. "id": "编号",
  426. "cowId": "牛号",
  427. "breedStatusName": "繁殖状态",
  428. "penName": "栏舍",
  429. "lact": "胎次",
  430. "matingAge": "配后天数",
  431. "dayAge": "日龄",
  432. "status": "是否完成",
  433. "bullId": "配种公牛号",
  434. "planDay": "预产时间",
  435. "matingAtFormat": "配种时间",
  436. },
  437. List: calvingItems,
  438. },
  439. }, nil
  440. }
  441. // WorkOrderCowList 暂时不处理工单业务
  442. func (s *StoreEntry) WorkOrderCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  443. return nil, nil
  444. }
  445. // TreatmentCowList 治疗清单
  446. func (s *StoreEntry) TreatmentCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) {
  447. return nil, nil
  448. }