| 
					
				 | 
			
			
				@@ -609,3 +609,128 @@ func (s *StoreEntry) CreateOrUpdateDiseaseType(ctx context.Context, req *pasture 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+func (s *StoreEntry) SearchPrescriptionList(ctx context.Context, req *pasturePb.SearchPrescriptionRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchPrescriptionResponse, error) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	prescriptionList := make([]*model.Prescription, 0) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	var count int64 = 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	pref := s.DB.Model(new(model.Prescription)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	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(&prescriptionList).Error; err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return nil, xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	prescriptionIds := make([]int64, 0) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for _, prescription := range prescriptionList { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		prescriptionIds = append(prescriptionIds, prescription.Id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	prescriptionDrugs := make([]*model.PrescriptionDrugs, 0) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if len(prescriptionIds) > 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err := s.DB.Model(new(model.PrescriptionDrugs)).Where("prescription_id in (?)", prescriptionIds). 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Where("is_show = ? ", pasturePb.IsShow_Ok). 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Find(&prescriptionDrugs).Error; err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return nil, xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	systemUserList, _ := s.SystemUserList(ctx) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	return &pasturePb.SearchPrescriptionResponse{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		Code:    http.StatusOK, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		Message: "ok", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		Data: &pasturePb.SearchPrescriptionData{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			List:     model.PrescriptionSlice(prescriptionList).ToPB(systemUserList, prescriptionDrugs), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Total:    int32(count), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			PageSize: pagination.PageSize, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			Page:     pagination.Page, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	}, nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+func (s *StoreEntry) CreateOrUpdatePrescription(ctx context.Context, req *pasturePb.PrescriptionRequest) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	currUser, _ := s.GetCurrentSystemUser(ctx) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	var maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays int32 = 0, 0, 0 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	for _, v := range req.PrescriptionDrugsItem { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if v.DrugsId <= 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return xerr.Customf("错误处方药品的数据") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if v.UseDays <= 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return xerr.Customf("错误处方药品使用天数") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if v.UseDays > maxUseDays { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			maxUseDays = v.UseDays 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		drugs, err := s.DrugsById(ctx, int64(v.DrugsId)) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if drugs.MeatExpiredDays > maxMeatExpiredDays { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			maxMeatExpiredDays = drugs.MeatExpiredDays 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if drugs.MilkExpiredDays > maxMilkExpiredDays { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			maxMilkExpiredDays = drugs.MilkExpiredDays 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	newPrescription := model.NewPrescription(req, maxUseDays, maxMeatExpiredDays, maxMilkExpiredDays, currUser.Id) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if req.Id > 0 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		prescription := &model.Prescription{Id: int64(req.Id)} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err := s.DB.Model(&model.Prescription{}).First(prescription).Error; err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if !errors.Is(err, gorm.ErrRecordNotFound) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				return xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err := s.DB.Transaction(func(tx *gorm.DB) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if err := tx.Model(&model.Prescription{}).Where(map[string]interface{}{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				"id": req.Id, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			}).Updates(map[string]interface{}{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				"name":               newPrescription.Name, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				"applicable_disease": newPrescription.ApplicableDisease, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				"use_days":           newPrescription.UseDays, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				"meat_expired_days":  newPrescription.MeatExpiredDays, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				"milk_expired_days":  newPrescription.MilkExpiredDays, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				"remarks":            newPrescription.Remarks, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			}).Error; err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				return xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if err := tx.Model(&model.PrescriptionDrugs{}).Where("prescription_id", req.Id).Delete(&model.PrescriptionDrugs{}).Error; err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				return xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			// 创建处方药品 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			newPrescriptionDrugs := model.NewPrescriptionDrugs(int64(req.Id), req) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if err := tx.Create(&newPrescriptionDrugs).Error; err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				return xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		}); err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	// 创建处方 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	if err := s.DB.Transaction(func(tx *gorm.DB) error { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 创建处方 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err := tx.Create(&newPrescription).Error; err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		// 创建处方药品 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		newPrescriptionDrugs := model.NewPrescriptionDrugs(newPrescription.Id, req) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if err := tx.Create(&newPrescriptionDrugs).Error; err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			return xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	}); err != nil { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		return xerr.WithStack(err) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+	return nil 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |