|
@@ -8,6 +8,9 @@ import (
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
+ "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"
|
|
@@ -19,17 +22,17 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
// 牛只信息
|
|
|
cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
|
|
|
if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+ return xerr.Customf("牛只信息错误: %d", req.CowId)
|
|
|
}
|
|
|
|
|
|
operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+ return xerr.Customf("请检查操作人信息")
|
|
|
}
|
|
|
|
|
|
currUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
if err != nil {
|
|
|
- return xerr.Custom("登录信息错误")
|
|
|
+ return xerr.Custom("登录信息错误,请退出重新登录")
|
|
|
}
|
|
|
|
|
|
disease, err := s.GetDiseaseById(ctx, req.DiseaseId)
|
|
@@ -140,7 +143,7 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
|
TreatmentAt: req.DiseaseAt,
|
|
|
}
|
|
|
|
|
|
- newEventCowTreatment = model.NewEventCowTreatment(prescription, newCowTreatmentRequest, diseaseTypeMap, operationUser)
|
|
|
+ newEventCowTreatment = model.NewEventCowTreatment(prescription, newCowTreatmentRequest, diseaseTypeMap, operationUser, currUser)
|
|
|
// 创建治疗记录
|
|
|
if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -211,14 +214,16 @@ func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEv
|
|
|
|
|
|
// CowDiseaseDiagnose 发病牛只诊断
|
|
|
func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowDiagnosedRequest) error {
|
|
|
+ cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.Customf("错误的牛只信息: %d", req.CowId)
|
|
|
+ }
|
|
|
+
|
|
|
eventCowDisease := &model.EventCowDisease{}
|
|
|
- if err := s.DB.Where("cow_id = ?", req.CowId).
|
|
|
+ if err = s.DB.Where("cow_id = ?", req.CowId).
|
|
|
Where("id = ?", req.Id).
|
|
|
+ Where("health_status = ?", pasturePb.HealthStatus_Health).
|
|
|
First(eventCowDisease).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
-
|
|
|
- if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Health {
|
|
|
return xerr.Custom("异常牛只数据")
|
|
|
}
|
|
|
|
|
@@ -226,17 +231,14 @@ func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowD
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
- // 未发病
|
|
|
+
|
|
|
if req.DiagnosedResult == pasturePb.IsShow_No {
|
|
|
+ // 未发病更新
|
|
|
+ eventCowDisease.EventUnDiseaseUpdate(currentUser, req.Remarks)
|
|
|
if err = s.DB.Model(eventCowDisease).
|
|
|
+ Select("diagnosed_result,diagnose_operation_id,diagnose_operation_name,remarks").
|
|
|
Where("id = ?", req.Id).
|
|
|
- Where("cow_id = ?", req.CowId).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "diagnosed_result": pasturePb.IsShow_No,
|
|
|
- "diagnose_operation_id": currentUser.Id,
|
|
|
- "diagnose_operation_name": currentUser.Name,
|
|
|
- "remarks": req.Remarks,
|
|
|
- }).Error; err != nil {
|
|
|
+ Updates(eventCowDisease).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
return nil
|
|
@@ -253,27 +255,20 @@ func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowD
|
|
|
}
|
|
|
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
+ eventCowDisease.EventDiseaseUpdate(disease, systemUser, req.Temperature)
|
|
|
if err = tx.Model(eventCowDisease).
|
|
|
+ Select("health_status,diagnosed_result,diagnose_id,diagnose_name,temperature,diagnose_operation_id,diagnose_operation_name,diagnosed_at").
|
|
|
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 * 100),
|
|
|
- "diagnose_operation_id": req.OperationId,
|
|
|
- "diagnose_operation_name": systemUser.Name,
|
|
|
- "diagnosed_at": time.Now().Unix(),
|
|
|
- }).Error; err != nil {
|
|
|
+ Updates(eventCowDisease).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 {
|
|
|
+ cow.EventDiseaseUpdate(pasturePb.HealthStatus_Disease)
|
|
|
+ if err = tx.Model(cow).
|
|
|
+ Select("health_status").
|
|
|
+ Where("id = ?", cow.Id).
|
|
|
+ Updates(cow).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
return nil
|
|
@@ -298,12 +293,25 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
return xerr.Custom("异常牛只数据")
|
|
|
}
|
|
|
|
|
|
- systemUser, _ := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
+ systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.Customf("操作人数据异常: %d", req.OperationId)
|
|
|
+ }
|
|
|
+
|
|
|
prescription, err := s.GetPrescriptionById(ctx, req.PrescriptionId)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
+ currUser, err := s.GetCurrentSystemUser(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
|
|
|
+ if err != nil {
|
|
|
+ return xerr.Customf("异常牛数据: %d", req.CowId)
|
|
|
+ }
|
|
|
+
|
|
|
disease, err := s.GetDiseaseById(ctx, eventCowDisease.DiagnoseId)
|
|
|
if err != nil {
|
|
|
return xerr.WithStack(err)
|
|
@@ -340,34 +348,34 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
healthStatus = pasturePb.HealthStatus_Dead
|
|
|
}
|
|
|
diseaseTypeMap := s.DiseaseTypeMap()
|
|
|
- newEventCowTreatment := model.NewEventCowTreatment(prescription, req, diseaseTypeMap, systemUser)
|
|
|
+ newEventCowTreatment := model.NewEventCowTreatment(prescription, req, diseaseTypeMap, systemUser, currUser)
|
|
|
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)
|
|
|
if err = tx.Model(eventCowDisease).
|
|
|
+ Select("health_status").
|
|
|
Where("id = ?", req.Id).
|
|
|
Where("cow_id = ?", req.CowId).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "health_status": healthStatus,
|
|
|
- }).Error; err != nil {
|
|
|
+ Updates(eventCowDisease).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
+ cow.EventDiseaseUpdate(healthStatus)
|
|
|
if err = tx.Model(new(model.Cow)).
|
|
|
+ Select("health_status").
|
|
|
Where("id = ?", req.CowId).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "health_status": healthStatus,
|
|
|
- }).Error; err != nil {
|
|
|
+ Updates(cow).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
|
- if err = tx.Model(new(model.Prescription)).
|
|
|
+ prescription.EventUseCountUpdate()
|
|
|
+ if err = tx.Model(prescription).
|
|
|
+ Select("use_count").
|
|
|
Where("id = ?", prescription.Id).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "use_count": prescription.UseCount + 1,
|
|
|
- }).Error; err != nil {
|
|
|
+ Updates(prescription).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
return nil
|
|
@@ -439,7 +447,7 @@ func (s *StoreEntry) CowDiseaseTreatmentDetail(
|
|
|
}
|
|
|
|
|
|
if err := s.DB.Model(new(model.EventCowTreatment)).
|
|
|
- Select("*").Where("cow_disease_id IN ?", cowDiseaseIds).
|
|
|
+ Where("cow_disease_id IN ?", cowDiseaseIds).
|
|
|
Where("cow_id = ?", req.CowId).
|
|
|
Group("cow_disease_id").
|
|
|
Order("id desc").
|
|
@@ -464,56 +472,64 @@ func (s *StoreEntry) CowDiseaseCurable(ctx context.Context, req *pasturePb.Event
|
|
|
if err := s.DB.Where("id IN ?", req.Ids).
|
|
|
Where("health_status = ?", pasturePb.HealthStatus_Treatment).
|
|
|
Find(&eventCowDiseaseList).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+ zaplog.Error("GetEventCowDiseaseList", zap.Any("err", err), zap.Any("req", req))
|
|
|
+ return xerr.Custom("异常数据")
|
|
|
}
|
|
|
|
|
|
if len(eventCowDiseaseList) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
- if len(eventCowDiseaseList) != len(req.Ids) {
|
|
|
- return xerr.New("id 参数错误")
|
|
|
- }
|
|
|
+
|
|
|
operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
if err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+ return xerr.Customf("该用户不存在: %d", req.OperationId)
|
|
|
}
|
|
|
eventCowTreatmentList := make([]*model.EventCowTreatment, 0)
|
|
|
for _, v := range eventCowDiseaseList {
|
|
|
eventCowTreatmentList = append(eventCowTreatmentList, &model.EventCowTreatment{
|
|
|
- CowId: v.CowId,
|
|
|
- CowDiseaseId: v.Id,
|
|
|
- DiseaseId: int64(v.DiseaseId),
|
|
|
- DiseaseName: v.DiseaseName,
|
|
|
- PrescriptionId: 0,
|
|
|
- PrescriptionName: "",
|
|
|
- PrescriptionDetail: "",
|
|
|
- TreatmentResult: pasturePb.TreatmentResult_Curable,
|
|
|
- OperationId: operationUser.Id,
|
|
|
- OperationName: operationUser.Name,
|
|
|
- Remarks: req.Remarks,
|
|
|
- TreatmentAt: int64(req.CurableAt),
|
|
|
+ CowId: v.CowId,
|
|
|
+ CowDiseaseId: v.Id,
|
|
|
+ DiseaseId: int64(v.DiseaseId),
|
|
|
+ DiseaseName: v.DiseaseName,
|
|
|
+ TreatmentResult: pasturePb.TreatmentResult_Curable,
|
|
|
+ OperationId: operationUser.Id,
|
|
|
+ OperationName: operationUser.Name,
|
|
|
+ Remarks: req.Remarks,
|
|
|
+ TreatmentAt: int64(req.CurableAt),
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ if len(eventCowTreatmentList) <= 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
- if err = tx.Model(model.EventCowDisease{}).Where("id IN ?", req.Ids).
|
|
|
- Where("health_status = ?", pasturePb.HealthStatus_Treatment).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "health_status": pasturePb.HealthStatus_Curable,
|
|
|
- "diagnosed_result": pasturePb.IsShow_Ok,
|
|
|
- "curable_at": req.CurableAt,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
- }
|
|
|
- if err = tx.Model(model.Cow{}).
|
|
|
- Where("id IN ?", req.Ids).
|
|
|
- Updates(map[string]interface{}{
|
|
|
- "health_status": pasturePb.HealthStatus_Curable,
|
|
|
- }).Error; err != nil {
|
|
|
- return xerr.WithStack(err)
|
|
|
+ cow := &model.Cow{}
|
|
|
+ for _, eventCowDisease := range eventCowDiseaseList {
|
|
|
+ eventCowDisease.EventCurableUpdate(int64(req.CurableAt))
|
|
|
+ if err = tx.Model(eventCowDisease).
|
|
|
+ Select("health_status,diagnosed_result,curable_at").
|
|
|
+ Where("id = ?", eventCowDisease.Id).
|
|
|
+ Where("health_status = ?", pasturePb.HealthStatus_Treatment).
|
|
|
+ Updates(eventCowDisease).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ cow, err = s.GetCowInfoByCowId(ctx, eventCowDisease.CowId)
|
|
|
+ if err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ // 更新牛只健康状态
|
|
|
+ cow.EventDiseaseUpdate(pasturePb.HealthStatus_Curable)
|
|
|
+ if err = tx.Model(cow).
|
|
|
+ Select("health_status").
|
|
|
+ Where("id = ?", eventCowDisease.CowId).
|
|
|
+ Updates(cow).Error; err != nil {
|
|
|
+ return xerr.WithStack(err)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if err = tx.Model(model.EventCowTreatment{}).Create(eventCowTreatmentList).Error; err != nil {
|
|
|
+ if err = tx.Model(new(model.EventCowTreatment)).Create(eventCowTreatmentList).Error; err != nil {
|
|
|
return xerr.WithStack(err)
|
|
|
}
|
|
|
return nil
|