|
@@ -56,10 +56,12 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+// CowDiseaseList 发病牛只清单
|
|
|
func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error) {
|
|
|
cowDiseaseList := make([]*model.EventCowDisease, 0)
|
|
|
var count int64 = 0
|
|
|
- pref := s.DB.Model(new(model.EventCowDisease))
|
|
|
+ pref := s.DB.Model(new(model.EventCowDisease)).
|
|
|
+ Where("diagnosed_result < ?", pasturePb.IsShow_No)
|
|
|
|
|
|
if len(req.CowIds) > 0 {
|
|
|
pref.Where("cow_id IN ?", req.CowIds)
|
|
@@ -83,12 +85,6 @@ func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEv
|
|
|
|
|
|
if req.HealthStatus > 0 {
|
|
|
pref.Where("health_status = ?", req.HealthStatus)
|
|
|
- } else {
|
|
|
- pref.Where("health_status IN ?", []pasturePb.HealthStatus_Kind{
|
|
|
- pasturePb.HealthStatus_Disease,
|
|
|
- pasturePb.HealthStatus_Treatment,
|
|
|
- pasturePb.HealthStatus_Diagnosed,
|
|
|
- })
|
|
|
}
|
|
|
|
|
|
if err := pref.Order("id desc").
|
|
@@ -98,11 +94,12 @@ func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEv
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
+ healthStatusMap := s.HealthStatusMap()
|
|
|
return &pasturePb.EventCowDiseaseResponse{
|
|
|
Code: http.StatusOK,
|
|
|
Message: "ok",
|
|
|
Data: &pasturePb.EventCowDiseaseData{
|
|
|
- List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(),
|
|
|
+ List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(healthStatusMap),
|
|
|
Total: int32(count),
|
|
|
PageSize: pagination.PageSize,
|
|
|
Page: pagination.Page,
|
|
@@ -110,6 +107,156 @@ func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEv
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context) {
|
|
|
+// CowDiseaseDiagnose 发病牛只诊断
|
|
|
+func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowDiagnosedRequest) error {
|
|
|
+ eventCowDisease := &model.EventCowDisease{}
|
|
|
+ if err := s.DB.Where("cow_id = ?", req.CowId).
|
|
|
+ Where("id = ?", req.Id).
|
|
|
+ First(eventCowDisease).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Health {
|
|
|
+ return xerr.Custom("异常牛只数据")
|
|
|
+ }
|
|
|
+
|
|
|
+ systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ // 未发病
|
|
|
+ if req.DiagnosedResult == pasturePb.IsShow_No {
|
|
|
+ if err := s.DB.Model(eventCowDisease).
|
|
|
+ Where("id = ?", req.Id).
|
|
|
+ Where("cow_id = ?", req.CowId).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "diagnosed_result": pasturePb.IsShow_No,
|
|
|
+ "diagnose_operation_id": req.OperationId,
|
|
|
+ "diagnose_operation_name": systemUser.Name,
|
|
|
+ "remarks": req.Remarks,
|
|
|
+ }).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ // 已发病
|
|
|
+ disease, err := s.GetDiseaseById(ctx, req.DiseaseId)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ if err = tx.Model(eventCowDisease).
|
|
|
+ Where("id = ?", req.Id).
|
|
|
+ Where("cow_id = ?", req.CowId).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "health_status": pasturePb.HealthStatus_Disease,
|
|
|
+ "diagnosed_result": pasturePb.IsShow_Ok,
|
|
|
+ "diagnose_id": req.DiseaseId,
|
|
|
+ "diagnose_name": disease.Name,
|
|
|
+ "temperature": int32(req.Temperature * 10),
|
|
|
+ "diagnose_operation_id": req.OperationId,
|
|
|
+ "diagnose_operation_name": systemUser.Name,
|
|
|
+ "remarks": req.Remarks,
|
|
|
+ }).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)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// CowDiseaseTreatment 发病牛只治疗
|
|
|
+func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.CowTreatmentRequest) error {
|
|
|
+ eventCowDisease := &model.EventCowDisease{}
|
|
|
+ if err := s.DB.Where("cow_id = ?", req.CowId).
|
|
|
+ Where("id = ?", req.Id).
|
|
|
+ First(eventCowDisease).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Disease &&
|
|
|
+ eventCowDisease.HealthStatus != pasturePb.HealthStatus_Treatment {
|
|
|
+ return xerr.Custom("异常牛只数据")
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ prescriptionDrugs := make([]*model.PrescriptionDrugs, 0)
|
|
|
+ if err := s.DB.Where("prescription_id = ?", req.PrescriptionId).Find(&prescriptionDrugs).Error; 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),
|
|
|
+ }
|
|
|
+ }
|
|
|
+ req.PrescriptionDetail = prescriptionDetail
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ healthStatus := pasturePb.HealthStatus_Treatment
|
|
|
+ if req.TreatmentResult == pasturePb.TreatmentResult_Curable {
|
|
|
+ healthStatus = pasturePb.HealthStatus_Curable
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.TreatmentResult == pasturePb.TreatmentResult_Out {
|
|
|
+ healthStatus = pasturePb.HealthStatus_Out
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.TreatmentResult == pasturePb.TreatmentResult_Dead {
|
|
|
+ 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 {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := tx.Model(eventCowDisease).
|
|
|
+ Where("id = ?", req.Id).
|
|
|
+ Where("cow_id = ?", req.CowId).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "health_status": healthStatus,
|
|
|
+ }).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := tx.Model(new(model.Cow)).
|
|
|
+ Where("id = ?", req.CowId).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "health_status": healthStatus,
|
|
|
+ }).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ }); err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
}
|