|
@@ -15,7 +15,7 @@ import (
|
|
|
)
|
|
|
|
|
|
// CowDiseaseCreate 牛只发病提交
|
|
|
-func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDisease) error {
|
|
|
+func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDiseaseRequest) error {
|
|
|
cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -26,8 +26,6 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- req.OperationName = systemUser.Name
|
|
|
-
|
|
|
disease, err := s.GetDiseaseById(ctx, req.DiseaseId)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -40,8 +38,7 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- req.DiseaseName = disease.Name
|
|
|
- newEventCowDisease := model.NewEventCowDisease(cow, req)
|
|
|
+ newEventCowDisease := model.NewEventCowDisease(cow, disease, req, systemUser)
|
|
|
if prescription.Id > 0 {
|
|
|
newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Treatment
|
|
|
}
|
|
@@ -78,9 +75,11 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- newEventCowTreatment := model.NewEventCowTreatment(&pasturePb.CowTreatmentRequest{
|
|
|
+ newEventCowTreatment := model.NewEventCowTreatment(prescription, &pasturePb.CowTreatmentRequest{
|
|
|
CowId: req.CowId,
|
|
|
PrescriptionId: prescription.Id,
|
|
|
+ DiseaseId: req.DiseaseId,
|
|
|
+ DiseaseName: disease.Name,
|
|
|
PrescriptionDetail: prescriptionDetail,
|
|
|
TreatmentResult: pasturePb.TreatmentResult_GoOn,
|
|
|
Remarks: "",
|
|
@@ -180,7 +179,7 @@ func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowD
|
|
|
}
|
|
|
// 未发病
|
|
|
if req.DiagnosedResult == pasturePb.IsShow_No {
|
|
|
- if err := s.DB.Model(eventCowDisease).
|
|
|
+ if err = s.DB.Model(eventCowDisease).
|
|
|
Where("id = ?", req.Id).
|
|
|
Where("cow_id = ?", req.CowId).
|
|
|
Updates(map[string]interface{}{
|
|
@@ -247,29 +246,31 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
}
|
|
|
|
|
|
systemUser, _ := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
+ prescription, err := s.GetPrescriptionById(ctx, req.PrescriptionId)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
|
|
|
- if req.PrescriptionId > 0 {
|
|
|
- prescription, err := s.GetPrescriptionById(ctx, req.PrescriptionId)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, prescription.Id)
|
|
|
- if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
+ disease, err := s.GetDiseaseById(ctx, eventCowDisease.DiagnoseId)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ req.DiseaseName = disease.Name
|
|
|
+ prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, prescription.Id)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
|
|
|
- prescriptionDetail := make([]*pasturePb.TreatmentDrugs, len(prescriptionDrugs))
|
|
|
- for i, v := range prescriptionDrugs {
|
|
|
- prescriptionDetail[i] = &pasturePb.TreatmentDrugs{
|
|
|
- DrugsId: int32(v.DrugsId),
|
|
|
- DrugsName: v.DrugsName,
|
|
|
- Unit: v.Unit,
|
|
|
- UseNum: float32(v.Dosages),
|
|
|
- }
|
|
|
+ prescriptionDetail := make([]*pasturePb.TreatmentDrugs, len(prescriptionDrugs))
|
|
|
+ for i, v := range prescriptionDrugs {
|
|
|
+ prescriptionDetail[i] = &pasturePb.TreatmentDrugs{
|
|
|
+ DrugsId: int32(v.DrugsId),
|
|
|
+ DrugsName: v.DrugsName,
|
|
|
+ Unit: v.Unit,
|
|
|
+ UseNum: float32(v.Dosages),
|
|
|
}
|
|
|
- req.PrescriptionDetail = prescriptionDetail
|
|
|
- return nil
|
|
|
}
|
|
|
+ req.PrescriptionDetail = prescriptionDetail
|
|
|
|
|
|
healthStatus := pasturePb.HealthStatus_Treatment
|
|
|
if req.TreatmentResult == pasturePb.TreatmentResult_Curable {
|
|
@@ -284,13 +285,13 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
healthStatus = pasturePb.HealthStatus_Dead
|
|
|
}
|
|
|
|
|
|
- newEventCowTreatment := model.NewEventCowTreatment(req, systemUser)
|
|
|
- if err := s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- if err := tx.Create(newEventCowTreatment).Error; err != nil {
|
|
|
+ newEventCowTreatment := model.NewEventCowTreatment(prescription, req, systemUser)
|
|
|
+ if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ if err = tx.Create(newEventCowTreatment).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if err := tx.Model(eventCowDisease).
|
|
|
+ if err = tx.Model(eventCowDisease).
|
|
|
Where("id = ?", req.Id).
|
|
|
Where("cow_id = ?", req.CowId).
|
|
|
Updates(map[string]interface{}{
|
|
@@ -299,7 +300,7 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if err := tx.Model(new(model.Cow)).
|
|
|
+ if err = tx.Model(new(model.Cow)).
|
|
|
Where("id = ?", req.CowId).
|
|
|
Updates(map[string]interface{}{
|
|
|
"health_status": healthStatus,
|
|
@@ -313,3 +314,50 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func (s *StoreEntry) CowDiseaseTreatmentDetail(ctx context.Context, req *pasturePb.EventCowTreatmentDetailRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowTreatmentDetailResponse, error) {
|
|
|
+ eventCowDiseaseList := make([]*model.EventCowDisease, 0)
|
|
|
+ var count int64 = 0
|
|
|
+ pref := s.DB.Where("cow_id = ?", req.CowId)
|
|
|
+
|
|
|
+ if req.DiseaseId > 0 {
|
|
|
+ pref.Where("disease_id = ?", req.DiseaseId)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.DiseaseStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseStartAt <= req.DiseaseEndAt {
|
|
|
+ pref.Where("disease_at BETWEEN ? AND ?", req.DiseaseStartAt, req.DiseaseEndAt)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := pref.Count(&count).
|
|
|
+ Limit(int(pagination.PageSize)).
|
|
|
+ Offset(int(pagination.PageOffset)).
|
|
|
+ Order("id desc").
|
|
|
+ Find(&eventCowDiseaseList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ eventCowTreatmentList := make([]*pasturePb.EventCowTreatment, 0)
|
|
|
+ cowDiseaseIds := make([]int64, len(eventCowDiseaseList))
|
|
|
+ for i, v := range eventCowDiseaseList {
|
|
|
+ cowDiseaseIds[i] = v.Id
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := s.DB.Where("cow_disease_id IN ?", cowDiseaseIds).
|
|
|
+ Where("cow_id = ?", req.CowId).
|
|
|
+ Group("cow_disease_id").
|
|
|
+ Order("id desc").
|
|
|
+ Find(&eventCowTreatmentList).Error; err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return &pasturePb.EventCowTreatmentDetailResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: &pasturePb.EventCowTreatmentDetail{
|
|
|
+ List: eventCowTreatmentList,
|
|
|
+ Total: int32(count),
|
|
|
+ PageSize: pagination.Page,
|
|
|
+ Page: pagination.PageSize,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+}
|