|
@@ -81,6 +81,8 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
if req.PrescriptionId > 0 || len(req.PrescriptionDetail) > 0 {
|
|
|
newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Treatment
|
|
|
newEventCowDisease.DiagnosedResult = pasturePb.IsShow_Ok
|
|
|
+ newEventCowDisease.FirstTreatmentAt = int64(req.DiseaseAt)
|
|
|
+ newEventCowDisease.LastTreatmentAt = int64(req.DiseaseAt)
|
|
|
}
|
|
|
|
|
|
newEventCowTreatment := &model.EventCowTreatment{}
|
|
@@ -104,9 +106,7 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
}
|
|
|
}
|
|
|
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)
|
|
|
- }
|
|
|
+ lastPrescriptionName := ""
|
|
|
// 已有的处方使用次数+1
|
|
|
if req.PrescriptionId > 0 {
|
|
|
prescription.EventUseCountUpdate()
|
|
@@ -115,6 +115,7 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
Updates(prescription).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
+ lastPrescriptionName = prescription.Name
|
|
|
}
|
|
|
// 新的临时处方
|
|
|
if req.PrescriptionId <= 0 && len(req.PrescriptionDetail) > 0 {
|
|
@@ -137,12 +138,19 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
prescription = newPrescription
|
|
|
+ lastPrescriptionName = newPrescription.Name
|
|
|
|
|
|
newPrescriptionDrugs := model.NewPrescriptionDrugs(userModel.AppPasture.Id, prescription.Id, req.PrescriptionDetail)
|
|
|
if err = tx.Model(new(model.PrescriptionDrugs)).Create(newPrescriptionDrugs).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // 创建 CowDisease
|
|
|
+ newEventCowDisease.LastPrescriptionName = lastPrescriptionName
|
|
|
+ if err = tx.Model(new(model.EventCowDisease)).Create(newEventCowDisease).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
// 创建治疗记录
|
|
|
if isCreatePrescription {
|
|
|
newCowTreatmentRequest = &pasturePb.CowTreatmentRequest{
|
|
@@ -245,6 +253,8 @@ func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEv
|
|
|
cowDiseaseList := make([]*model.EventCowDisease, 0)
|
|
|
var count int64 = 0
|
|
|
pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowDisease).TableName())).
|
|
|
+ Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
|
|
|
+ Select("a.*,b.pen_name").
|
|
|
Where("a.pasture_id = ?", userModel.AppPasture.Id)
|
|
|
|
|
|
if len(req.CowIds) > 0 {
|
|
@@ -256,15 +266,7 @@ func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEv
|
|
|
}
|
|
|
|
|
|
if req.PenId > 0 {
|
|
|
- pref.Where("a.pen_id = ?", req.PenId)
|
|
|
- }
|
|
|
-
|
|
|
- if req.Lact > 0 {
|
|
|
- pref.Where("a.lact = ?", req.Lact)
|
|
|
- }
|
|
|
-
|
|
|
- if req.MinDayAge > 0 && req.MaxDayAge > 0 && req.MaxDayAge >= req.MinDayAge {
|
|
|
- pref.Where("a.day_age BETWEEN ? AND ?", req.MinDayAge, req.MaxDayAge)
|
|
|
+ pref.Where("b.pen_id = ?", req.PenId)
|
|
|
}
|
|
|
|
|
|
if req.HealthStatus > 0 {
|
|
@@ -376,8 +378,10 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
+ // 牛只疾病信息
|
|
|
eventCowDisease := &model.EventCowDisease{}
|
|
|
if err = s.DB.Where("cow_id = ?", req.CowId).
|
|
|
+ Where("pasture_id = ?", userModel.AppPasture.Id).
|
|
|
Where("id = ?", req.Id).
|
|
|
First(eventCowDisease).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -388,21 +392,24 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
return xerr.Custom("异常牛只数据")
|
|
|
}
|
|
|
|
|
|
+ cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 操作人信息
|
|
|
operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
if err != nil {
|
|
|
return xerr.Customf("操作人数据异常: %d", req.OperationId)
|
|
|
}
|
|
|
|
|
|
+ // 处方信息
|
|
|
prescription, err := s.GetPrescriptionById(ctx, userModel.AppPasture.Id, req.PrescriptionId)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
|
|
|
- if err != nil {
|
|
|
- return xerr.Customf("异常牛数据: %d", req.CowId)
|
|
|
- }
|
|
|
-
|
|
|
+ // 疾病信息
|
|
|
disease, err := s.GetDiseaseById(ctx, userModel.AppPasture.Id, eventCowDisease.DiseaseId)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -410,11 +417,13 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
req.DiseaseName = disease.Name
|
|
|
req.DiseaseType = disease.DiseaseType
|
|
|
|
|
|
+ // 处方信息
|
|
|
prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, userModel.AppPasture.Id, prescription.Id)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
+ // 处方详情
|
|
|
unitMap := s.UnitMap()
|
|
|
prescriptionDetail := make([]*pasturePb.PrescriptionDrugsList, len(prescriptionDrugs))
|
|
|
for i, v := range prescriptionDrugs {
|
|
@@ -441,14 +450,14 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
}
|
|
|
diseaseTypeMap := s.DiseaseTypeMap()
|
|
|
newEventCowTreatment := model.NewEventCowTreatment(userModel.AppPasture.Id, prescription, req, diseaseTypeMap, operationUser, userModel.SystemUser)
|
|
|
+
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
if err = tx.Create(newEventCowTreatment).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
-
|
|
|
- eventCowDisease.EventHealthStatusUpdate(healthStatus)
|
|
|
+ eventCowDisease.EventTreatmentUpdate(healthStatus, int64(req.TreatmentAt), prescription.Name)
|
|
|
if err = tx.Model(eventCowDisease).
|
|
|
- Select("health_status").
|
|
|
+ Select("health_status", "first_treatment_at", "last_treatment_at", "last_prescription_name").
|
|
|
Where("id = ?", req.Id).
|
|
|
Where("cow_id = ?", req.CowId).
|
|
|
Updates(eventCowDisease).Error; err != nil {
|