123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- package backend
- import (
- "context"
- "kpt-pasture/model"
- "net/http"
- "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.EventCowDisease) 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)
- }
- req.OperationName = systemUser.Name
- disease, err := s.GetDiseaseById(ctx, req.DiseaseId)
- if err != nil {
- return xerr.WithStack(err)
- }
- req.DiseaseName = disease.Name
- newEventCowDisease := model.NewEventCowDisease(cow, req)
- if err = s.DB.Transaction(func(tx *gorm.DB) error {
- if err = tx.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 {
- 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)).
- 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))
- 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
- }
|