258 lines
10 KiB
PHP
258 lines
10 KiB
PHP
<?php
|
|
|
|
namespace app\model;
|
|
|
|
// 消息推送
|
|
use support\Log;
|
|
use support\think\Db;
|
|
|
|
class MessagePushTask
|
|
{
|
|
public static function send_msg()
|
|
{
|
|
// 查询任务
|
|
$event_list = Db::name('tb_push_event')->where(['status' => 0])->select();
|
|
if($event_list) {
|
|
foreach ($event_list as $k => $event) {
|
|
$user_list = self::getPullInfoUser($event);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 找到可以接收订阅消息的用户
|
|
* @param $type类型 1按次数发送 2自定义 3按标签
|
|
* @return array
|
|
*/
|
|
public static function getPullInfoUser($event): array
|
|
{
|
|
$user_arr = [];
|
|
switch ($event['type']) {
|
|
case 1;
|
|
$user_arr = self::getPullInfoUserToNumber($event);
|
|
break;
|
|
case 2;
|
|
$user_arr = self::getPullInfoUserToCustom($event);
|
|
break;
|
|
case 3;
|
|
$user_arr = self::getPullInfoUserToLabel($event);
|
|
break;
|
|
}
|
|
return $user_arr;
|
|
}
|
|
|
|
|
|
// 按照可接收次数获取用户信息
|
|
public static function getPullInfoUserToNumber($event)
|
|
{
|
|
$user_num = [];
|
|
if($event['can_send_times'] > 0) {
|
|
$user_num = Db::name('tb_user_push_event_num')->alias('upen')->where(['num', '>=', $event['can_send_times']])
|
|
->join('tb_user_info u', 'upen.user_id = u.id')
|
|
->field('upen.user_id as user_id, u.wechat_ac_open_id as open_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
return $user_num;
|
|
}
|
|
|
|
|
|
// 按照自定义获取用户信息
|
|
public static function getPullInfoUserToCustom($event)
|
|
{
|
|
$data_arr = [];
|
|
// 按照性别
|
|
if(isset($event['gender']) && is_string($event['gender'])) {
|
|
$gender_arr = explode(',', $event['gender']);
|
|
foreach ($gender_arr as $k => $gender) {
|
|
// 未知
|
|
if($gender == 0) {
|
|
$data_arr[] = Db::name('tb_user_info')->whereNotIn('sex', [0,1])
|
|
->field('user_id as user_id, wechat_ac_open_id as open_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
// 男
|
|
if($gender == 1) {
|
|
$data_arr[] = Db::name('tb_user_info')->where('sex', 1)
|
|
->field('user_id as user_id, wechat_ac_open_id as open_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
// 女
|
|
if($gender == 2) {
|
|
$data_arr[] = Db::name('tb_user_info')->where('sex', 0)
|
|
->field('user_id as user_id, wechat_ac_open_id as open_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 按照下单次数
|
|
if(isset($event['order_history']) && is_string($event['order_history'])) {
|
|
$order_history_arr = explode(',', $event['order_history']);
|
|
foreach ($order_history_arr as $k => $order_history) {
|
|
// 从未下单
|
|
if($order_history == 0) {
|
|
$data_arr[] = Db::name('tb_user_info')
|
|
->alias('u') // 用户表别名u
|
|
->leftJoin('tb_order_info o', 'u.id = o.user_id')
|
|
->where('o.user_id', 'null')
|
|
->field('u.id as user_id, u.wechat_ac_open_id as open_id')
|
|
->distinct(true)
|
|
->column('open_id', 'user_id');
|
|
}
|
|
// 下过1单
|
|
if($order_history == 1) {
|
|
$data_arr[] = Db::name('tb_user_info')
|
|
->alias('u')
|
|
// 关联订单表,使用内连接确保只包含有订单的用户
|
|
->innerJoin('tb_order_info o', 'u.id = o.user_id')
|
|
// 按用户ID分组
|
|
->group('u.id')
|
|
// 筛选出订单数量为1的用户
|
|
->having('COUNT(o.id) = 1')
|
|
// 选择需要的用户字段,同时查询订单数量(可选)
|
|
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
// 下过2-5单
|
|
if($order_history == 2) {
|
|
$data_arr[] = Db::name('tb_user_info')
|
|
->alias('u')
|
|
// 关联订单表,使用内连接确保只包含有订单的用户
|
|
->innerJoin('tb_order_info o', 'u.id = o.user_id')
|
|
// 按用户ID分组
|
|
->group('u.id')
|
|
// 筛选出订单数量为1的用户
|
|
->having('COUNT(o.id) BETWEEN 2 AND 5')
|
|
// 选择需要的用户字段,同时查询订单数量(可选)
|
|
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
// 下过5单起
|
|
if($order_history == 3) {
|
|
$data_arr[] = Db::name('tb_user_info')
|
|
->alias('u')
|
|
// 关联订单表,使用内连接确保只包含有订单的用户
|
|
->innerJoin('tb_order_info o', 'u.id = o.user_id')
|
|
// 按用户ID分组
|
|
->group('u.id')
|
|
// 筛选出订单数量为1的用户
|
|
->having('COUNT(o.id) >= 5')
|
|
// 选择需要的用户字段,同时查询订单数量(可选)
|
|
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 按照下单时间 no
|
|
if(isset($event['order_times']) && is_string($event['order_times'])) {
|
|
$order_times_arr = explode(',', $event['order_times']);
|
|
foreach ($order_times_arr as $k => $order_times) {
|
|
// 今天下过
|
|
if($order_times == 0) {
|
|
$data_arr[] = Db::name('tb_user_info')
|
|
->alias('u') // 用户表别名u
|
|
->innerJoin('tb_order_info o', 'u.id = o.user_id')
|
|
->where('o.create_time', '>=', date('Y-m-d 00:00:00'))
|
|
->where('o.create_time', '<=', date('Y-m-d 23:59:59'))
|
|
->field('u.id as user_id, u.wechat_ac_open_id as open_id')
|
|
->distinct(true)
|
|
->column('open_id', 'user_id');
|
|
}
|
|
// 昨天下过
|
|
if($order_times == 1) {
|
|
$data_arr[] = Db::name('tb_user_info')
|
|
->alias('u')
|
|
// 关联订单表,使用内连接确保只包含有订单的用户
|
|
->innerJoin('tb_order_info o', 'u.id = o.user_id')
|
|
// 按用户ID分组
|
|
->group('u.id')
|
|
// 筛选出订单数量为1的用户
|
|
->having('COUNT(o.id) = 1')
|
|
// 选择需要的用户字段,同时查询订单数量(可选)
|
|
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
// 2周内没下过
|
|
if($order_times == 2) {
|
|
$data_arr[] = Db::name('tb_user_info')
|
|
->alias('u')
|
|
// 关联订单表,使用内连接确保只包含有订单的用户
|
|
->innerJoin('tb_order_info o', 'u.id = o.user_id')
|
|
// 按用户ID分组
|
|
->group('u.id')
|
|
// 筛选出订单数量为1的用户
|
|
->having('COUNT(o.id) BETWEEN 2 AND 5')
|
|
// 选择需要的用户字段,同时查询订单数量(可选)
|
|
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
// 半个月-1个月没下过
|
|
if($order_times == 3) {
|
|
$data_arr[] = Db::name('tb_user_info')
|
|
->alias('u')
|
|
// 关联订单表,使用内连接确保只包含有订单的用户
|
|
->innerJoin('tb_order_info o', 'u.id = o.user_id')
|
|
// 按用户ID分组
|
|
->group('u.id')
|
|
// 筛选出订单数量为1的用户
|
|
->having('COUNT(o.id) >= 5')
|
|
// 选择需要的用户字段,同时查询订单数量(可选)
|
|
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
// 1个月以上没下过
|
|
if($order_times == 4) {
|
|
$data_arr[] = Db::name('tb_user_info')
|
|
->alias('u')
|
|
// 关联订单表,使用内连接确保只包含有订单的用户
|
|
->innerJoin('tb_order_info o', 'u.id = o.user_id')
|
|
// 按用户ID分组
|
|
->group('u.id')
|
|
// 筛选出订单数量为1的用户
|
|
->having('COUNT(o.id) >= 5')
|
|
// 选择需要的用户字段,同时查询订单数量(可选)
|
|
->field('u.id as user_id, u.wechat_ac_open_id as user_id')
|
|
->column('open_id', 'user_id');
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return $data_arr;
|
|
}
|
|
|
|
// 按照标签获取用户信息
|
|
public static function getPullInfoUserToLabel($event)
|
|
{
|
|
return [];
|
|
}
|
|
|
|
|
|
// 给用户送送优惠券
|
|
public static function sendUserCoupon(int | array $user_id, $coupon_id, $number = 1)
|
|
{
|
|
try {
|
|
Db::startTrans();
|
|
if(is_int($user_id)) {
|
|
|
|
}elseif (is_array($user_id)) {
|
|
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
}catch (\Exception $e) {
|
|
Db::rollback();
|
|
Log::info('用户送券异常-->' . $e->getMessage());
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} |