package model import ( "fmt" "strings" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) 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"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } func (p *Prescription) TableName() string { return "prescription" } func NewPrescription(req *pasturePb.PrescriptionRequest, useDays, meatExpiredDays, milkExpiredDays int32, operationId int64) *Prescription { applicableDisease := "" for _, v := range req.ApplicableDisease { applicableDisease += fmt.Sprintf("%d", v) + "|" } return &Prescription{ Name: req.Name, ApplicableDisease: applicableDisease, UseDays: useDays, UseCount: 0, MeatExpiredDays: meatExpiredDays, MilkExpiredDays: milkExpiredDays, IsShow: req.IsShow, Remarks: req.Remarks, OperationId: operationId, } } type PrescriptionSlice []*Prescription func (p PrescriptionSlice) ToPB(userList []*SystemUser, prescriptionDrugsList []*PrescriptionDrugs) []*pasturePb.SearchPrescriptionList { res := make([]*pasturePb.SearchPrescriptionList, len(p)) for i, v := range p { operationName := "" for _, u := range userList { if u.Id == v.OperationId { operationName = u.Name break } } 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), Unit: d.Unit, UnitName: d.UnitName, Dosages: d.Dosages, UseDays: d.UseDays, }) } res[i] = &pasturePb.SearchPrescriptionList{ Id: int32(v.Id), Name: v.Name, ApplicableDisease: strings.Split(v.ApplicableDisease, "|"), UseDays: v.UseDays, UseCount: v.UseCount, MeatExpiredDays: v.MeatExpiredDays, MilkExpiredDays: v.MilkExpiredDays, IsShow: v.IsShow, Remarks: v.Remarks, OperationId: int32(v.OperationId), OperationName: operationName, CreatedAt: int32(v.CreatedAt), UpdatedAt: int32(v.UpdatedAt), DrugsList: drugsList, } } return res }