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.

119 lines
3.8 KiB
PHP

<?php
namespace Base\Service;
use Base\Tool\GameResource\SbcqClient;
use Base\Tool\GameResource\XyyClient;
use Exception;
use GuzzleHttp\Client;
class GameRebateService
{
public function sendByOrder($order)
{
$gift = M('rebate_gifts', 'tab_')->where(['base_game_id' => $order['base_game_id'], 'type' => $order['type'], 'gift_key' => $order['gift_key']])->find();
$hasError = false;
$sendResult = '';
if ($gift['game_currency'] > 0) {
$result = $this->sendGold($order, $gift['game_currency']);
if (!$result['status']) {
$hasError = true;
}
$sendResult .= ($result['message'] ?? '') . ';';
}
$giftItems = json_decode($gift['gifts'], true);
foreach ($giftItems as $giftItem) {
$result = $this->sendGift($order, $giftItem);
if (!$result['status']) {
$hasError = true;
}
$sendResult .= ($result['message'] ?? '') . ';';
}
M('rebate_orders', 'tab_')->where(['id' => $order['id']])->save([
'send_status' => $hasError ? 2 : 1,
'send_time' => time(),
'send_result' => $sendResult,
]);
}
private function getClient($baseGameId)
{
$client = null;
switch ($baseGameId) {
case 70:
$client = new XyyClient();
break;
case 73:
$client = new SbcqClient();
break;
/* case 9:
$client = new SbcqClient();
break; */
default:
throw new \Exception('客户端未实现');
break;
}
return $client;
}
public function sendGold($order, $gold)
{
$client = $this->getClient($order['base_game_id']);
return $client->sendGold($gold, $order);
}
public function sendGift($order, $giftItem)
{
$client = $this->getClient($order['base_game_id']);
return $client->sendEmail($giftItem, $order);
}
public function review(array $ids, $status)
{
if (!in_array($status, [1, 2])) {
throw new \Exception('状态异常');
}
if (count($ids) == 0) {
throw new \Exception('请选择要操作的记录');
}
$orders = M('rebate_orders', 'tab_')->field(['id'])->where(['review_status' => 0, 'id' => ['in', $ids]])->select();
if (count($orders) != count($ids)) {
throw new \Exception('含有不存在的记录或者已审核的记录');
}
$adminInfo = $_SESSION['onethink_admin']['user_auth'];
M('rebate_orders', 'tab_')->where(['review_status' => 0, 'id' => ['in', $ids]])->save([
'review_status' => $status,
'review_time' => time(),
'reviewer_id' => $adminInfo['uid'],
'reviewer_username' => $adminInfo['username'],
]);
}
public function sendAll($type)
{
$orders = M('rebate_orders', 'tab_')->where(['type' => $type, 'review_status' => 1, 'send_status' => 0])->select();
foreach ($orders as $order) {
if ($type == 'B') {
if (time() < strtotime($order['award_started_at'] . ' 00:00:00') || time() > strtotime($order['award_ended_at'] . ' 23:59:59')) {
continue;
}
}
$this->sendByOrder($order);
}
}
public function sendDaily($date)
{
$map = [
'type' => 'B',
'review_status' => 1,
'send_status' => 1,
'award_started_at' => ['elt', $date],
'award_ended_at' => ['egt', $date]
];
$orders = M('rebate_orders', 'tab_')->where($map)->select();
foreach ($orders as $order) {
$this->sendByOrder($order);
}
}
}