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.

113 lines
4.1 KiB
PHTML

3 years ago
<?php
namespace Base\Service;
class DiscountService
{
const FOREVER_TIME = 4102444800;
public function save($params, $admin = null)
{
$id = $params['id'] ?? 0;
$baseGame = M('base_game', 'tab_')->where(['id' => $params['base_game_id']])->find();
$data = [
'base_game_id' => $params['base_game_id'],
'base_game_name' => $baseGame['name'],
3 years ago
'start_time' => strtotime($params['start_time'] . ' 00:00:00'),
'end_time' => empty($params['end_time']) ? self::FOREVER_TIME : strtotime($params['end_time'] . ' 23:59:59'),
3 years ago
'first_rate' => $params['first_rate'],
'second_rate' => $params['second_rate'],
'status' => $params['status'],
];
$data['update_time'] = time();
$data['update_admin_id'] = $admin ? $admin['uid'] : 0;
$data['update_admin_username'] = $admin ? $admin['username'] : '';
if ($id > 0) {
M('game_discount', 'tab_')->where(['id' => $id])->save($data);
} else {
$data['create_time'] = time();
$data['create_admin_id'] = $admin ? $admin['uid'] : 0;
$data['create_admin_username'] = $admin ? $admin['username'] : '';
M('game_discount', 'tab_')->add($data);
}
}
public function check($params)
{
$id = $params['id'] ?? 0;
if (empty($params['base_game_id'])) {
throw new \Exception('请选择游戏');
}
if (empty($params['start_time'])) {
throw new \Exception('请选择生效时间');
}
if (empty($params['base_game_id'])) {
throw new \Exception('请选择游戏');
}
$startTime = strtotime($params['start_time'] . ' 00:00:00');
$endTime = empty($params['end_time']) ? self::FOREVER_TIME : strtotime($params['end_time'] . ' 23:59:59');
if ($endTime < $startTime) {
throw new \Exception('生效开始时间不能大于结束时间');
}
$firstRate = $params['first_rate'] ?? 0;
$secondRate = $params['second_rate'] ?? 0;
// /^[0-9]+(.[0-9]{1,2})?$/
if (!preg_match('/^[0-9]+(.[0-9]{1})?$/', $firstRate)) {
throw new \Exception('首充折扣为大于等于0的整数或者一位小数');
}
if (!preg_match('/^[0-9]+(.[0-9]{1})?$/', $secondRate)) {
throw new \Exception('次充折扣为大于等于0的整数或者一位小数');
}
if ($id > 0) {
$record = M('game_discount', 'tab_')->where(['id' => $id])->find();
if ($record['end_time'] < time()) {
throw new \Exception('生效时间已过,不可修改');
}
3 years ago
if ($record['start_time'] < time()) {
if ($record['base_game_id'] != $params['base_game_id']) {
throw new \Exception('已生效不能修改游戏');
}
if (intval($firstRate * 10) != intval($record['first_rate'] * 10)) {
throw new \Exception('已生效不能修改首充折扣');
}
if (intval($secondRate * 10) != intval($record['second_rate'] * 10)) {
throw new \Exception('已生效不能修改次充折扣');
}
}
3 years ago
}
$map = [];
$map['base_game_id'] = $params['base_game_id'];
$map['_string'] = 'NOT (end_time < ' . $startTime . ' OR start_time > ' . $endTime . ')';
if ($id > 0) {
$map['_string'] .= ' and id<>' . $id;
}
$item = M('game_discount', 'tab_')->where($map)->limit(1)->find();
if ($item) {
throw new \Exception('该游戏在相同时间区间已存在打折规则');
}
}
public function delete($id)
{
M('game_discount', 'tab_')->where(['id' => $id])->delete();
}
public function isActived($record)
{
if ($record['status'] == 0) {
return false;
}
if ($record['start_time'] <= time() && $record['end_time'] >= time()) {
return true;
}
return false;
}
}