package backend import ( "context" "fmt" "kpt-pasture/model" "net/http" "time" "gorm.io/gorm" "gitee.com/xuyiping_admin/pkg/xerr" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) func (s *StoreEntry) DrugsList(ctx context.Context, req *pasturePb.SearchDrugsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDrugsResponse, error) { drugsList := make([]*model.Drugs, 0) var count int64 = 0 pref := s.DB.Model(new(model.Drugs)) if req.Name != "" { pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%")) } if req.Usage > 0 { pref.Where("usage_method = ?", req.Usage) } if req.CategoryId > 0 { pref.Where("category_id = ?", req.CategoryId) } if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)). Find(&drugsList).Error; err != nil { return nil, xerr.WithStack(err) } drugsCategoryMap := s.DrugCategoryMaps() unitMap := s.UnitMap() drugUsageMap := s.DrugUsageMaps() return &pasturePb.SearchDrugsResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.SearchDrugsData{ List: model.DrugsSlice(drugsList).ToPB(drugsCategoryMap, unitMap, drugUsageMap), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) DrugsCreateOrUpdate(ctx context.Context, req *pasturePb.SearchDrugsList) error { currentUser, _ := s.GetCurrentSystemUser(ctx) newDrugs := model.NewDrugs(req, currentUser) if req.Id <= 0 { if err := s.DB.Create(newDrugs).Error; err != nil { return xerr.WithStack(err) } } else { if err := s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil { return xerr.WithStack(err) } } return nil } func (s *StoreEntry) MedicalEquipmentList(ctx context.Context, req *pasturePb.SearchMedicalEquipmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchMedicalEquipmentResponse, error) { medicalEquipmentList := make([]*model.MedicalEquipment, 0) var count int64 = 0 pref := s.DB.Model(new(model.MedicalEquipment)) if req.Name != "" { pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%")) } if err := pref.Order("id desc").Count(&count).Limit(int(pagination.PageSize)).Offset(int(pagination.PageOffset)). Find(&medicalEquipmentList).Error; err != nil { return nil, xerr.WithStack(err) } unitMap := s.UnitMap() return &pasturePb.SearchMedicalEquipmentResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.SearchMedicalEquipmentData{ List: model.MedicalEquipmentSlice(medicalEquipmentList).ToPB(unitMap), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil } func (s *StoreEntry) MedicalEquipmentCreateOrUpdate(ctx context.Context, req *pasturePb.SearchMedicalEquipmentList) error { currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil { return xerr.Custom("登录人信息失效") } newDrugs := model.NewMedicalEquipment(req, currentUser) if req.Id <= 0 { if err = s.DB.Create(newDrugs).Error; err != nil { return xerr.WithStack(err) } } else { if err = s.DB.Where("id = ?", req.Id).Updates(newDrugs).Error; err != nil { return xerr.WithStack(err) } } return nil } func (s *StoreEntry) NeckRingLogCreateOrUpdate(ctx context.Context, req *pasturePb.NeckRingCreateRequest) error { currentUser, err := s.GetCurrentSystemUser(ctx) if err != nil { return xerr.Custom("登录人信息失效") } if req.Items == nil || len(req.Items) == 0 { return xerr.Custom("请选择要脖环数据") } newNeckRingLogList := model.NewNeckRingLogList(req.Items, currentUser) cowIds := make([]int64, 0) for _, v := range newNeckRingLogList { cowIds = append(cowIds, v.CowId) } if err = s.DB.Transaction(func(tx *gorm.DB) error { // 解绑脖环号 if err = tx.Model(new(model.NeckRingLog)). Where("cowId IN ?", cowIds). Updates(map[string]interface{}{ "unbind_at": time.Now().Unix(), "status": pasturePb.NeckRingStatus_Unbind, "operation_id": currentUser.Id, "operation_name": currentUser.Name, }).Error; err != nil { return xerr.WithStack(err) } // 绑定新脖环 if err = tx.Create(newNeckRingLogList).Error; err != nil { return xerr.WithStack(err) } for _, v := range req.Items { if v.CowId > 0 { if err = tx.Model(new(model.Cow)). Where("id = ?", v.CowId). Where("admission_status = ?", pasturePb.AdmissionStatus_Admission). Updates(map[string]interface{}{ "neck_ring_number": v.Number, }).Error; err != nil { return xerr.WithStack(err) } } } return nil }); err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) NeckRingLogList(ctx context.Context, req *pasturePb.SearchNeckRingRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchNeckRingResponse, error) { neckRingLogList := make([]*model.NeckRingLog, 0) var count int64 = 0 pref := s.DB.Model(new(model.NeckRingLog)).Where("status > ?", pasturePb.NeckRingStatus_Unbind) if req.Status > 0 { pref.Where("status = ?", req.Status) } if req.CowId > 0 { pref.Where("cow_id = ?", req.CowId) } if req.Number != "" { pref.Where("number like ?", fmt.Sprintf("%s%s%s", "%", req.Number, "%")) } if err := pref.Order("id desc"). Count(&count). Limit(int(pagination.PageSize)). Offset(int(pagination.PageOffset)). Find(&neckRingLogList).Error; err != nil { return nil, xerr.WithStack(err) } neckRingStatusMap := s.NeckRingStatusMap() return &pasturePb.SearchNeckRingResponse{ Code: http.StatusOK, Message: "ok", Data: &pasturePb.SearchNeckRingData{ List: model.NeckRingLogSlice(neckRingLogList).ToPB(neckRingStatusMap), Total: int32(count), PageSize: pagination.PageSize, Page: pagination.Page, }, }, nil }