<?php
namespace app\task\behavior\sharing;
use think\Cache;
use app\common\service\Message;
use app\task\model\sharing\Setting;
use app\task\model\sharing\Active as ActiveModel;
use app\task\model\sharing\Order as OrderModel;
class Active
{
private $model;
public function run($model)
{
if (!$model instanceof ActiveModel) {
return new ActiveModel and false;
}
$this->model = $model;
if (!$model::$wxapp_id) {
return false;
}
if (!Cache::has('__task_space__sharing_active__' . $model::$wxapp_id)) {
try {
$config = Setting::getItem('basic');
$this->onUpdateActiveEnd();
if ($config['auto_refund'] == true) {
$this->onOrderRefund();
}
} catch (\Exception $e) {
}
Cache::set('__task_space__sharing_active__' . $model::$wxapp_id, time(), 10);
}
return true;
}
private function onUpdateActiveEnd()
{
$list = $this->model->getEndedList();
$activeIds = [];
foreach ($list as $item) {
$activeIds[] = $item['active_id'];
}
$this->dologs('onSetActiveEnd', [
'activeIds' => json_encode($activeIds),
]);
$Message = new Message;
foreach ($list as $item) {
$Message->sharingActive($item, '拼团失败');
}
return $this->model->updateEndedStatus($activeIds);
}
private function onOrderRefund()
{
$model = new OrderModel;
$maxLimit = 100;
$orderList = $model->getFailedOrderList($maxLimit);
$orderIds = [];
foreach ($orderList as $order) {
$orderIds[] = $order['order_id'];
}
$this->dologs('onOrderRefund', [
'orderIds' => json_encode($orderIds),
]);
if (empty($orderIds)) {
return false;
}
if ($model->updateFailedStatus($orderList)) {
return true;
}
$this->dologs('onOrderRefund', [
'error: ' => $model->getError()
]);
return false;
}
private function dologs($method, $params = [])
{
$value = 'behavior sharing Active --' . $method;
foreach ($params as $key => $val)
$value .= ' --' . $key . ' ' . $val;
return log_write($value);
}
}