|
@@ -2,11 +2,9 @@ package backend
|
|
|
|
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
- "errors"
|
|
|
|
"fmt"
|
|
"fmt"
|
|
"kpt-pasture/model"
|
|
"kpt-pasture/model"
|
|
"net/http"
|
|
"net/http"
|
|
- "strings"
|
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"gitee.com/xuyiping_admin/pkg/logger/zaplog"
|
|
"gitee.com/xuyiping_admin/pkg/logger/zaplog"
|
|
@@ -18,14 +16,75 @@ import (
|
|
"gitee.com/xuyiping_admin/pkg/xerr"
|
|
"gitee.com/xuyiping_admin/pkg/xerr"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+// CowDiseaseList 发病牛只清单
|
|
|
|
+func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error) {
|
|
|
|
+ userModel, err := s.GetUserModel(ctx)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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).
|
|
|
|
+ Where("a.health_status != ?", pasturePb.HealthStatus_Curable)
|
|
|
|
+
|
|
|
|
+ if len(req.CowIds) > 0 {
|
|
|
|
+ pref.Where("a.cow_id IN ?", req.CowIds)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if req.EarNumber != "" {
|
|
|
|
+ pref.Where("a.ear_number = ?", req.EarNumber)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if req.DiseaseId > 0 {
|
|
|
|
+ pref.Where("a.disease_id = ?", req.DiseaseId)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if req.PenId > 0 {
|
|
|
|
+ pref.Where("b.pen_id = ?", req.PenId)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if req.HealthStatus > 0 {
|
|
|
|
+ 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)).
|
|
|
|
+ Find(&cowDiseaseList).Error; err != nil {
|
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ healthStatusMap := s.HealthStatusMap()
|
|
|
|
+ return &pasturePb.EventCowDiseaseResponse{
|
|
|
|
+ Code: http.StatusOK,
|
|
|
|
+ Msg: "ok",
|
|
|
|
+ Data: &pasturePb.EventCowDiseaseData{
|
|
|
|
+ List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(healthStatusMap),
|
|
|
|
+ Total: int32(count),
|
|
|
|
+ PageSize: pagination.PageSize,
|
|
|
|
+ Page: pagination.Page,
|
|
|
|
+ },
|
|
|
|
+ }, nil
|
|
|
|
+}
|
|
|
|
+
|
|
// CowDiseaseCreate 牛只发病提交
|
|
// CowDiseaseCreate 牛只发病提交
|
|
func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDiseaseRequest, source string) error {
|
|
func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDiseaseRequest, source string) error {
|
|
userModel, err := s.GetUserModel(ctx)
|
|
userModel, err := s.GetUserModel(ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
return xerr.WithStack(err)
|
|
return xerr.WithStack(err)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ pastureId := userModel.AppPasture.Id
|
|
// 牛只信息
|
|
// 牛只信息
|
|
- cow, err := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, req.EarNumber)
|
|
|
|
|
|
+ cow, err := s.GetCowInfoByEarNumber(ctx, pastureId, req.EarNumber)
|
|
if err != nil {
|
|
if err != nil {
|
|
return xerr.Customf("牛只信息错误: %d", req.CowId)
|
|
return xerr.Customf("牛只信息错误: %d", req.CowId)
|
|
}
|
|
}
|
|
@@ -35,14 +94,14 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
return xerr.Customf("请检查操作人信息")
|
|
return xerr.Customf("请检查操作人信息")
|
|
}
|
|
}
|
|
|
|
|
|
- disease, err := s.GetDiseaseById(ctx, userModel.AppPasture.Id, req.DiseaseId)
|
|
|
|
|
|
+ disease, err := s.GetDiseaseById(ctx, pastureId, req.DiseaseId)
|
|
if err != nil {
|
|
if err != nil {
|
|
return xerr.WithStack(err)
|
|
return xerr.WithStack(err)
|
|
}
|
|
}
|
|
- newEventCowDisease := model.NewEventCowDisease(userModel.AppPasture.Id, cow, disease, req, operationUser, userModel.SystemUser)
|
|
|
|
- if req.ExposeDiseaseType == pasturePb.ExposeDiseaseType_Neck_Ring {
|
|
|
|
- newEventCowDisease.ExposeDiseaseType = pasturePb.ExposeDiseaseType_Neck_Ring
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ // 牛只疾病信息
|
|
|
|
+ newEventCowDisease := model.NewEventCowDisease(pastureId, cow, disease, req, operationUser, userModel.SystemUser)
|
|
|
|
+
|
|
defer func() {
|
|
defer func() {
|
|
// 更新牛只健康状态
|
|
// 更新牛只健康状态
|
|
if newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Disease || newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Treatment {
|
|
if newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Disease || newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Treatment {
|
|
@@ -55,6 +114,7 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 更新栏舍信息
|
|
if req.PenId > 0 {
|
|
if req.PenId > 0 {
|
|
penMap := s.PenMap(ctx, userModel.AppPasture.Id)
|
|
penMap := s.PenMap(ctx, userModel.AppPasture.Id)
|
|
penData, ok := penMap[req.PenId]
|
|
penData, ok := penMap[req.PenId]
|
|
@@ -70,7 +130,7 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
}()
|
|
- // PC端直接跳过诊断过程
|
|
|
|
|
|
+ // PC端h和脖环揭发直接跳过诊断过程
|
|
if source == model.SourcePC || req.ExposeDiseaseType == pasturePb.ExposeDiseaseType_Neck_Ring {
|
|
if source == model.SourcePC || req.ExposeDiseaseType == pasturePb.ExposeDiseaseType_Neck_Ring {
|
|
newEventCowDisease.DiagnosedResult = pasturePb.IsShow_Ok
|
|
newEventCowDisease.DiagnosedResult = pasturePb.IsShow_Ok
|
|
newEventCowDisease.DiagnoseOperationId = int32(operationUser.Id)
|
|
newEventCowDisease.DiagnoseOperationId = int32(operationUser.Id)
|
|
@@ -90,31 +150,53 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
newEventCowDisease.DiagnoseName = disease.Name
|
|
newEventCowDisease.DiagnoseName = disease.Name
|
|
}
|
|
}
|
|
|
|
|
|
- newEventCowTreatment := &model.EventCowTreatment{}
|
|
|
|
- newCowTreatmentRequest := &pasturePb.CowTreatmentRequest{}
|
|
|
|
- var isCreatePrescription bool
|
|
|
|
- diseaseTypeMap := s.DiseaseTypeMap()
|
|
|
|
prescription := &model.Prescription{}
|
|
prescription := &model.Prescription{}
|
|
- if req.PrescriptionId > 0 || len(req.PrescriptionDetail) > 0 {
|
|
|
|
- isCreatePrescription = true
|
|
|
|
- if req.PrescriptionId > 0 {
|
|
|
|
- prescription, err = s.GetPrescriptionById(ctx, userModel.AppPasture.Id, req.PrescriptionId)
|
|
|
|
- if err != nil {
|
|
|
|
- return xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
|
|
+ prescriptionDetail := make([]*pasturePb.PrescriptionDrugsList, 0)
|
|
|
|
+ // 获取处方信息
|
|
|
|
+ if req.PrescriptionId > 0 && len(req.PrescriptionDetail) <= 0 {
|
|
|
|
+ prescription, err = s.GetPrescriptionById(ctx, pastureId, req.PrescriptionId)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return xerr.WithStack(err)
|
|
|
|
+ }
|
|
|
|
|
|
- prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, userModel.AppPasture.Id, prescription.Id)
|
|
|
|
- if err != nil {
|
|
|
|
- return xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
- req.PrescriptionDetail = model.PrescriptionDrugsSlice(prescriptionDrugs).ToPB()
|
|
|
|
|
|
+ prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, pastureId, prescription.Id)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return xerr.WithStack(err)
|
|
}
|
|
}
|
|
|
|
+ prescriptionDetail = model.PrescriptionDrugsSlice(prescriptionDrugs).ToPB()
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // 创建新的处方
|
|
|
|
+ if req.PrescriptionId <= 0 && len(req.PrescriptionDetail) > 0 {
|
|
|
|
+ newPrescriptionRequest := &pasturePb.PrescriptionRequest{
|
|
|
|
+ Name: fmt.Sprintf("%s-%s-%s", disease.Name, time.Now().Local().Format("20060102"), operationUser.Name),
|
|
|
|
+ ApplicableDiseaseIds: []int32{req.DiseaseId},
|
|
|
|
+ IsShow: pasturePb.IsShow_Ok,
|
|
|
|
+ }
|
|
|
|
+ prescription = model.NewPrescription(
|
|
|
|
+ pastureId, newPrescriptionRequest, fmt.Sprintf("%d", disease.Id),
|
|
|
|
+ 1, 0, 0, userModel.SystemUser,
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ if err = s.DB.Model(new(model.Prescription)).
|
|
|
|
+ Create(prescription).Error; err != nil {
|
|
|
|
+ zaplog.Error("CowDiseaseCreate", zap.Any("err", err), zap.Any("prescription", prescription))
|
|
|
|
+ return xerr.Customf("创建处方错误: %s", err.Error())
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ newPrescriptionDrugs := model.NewPrescriptionDrugs(pastureId, prescription.Id, req.PrescriptionDetail)
|
|
|
|
+ if err = s.DB.Model(new(model.PrescriptionDrugs)).
|
|
|
|
+ Create(newPrescriptionDrugs).Error; err != nil {
|
|
|
|
+ return xerr.WithStack(err)
|
|
|
|
+ }
|
|
|
|
+ prescriptionDetail = model.PrescriptionDrugsSlice(newPrescriptionDrugs).ToPB()
|
|
|
|
+ }
|
|
|
|
+
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
lastPrescriptionName := ""
|
|
lastPrescriptionName := ""
|
|
- // 已有的处方使用次数+1
|
|
|
|
- if req.PrescriptionId > 0 {
|
|
|
|
- prescription.EventUseCountUpdate()
|
|
|
|
|
|
+ // 1. 更新处方使用次数
|
|
|
|
+ if prescription.Id > 0 {
|
|
|
|
+ prescription.EventUseCountUpdate() // 处方使用次数+1
|
|
if err = tx.Model(new(model.Prescription)).
|
|
if err = tx.Model(new(model.Prescription)).
|
|
Select("use_count").
|
|
Select("use_count").
|
|
Where("id = ?", prescription.Id).
|
|
Where("id = ?", prescription.Id).
|
|
@@ -122,65 +204,43 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
return xerr.WithStack(err)
|
|
return xerr.WithStack(err)
|
|
}
|
|
}
|
|
lastPrescriptionName = prescription.Name
|
|
lastPrescriptionName = prescription.Name
|
|
- }
|
|
|
|
- // 新的临时处方
|
|
|
|
- if req.PrescriptionId <= 0 && len(req.PrescriptionDetail) > 0 {
|
|
|
|
- newPrescriptionRequest := &pasturePb.PrescriptionRequest{
|
|
|
|
- Name: fmt.Sprintf("%s-%s-%s", disease.Name, time.Now().Local().Format("20060102"), operationUser.Name),
|
|
|
|
- ApplicableDiseaseIds: []int32{req.DiseaseId},
|
|
|
|
- IsShow: pasturePb.IsShow_Ok,
|
|
|
|
- }
|
|
|
|
- newPrescription := model.NewPrescription(
|
|
|
|
- userModel.AppPasture.Id,
|
|
|
|
- newPrescriptionRequest,
|
|
|
|
- fmt.Sprintf("%d", disease.Id),
|
|
|
|
- 1,
|
|
|
|
- 0,
|
|
|
|
- 0,
|
|
|
|
- userModel.SystemUser,
|
|
|
|
- )
|
|
|
|
- newPrescription.UseCount += 1
|
|
|
|
- if err = tx.Model(new(model.Prescription)).Create(newPrescription).Error; err != nil {
|
|
|
|
- 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)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// 记录事件日志
|
|
// 记录事件日志
|
|
- cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Disease, newEventCowDisease)
|
|
|
|
|
|
+ cowLogs := s.SubmitEventLog(ctx, pastureId, cow, pasturePb.EventType_Disease, newEventCowDisease)
|
|
if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
|
|
if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
|
|
return xerr.WithStack(err)
|
|
return xerr.WithStack(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- // 创建 CowDisease
|
|
|
|
|
|
+ // 2. 创建牛只疾病信息
|
|
newEventCowDisease.LastPrescriptionName = lastPrescriptionName
|
|
newEventCowDisease.LastPrescriptionName = lastPrescriptionName
|
|
- if err = tx.Model(new(model.EventCowDisease)).Create(newEventCowDisease).Error; err != nil {
|
|
|
|
|
|
+ if err = tx.Model(new(model.EventCowDisease)).
|
|
|
|
+ Create(newEventCowDisease).Error; err != nil {
|
|
return xerr.WithStack(err)
|
|
return xerr.WithStack(err)
|
|
}
|
|
}
|
|
- // 创建治疗记录
|
|
|
|
- if isCreatePrescription {
|
|
|
|
- newCowTreatmentRequest = &pasturePb.CowTreatmentRequest{
|
|
|
|
|
|
+ // 3. 创建治疗记录
|
|
|
|
+ if len(prescriptionDetail) > 0 {
|
|
|
|
+ diseaseTypeMap := s.DiseaseTypeMap()
|
|
|
|
+ newCowTreatmentRequest := &pasturePb.CowTreatmentRequest{
|
|
CowId: req.CowId,
|
|
CowId: req.CowId,
|
|
PrescriptionId: prescription.Id,
|
|
PrescriptionId: prescription.Id,
|
|
- DiseaseId: req.DiseaseId,
|
|
|
|
|
|
+ DiseaseId: int32(disease.Id),
|
|
DiseaseName: disease.Name,
|
|
DiseaseName: disease.Name,
|
|
DiseaseType: disease.DiseaseType,
|
|
DiseaseType: disease.DiseaseType,
|
|
- PrescriptionDetail: req.PrescriptionDetail,
|
|
|
|
|
|
+ PrescriptionDetail: prescriptionDetail,
|
|
TreatmentResult: pasturePb.TreatmentResult_GoOn,
|
|
TreatmentResult: pasturePb.TreatmentResult_GoOn,
|
|
Remarks: req.Remarks,
|
|
Remarks: req.Remarks,
|
|
TreatmentAt: req.DiseaseAt,
|
|
TreatmentAt: req.DiseaseAt,
|
|
}
|
|
}
|
|
- newEventCowTreatment = model.NewEventCowTreatment(userModel.AppPasture.Id, prescription, newCowTreatmentRequest, diseaseTypeMap, operationUser, userModel.SystemUser)
|
|
|
|
|
|
+ newEventCowTreatment := model.NewEventCowTreatment(pastureId, prescription, newCowTreatmentRequest, diseaseTypeMap, operationUser, userModel.SystemUser)
|
|
newEventCowTreatment.CowDiseaseId = newEventCowDisease.Id
|
|
newEventCowTreatment.CowDiseaseId = newEventCowDisease.Id
|
|
|
|
+ if err = tx.Model(new(model.EventCowTreatment)).
|
|
|
|
+ Create(newEventCowTreatment).Error; err != nil {
|
|
|
|
+ return xerr.WithStack(err)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- // 创建治疗记录
|
|
|
|
- if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).Error; err != nil {
|
|
|
|
|
|
+ // 4. 如果是脖环揭发
|
|
|
|
+ if req.ExposeDiseaseType == pasturePb.ExposeDiseaseType_Neck_Ring {
|
|
|
|
+ if err = s.NeckRingUpdateHealth(ctx, pastureId, cow.Id, operationUser); err != nil {
|
|
return xerr.WithStack(err)
|
|
return xerr.WithStack(err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -191,65 +251,6 @@ func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventC
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-// CowDiseaseList 发病牛只清单
|
|
|
|
-func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error) {
|
|
|
|
- userModel, err := s.GetUserModel(ctx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- 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).
|
|
|
|
- Where("a.health_status != ?", pasturePb.HealthStatus_Curable)
|
|
|
|
-
|
|
|
|
- if len(req.CowIds) > 0 {
|
|
|
|
- pref.Where("a.cow_id IN ?", req.CowIds)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if req.EarNumber != "" {
|
|
|
|
- pref.Where("a.ear_number = ?", req.EarNumber)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if req.DiseaseId > 0 {
|
|
|
|
- pref.Where("a.disease_id = ?", req.DiseaseId)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if req.PenId > 0 {
|
|
|
|
- pref.Where("b.pen_id = ?", req.PenId)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if req.HealthStatus > 0 {
|
|
|
|
- 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)).
|
|
|
|
- Find(&cowDiseaseList).Error; err != nil {
|
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- healthStatusMap := s.HealthStatusMap()
|
|
|
|
- return &pasturePb.EventCowDiseaseResponse{
|
|
|
|
- Code: http.StatusOK,
|
|
|
|
- Msg: "ok",
|
|
|
|
- Data: &pasturePb.EventCowDiseaseData{
|
|
|
|
- List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(healthStatusMap),
|
|
|
|
- Total: int32(count),
|
|
|
|
- PageSize: pagination.PageSize,
|
|
|
|
- Page: pagination.Page,
|
|
|
|
- },
|
|
|
|
- }, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// CowDiseaseDiagnose 发病牛只诊断
|
|
// CowDiseaseDiagnose 发病牛只诊断
|
|
func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowDiagnosedRequest) error {
|
|
func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowDiagnosedRequest) error {
|
|
userModel, err := s.GetUserModel(ctx)
|
|
userModel, err := s.GetUserModel(ctx)
|
|
@@ -447,192 +448,3 @@ func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.Cow
|
|
|
|
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
-
|
|
|
|
-func (s *StoreEntry) DiseaseSuggestPrescription(ctx context.Context, diseaseId int64) (*pasturePb.ConfigOptionsListResponse, error) {
|
|
|
|
- userModel, err := s.GetUserModel(ctx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- res := make([]*pasturePb.ConfigOptionsList, 0)
|
|
|
|
- prescriptionList := make([]*model.Prescription, 0)
|
|
|
|
- if err = s.DB.Model(new(model.Prescription)).
|
|
|
|
- Where("is_show = ?", pasturePb.IsShow_Ok).
|
|
|
|
- Where("pasture_id = ?", userModel.AppPasture.Id).
|
|
|
|
- Find(&prescriptionList).Error; err != nil {
|
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- for _, v := range prescriptionList {
|
|
|
|
- disabled := false
|
|
|
|
- if strings.Contains(v.ApplicableDisease, fmt.Sprintf("%d", diseaseId)) {
|
|
|
|
- disabled = true
|
|
|
|
- }
|
|
|
|
- res = append(res, &pasturePb.ConfigOptionsList{
|
|
|
|
- Value: v.Id,
|
|
|
|
- Label: v.Name,
|
|
|
|
- Disabled: disabled,
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return &pasturePb.ConfigOptionsListResponse{
|
|
|
|
- Code: http.StatusOK,
|
|
|
|
- Msg: "ok",
|
|
|
|
- Data: res,
|
|
|
|
- }, nil
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// CowDiseaseTreatmentDetail 发病牛只治疗详情列表
|
|
|
|
-func (s *StoreEntry) CowDiseaseTreatmentDetail(ctx context.Context, req *pasturePb.EventCowTreatmentDetailRequest,
|
|
|
|
- pagination *pasturePb.PaginationModel) (*pasturePb.EventCowTreatmentDetailResponse, error) {
|
|
|
|
- userModel, err := s.GetUserModel(ctx)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- eventCowDisease := &model.EventCowDisease{}
|
|
|
|
- var count int64 = 0
|
|
|
|
- pref := s.DB.Model(new(model.EventCowDisease)).
|
|
|
|
- Where("cow_id = ?", req.CowId).
|
|
|
|
- Where("pasture_id = ?", userModel.AppPasture.Id).
|
|
|
|
- Where("id = ?", req.Id)
|
|
|
|
-
|
|
|
|
- if req.DiseaseId > 0 {
|
|
|
|
- pref.Where("disease_id = ?", req.DiseaseId)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if req.DiseaseStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseStartAt <= req.DiseaseEndAt {
|
|
|
|
- pref.Where("disease_at BETWEEN ? AND ?", req.DiseaseStartAt, req.DiseaseEndAt)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if err = pref.Count(&count).
|
|
|
|
- Limit(int(pagination.PageSize)).
|
|
|
|
- Offset(int(pagination.PageOffset)).
|
|
|
|
- Order("id desc").
|
|
|
|
- First(&eventCowDisease).Error; err != nil {
|
|
|
|
- if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
- return &pasturePb.EventCowTreatmentDetailResponse{
|
|
|
|
- Code: http.StatusOK,
|
|
|
|
- Msg: "ok",
|
|
|
|
- Data: &pasturePb.EventCowTreatmentDetail{
|
|
|
|
- List: make([]*pasturePb.EventCowTreatment, 0),
|
|
|
|
- Total: 0,
|
|
|
|
- PageSize: pagination.PageSize,
|
|
|
|
- Page: pagination.Page,
|
|
|
|
- },
|
|
|
|
- }, nil
|
|
|
|
- } else {
|
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- eventCowTreatmentList := make([]*model.EventCowTreatment, 0)
|
|
|
|
- if err = s.DB.Model(new(model.EventCowTreatment)).
|
|
|
|
- Where("cow_disease_id = ?", req.Id).
|
|
|
|
- Where("cow_id = ?", req.CowId).
|
|
|
|
- Order("id desc").
|
|
|
|
- Find(&eventCowTreatmentList).Error; err != nil {
|
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return &pasturePb.EventCowTreatmentDetailResponse{
|
|
|
|
- Code: http.StatusOK,
|
|
|
|
- Msg: "ok",
|
|
|
|
- Data: &pasturePb.EventCowTreatmentDetail{
|
|
|
|
- List: model.EventCowTreatmentSlice(eventCowTreatmentList).ToPB(eventCowDisease),
|
|
|
|
- Total: int32(count),
|
|
|
|
- PageSize: pagination.Page,
|
|
|
|
- Page: pagination.PageSize,
|
|
|
|
- },
|
|
|
|
- }, nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (s *StoreEntry) CowDiseaseCurable(ctx context.Context, req *pasturePb.EventCowCurableRequest) error {
|
|
|
|
- userModel, err := s.GetUserModel(ctx)
|
|
|
|
- if err != nil {
|
|
|
|
- return xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- eventCowDiseaseList := make([]*model.EventCowDisease, 0)
|
|
|
|
- if err = s.DB.Where("id IN ?", req.Ids).
|
|
|
|
- Where("health_status = ?", pasturePb.HealthStatus_Treatment).
|
|
|
|
- Where("pasture_id = ?", userModel.AppPasture.Id).
|
|
|
|
- Find(&eventCowDiseaseList).Error; err != nil {
|
|
|
|
- zaplog.Error("GetEventCowDiseaseList", zap.Any("err", err), zap.Any("req", req))
|
|
|
|
- return xerr.Custom("异常数据")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if len(eventCowDiseaseList) == 0 {
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
|
|
|
|
- if err != nil {
|
|
|
|
- return xerr.Customf("该用户不存在: %d", req.OperationId)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- eventCowTreatmentList := make([]*model.EventCowTreatment, 0)
|
|
|
|
- for _, v := range eventCowDiseaseList {
|
|
|
|
- newEventCowTreatment := model.NewEventCowCurableTreatment(userModel.AppPasture.Id, userModel.SystemUser, operationUser, v, req.Remarks, int64(req.CurableAt))
|
|
|
|
- eventCowTreatmentList = append(eventCowTreatmentList, newEventCowTreatment)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if len(eventCowTreatmentList) <= 0 {
|
|
|
|
- return nil
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
|
- 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, userModel.AppPasture.Id, eventCowDisease.CowId)
|
|
|
|
- if err != nil {
|
|
|
|
- return xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
- // 更新牛只健康状态
|
|
|
|
- cow.EventHealthStatusUpdate(pasturePb.HealthStatus_Curable)
|
|
|
|
- if err = tx.Model(cow).
|
|
|
|
- Select("health_status").
|
|
|
|
- Where("id = ?", eventCowDisease.CowId).
|
|
|
|
- Updates(cow).Error; err != nil {
|
|
|
|
- return xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 更新发病事件日志
|
|
|
|
- cowLogs := &model.EventCowLog{CowId: cow.Id}
|
|
|
|
- curableAtFormat := ""
|
|
|
|
- if eventCowDisease.CurableAt > 0 {
|
|
|
|
- curableAtFormat = time.Unix(eventCowDisease.CurableAt, 0).Local().Format(model.LayoutDate2)
|
|
|
|
- }
|
|
|
|
- curableAtParse := time.Unix(eventCowDisease.CurableAt, 0).Local()
|
|
|
|
- diseaseAtParse := time.Unix(eventCowDisease.DiseaseAt, 0).Local()
|
|
|
|
- curableDays := int64(curableAtParse.Sub(diseaseAtParse).Hours() / 24)
|
|
|
|
-
|
|
|
|
- if err = tx.Table(cowLogs.TableName()).
|
|
|
|
- Where("event_type = ?", pasturePb.EventType_Disease).
|
|
|
|
- Where("event_at = ?", eventCowDisease.DiseaseAt).
|
|
|
|
- Update("event_description", fmt.Sprintf("疾病名称: %s; 状态: 已治愈; 治愈时间: %s; 治疗天数: %d;", eventCowDisease.DiseaseName, curableAtFormat, curableDays)).
|
|
|
|
- Error; err != nil {
|
|
|
|
- return xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if err = tx.Model(new(model.EventCowTreatment)).Create(eventCowTreatmentList).Error; err != nil {
|
|
|
|
- return xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return nil
|
|
|
|
- }); err != nil {
|
|
|
|
- return xerr.WithStack(err)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return nil
|
|
|
|
-}
|
|
|