You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
5.0 KiB
PHTML

4 years ago
<?php
namespace Base\Service;
use Base\Facade\Request;
4 years ago
class PaymentRuleService
{
/**
* @var 永久默认时间(为方便查询,设为 2100-01-01 00:00:00
*/
const FOREVER_TIME = 4102444800;
4 years ago
public function getRulesByIds(array $ids = null, $fields = '*')
{
$map = [];
if (is_null($ids)) {
$map['_string'] = '1=1';
} elseif (count($ids) == 0) {
return [];
} else {
$map['id'] = ['in', $ids];
}
$rules = M('payment_rule', 'tab_')->field($fields)->where($map)->select();
return index_by_column('id', $rules);
}
public function getEffectiveTimeDisplay($rule)
{
$startTime = $rule['start_time'] == 0 ? null : date('Y-m-d', $rule['start_time']);
4 years ago
$endTime = $rule['end_time'] == self::FOREVER_TIME ? null : date('Y-m-d', $rule['end_time']);
4 years ago
if ($startTime == null && $endTime == null) {
return '永久';
}
if ($startTime != null && $endTime != null) {
return $startTime . ' 至 ' . $endTime;
}
if ($startTime == null && $endTime != null) {
return '从前 至 ' . $endTime;
}
if ($startTime != null && $endTime == null) {
return $startTime . ' 至 永久';
}
}
4 years ago
public function getTimeRepeatCondition($startTime, $endTime)
{
return ' ((start_time >= ' . $startTime . ' AND start_time <= ' . $endTime . ') OR (start_time <= ' . $startTime . ' AND end_time >= ' . $endTime
. ') OR (end_time >= ' . $startTime . ' AND end_time <= ' . $endTime . '))';
}
public function getTimeRepeatRules($record)
{
$conditions = [
'company_belong' => $record['company_belong'],
'company_id' => $record['company_id'] ?? 0,
'game_time_id' => $record['game_time_id'],
'game_id' => $record['game_id'] ?? 0,
];
$startTime = $record['start_time'] == '' ? 0 : strtotime($record['start_time'] . ' 00:00:00');
$endTime = $record['end_time'] == '' ? PaymentRuleService::FOREVER_TIME : strtotime($record['end_time'] . ' 23:59:59');
$conditions['_string'] = $this->getTimeRepeatCondition($startTime, $endTime);
return M('payment_rule', 'tab_')->where($conditions)->order('start_time asc')->select();
}
public function resetTimeRules($repeatRules, $startTime, $endTime)
{
$length = count($repeatRules);
if ($length == 0) {
return;
}
if ($length == 1) {
$first = $repeatRules[0];
if ($startTime <= $first['start_time'] && $endTime >= $last['end_time']) {
M('payment_rule', 'tab_')->where(['id' => $first['id']])->delete();
} elseif ($endTime < $first['end_time'] && $startTime <= $first['start_time']) {
M('payment_rule', 'tab_')->where(['id' => $first['id']])->save(['start_time' => $endTime + 1, 'update_time' => time()]);
} elseif ($endTime >= $first['end_time'] && $startTime > $first['start_time']) {
M('payment_rule', 'tab_')->where(['id' => $first['id']])->save(['end_time' => $startTime - 1, 'update_time' => time()]);
} else {
$newRule = $first;
M('payment_rule', 'tab_')->where(['id' => $first['id']])->save(['end_time' => $startTime - 1, 'update_time' => time()]);
unset($newRule['id']);
unset($newRule['create_time']);
$newRule['start_time'] = $endTime + 1;
$newRule['update_time'] = time();
M('payment_rule', 'tab_')->add($newRule);
}
} else {
$first = $repeatRules[0];
$last = $repeatRules[$length - 1];
$exceptIds = [];
if ($startTime <= $first['start_time'] && $endTime >= $last['end_time']) {
} elseif ($startTime <= $first['start_time'] && $endTime < $last['end_time']) {
M('payment_rule', 'tab_')->where(['id' => $last['id']])->save(['start_time' => $endTime + 1, 'update_time' => time()]);
$exceptIds = [$last['id']];
} elseif ($startTime > $first['start_time'] && $endTime >= $last['end_time']) {
M('payment_rule', 'tab_')->where(['id' => $first['id']])->save(['end_time' => $startTime - 1, 'update_time' => time()]);
$exceptIds = [$first['id']];
} else {
M('payment_rule', 'tab_')->where(['id' => $first['id']])->save(['end_time' => $startTime - 1, 'update_time' => time()]);
M('payment_rule', 'tab_')->where(['id' => $last['id']])->save(['start_time' => $endTime + 1, 'update_time' => time()]);
$exceptIds = [$first['id'], $last['id']];
}
foreach ($repeatRules as $rule) {
if (!in_array($rule['id'], $exceptIds)) {
M('payment_rule', 'tab_')->where(['id' => $rule['id']])->delete();
}
}
}
}
4 years ago
}