package backend import ( "context" "fmt" "kpt-pasture/model" "kpt-pasture/util" "net/http" "time" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/xerr" ) // CalendarToDoList 获取日历待办列表 func (s *StoreEntry) CalendarToDoList(ctx context.Context, req *pasturePb.CalendarToDoRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CalendarToDoResponse, error) { currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil || currentUser.Id <= 0 { return nil, xerr.WithStack(err) } var count int64 = 0 eventItem := make([]*model.EventItem, 0) pref := s.DB.Model(&model.EventItem{}) if req.StartAt > 0 && req.EndAt > 0 && req.StartAt <= req.EndAt { pref = pref.Where("plan_day >= ?", req.StartAt). Where("plan_day <= ?", req.EndAt) } if len(req.CowIds) > 0 { cowList, _ := s.ParseCowIds(ctx, req.CowIds) cowIds := make([]int64, 0) for _, cow := range cowList { cowIds = append(cowIds, cow.Id) } if len(cowIds) > 0 { pref.Where("cow_id IN (?)", cowIds) } } if req.CalendarType > 0 { pref.Where("calendar_type = ?", req.CalendarType) } if req.IsFinish > 0 { pref.Where("is_finish = ?", req.IsFinish) } if req.IsExpire > 0 { pref.Where("is_expire = ?", req.IsExpire) } if err = pref.Order("id desc"). Count(&count). Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&eventItem).Error; err != nil { return nil, xerr.WithStack(err) } penMap := s.PenMap(ctx) calendarMap := CalendarTypeMap() return &pasturePb.CalendarToDoResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.CalendarToDoData{ List: model.EventItemSlice(eventItem).ToPB(penMap, calendarMap), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) CalendarList(ctx context.Context, req *pasturePb.CalendarRequest) (*pasturePb.CalendarResponse, error) { calendarList := make([]*model.Calendar, 0) if err := s.DB.Model(&model.Calendar{}). Where("show_day >= ?", req.ShowStartDay). Where("show_day <= ?", req.ShowEndDay). Where("is_show = ?", pasturePb.IsShow_Ok). Find(&calendarList).Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.CalendarResponse{ Code: http.StatusOK, Message: "ok", Data: model.CalendarSlice(calendarList).ToPB(), }, nil } func (s *StoreEntry) CalendarTableDetail( ctx context.Context, req *pasturePb.CalendarTableRequest, pagination *pasturePb.PaginationModel, ) (interface{}, error) { if req.Start != time.Now().Format(model.LayoutDate2) { return nil, xerr.New("参数错误") } newCalendar := &model.Calendar{} if err := s.DB.Model(&model.Calendar{}). Where("calendar_type = ?", req.CalendarType). Where("show_day = ?", req.Start). Where("is_show = ?", pasturePb.IsShow_Ok). First(newCalendar).Error; err != nil { return nil, xerr.WithStack(err) } if newCalendar.Id <= 0 { return nil, xerr.New("不存在该日历数据") } return s.getCalendarCowList(ctx, req.CalendarType, req.Start, pagination) } func (s *StoreEntry) getCalendarCowList( ctx context.Context, calendarType pasturePb.CalendarType_Kind, showDay string, pagination *pasturePb.PaginationModel) (interface{}, error) { req := &pasturePb.ItemsRequest{EndDay: showDay, CalendarType: calendarType, Status: pasturePb.IsShow_No} switch calendarType { case pasturePb.CalendarType_Immunisation: // 免疫 return s.ImmunisationCowList(ctx, req, pagination) case pasturePb.CalendarType_PG, pasturePb.CalendarType_RnGH: // 同期 return s.SameTimeCowList(ctx, req, pagination) case pasturePb.CalendarType_Pregnancy_Check: // 孕检 return s.PregnancyCheckCowList(ctx, req, pagination) case pasturePb.CalendarType_WorkOrder: // 工作单 return s.WorkOrderCowList(ctx, req, pagination) case pasturePb.CalendarType_Weaning: // 断奶 return s.WeaningCowList(ctx, req, pagination) case pasturePb.CalendarType_Treatment: // 治疗 return s.TreatmentCowList(ctx, req, pagination) case pasturePb.CalendarType_Mating: // 配种 return s.MatingCowList(ctx, req, pagination) case pasturePb.CalendarType_Calving: // 产犊 return s.CalvingCowList(ctx, req, pagination) default: return nil, xerr.New("不支持的日历类型") } } func (s *StoreEntry) ImmunisationCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.ImmunizationItemsResponse, error) { eventImmunizationPlanList := make([]*model.EventImmunizationPlan, 0) count := int64(0) pref := s.DB.Model(&model.EventImmunizationPlan{}). Where("status = ?", pasturePb.IsShow_No) if req.StartDay != "" { dateTime := util.TimeParseLocalUnix(req.StartDay) pref.Where("plan_day >= ?", dateTime) } if req.EndDay != "" { dateTime := util.TimeParseLocalEndUnix(req.EndDay) pref.Where("plan_day <= ?", dateTime) } if req.CowId > 0 { pref.Where("cow_id = ?", req.CowId) } if req.ImmunizationId > 0 { pref.Where("plan_id = ?", req.ImmunizationId) } if req.Lact >= 0 { pref.Where("lact = ?", req.Lact) } if req.PenId > 0 { pref.Where("pen_id = ?", req.PenId) } if req.Status > 0 { pref.Where("status = ?", req.Status) } if err := pref.Order("id desc"). Count(&count). Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Order("id desc"). Find(&eventImmunizationPlanList).Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.ImmunizationItemsResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.ImmunizationItemsData{ Total: int32(count), Page: pagination.Page, PageSize: pagination.PageSize, Header: map[string]string{ "id": "编号", "cowId": "牛号", "penName": "栏舍", "dayAge": "日龄", "planStartTime": "免疫开始时间", "immunizationPlanName": "免疫名称", "status": "状态", }, List: model.EventImmunizationPlanSlice(eventImmunizationPlanList).ToPB(), }, }, nil } func (s *StoreEntry) SameTimeCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SameTimeItemResponse, error) { sameTimeBodyList := make([]*model.SameTimeItemBody, 0) count := int64(0) pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowSameTime).TableName())). 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"). Joins("left join cow as b on a.cow_id = b.id"). Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission). Where("a.status = ?", pasturePb.IsShow_No) if req.EndDay != "" { dateTime := util.TimeParseLocalEndUnix(req.EndDay) pref.Where("a.plan_day <= ?", dateTime) } if req.CalendarType > 0 { pref.Where("a.same_time_type = ?", req.CalendarType) } if req.CowType > 0 { pref.Where("b.cow_type = ?", req.CowType) } if req.Status > 0 { pref.Where("a.status = ?", pasturePb.IsShow_No) } if err := pref.Order("a.id desc").Count(&count). Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Order("id desc"). Find(&sameTimeBodyList).Error; err != nil { return nil, xerr.WithStack(err) } breedStatusMap := s.CowBreedStatusMap() penMap := s.PenMap(ctx) sameTimeTypeMap := s.SameTimeTypeMap() return &pasturePb.SameTimeItemResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.SameTimeItemsData{ Total: int32(count), Page: pagination.Page, PageSize: pagination.PageSize, Header: map[string]string{ "id": "编号", "cowId": "牛号", "breedStatusName": "繁殖状态", "cowTypeName": "牛只类型", "penName": "栏舍", "lact": "胎次", "calvingAge": "产后天数", "abortionAge": "流产天数", "dayAge": "日龄", "status": "状态", "sameTimeTypeName": "处理方式", "matingTimes": "本胎次配次", "calvingAtFormat": "产犊日期", "abortionAtFormat": "流产日期", "sameTimeName": "同情名称", }, List: model.SameTimeBodySlice(sameTimeBodyList).ToPB(breedStatusMap, penMap, sameTimeTypeMap), }, }, nil } func (s *StoreEntry) PregnancyCheckCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnancyCheckItemsResponse, error) { newPregnancyCheckItems := make([]*pasturePb.PregnancyCheckItems, 0) var count int64 pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventPregnantCheck).TableName())). 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"). Joins("left join cow as b on a.cow_id = b.id"). Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission). Where("a.status = ?", pasturePb.IsShow_No) if req.EndDay != "" { dateTime := util.TimeParseLocalEndUnix(req.EndDay) pref.Where("plan_day <= ?", dateTime) } if req.CowType > 0 { pref.Where("cow_type = ?", req.CowType) } if req.Status > 0 { pref.Where("a.status = ?", req.Status) } if req.PregnantCheckType > 0 { pref.Where("pregnant_check_name = ?", model.PregnantCheckNameValueMap[req.PregnantCheckType]) } if err := pref.Order("id desc"). Count(&count). Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Order("id desc"). Find(&newPregnancyCheckItems).Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.PregnancyCheckItemsResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.PregnancyCheckItemsData{ Total: int32(count), Page: pagination.Page, PageSize: pagination.PageSize, Header: map[string]string{ "id": "编号", "cowId": "牛号", "cowTypeName": "牛只类型", "penName": "栏舍", "lact": "胎次", "dayAge": "日龄", "planDay": "孕检日期", "checkTypeName": "孕检名称", "status": "状态", "matingTimes": "配次", "calvingAtFormat": "产检日期", "matingAtFormat": "配种日期", "matingAge": "配后天数", "bullId": "配种公牛", }, List: newPregnancyCheckItems, }, }, nil } func (s *StoreEntry) WeaningCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.WeaningItemsResponse, error) { weaningItems := make([]*pasturePb.WeaningItems, 0) count := int64(0) pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventWeaning).TableName())). Select("a.*,b.day_age,c.name as pen_name"). Joins("left join cow as b on a.cow_id = b.id"). Joins("left join pen as c on a.before_pen_id = c.id"). Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission). Where("a.status = ?", pasturePb.IsShow_No) if req.EndDay != "" { dateTime := util.TimeParseLocalEndUnix(req.EndDay) pref.Where("a.plan_day <= ?", dateTime) } if req.Status > 0 { pref.Where("a.status = ?", req.Status) } if err := pref.Order("a.id desc").Count(&count). Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&weaningItems).Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.WeaningItemsResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.WeaningItemsData{ Total: int32(count), Page: pagination.Page, PageSize: pagination.PageSize, Header: map[string]string{ "id": "编号", "cowId": "牛号", "penName": "栏舍", "planDay": "断奶日期", "dayAge": "日龄", "status": "状态", }, List: weaningItems, }, }, nil } func (s *StoreEntry) MatingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.MatingItemsResponse, error) { matingItems := make([]*pasturePb.MatingItems, 0) count := int64(0) pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventMating).TableName())). 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"). Joins("left join cow as b on a.cow_id = b.id"). Joins("left join pen as c on a.pen_id = c.id"). Where("a.status = ?", pasturePb.IsShow_No) if req.EndDay != "" { dateTime := util.TimeParseLocalEndUnix(req.EndDay) pref.Where("a.plan_day <= ?", dateTime) } if req.PenId > 0 { pref.Where("a.pen_id = ?", req.PenId) } if req.Status > 0 { pref.Where("a.status = ?", req.Status) } if err := pref.Order("a.id desc"). Count(&count). Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&matingItems).Error; err != nil { return nil, xerr.WithStack(err) } return &pasturePb.MatingItemsResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.MatingItemsData{ Total: int32(count), Page: pagination.Page, PageSize: pagination.PageSize, Header: map[string]string{ "id": "编号", "cowId": "牛号", "breedStatusName": "繁殖状态", "cowTypeName": "牛只类型", "penName": "栏舍", "lact": "胎次", "calvingAge": "产后天数", "abortionAge": "流产天数", "dayAge": "日龄", "status": "状态", }, List: matingItems, }, }, nil } func (s *StoreEntry) CalvingCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.CalvingItemsResponse, error) { calvingItems := make([]*pasturePb.CalvingItems, 0) count := int64(0) pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCalving).TableName())). 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, 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`). Joins("left join cow as b on a.cow_id = b.id"). Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission) if req.EndDay != "" { dateTime := util.TimeParseLocalEndUnix(req.EndDay) pref.Where("a.plan_day <= ?", dateTime) } if req.Status > 0 { pref.Where("a.status = ?", req.Status) } if err := pref.Order("a.id desc"). Count(&count). Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&calvingItems).Error; err != nil { return nil, xerr.WithStack(err) } breedStatusMap := s.CowBreedStatusMap() for _, v := range calvingItems { breedStatusName := "" if breedStatus, ok := breedStatusMap[v.BreedStatus]; ok { breedStatusName = breedStatus } v.BreedStatusName = breedStatusName } return &pasturePb.CalvingItemsResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.CalvingItemsData{ Total: int32(count), Page: pagination.Page, PageSize: pagination.PageSize, Header: map[string]string{ "id": "编号", "cowId": "牛号", "breedStatusName": "繁殖状态", "penName": "栏舍", "lact": "胎次", "matingAge": "配后天数", "dayAge": "日龄", "status": "是否完成", "bullId": "配种公牛号", "planDay": "预产时间", "matingAtFormat": "配种时间", }, List: calvingItems, }, }, nil } // WorkOrderCowList 暂时不处理工单业务 func (s *StoreEntry) WorkOrderCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) { return nil, nil } // TreatmentCowList 治疗清单 func (s *StoreEntry) TreatmentCowList(ctx context.Context, req *pasturePb.ItemsRequest, pagination *pasturePb.PaginationModel) (interface{}, error) { return nil, nil }