|  | @@ -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)
 | 
	
	
		
			
				|  | @@ -138,6 +206,10 @@ func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEv
 | 
	
		
			
				|  |  |  		pref.Where("a.health_status = ?", req.HealthStatus)
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +	if req.DiseasedStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseEndAt >= req.DiseasedStartAt {
 | 
	
		
			
				|  |  | +		pref.Where("a.disease_at BETWEEN ? AND ?", req.DiseasedStartAt, req.DiseaseEndAt)
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  	if err := pref.Order("a.id desc").
 | 
	
		
			
				|  |  |  		Count(&count).Limit(int(pagination.PageSize)).
 | 
	
		
			
				|  |  |  		Offset(int(pagination.PageOffset)).
 |