123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- package backend
- import (
- "context"
- "kpt-pasture/model"
- "net/http"
- "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"
- "gitee.com/xuyiping_admin/pkg/xerr"
- )
- // CowDiseaseCreate 牛只发病提交
- func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDiseaseRequest) error {
- cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
- if err != nil {
- return xerr.WithStack(err)
- }
- systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
- if err != nil {
- return xerr.WithStack(err)
- }
- disease, err := s.GetDiseaseById(ctx, req.DiseaseId)
- 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, systemUser)
- if prescription.Id > 0 {
- newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Treatment
- }
- 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)
- }
- // 更新牛舍
- if req.PenId > 0 {
- if err = tx.Model(new(model.Cow)).
- Where("id = ?", req.CowId).
- Updates(map[string]interface{}{
- "pen_id": req.PenId,
- }).Error; err != nil {
- zaplog.Error("CowDiseaseCreate", zap.Any("UpdatePen", err), zap.Any("req", req))
- }
- }
- // 如果有处方Id,则创建治疗记录
- 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,
- UseNum: float32(v.Dosages),
- }
- }
- newEventCowTreatment := model.NewEventCowTreatment(prescription, &pasturePb.CowTreatmentRequest{
- CowId: req.CowId,
- PrescriptionId: prescription.Id,
- DiseaseId: req.DiseaseId,
- DiseaseName: disease.Name,
- PrescriptionDetail: prescriptionDetail,
- TreatmentResult: pasturePb.TreatmentResult_GoOn,
- Remarks: "",
- TreatmentAt: req.DiseaseAt,
- }, systemUser)
- // 创建治疗记录
- if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).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
- }
- // 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)).
- Where("diagnosed_result < ?", pasturePb.IsShow_No)
- if len(req.CowIds) > 0 {
- pref.Where("cow_id IN ?", req.CowIds)
- }
- if req.DiseaseId > 0 {
- pref.Where("disease_id = ?", req.DiseaseId)
- }
- if req.PenId > 0 {
- pref.Where("pen_id = ?", req.PenId)
- }
- if req.Lact > 0 {
- pref.Where("lact = ?", req.Lact)
- }
- if req.MinDayAge > 0 && req.MaxDayAge > 0 && req.MaxDayAge >= req.MinDayAge {
- pref.Where("day_age BETWEEN ? AND ?", req.MinDayAge, req.MaxDayAge)
- }
- if req.HealthStatus > 0 {
- pref.Where("health_status = ?", req.HealthStatus)
- }
- if err := pref.Order("id desc").
- Count(&count).Limit(int(pagination.PageSize)).
- Offset(int(pagination.PageOffset)).
- Order("id desc").
- Find(&cowDiseaseList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- healthStatusMap := s.HealthStatusMap()
- return &pasturePb.EventCowDiseaseResponse{
- Code: http.StatusOK,
- Message: "ok",
- Data: &pasturePb.EventCowDiseaseData{
- List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(healthStatusMap),
- Total: int32(count),
- PageSize: pagination.PageSize,
- Page: pagination.Page,
- },
- }, nil
- }
- // 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))
- prescription, err := s.GetPrescriptionById(ctx, req.PrescriptionId)
- if err != nil {
- return xerr.WithStack(err)
- }
- disease, err := s.GetDiseaseById(ctx, eventCowDisease.DiagnoseId)
- if err != nil {
- return xerr.WithStack(err)
- }
- req.DiseaseName = disease.Name
- 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,
- UseNum: float32(v.Dosages),
- }
- }
- req.PrescriptionDetail = prescriptionDetail
- 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(prescription, 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
- }
- func (s *StoreEntry) CowDiseaseTreatmentDetail(ctx context.Context, req *pasturePb.EventCowTreatmentDetailRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowTreatmentDetailResponse, error) {
- eventCowDiseaseList := make([]*model.EventCowDisease, 0)
- var count int64 = 0
- pref := s.DB.Where("cow_id = ?", req.CowId)
- 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").
- Find(&eventCowDiseaseList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- eventCowTreatmentList := make([]*pasturePb.EventCowTreatment, 0)
- cowDiseaseIds := make([]int64, len(eventCowDiseaseList))
- for i, v := range eventCowDiseaseList {
- cowDiseaseIds[i] = v.Id
- }
- if err := s.DB.Where("cow_disease_id IN ?", cowDiseaseIds).
- Where("cow_id = ?", req.CowId).
- Group("cow_disease_id").
- Order("id desc").
- Find(&eventCowTreatmentList).Error; err != nil {
- return nil, xerr.WithStack(err)
- }
- return &pasturePb.EventCowTreatmentDetailResponse{
- Code: http.StatusOK,
- Message: "ok",
- Data: &pasturePb.EventCowTreatmentDetail{
- List: eventCowTreatmentList,
- Total: int32(count),
- PageSize: pagination.Page,
- Page: pagination.PageSize,
- },
- }, nil
- }
|