package model import ( "encoding/json" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "go.uber.org/zap" ) type Prescription struct { Id int64 `json:"id"` Name string `json:"name"` ApplicableDisease string `json:"applicable_disease"` UseDays int32 `json:"use_days"` UseCount int32 `json:"use_count"` MeatExpiredDays int32 `json:"meat_expired_days"` MilkExpiredDays int32 `json:"milk_expired_days"` IsShow pasturePb.IsShow_Kind `json:"is_show"` Remarks string `json:"remarks"` OperationId int64 `json:"operation_id"` OperationName string `json:"operation_name"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } func (p *Prescription) TableName() string { return "prescription" } type ApplicableDisease struct { DiseaseId int64 `json:"disease_id"` DiseaseName string `json:"disease_name"` } func NewPrescription(req *pasturePb.PrescriptionRequest, applicableDisease string, useDays, meatExpiredDays, milkExpiredDays int32, systemUser *SystemUser) *Prescription { return &Prescription{ Name: req.Name, ApplicableDisease: applicableDisease, UseDays: useDays, UseCount: 0, MeatExpiredDays: meatExpiredDays, MilkExpiredDays: milkExpiredDays, IsShow: req.IsShow, Remarks: req.Remarks, OperationId: systemUser.Id, OperationName: systemUser.Name, } } type PrescriptionSlice []*Prescription func (p PrescriptionSlice) ToPB(prescriptionDrugsList []*PrescriptionDrugs) []*pasturePb.SearchPrescriptionList { res := make([]*pasturePb.SearchPrescriptionList, len(p)) for i, v := range p { drugsList := make([]*pasturePb.PrescriptionDrugsList, 0) for _, d := range prescriptionDrugsList { if d.PrescriptionId != v.Id { continue } drugsList = append(drugsList, &pasturePb.PrescriptionDrugsList{ Id: int32(d.Id), DrugsId: int32(d.DrugsId), DrugsName: d.DrugsName, Unit: d.Unit, UnitName: d.UnitName, Dosages: d.Dosages, UseDays: d.UseDays, Specs: d.Specs, }) } applicableDiseaseList := make([]*ApplicableDisease, 0) if v.ApplicableDisease != "" { err := json.Unmarshal([]byte(v.ApplicableDisease), &applicableDiseaseList) if err != nil { zaplog.Error("PrescriptionSliceToPB", zap.Any("err", err)) } } applicableDiseaseNames := make([]string, 0) applicableDiseaseIds := make([]int32, 0) for _, a := range applicableDiseaseList { applicableDiseaseNames = append(applicableDiseaseNames, a.DiseaseName) applicableDiseaseIds = append(applicableDiseaseIds, int32(a.DiseaseId)) } res[i] = &pasturePb.SearchPrescriptionList{ Id: int32(v.Id), Name: v.Name, ApplicableDisease: v.ApplicableDisease, ApplicableDiseaseIds: applicableDiseaseIds, ApplicableDiseaseNames: applicableDiseaseNames, UseDays: v.UseDays, UseCount: v.UseCount, MeatExpiredDays: v.MeatExpiredDays, MilkExpiredDays: v.MilkExpiredDays, IsShow: v.IsShow, Remarks: v.Remarks, OperationId: int32(v.OperationId), OperationName: v.OperationName, CreatedAt: int32(v.CreatedAt), UpdatedAt: int32(v.UpdatedAt), DrugsList: drugsList, } } return res }