feat(drug): 设计新增药品领域实体 #12
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
yingmo/PharmacyERP!12
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/drug/entity"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Pull request overview
This PR introduces a new Drug domain entity in the
internal/drugmodule to encapsulate drug business rules and provide conversion helpers between the domain layer and the GORM persistence model.Changes:
internal/drug/entity/Drugdomain entity with basic business methods (IsEnabled,NeedsAudit,ToggleStatus).FromModel,ToModel) formodel.DrugInfo.Validate().💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
@ -0,0 +23,4 @@}// FromModel 从 GORM 数据库模型构造药品领域实体。func FromModel(m *model.DrugInfo) *Drug {FromModelwill panic if called with a nil*model.DrugInfo(dereferencesmwithout checking). Since this is an exported constructor, consider guarding against nil and returning nil (or an error) to make the conversion safe for callers.@ -0,0 +42,4 @@// ToModel 将领域实体转换为 GORM 数据库模型,供仓储层写入使用。// ID 为零时表示新建,GORM 将自动填充自增主键。func (d *Drug) ToModel() *model.DrugInfo {return &model.DrugInfo{ToModel’s comment says ID=0 indicates create, but the returnedmodel.DrugInfonever setsIDfrom the entity. If this method is used for updates, GORM will treat it as a new record. Set the ID on the model (and align the comment accordingly).@ -0,0 +53,4 @@IsPrescription: d.IsPrescription,IsMedicare: d.IsMedicare,Status: d.Status,}ToModelalways setsStatusfrom the entity. Becauseint8zero value is 0 (disabled) but the DB model hasgorm:"default:1", creating a newDrugwithout explicitly settingStatuswill override the DB default and insert a disabled drug. Consider initializing status explicitly in the domain layer (e.g., constructor/default) or representing “unset” status so DB defaults can apply.@ -0,0 +72,4 @@d.Status = model.DrugStatusDisabled} else {d.Status = model.DrugStatusEnabled}ToggleStatustreats any non-"enabled" status as "disabled" and flips it to enabled. IfStatusis ever an unexpected value (e.g., data corruption or future added statuses), this will silently “correct” it and can hide errors. Consider validatingStatusis one of the known constants and either no-op or return an error on invalid values (which may require changing the method signature).@ -0,0 +87,4 @@if strings.TrimSpace(d.Specification) == "" {return ecode.ErrParamInvalid}if strings.TrimSpace(d.Manufacturer) == "" {Validateusesstrings.TrimSpaceonly for checks, but it doesn’t normalize the fields before persistence. This allows values like"ABC "to be stored (and possibly bypass uniqueness/lookup expectations). Consider trimming and assigning the normalized values (e.g., forDrugCode,CommonName,Specification,Manufacturer) as part of validation/normalization.