feat(drug): 设计新增药品领域实体 #12

Merged
YingmoY merged 1 commit from feat/drug/entity into main 2026-04-13 08:54:18 +08:00
YingmoY commented 2026-04-13 08:54:04 +08:00 (Migrated from github.com)
No description provided.
copilot-pull-request-reviewer[bot] (Migrated from github.com) reviewed 2026-04-13 08:57:09 +08:00
copilot-pull-request-reviewer[bot] (Migrated from github.com) left a comment

Pull request overview

This PR introduces a new Drug domain entity in the internal/drug module to encapsulate drug business rules and provide conversion helpers between the domain layer and the GORM persistence model.

Changes:

  • Added internal/drug/entity/Drug domain entity with basic business methods (IsEnabled, NeedsAudit, ToggleStatus).
  • Implemented model conversion helpers (FromModel, ToModel) for model.DrugInfo.
  • Added basic required-field validation via Validate().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

## Pull request overview This PR introduces a new **Drug domain entity** in the `internal/drug` module to encapsulate drug business rules and provide conversion helpers between the domain layer and the GORM persistence model. **Changes:** - Added `internal/drug/entity/Drug` domain entity with basic business methods (`IsEnabled`, `NeedsAudit`, `ToggleStatus`). - Implemented model conversion helpers (`FromModel`, `ToModel`) for `model.DrugInfo`. - Added basic required-field validation via `Validate()`. --- 💡 <a href="/YingmoY/PharmacyERP/new/main?filename=.github/instructions/*.instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add Copilot custom instructions</a> for smarter, more guided reviews. <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn how to get started</a>.
@ -0,0 +23,4 @@
}
// FromModel 从 GORM 数据库模型构造药品领域实体。
func FromModel(m *model.DrugInfo) *Drug {
copilot-pull-request-reviewer[bot] (Migrated from github.com) commented 2026-04-13 08:57:08 +08:00

FromModel will panic if called with a nil *model.DrugInfo (dereferences m without checking). Since this is an exported constructor, consider guarding against nil and returning nil (or an error) to make the conversion safe for callers.

func FromModel(m *model.DrugInfo) *Drug {
	if m == nil {
		return nil
	}
`FromModel` will panic if called with a nil `*model.DrugInfo` (dereferences `m` without checking). Since this is an exported constructor, consider guarding against nil and returning nil (or an error) to make the conversion safe for callers. ```suggestion func FromModel(m *model.DrugInfo) *Drug { if m == nil { return nil } ```
@ -0,0 +42,4 @@
// ToModel 将领域实体转换为 GORM 数据库模型,供仓储层写入使用。
// ID 为零时表示新建,GORM 将自动填充自增主键。
func (d *Drug) ToModel() *model.DrugInfo {
return &model.DrugInfo{
copilot-pull-request-reviewer[bot] (Migrated from github.com) commented 2026-04-13 08:57:09 +08:00

ToModel’s comment says ID=0 indicates create, but the returned model.DrugInfo never sets ID from 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).

// 会保留实体 ID;当 ID 为零时表示新建,GORM 将自动填充自增主键。
func (d *Drug) ToModel() *model.DrugInfo {
	return &model.DrugInfo{
		ID:             d.ID,
`ToModel`’s comment says ID=0 indicates create, but the returned `model.DrugInfo` never sets `ID` from 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). ```suggestion // 会保留实体 ID;当 ID 为零时表示新建,GORM 将自动填充自增主键。 func (d *Drug) ToModel() *model.DrugInfo { return &model.DrugInfo{ ID: d.ID, ```
@ -0,0 +53,4 @@
IsPrescription: d.IsPrescription,
IsMedicare: d.IsMedicare,
Status: d.Status,
}
copilot-pull-request-reviewer[bot] (Migrated from github.com) commented 2026-04-13 08:57:09 +08:00

ToModel always sets Status from the entity. Because int8 zero value is 0 (disabled) but the DB model has gorm:"default:1", creating a new Drug without explicitly setting Status will 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.

`ToModel` always sets `Status` from the entity. Because `int8` zero value is 0 (disabled) but the DB model has `gorm:"default:1"`, creating a new `Drug` without explicitly setting `Status` will 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
}
copilot-pull-request-reviewer[bot] (Migrated from github.com) commented 2026-04-13 08:57:09 +08:00

ToggleStatus treats any non-"enabled" status as "disabled" and flips it to enabled. If Status is ever an unexpected value (e.g., data corruption or future added statuses), this will silently “correct” it and can hide errors. Consider validating Status is one of the known constants and either no-op or return an error on invalid values (which may require changing the method signature).

`ToggleStatus` treats any non-"enabled" status as "disabled" and flips it to enabled. If `Status` is ever an unexpected value (e.g., data corruption or future added statuses), this will silently “correct” it and can hide errors. Consider validating `Status` is 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) == "" {
copilot-pull-request-reviewer[bot] (Migrated from github.com) commented 2026-04-13 08:57:09 +08:00

Validate uses strings.TrimSpace only 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., for DrugCode, CommonName, Specification, Manufacturer) as part of validation/normalization.

	d.DrugCode = strings.TrimSpace(d.DrugCode)
	d.CommonName = strings.TrimSpace(d.CommonName)
	d.Specification = strings.TrimSpace(d.Specification)
	d.Manufacturer = strings.TrimSpace(d.Manufacturer)

	if d.DrugCode == "" {
		return ecode.ErrParamInvalid
	}
	if d.CommonName == "" {
		return ecode.ErrParamInvalid
	}
	if d.Specification == "" {
		return ecode.ErrParamInvalid
	}
	if d.Manufacturer == "" {
`Validate` uses `strings.TrimSpace` only 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., for `DrugCode`, `CommonName`, `Specification`, `Manufacturer`) as part of validation/normalization. ```suggestion d.DrugCode = strings.TrimSpace(d.DrugCode) d.CommonName = strings.TrimSpace(d.CommonName) d.Specification = strings.TrimSpace(d.Specification) d.Manufacturer = strings.TrimSpace(d.Manufacturer) if d.DrugCode == "" { return ecode.ErrParamInvalid } if d.CommonName == "" { return ecode.ErrParamInvalid } if d.Specification == "" { return ecode.ErrParamInvalid } if d.Manufacturer == "" { ```
Sign in to join this conversation.
No description provided.