Files
webman-buildadmin-mall/app/admin/controller/mall/PlayxConfig.php
2026-07-27 16:19:31 +08:00

191 lines
7.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace app\admin\controller\mall;
use app\common\controller\Backend;
use app\common\library\MallPlayxRatios;
use app\common\library\MallPointsConversion;
use app\common\model\MallBusinessConfig;
use support\Response;
use support\think\Db;
use Webman\Http\Request;
/**
* 积分商城参数PlayX 比例等,读写 mall_business_config
*/
class PlayxConfig extends Backend
{
public function index(Request $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
$row = MallBusinessConfig::ensureRow();
MallPlayxRatios::syncFromRow($row);
$payload = $row->toArray();
$payload['active_mall_open_date'] = MallPointsConversion::resolveActiveMallOpenDate($row);
$payload['active_event_end_date'] = MallPointsConversion::resolveActiveEventEndDate($row);
return $this->success('', [
'row' => $payload,
'remark' => get_route_remark(),
]);
}
public function save(Request $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
$data = $request->post();
if (empty($data) || !is_array($data)) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
$return = $data['return_ratio'] ?? null;
$unlock = $data['unlock_ratio'] ?? null;
if (!is_numeric($return) || !is_numeric($unlock)) {
return $this->error(__('Parameter error'));
}
$returnF = floatval($return);
$unlockF = floatval($unlock);
if ($returnF < 0 || $unlockF < 0) {
return $this->error(__('Parameter error'));
}
$cashF = null;
if (array_key_exists('points_to_cash_ratio', $data) && $data['points_to_cash_ratio'] !== null && $data['points_to_cash_ratio'] !== '') {
if (!is_numeric($data['points_to_cash_ratio'])) {
return $this->error(__('Parameter error'));
}
$cashF = floatval($data['points_to_cash_ratio']);
if ($cashF < 0) {
return $this->error(__('Parameter error'));
}
}
$dailyEnabled = !empty($data['daily_points_enabled']) ? 1 : 0;
$lifetimeEnabled = !empty($data['lifetime_points_enabled']) ? 1 : 0;
$lifetimeRatio = $data['lifetime_points_ratio'] ?? null;
if (!is_numeric($lifetimeRatio)) {
return $this->error(__('Parameter error'));
}
$lifetimeRatioF = floatval($lifetimeRatio);
if ($lifetimeRatioF < 0) {
return $this->error(__('Parameter error'));
}
$mallOpenDate = MallPointsConversion::normalizeDate($data['mall_open_date'] ?? null);
$mallEventEndDate = MallPointsConversion::normalizeDate($data['mall_event_end_date'] ?? null);
if ($mallOpenDate !== null && $mallEventEndDate !== null && $mallEventEndDate < $mallOpenDate) {
return $this->error(__('Event end date cannot be earlier than start date'));
}
$row = MallBusinessConfig::order('id', 'asc')->find();
$now = time();
if (!$row) {
$effectiveOn = $mallOpenDate !== null ? MallPointsConversion::nextCalendarDate() : null;
$cashPersist = $cashF ?? MallBusinessConfig::DEFAULT_POINTS_TO_CASH_RATIO;
MallBusinessConfig::create([
'return_ratio' => $returnF,
'unlock_ratio' => $unlockF,
'points_to_cash_ratio' => $cashPersist,
'daily_points_enabled' => $dailyEnabled,
'lifetime_points_enabled' => $lifetimeEnabled,
'lifetime_points_ratio' => $lifetimeRatioF,
'mall_open_date' => $mallOpenDate,
'mall_open_date_previous' => null,
'mall_open_date_effective_on' => $effectiveOn,
'mall_event_end_date' => $mallEventEndDate,
'mall_event_end_date_previous' => null,
'mall_event_end_date_effective_on' => MallPointsConversion::nextCalendarDate(),
'create_time' => $now,
'update_time' => $now,
]);
} else {
$previousOpen = MallPointsConversion::resolveActiveMallOpenDate($row);
$currentPending = MallPointsConversion::normalizeDate($row->mall_open_date ?? null);
$openDateChanged = $mallOpenDate !== $currentPending;
$previousEnd = MallPointsConversion::resolveActiveEventEndDate($row);
$currentPendingEnd = MallPointsConversion::normalizeDate($row->mall_event_end_date ?? null);
$endDateChanged = $mallEventEndDate !== $currentPendingEnd;
$row->return_ratio = $returnF;
$row->unlock_ratio = $unlockF;
if ($cashF !== null) {
$row->points_to_cash_ratio = $cashF;
}
$row->daily_points_enabled = $dailyEnabled;
$row->lifetime_points_enabled = $lifetimeEnabled;
$row->lifetime_points_ratio = $lifetimeRatioF;
if ($openDateChanged) {
$row->mall_open_date_previous = $previousOpen;
$row->mall_open_date = $mallOpenDate;
$row->mall_open_date_effective_on = $mallOpenDate !== null ? MallPointsConversion::nextCalendarDate() : null;
}
if ($endDateChanged) {
$row->mall_event_end_date_previous = $previousEnd;
$row->mall_event_end_date = $mallEventEndDate;
$row->mall_event_end_date_effective_on = MallPointsConversion::nextCalendarDate();
}
$row->save();
}
$fresh = MallBusinessConfig::order('id', 'asc')->find();
if ($fresh) {
MallPlayxRatios::syncFromRow($fresh);
}
return $this->success(__('The current page configuration item was updated successfully'));
}
/**
* 一键按最新终身总输赢与比例折算积分(重复执行会先扣旧比例再加新比例)
*/
public function calculateLifetimePoints(Request $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
$row = MallBusinessConfig::order('id', 'asc')->find();
if (!$row) {
return $this->error(__('Parameter error'));
}
Db::startTrans();
try {
$stats = MallPointsConversion::recalculateAllLifetimePoints($row);
Db::commit();
} catch (\Throwable $e) {
Db::rollback();
return $this->error(__($e->getMessage()));
}
return $this->success(
sprintf(
__('Lifetime points calculation completed'),
$stats['processed'],
$stats['updated'],
$stats['skipped']
),
$stats
);
}
}