feat: add syj promote workflow
This commit is contained in:
100
pro_v3.5.1/app/services/syj/SyjPromoteConfigServices.php
Normal file
100
pro_v3.5.1/app/services/syj/SyjPromoteConfigServices.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\services\syj;
|
||||
|
||||
use app\services\BaseServices;
|
||||
use app\services\system\config\SystemConfigServices;
|
||||
use crmeb\services\SystemConfigService;
|
||||
use think\exception\ValidateException;
|
||||
|
||||
class SyjPromoteConfigServices extends BaseServices
|
||||
{
|
||||
public function getConfig(): array
|
||||
{
|
||||
return [
|
||||
'base_amount' => $this->money('syj_task_base_amount', '4333'),
|
||||
'target_count' => (int)SystemConfigService::get('syj_task_target_count', 4),
|
||||
'reward_rates' => $this->rewardRates(),
|
||||
'early_cashout_fee_rate' => $this->money('syj_early_cashout_fee_rate', '7'),
|
||||
'task_generate_timing' => (string)SystemConfigService::get('syj_task_generate_timing', 'on_confirm'),
|
||||
'task_order_dedupe' => (string)SystemConfigService::get('syj_task_order_dedupe', 'order'),
|
||||
'reward_trigger_enable' => (int)SystemConfigService::get('syj_reward_trigger_enable', 1),
|
||||
'brokerage_timing' => (string)SystemConfigService::get('brokerage_timing', 'on_confirm'),
|
||||
];
|
||||
}
|
||||
|
||||
public function saveConfig(array $data): void
|
||||
{
|
||||
$baseAmount = (float)($data['base_amount'] ?? 0);
|
||||
$targetCount = (int)($data['target_count'] ?? 0);
|
||||
$rates = $data['reward_rates'] ?? [];
|
||||
if (is_string($rates)) {
|
||||
$rates = json_decode($rates, true) ?: [];
|
||||
}
|
||||
$feeRate = (float)($data['early_cashout_fee_rate'] ?? -1);
|
||||
$generateTiming = (string)($data['task_generate_timing'] ?? 'on_confirm');
|
||||
|
||||
if ($baseAmount <= 0) {
|
||||
throw new ValidateException('任务基准金额必须大于0');
|
||||
}
|
||||
if ($targetCount <= 0) {
|
||||
throw new ValidateException('目标单数必须大于0');
|
||||
}
|
||||
if (count($rates) !== $targetCount) {
|
||||
throw new ValidateException('奖励比例数量必须与目标单数一致');
|
||||
}
|
||||
foreach ($rates as $rate) {
|
||||
if ((float)$rate < 0 || (float)$rate > 100) {
|
||||
throw new ValidateException('奖励比例必须在0到100之间');
|
||||
}
|
||||
}
|
||||
if ($feeRate < 0 || $feeRate > 100) {
|
||||
throw new ValidateException('扣费比例必须在0到100之间');
|
||||
}
|
||||
if (!in_array($generateTiming, ['on_confirm', 'on_pay'], true)) {
|
||||
throw new ValidateException('任务生成节点不正确');
|
||||
}
|
||||
|
||||
/** @var SystemConfigServices $configServices */
|
||||
$configServices = app()->make(SystemConfigServices::class);
|
||||
$map = [
|
||||
'syj_task_base_amount' => $this->formatMoney($baseAmount),
|
||||
'syj_task_target_count' => (string)$targetCount,
|
||||
'syj_task_reward_rates' => json_encode(array_values(array_map('floatval', $rates)), JSON_UNESCAPED_UNICODE),
|
||||
'syj_early_cashout_fee_rate' => $this->formatMoney($feeRate),
|
||||
'syj_task_generate_timing' => $generateTiming,
|
||||
'syj_task_order_dedupe' => (string)($data['task_order_dedupe'] ?? 'order'),
|
||||
'syj_reward_trigger_enable' => (string)(int)($data['reward_trigger_enable'] ?? 1),
|
||||
];
|
||||
foreach ($map as $key => $value) {
|
||||
$configServices->setConfig($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function rewardRates(): array
|
||||
{
|
||||
$value = SystemConfigService::get('syj_task_reward_rates', '[10,20,30,40]');
|
||||
if (is_array($value)) {
|
||||
return array_values(array_map('floatval', $value));
|
||||
}
|
||||
$decoded = json_decode((string)$value, true);
|
||||
return is_array($decoded) ? array_values(array_map('floatval', $decoded)) : [10, 20, 30, 40];
|
||||
}
|
||||
|
||||
public function rateForStep(int $step): float
|
||||
{
|
||||
$rates = $this->rewardRates();
|
||||
return (float)($rates[$step - 1] ?? 0);
|
||||
}
|
||||
|
||||
private function money(string $key, string $default): string
|
||||
{
|
||||
return $this->formatMoney((float)SystemConfigService::get($key, $default));
|
||||
}
|
||||
|
||||
public function formatMoney(float|string $number): string
|
||||
{
|
||||
return number_format((float)$number, 2, '.', '');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user