|
@@ -15,7 +15,8 @@ import (
|
|
|
)
|
|
|
|
|
|
// CowDiseaseCreate 牛只发病提交
|
|
|
-func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDiseaseRequest) error {
|
|
|
+func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDiseaseRequest, source string) error {
|
|
|
+ // 牛只信息
|
|
|
cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -35,138 +36,116 @@ 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)
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
+ newEventCowDisease := model.NewEventCowDisease(cow, disease, req, operationUser, currUser)
|
|
|
defer func() {
|
|
|
if req.PenId > 0 {
|
|
|
s.UpdateCowPenId(ctx, int64(req.CowId), int64(req.PenId))
|
|
|
}
|
|
|
- }()
|
|
|
|
|
|
- newEventCowDisease := model.NewEventCowDisease(cow, disease, req, operationUser, currUser)
|
|
|
- if prescription.Id > 0 {
|
|
|
+ // 更新牛只健康状态
|
|
|
+ if newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Disease || newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Treatment {
|
|
|
+ s.DB.Model(new(model.Cow)).
|
|
|
+ Where("id = ?", req.CowId).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "health_status": pasturePb.HealthStatus_Disease,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ // PC端直接跳过诊断过程
|
|
|
+ if source == model.SourcePC {
|
|
|
+ newEventCowDisease.DiagnosedResult = pasturePb.IsShow_Ok
|
|
|
+ newEventCowDisease.DiagnoseOperationId = int32(operationUser.Id)
|
|
|
+ newEventCowDisease.DiagnoseOperationName = operationUser.Name
|
|
|
+ newEventCowDisease.Source = model.SourcePC
|
|
|
+ newEventCowDisease.DiagnoseId = req.DiseaseId
|
|
|
+ newEventCowDisease.DiagnoseName = disease.Name
|
|
|
+ newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Disease
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.PrescriptionId > 0 || len(req.PrescriptionDetail) > 0 {
|
|
|
newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Treatment
|
|
|
}
|
|
|
|
|
|
var newEventCowTreatment *model.EventCowTreatment
|
|
|
var newCowTreatmentRequest *pasturePb.CowTreatmentRequest
|
|
|
- unitMap := s.UnitMap()
|
|
|
+ var isCreatePrescription bool
|
|
|
diseaseTypeMap := s.DiseaseTypeMap()
|
|
|
- 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,
|
|
|
- UnitName: unitMap[v.Unit],
|
|
|
- UseNum: float32(v.Dosages),
|
|
|
- }
|
|
|
- }
|
|
|
+ prescription := &model.Prescription{}
|
|
|
+ if req.PrescriptionId > 0 || len(req.PrescriptionDetail) > 0 {
|
|
|
+ isCreatePrescription = true
|
|
|
|
|
|
- newCowTreatmentRequest = &pasturePb.CowTreatmentRequest{
|
|
|
- CowId: req.CowId,
|
|
|
- PrescriptionId: prescription.Id,
|
|
|
- DiseaseId: req.DiseaseId,
|
|
|
- DiseaseName: disease.Name,
|
|
|
- PrescriptionDetail: prescriptionDetail,
|
|
|
- TreatmentResult: pasturePb.TreatmentResult_GoOn,
|
|
|
- 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 req.PrescriptionId > 0 {
|
|
|
+ prescription, err = s.GetPrescriptionById(ctx, req.PrescriptionId)
|
|
|
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),
|
|
|
+
|
|
|
+ prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, prescription.Id)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
}
|
|
|
- }
|
|
|
- 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,
|
|
|
+ req.PrescriptionDetail = model.PrescriptionDrugsSlice(prescriptionDrugs).ToPB()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- 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)
|
|
|
}
|
|
|
+ newEventCowTreatment.CowDiseaseId = newEventCowDisease.Id
|
|
|
|
|
|
- // 创建治疗记录
|
|
|
- if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+ // 已有的处方使用次数+1
|
|
|
+ if req.PrescriptionId > 0 {
|
|
|
+ if err = tx.Model(new(model.Prescription)).Where("id = ?", req.PrescriptionId).
|
|
|
+ Update("use_count", gorm.Expr("use_count + 1")).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 创建新的处方
|
|
|
- if req.PrescriptionId <= 0 {
|
|
|
+ // 新的临时处方
|
|
|
+ if req.PrescriptionId <= 0 && len(req.PrescriptionDetail) > 0 {
|
|
|
newPrescriptionRequest := &pasturePb.PrescriptionRequest{
|
|
|
Name: fmt.Sprintf("%s-%s-%s", disease.Name, time.Now().Format("20060102"), operationUser.Name),
|
|
|
ApplicableDiseaseIds: []int32{req.DiseaseId},
|
|
|
+ IsShow: pasturePb.IsShow_Ok,
|
|
|
}
|
|
|
- newPrescription := model.NewPrescription(newPrescriptionRequest, disease.Name, 1, 0, 0, currUser)
|
|
|
- if err = tx.Create(newPrescription).Error; err != nil {
|
|
|
+ newPrescription := model.NewPrescription(newPrescriptionRequest, fmt.Sprintf("%d", disease.Id), 1, 0, 0, currUser)
|
|
|
+ newPrescription.UseCount += 1
|
|
|
+ if err = tx.Model(new(model.Prescription)).Create(newPrescription).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- // 创建处方药品
|
|
|
- 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 {
|
|
|
+ prescription = newPrescription
|
|
|
+
|
|
|
+ newPrescriptionDrugs := model.NewPrescriptionDrugs(prescription.Id, req.PrescriptionDetail)
|
|
|
+ if err = tx.Model(new(model.PrescriptionDrugs)).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)
|
|
|
+ newEventCowTreatment.PrescriptionId = prescription.Id
|
|
|
+ newEventCowTreatment.PrescriptionName = prescription.Name
|
|
|
}
|
|
|
|
|
|
+ // 创建治疗记录
|
|
|
+ if isCreatePrescription {
|
|
|
+ newCowTreatmentRequest = &pasturePb.CowTreatmentRequest{
|
|
|
+ CowId: req.CowId,
|
|
|
+ PrescriptionId: prescription.Id,
|
|
|
+ DiseaseId: req.DiseaseId,
|
|
|
+ DiseaseName: disease.Name,
|
|
|
+ DiseaseType: disease.DiseaseType,
|
|
|
+ PrescriptionDetail: req.PrescriptionDetail,
|
|
|
+ TreatmentResult: pasturePb.TreatmentResult_GoOn,
|
|
|
+ Remarks: req.Remarks,
|
|
|
+ TreatmentAt: req.DiseaseAt,
|
|
|
+ }
|
|
|
+
|
|
|
+ newEventCowTreatment = model.NewEventCowTreatment(prescription, newCowTreatmentRequest, diseaseTypeMap, operationUser)
|
|
|
+ // 创建治疗记录
|
|
|
+ if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
return nil
|
|
|
}); err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -337,14 +316,14 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
}
|
|
|
|
|
|
unitMap := s.UnitMap()
|
|
|
- prescriptionDetail := make([]*pasturePb.TreatmentDrugs, len(prescriptionDrugs))
|
|
|
+ prescriptionDetail := make([]*pasturePb.PrescriptionDrugsList, len(prescriptionDrugs))
|
|
|
for i, v := range prescriptionDrugs {
|
|
|
- prescriptionDetail[i] = &pasturePb.TreatmentDrugs{
|
|
|
+ prescriptionDetail[i] = &pasturePb.PrescriptionDrugsList{
|
|
|
DrugsId: int32(v.DrugsId),
|
|
|
DrugsName: v.DrugsName,
|
|
|
Unit: v.Unit,
|
|
|
UnitName: unitMap[v.Unit],
|
|
|
- UseNum: float32(v.Dosages),
|
|
|
+ Dosages: v.Dosages,
|
|
|
}
|
|
|
}
|
|
|
req.PrescriptionDetail = prescriptionDetail
|