SuperLossRewardRule.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace app\api\service;
  3. /**
  4. * 超节损奖罚倍率规则(需求 5.5.6 / 表 2)
  5. *
  6. * 纯函数实现,不依赖数据库,便于单元测试:
  7. * - 输入:目标合格率、实际合格率(均以 bp=0.01% 表示)、材料废数量、实际投料数量、是否存在索赔
  8. * - 输出:倍率(bp 与倍数两种表示)、命中档位、各中间比率、是否因索赔豁免等
  9. *
  10. * 规则:
  11. * 1. 投料 <= 0 或规则关闭 -> 1.0;
  12. * 2. 材料原因报废率 > 1.5% 且没有索赔 -> 1.0(需求 5.5.6 括号内例外,不加大奖罚);
  13. * 3. 其余按目标合格率分档,取超出/低于目标合格率的点数阈值与倍率;
  14. * 4. 实际合格率 - 目标合格率 达到阈值 -> 返回倍率,否则 1.0。
  15. */
  16. class SuperLossRewardRule
  17. {
  18. const DEFAULT_RULE_VERSION = 'v2';
  19. const BP_BASE = 10000;
  20. /**
  21. * 内置默认配置(与 application/extra/superloss.php 保持一致,作为兜底)
  22. */
  23. public static function defaults(): array
  24. {
  25. return [
  26. 'enabled' => true,
  27. 'rule_version' => self::DEFAULT_RULE_VERSION,
  28. 'material_threshold_bp' => 150,
  29. 'claim_exempt' => true,
  30. 'trigger_inclusive' => true,
  31. 'material_denominator' => 'feed',
  32. 'tiers' => [
  33. ['min_target_rate_bp' => 9700, 'high_bp' => 150, 'low_bp' => 200, 'multiplier_bp' => 130, 'tier' => '>=97'],
  34. ['min_target_rate_bp' => 9500, 'high_bp' => 200, 'low_bp' => 250, 'multiplier_bp' => 130, 'tier' => '95-97'],
  35. ['min_target_rate_bp' => 9000, 'high_bp' => 300, 'low_bp' => 300, 'multiplier_bp' => 130, 'tier' => '90-95'],
  36. ['min_target_rate_bp' => 8000, 'high_bp' => 400, 'low_bp' => 400, 'multiplier_bp' => 160, 'tier' => '80-90'],
  37. ['min_target_rate_bp' => 0, 'high_bp' => 500, 'low_bp' => 500, 'multiplier_bp' => 160, 'tier' => '<=80'],
  38. ],
  39. ];
  40. }
  41. /**
  42. * 读取配置(框架外调用时返回默认值)
  43. */
  44. public static function config(): array
  45. {
  46. $conf = [];
  47. if (function_exists('config')) {
  48. try {
  49. $conf = config('superloss');
  50. } catch (\Throwable $e) {
  51. $conf = [];
  52. }
  53. }
  54. return self::normalize(is_array($conf) ? $conf : []);
  55. }
  56. /**
  57. * 合并默认值并做类型规整
  58. */
  59. public static function normalize(array $conf): array
  60. {
  61. $defaults = self::defaults();
  62. $conf = array_merge($defaults, $conf);
  63. $conf['enabled'] = !empty($conf['enabled']);
  64. $conf['claim_exempt'] = !empty($conf['claim_exempt']);
  65. $conf['trigger_inclusive'] = !empty($conf['trigger_inclusive']);
  66. $conf['material_threshold_bp'] = (int)$conf['material_threshold_bp'];
  67. $conf['rule_version'] = (string)$conf['rule_version'];
  68. $conf['material_denominator'] = (string)$conf['material_denominator'];
  69. $tiers = is_array($conf['tiers']) ? $conf['tiers'] : [];
  70. $normalized = [];
  71. foreach ($tiers as $tier) {
  72. if (!is_array($tier)) {
  73. continue;
  74. }
  75. $normalized[] = [
  76. 'min_target_rate_bp' => (int)($tier['min_target_rate_bp'] ?? 0),
  77. 'high_bp' => (int)($tier['high_bp'] ?? 0),
  78. 'low_bp' => (int)($tier['low_bp'] ?? 0),
  79. 'multiplier_bp' => (int)($tier['multiplier_bp'] ?? 100),
  80. 'tier' => (string)($tier['tier'] ?? ''),
  81. ];
  82. }
  83. usort($normalized, function ($a, $b) {
  84. return $b['min_target_rate_bp'] <=> $a['min_target_rate_bp'];
  85. });
  86. $conf['tiers'] = empty($normalized) ? $defaults['tiers'] : $normalized;
  87. return $conf;
  88. }
  89. /**
  90. * 计算奖罚倍率
  91. *
  92. * @param float|int $targetRateBp 目标合格率(bp)
  93. * @param float|int $realRateBp 实际合格率(bp)
  94. * @param float $materialWaste 材料原因报废数量(张)
  95. * @param float $feedQty 实际投料数量(张)
  96. * @param bool $hasClaim 是否存在材料索赔记录
  97. * @param array|null $conf 规则配置,null 时读取 application/extra/superloss.php
  98. * @return array
  99. */
  100. public static function resolve($targetRateBp, $realRateBp, float $materialWaste, float $feedQty, bool $hasClaim, ?array $conf = null): array
  101. {
  102. $conf = $conf === null ? self::config() : self::normalize($conf);
  103. $targetBp = (int)round((float)$targetRateBp);
  104. $realBp = (int)round((float)$realRateBp);
  105. $result = [
  106. 'multiplier_bp' => 100,
  107. 'multiplier_rate' => 1.0,
  108. 'multiplier' => 1.0,
  109. 'tier' => '',
  110. 'high_bp' => 0,
  111. 'low_bp' => 0,
  112. 'target_rate_bp' => $targetBp,
  113. 'real_rate_bp' => $realBp,
  114. 'material_waste_rate_bp' => 0,
  115. 'claim_exempt' => 0,
  116. 'hit' => 0,
  117. 'rule_version' => $conf['rule_version'],
  118. ];
  119. if (empty($conf['enabled']) || $feedQty <= 0) {
  120. return $result;
  121. }
  122. // 材料原因报废率(bp),分母固定为实际投料;除数已保证 > 0
  123. $materialRateBp = (int)round($materialWaste / $feedQty * self::BP_BASE);
  124. if ($materialRateBp < 0) {
  125. $materialRateBp = 0;
  126. }
  127. $result['material_waste_rate_bp'] = $materialRateBp;
  128. // 需求 5.5.6 例外:材料原因报废率超过 1.5% 且没有索赔 -> 不加大奖罚
  129. if (!empty($conf['claim_exempt'])
  130. && $materialRateBp > $conf['material_threshold_bp']
  131. && !$hasClaim
  132. ) {
  133. $result['claim_exempt'] = 1;
  134. return $result;
  135. }
  136. $tier = self::matchTier($targetBp, $conf['tiers']);
  137. if ($tier === null) {
  138. return $result;
  139. }
  140. $result['tier'] = $tier['tier'];
  141. $result['high_bp'] = $tier['high_bp'];
  142. $result['low_bp'] = $tier['low_bp'];
  143. $diff = $realBp - $targetBp;
  144. if (!empty($conf['trigger_inclusive'])) {
  145. $hit = ($diff >= $tier['high_bp']) || ($diff <= -$tier['low_bp']);
  146. } else {
  147. $hit = ($diff > $tier['high_bp']) || ($diff < -$tier['low_bp']);
  148. }
  149. if (!$hit) {
  150. return $result;
  151. }
  152. $result['multiplier_bp'] = $tier['multiplier_bp'];
  153. $result['multiplier_rate'] = $tier['multiplier_bp'] / 100;
  154. $result['multiplier'] = $result['multiplier_rate'];
  155. $result['hit'] = 1;
  156. return $result;
  157. }
  158. /**
  159. * 按目标合格率选档:目标 >= 档位下限即命中(端点包含)
  160. */
  161. public static function matchTier(int $targetBp, array $tiers): ?array
  162. {
  163. foreach ($tiers as $tier) {
  164. if ($targetBp >= (int)$tier['min_target_rate_bp']) {
  165. return $tier;
  166. }
  167. }
  168. return null;
  169. }
  170. /**
  171. * 把百分数(如 96.5)换算为 bp(9650)
  172. */
  173. public static function toBp($percent): int
  174. {
  175. return (int)round((float)$percent * 100);
  176. }
  177. /**
  178. * 把 bp 换算为百分数(9650 -> 96.5)
  179. */
  180. public static function fromBp($bp): float
  181. {
  182. return ((int)round((float)$bp)) / 100;
  183. }
  184. }