|  | @@ -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
 |