소스 검색

cowDisease: 牛只疾病创建业务更新

Yi 5 달 전
부모
커밋
55f62a832f
4개의 변경된 파일86개의 추가작업 그리고 9개의 파일을 삭제
  1. 1 1
      go.mod
  2. 4 0
      go.sum
  3. 61 8
      module/backend/event_health.go
  4. 20 0
      module/backend/sql.go

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20241018062323-e9d24b0e37b7
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20241021095634-62b455bd33a1
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241010101255-0c6bd229b939
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eko/gocache v1.1.0

+ 4 - 0
go.sum

@@ -92,6 +92,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20241017071700-cf080afaeec6 h1:ahpnQMqv
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241017071700-cf080afaeec6/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241018062323-e9d24b0e37b7 h1:ifjGzo+Kz+arWC0+MOuD7Cdd0Ph2cQ9Eg3eJyp8wEXY=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241018062323-e9d24b0e37b7/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241021094122-8c83f9d0bd28 h1:JW+Xqeeflr76HSGcC/JMs7v0KdjQAmtiMgK8nG+vpMg=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241021094122-8c83f9d0bd28/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241021095634-62b455bd33a1 h1:umkLXvBmP5RVv50FHhQo3Z0U50BdmLwtSAXEmDWpIoI=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241021095634-62b455bd33a1/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015 h1:dfb5dRd57L2HKjdwLT93UFmPYFPOmEl56gtZmqcNnaE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015/go.mod h1:Fk4GYI/v0IK3XFrm1Gn+VkgCz5Y7mfswD5hsTJYOG6A=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241008063555-121a776fd331 h1:qJcpJ3o+O7uxDqR0I9LijQmi607IKvhf4mGum/ZUPT0=

+ 61 - 8
module/backend/event_health.go

@@ -5,6 +5,9 @@ import (
 	"kpt-pasture/model"
 	"net/http"
 
+	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
+	"go.uber.org/zap"
+
 	"gorm.io/gorm"
 
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
@@ -29,14 +32,24 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
 	if err != nil {
 		return xerr.WithStack(err)
 	}
+	prescription := &model.Prescription{}
+	if req.PrescriptionId > 0 {
+		prescription, err = s.GetPrescriptionById(ctx, req.PrescriptionId)
+		if err != nil {
+			return xerr.WithStack(err)
+		}
+	}
 
 	req.DiseaseName = disease.Name
 	newEventCowDisease := model.NewEventCowDisease(cow, req)
+	if prescription.Id > 0 {
+		newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Treatment
+	}
+
 	if err = s.DB.Transaction(func(tx *gorm.DB) error {
-		if err = tx.Create(newEventCowDisease).Error; err != nil {
+		if err = tx.Model(new(model.EventCowDisease)).Create(newEventCowDisease).Error; err != nil {
 			return xerr.WithStack(err)
 		}
-
 		// 更新牛舍
 		if req.PenId > 0 {
 			if err = tx.Model(new(model.Cow)).
@@ -44,8 +57,48 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
 				Updates(map[string]interface{}{
 					"pen_id": req.PenId,
 				}).Error; err != nil {
+				zaplog.Error("CowDiseaseCreate", zap.Any("UpdatePen", err), zap.Any("req", req))
+			}
+		}
+
+		// 如果有处方Id,则创建治疗记录
+		if prescription.Id > 0 {
+			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),
+				}
+			}
+
+			newEventCowTreatment := model.NewEventCowTreatment(&pasturePb.CowTreatmentRequest{
+				CowId:              req.CowId,
+				PrescriptionId:     prescription.Id,
+				PrescriptionDetail: prescriptionDetail,
+				TreatmentResult:    pasturePb.TreatmentResult_GoOn,
+				Remarks:            "",
+				TreatmentAt:        req.DiseaseAt,
+			}, systemUser)
+			// 创建治疗记录
+			if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).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
@@ -90,6 +143,7 @@ func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEv
 	if err := pref.Order("id desc").
 		Count(&count).Limit(int(pagination.PageSize)).
 		Offset(int(pagination.PageOffset)).
+		Order("id desc").
 		Find(&cowDiseaseList).Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
@@ -195,14 +249,12 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
 	systemUser, _ := s.GetSystemUserById(ctx, int64(req.OperationId))
 
 	if req.PrescriptionId > 0 {
-		prescription := &model.Prescription{}
-		if err := s.DB.Where("id = ?", req.PrescriptionId).
-			Where("is_show = ?", pasturePb.IsShow_Ok).
-			First(prescription).Error; err != nil {
+		prescription, err := s.GetPrescriptionById(ctx, req.PrescriptionId)
+		if err != nil {
 			return xerr.WithStack(err)
 		}
-		prescriptionDrugs := make([]*model.PrescriptionDrugs, 0)
-		if err := s.DB.Where("prescription_id = ?", req.PrescriptionId).Find(&prescriptionDrugs).Error; err != nil {
+		prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, prescription.Id)
+		if err != nil {
 			return xerr.WithStack(err)
 		}
 
@@ -218,6 +270,7 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
 		req.PrescriptionDetail = prescriptionDetail
 		return nil
 	}
+
 	healthStatus := pasturePb.HealthStatus_Treatment
 	if req.TreatmentResult == pasturePb.TreatmentResult_Curable {
 		healthStatus = pasturePb.HealthStatus_Curable

+ 20 - 0
module/backend/sql.go

@@ -185,6 +185,26 @@ func (s *StoreEntry) DiseaseListByIds(ctx context.Context, ids []int32) ([]*mode
 	return diseaseList, nil
 }
 
+func (s *StoreEntry) GetPrescriptionById(ctx context.Context, id int32) (*model.Prescription, error) {
+	newPrescription := &model.Prescription{}
+	if err := s.DB.Where("id = ?", id).
+		Where("is_show = ?", pasturePb.IsShow_Ok).
+		First(&newPrescription).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	return newPrescription, nil
+}
+
+func (s *StoreEntry) PrescriptionDrugsByPrescriptionId(ctx context.Context, prescriptionId int32) ([]*model.PrescriptionDrugs, error) {
+	newPrescriptionDrugsList := make([]*model.PrescriptionDrugs, 0)
+	if err := s.DB.Where("prescription_id = ?", prescriptionId).
+		Where("is_show = ?", pasturePb.IsShow_Ok).
+		Find(&newPrescriptionDrugsList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	return newPrescriptionDrugsList, nil
+}
+
 func (s *StoreEntry) GetImmunizationById(ctx context.Context, id int64) (*model.ImmunizationPlan, error) {
 	immunizationPlan := &model.ImmunizationPlan{Id: id}
 	if err := s.DB.First(immunizationPlan).Error; err != nil {