bztang-admin/app/common/cron/CouponExpireNotice.php

137 lines
5.0 KiB
PHP

<?php
namespace app\common\cron;
use app\common\events\coupon\CouponExpireEvent;
use app\common\facades\Setting;
use app\common\models\Coupon;
use app\common\models\Member;
use app\common\models\MemberCoupon;
use app\common\models\notice\MessageTemp;
use app\common\models\UniAccount;
/**
* Author:
* Date: 2017/7/12
* Time: 下午4:28
*/
class CouponExpireNotice
{
public $set;
public $setLog;
public $uniacid;
public function handle()
{
\Log::info('优惠券到期处理');
set_time_limit(0);
$uniAccount = UniAccount::getEnable();
foreach ($uniAccount as $u) {
\YunShop::app()->uniacid = $u->uniacid;
Setting::$uniqueAccountId = $u->uniacid;
$this->uniacid = $u->uniacid;
$this->set = Setting::get('shop.coupon');
$this->setLog = Setting::get('shop.coupon_log');
$temp_id = $this->set['expire'];
if (!$temp_id) {
return;
}
$this->sendExpireNotice();
}
}
public function sendExpireNotice()
{
if ($this->set['every_day'] != date('H')) {
return;
}
if ($this->setLog['current_d'] == date('d')) {
return;
}
$this->setLog['current_d'] = date('d');
Setting::set('shop.coupon_log', $this->setLog);
$memberCoupons = MemberCoupon::getExpireCoupon()
->select(['yz_member_coupon.*'])
->join('yz_coupon', function ($join) {
$join->on('yz_member_coupon.coupon_id', '=', 'yz_coupon.id')
->where('time_limit', '!=', 0)->where('time_days', '!=', 0);
})
->with('belongsToCoupon')
->get();
foreach ($memberCoupons as $memberCoupon) {
$present = time();
$end = strtotime(date('Y-m-d H:i:s', strtotime($memberCoupon->time_end) - $this->set['delayed'] * 86400));
if ($present < $end || strtotime($memberCoupon->time_end) < $present) {
continue;
}
$member = Member::getMemberByUid($memberCoupon->uid)->with('hasOneFans')->first();
$couponData = [
'name' => $memberCoupon->belongsToCoupon->name,
'api_limit' => $this->apiLimit($memberCoupon->belongsToCoupon),
'time_end' => $memberCoupon->time_end
];
$this->sendNotice($couponData, $member);
event(new CouponExpireEvent($memberCoupon->coupon_id, $member->uid, $memberCoupon->time_end));
}
}
public function sendNotice($ouponData, $member)
{
if ($member->hasOneFans->follow == 1) {
// $message = $this->set['expire'];
// $message = str_replace('[优惠券名称]', $ouponData['name'], $message);
// $message = str_replace('[优惠券使用范围]', $ouponData['api_limit'], $message);
// $message = str_replace('[过期时间]', $ouponData['time_end'], $message);
// $msg = [
// "first" => '您好',
// "keyword1" => $this->set['expire_title'] ? $this->set['expire_title'] : '优惠券过期提醒',
// "keyword2" => $message,
// "remark" => "",
// ];
// \app\common\services\MessageService::notice($this->set['template_id'], $msg, $member['openid'], $this->uniacid);
$temp_id = $this->set['expire'];
if (!$temp_id) {
return;
}
$params = [
['name' => '优惠券名称', 'value' => $ouponData['name']],
['name' => '昵称', 'value' => $member['nickname']],
['name' => '优惠券使用范围', 'value' => $ouponData['api_limit']],
['name' => '过期时间', 'value' => $ouponData['time_end']],
];
$msg = MessageTemp::getSendMsg($temp_id, $params);
if (!$msg) {
return;
}
$tempData = MessageTemp::getTempById($temp_id)->first();
\app\common\services\MessageService::notice(MessageTemp::$template_id, $msg, $member->uid, $this->uniacid, ($tempData ? $tempData->news_link : ''));
}
return;
}
public function apiLimit($coupon)
{
$api_limit = '';
switch ($coupon->use_type) {
case Coupon::COUPON_SHOP_USE:
$api_limit = '商城通用';
break;
case Coupon::COUPON_CATEGORY_USE:
$api_limit = '适用于下列分类: ';
$api_limit .= implode(',', $coupon['categorynames']);
break;
case Coupon::COUPON_GOODS_USE:
$api_limit = '适用于下列商品: ';
$api_limit .= implode(',', $coupon['goods_names']);
break;
case Coupon::COUPON_STORE_USE:
case Coupon::COUPON_SINGLE_STORE_USE:
$api_limit = '适用于下列门店: ';
$api_limit .= implode(',', $coupon['storenames']);
break;
}
return $api_limit;
}
}