Prechádzať zdrojové kódy

Merge branch 'feature/disease' into develop

Yi 3 mesiacov pred
rodič
commit
a6bb57d394

+ 7 - 0
http/handler/event/event_health.go

@@ -25,6 +25,13 @@ func CowDiseaseCreate(c *gin.Context) {
 		valid.Field(&req.DiseaseAt, valid.Required),
 		valid.Field(&req.DiseaseId, valid.Required),
 		valid.Field(&req.OperationId, valid.Required),
+		valid.Field(&req.PrescriptionDetail, valid.Required, valid.Each(valid.By(func(value interface{}) error {
+			item := value.(pasturePb.TreatmentDrugs)
+			return valid.ValidateStruct(&item,
+				valid.Field(&item.DrugsId, valid.Required),
+				valid.Field(&item.UseNum, valid.Required),
+			)
+		}))),
 	); err != nil {
 		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
 		return

+ 5 - 1
model/event_cow_disease.go

@@ -23,6 +23,8 @@ type EventCowDisease struct {
 	Remarks               string                      `json:"remarks"`
 	OperationId           int32                       `json:"operationId"`
 	OperationName         string                      `json:"operationName"`
+	MessageId             int32                       `json:"messageId"`
+	MessageName           string                      `json:"messageName"`
 	DiagnoseOperationId   int32                       `json:"diagnoseOperationId"`
 	DiagnoseOperationName string                      `json:"diagnoseOperationName"`
 	CurableAt             int64                       `json:"curableAt"`
@@ -34,7 +36,7 @@ func (e *EventCowDisease) TableName() string {
 	return "event_cow_disease"
 }
 
-func NewEventCowDisease(cow *Cow, disease *Disease, req *pasturePb.EventCowDiseaseRequest, operation *SystemUser) *EventCowDisease {
+func NewEventCowDisease(cow *Cow, disease *Disease, req *pasturePb.EventCowDiseaseRequest, operation, currUser *SystemUser) *EventCowDisease {
 	return &EventCowDisease{
 		CowId:           cow.Id,
 		CowType:         cow.CowType,
@@ -50,6 +52,8 @@ func NewEventCowDisease(cow *Cow, disease *Disease, req *pasturePb.EventCowDisea
 		Temperature:     int32(req.Temperature * 10),
 		OperationId:     int32(operation.Id),
 		OperationName:   operation.Name,
+		MessageId:       int32(currUser.Id),
+		MessageName:     currUser.Name,
 		Remarks:         req.Remarks,
 	}
 }

+ 3 - 3
model/prescription_drugs.go

@@ -21,9 +21,9 @@ func (p *PrescriptionDrugs) TableName() string {
 	return "prescription_drugs"
 }
 
-func NewPrescriptionDrugs(prescriptionId int32, req *pasturePb.PrescriptionRequest) []*PrescriptionDrugs {
-	res := make([]*PrescriptionDrugs, len(req.DrugsList))
-	for i, v := range req.DrugsList {
+func NewPrescriptionDrugs(prescriptionId int32, list []*pasturePb.PrescriptionDrugsList) []*PrescriptionDrugs {
+	res := make([]*PrescriptionDrugs, len(list))
+	for i, v := range list {
 		res[i] = &PrescriptionDrugs{
 			PrescriptionId: prescriptionId,
 			DrugsId:        int64(v.DrugsId),

+ 86 - 18
module/backend/event_health.go

@@ -21,11 +21,16 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
 		return xerr.WithStack(err)
 	}
 
-	systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
+	operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
 	if err != nil {
 		return xerr.WithStack(err)
 	}
 
+	currUser, err := s.GetCurrentSystemUser(ctx)
+	if err != nil {
+		return xerr.Custom("登录信息错误")
+	}
+
 	disease, err := s.GetDiseaseById(ctx, req.DiseaseId)
 	if err != nil {
 		return xerr.WithStack(err)
@@ -39,21 +44,25 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
 	}
 
 	defer func() {
-		s.UpdateCowPenId(ctx, int64(req.CowId), int64(req.PenId))
+		if req.PenId > 0 {
+			s.UpdateCowPenId(ctx, int64(req.CowId), int64(req.PenId))
+		}
 	}()
 
-	newEventCowDisease := model.NewEventCowDisease(cow, disease, req, systemUser)
+	newEventCowDisease := model.NewEventCowDisease(cow, disease, req, operationUser, currUser)
 	if prescription.Id > 0 {
 		newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Treatment
 	}
 
 	var newEventCowTreatment *model.EventCowTreatment
+	var newCowTreatmentRequest *pasturePb.CowTreatmentRequest
+	unitMap := s.UnitMap()
+	diseaseTypeMap := s.DiseaseTypeMap()
 	if prescription.Id > 0 {
 		prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, prescription.Id)
 		if err != nil {
 			return xerr.WithStack(err)
 		}
-		unitMap := s.UnitMap()
 		prescriptionDetail := make([]*pasturePb.TreatmentDrugs, len(prescriptionDrugs))
 		for i, v := range prescriptionDrugs {
 			prescriptionDetail[i] = &pasturePb.TreatmentDrugs{
@@ -65,40 +74,99 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
 			}
 		}
 
-		newCowTreatmentRequest := &pasturePb.CowTreatmentRequest{
+		newCowTreatmentRequest = &pasturePb.CowTreatmentRequest{
 			CowId:              req.CowId,
 			PrescriptionId:     prescription.Id,
 			DiseaseId:          req.DiseaseId,
 			DiseaseName:        disease.Name,
 			PrescriptionDetail: prescriptionDetail,
 			TreatmentResult:    pasturePb.TreatmentResult_GoOn,
-			Remarks:            "",
+			Remarks:            req.Remarks,
+			TreatmentAt:        req.DiseaseAt,
+		}
+	} else {
+		if len(req.PrescriptionDetail) <= 0 {
+			return xerr.Custom("请填写治疗处方详情")
+		}
+		prescriptionDetail := make([]*pasturePb.TreatmentDrugs, len(req.PrescriptionDetail))
+		for i, v := range req.PrescriptionDetail {
+			if v.DrugsId <= 0 {
+				return xerr.Custom("请选择药方中的药品")
+			}
+			drugsData, err := s.GetDrugsById(ctx, int64(v.DrugsId))
+			if err != nil {
+				return xerr.WithStack(err)
+			}
+			prescriptionDetail[i] = &pasturePb.TreatmentDrugs{
+				DrugsId:   v.DrugsId,
+				DrugsName: drugsData.Name,
+				Unit:      v.Unit,
+				UnitName:  unitMap[v.Unit],
+				UseNum:    float32(v.UseNum),
+			}
+		}
+		req.PrescriptionDetail = prescriptionDetail
+		newCowTreatmentRequest = &pasturePb.CowTreatmentRequest{
+			CowId:              req.CowId,
+			PrescriptionId:     prescription.Id,
+			DiseaseId:          req.DiseaseId,
+			DiseaseName:        disease.Name,
+			PrescriptionDetail: req.PrescriptionDetail,
+			TreatmentResult:    pasturePb.TreatmentResult_GoOn,
+			Remarks:            req.Remarks,
 			TreatmentAt:        req.DiseaseAt,
 		}
-		diseaseTypeMap := s.DiseaseTypeMap()
-		newEventCowTreatment = model.NewEventCowTreatment(prescription, newCowTreatmentRequest, diseaseTypeMap, systemUser)
-		newEventCowDisease.DiagnosedAt = int64(req.DiseaseAt)
 	}
 
+	newEventCowTreatment = model.NewEventCowTreatment(prescription, newCowTreatmentRequest, diseaseTypeMap, operationUser)
+	newEventCowDisease.DiagnosedAt = int64(req.DiseaseAt)
+
 	if err = s.DB.Transaction(func(tx *gorm.DB) error {
 		if err = tx.Model(new(model.EventCowDisease)).Create(newEventCowDisease).Error; err != nil {
 			return xerr.WithStack(err)
 		}
-		// 如果有处方Id,则创建治疗记录
-		if prescription.Id > 0 {
-			// 创建治疗记录
-			if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).Error; err != nil {
+
+		// 创建治疗记录
+		if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+
+		// 创建新的处方
+		if req.PrescriptionId <= 0 {
+			newPrescriptionRequest := &pasturePb.PrescriptionRequest{
+				Name:                 fmt.Sprintf("%s-%s-%s", disease.Name, time.Now().Format("20060102"), operationUser.Name),
+				ApplicableDiseaseIds: []int32{req.DiseaseId},
+			}
+			newPrescription := model.NewPrescription(newPrescriptionRequest, disease.Name, 1, 0, 0, currUser)
+			if err = tx.Create(newPrescription).Error; err != nil {
 				return xerr.WithStack(err)
 			}
-			if err = tx.Model(new(model.Cow)).
-				Where("id = ?", req.CowId).
-				Updates(map[string]interface{}{
-					"health_status": pasturePb.HealthStatus_Disease,
-				}).Error; err != nil {
+			// 创建处方药品
+			drugsList := make([]*pasturePb.PrescriptionDrugsList, len(req.PrescriptionDetail))
+			for _, v := range req.PrescriptionDetail {
+				drugsList = append(drugsList, &pasturePb.PrescriptionDrugsList{
+					DrugsId:   v.DrugsId,
+					DrugsName: v.DrugsName,
+					Dosages:   int32(v.UseNum),
+					Unit:      v.Unit,
+					UnitName:  v.UnitName,
+					UseDays:   1,
+				})
+			}
+			newPrescriptionDrugs := model.NewPrescriptionDrugs(newPrescription.Id, drugsList)
+			if err = tx.Create(&newPrescriptionDrugs).Error; err != nil {
 				return xerr.WithStack(err)
 			}
 		}
 
+		if err = tx.Model(new(model.Cow)).
+			Where("id = ?", req.CowId).
+			Updates(map[string]interface{}{
+				"health_status": pasturePb.HealthStatus_Disease,
+			}).Error; err != nil {
+			return xerr.WithStack(err)
+		}
+
 		return nil
 	}); err != nil {
 		return xerr.WithStack(err)

+ 2 - 2
module/backend/prescription.go

@@ -297,7 +297,7 @@ func (s *StoreEntry) CreateOrUpdatePrescription(ctx context.Context, req *pastur
 				return xerr.WithStack(err)
 			}
 			// 创建处方药品
-			newPrescriptionDrugs := model.NewPrescriptionDrugs(req.Id, req)
+			newPrescriptionDrugs := model.NewPrescriptionDrugs(req.Id, req.DrugsList)
 			if err := tx.Create(&newPrescriptionDrugs).Error; err != nil {
 				return xerr.WithStack(err)
 			}
@@ -316,7 +316,7 @@ func (s *StoreEntry) CreateOrUpdatePrescription(ctx context.Context, req *pastur
 			return xerr.WithStack(err)
 		}
 		// 创建处方药品
-		newPrescriptionDrugs := model.NewPrescriptionDrugs(newPrescription.Id, req)
+		newPrescriptionDrugs := model.NewPrescriptionDrugs(newPrescription.Id, req.DrugsList)
 		if err := tx.Create(&newPrescriptionDrugs).Error; err != nil {
 			return xerr.WithStack(err)
 		}