'decimal:2', 'sort' => 'integer', 'status' => 'integer', 'created_at' => 'datetime:Y-m-d H:i:s', 'updated_at' => 'datetime:Y-m-d H:i:s', ]; protected $appends = ['icon_url']; /** * 关联图标 */ public function icon(): HasOne { return $this->hasOne(SysFileModel::class, 'id', 'icon_id'); } // 图标链接 public function getIconUrlAttribute() { if($this->icon) { return $this->icon->preview_url; } return null; } /** * 该等级下的门店 */ public function stores(): HasMany { return $this->hasMany(StoreModel::class, 'level_id', 'id'); } /** * 按等级上浮比例计算售价(统一换算入口,金额走 bcmath 保证两位小数精度) * * 售价 = 成本价 × (100 + percent) / 100(四舍五入保留两位)。 * * @param string|int|float $costPrice 商品成本价(decimal cast 字符串) * @param string|int|float $percent 价格上浮比例(30 = 上浮 30%) * @return string 两位小数字符串,如 '13.05' */ public static function calcLevelPrice(string|int|float $costPrice, string|int|float $percent): string { $multiplier = bcadd('100', (string) $percent, 4); return bcdiv(bcmul((string) $costPrice, $multiplier, 4), '100', 2); } }