| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace app\api\service;
- /**
- * 超节损奖罚倍率规则(需求 5.5.6 / 表 2)
- *
- * 纯函数实现,不依赖数据库,便于单元测试:
- * - 输入:目标合格率、实际合格率(均以 bp=0.01% 表示)、材料废数量、实际投料数量、是否存在索赔
- * - 输出:倍率(bp 与倍数两种表示)、命中档位、各中间比率、是否因索赔豁免等
- *
- * 规则:
- * 1. 投料 <= 0 或规则关闭 -> 1.0;
- * 2. 材料原因报废率 > 1.5% 且没有索赔 -> 1.0(需求 5.5.6 括号内例外,不加大奖罚);
- * 3. 其余按目标合格率分档,取超出/低于目标合格率的点数阈值与倍率;
- * 4. 实际合格率 - 目标合格率 达到阈值 -> 返回倍率,否则 1.0。
- */
- class SuperLossRewardRule
- {
- const DEFAULT_RULE_VERSION = 'v2';
- const BP_BASE = 10000;
- /**
- * 内置默认配置(与 application/extra/superloss.php 保持一致,作为兜底)
- */
- public static function defaults(): array
- {
- return [
- 'enabled' => true,
- 'rule_version' => self::DEFAULT_RULE_VERSION,
- 'material_threshold_bp' => 150,
- 'claim_exempt' => true,
- 'trigger_inclusive' => true,
- 'material_denominator' => 'feed',
- 'tiers' => [
- ['min_target_rate_bp' => 9700, 'high_bp' => 150, 'low_bp' => 200, 'multiplier_bp' => 130, 'tier' => '>=97'],
- ['min_target_rate_bp' => 9500, 'high_bp' => 200, 'low_bp' => 250, 'multiplier_bp' => 130, 'tier' => '95-97'],
- ['min_target_rate_bp' => 9000, 'high_bp' => 300, 'low_bp' => 300, 'multiplier_bp' => 130, 'tier' => '90-95'],
- ['min_target_rate_bp' => 8000, 'high_bp' => 400, 'low_bp' => 400, 'multiplier_bp' => 160, 'tier' => '80-90'],
- ['min_target_rate_bp' => 0, 'high_bp' => 500, 'low_bp' => 500, 'multiplier_bp' => 160, 'tier' => '<=80'],
- ],
- ];
- }
- /**
- * 读取配置(框架外调用时返回默认值)
- */
- public static function config(): array
- {
- $conf = [];
- if (function_exists('config')) {
- try {
- $conf = config('superloss');
- } catch (\Throwable $e) {
- $conf = [];
- }
- }
- return self::normalize(is_array($conf) ? $conf : []);
- }
- /**
- * 合并默认值并做类型规整
- */
- public static function normalize(array $conf): array
- {
- $defaults = self::defaults();
- $conf = array_merge($defaults, $conf);
- $conf['enabled'] = !empty($conf['enabled']);
- $conf['claim_exempt'] = !empty($conf['claim_exempt']);
- $conf['trigger_inclusive'] = !empty($conf['trigger_inclusive']);
- $conf['material_threshold_bp'] = (int)$conf['material_threshold_bp'];
- $conf['rule_version'] = (string)$conf['rule_version'];
- $conf['material_denominator'] = (string)$conf['material_denominator'];
- $tiers = is_array($conf['tiers']) ? $conf['tiers'] : [];
- $normalized = [];
- foreach ($tiers as $tier) {
- if (!is_array($tier)) {
- continue;
- }
- $normalized[] = [
- 'min_target_rate_bp' => (int)($tier['min_target_rate_bp'] ?? 0),
- 'high_bp' => (int)($tier['high_bp'] ?? 0),
- 'low_bp' => (int)($tier['low_bp'] ?? 0),
- 'multiplier_bp' => (int)($tier['multiplier_bp'] ?? 100),
- 'tier' => (string)($tier['tier'] ?? ''),
- ];
- }
- usort($normalized, function ($a, $b) {
- return $b['min_target_rate_bp'] <=> $a['min_target_rate_bp'];
- });
- $conf['tiers'] = empty($normalized) ? $defaults['tiers'] : $normalized;
- return $conf;
- }
- /**
- * 计算奖罚倍率
- *
- * @param float|int $targetRateBp 目标合格率(bp)
- * @param float|int $realRateBp 实际合格率(bp)
- * @param float $materialWaste 材料原因报废数量(张)
- * @param float $feedQty 实际投料数量(张)
- * @param bool $hasClaim 是否存在材料索赔记录
- * @param array|null $conf 规则配置,null 时读取 application/extra/superloss.php
- * @return array
- */
- public static function resolve($targetRateBp, $realRateBp, float $materialWaste, float $feedQty, bool $hasClaim, ?array $conf = null): array
- {
- $conf = $conf === null ? self::config() : self::normalize($conf);
- $targetBp = (int)round((float)$targetRateBp);
- $realBp = (int)round((float)$realRateBp);
- $result = [
- 'multiplier_bp' => 100,
- 'multiplier_rate' => 1.0,
- 'multiplier' => 1.0,
- 'tier' => '',
- 'high_bp' => 0,
- 'low_bp' => 0,
- 'target_rate_bp' => $targetBp,
- 'real_rate_bp' => $realBp,
- 'material_waste_rate_bp' => 0,
- 'claim_exempt' => 0,
- 'hit' => 0,
- 'rule_version' => $conf['rule_version'],
- ];
- if (empty($conf['enabled']) || $feedQty <= 0) {
- return $result;
- }
- // 材料原因报废率(bp),分母固定为实际投料;除数已保证 > 0
- $materialRateBp = (int)round($materialWaste / $feedQty * self::BP_BASE);
- if ($materialRateBp < 0) {
- $materialRateBp = 0;
- }
- $result['material_waste_rate_bp'] = $materialRateBp;
- // 需求 5.5.6 例外:材料原因报废率超过 1.5% 且没有索赔 -> 不加大奖罚
- if (!empty($conf['claim_exempt'])
- && $materialRateBp > $conf['material_threshold_bp']
- && !$hasClaim
- ) {
- $result['claim_exempt'] = 1;
- return $result;
- }
- $tier = self::matchTier($targetBp, $conf['tiers']);
- if ($tier === null) {
- return $result;
- }
- $result['tier'] = $tier['tier'];
- $result['high_bp'] = $tier['high_bp'];
- $result['low_bp'] = $tier['low_bp'];
- $diff = $realBp - $targetBp;
- if (!empty($conf['trigger_inclusive'])) {
- $hit = ($diff >= $tier['high_bp']) || ($diff <= -$tier['low_bp']);
- } else {
- $hit = ($diff > $tier['high_bp']) || ($diff < -$tier['low_bp']);
- }
- if (!$hit) {
- return $result;
- }
- $result['multiplier_bp'] = $tier['multiplier_bp'];
- $result['multiplier_rate'] = $tier['multiplier_bp'] / 100;
- $result['multiplier'] = $result['multiplier_rate'];
- $result['hit'] = 1;
- return $result;
- }
- /**
- * 按目标合格率选档:目标 >= 档位下限即命中(端点包含)
- */
- public static function matchTier(int $targetBp, array $tiers): ?array
- {
- foreach ($tiers as $tier) {
- if ($targetBp >= (int)$tier['min_target_rate_bp']) {
- return $tier;
- }
- }
- return null;
- }
- /**
- * 把百分数(如 96.5)换算为 bp(9650)
- */
- public static function toBp($percent): int
- {
- return (int)round((float)$percent * 100);
- }
- /**
- * 把 bp 换算为百分数(9650 -> 96.5)
- */
- public static function fromBp($bp): float
- {
- return ((int)round((float)$bp)) / 100;
- }
- }
|