201 lines
6.1 KiB
PHP
201 lines
6.1 KiB
PHP
<?php
|
||
|
||
namespace app\common\model;
|
||
|
||
use think\Model;
|
||
|
||
/**
|
||
* 流量行为表模型类
|
||
* 表名:ck_traffic_pool_behavior
|
||
* 用途:记录流量的各种行为(包括所有消息互动)
|
||
*/
|
||
class TrafficPoolBehavior extends Model
|
||
{
|
||
// 设置数据表名
|
||
protected $name = 'traffic_pool_behavior';
|
||
|
||
// 主键
|
||
protected $pk = 'id';
|
||
|
||
// 自动写入时间戳
|
||
protected $autoWriteTimestamp = 'createTime';
|
||
protected $createTime = 'createTime';
|
||
protected $updateTime = false;
|
||
|
||
// 行为类型常量
|
||
const BEHAVIOR_TYPE_SEND_MSG = 1; // 发送消息
|
||
const BEHAVIOR_TYPE_RECEIVE_MSG = 2; // 接收消息
|
||
const BEHAVIOR_TYPE_VIEW = 3; // 浏览
|
||
const BEHAVIOR_TYPE_CLICK = 4; // 点击
|
||
const BEHAVIOR_TYPE_CONSULT = 5; // 咨询
|
||
const BEHAVIOR_TYPE_ORDER = 6; // 下单
|
||
const BEHAVIOR_TYPE_PAY = 7; // 支付
|
||
const BEHAVIOR_TYPE_REFUND = 8; // 退款
|
||
const BEHAVIOR_TYPE_LIKE_MOMENTS = 9; // 点赞朋友圈
|
||
const BEHAVIOR_TYPE_COMMENT_MOMENTS = 10; // 评论朋友圈
|
||
|
||
// 行为类型名称映射
|
||
const BEHAVIOR_TYPE_NAMES = [
|
||
self::BEHAVIOR_TYPE_SEND_MSG => '发送消息',
|
||
self::BEHAVIOR_TYPE_RECEIVE_MSG => '接收消息',
|
||
self::BEHAVIOR_TYPE_VIEW => '浏览',
|
||
self::BEHAVIOR_TYPE_CLICK => '点击',
|
||
self::BEHAVIOR_TYPE_CONSULT => '咨询',
|
||
self::BEHAVIOR_TYPE_ORDER => '下单',
|
||
self::BEHAVIOR_TYPE_PAY => '支付',
|
||
self::BEHAVIOR_TYPE_REFUND => '退款',
|
||
self::BEHAVIOR_TYPE_LIKE_MOMENTS => '点赞朋友圈',
|
||
self::BEHAVIOR_TYPE_COMMENT_MOMENTS => '评论朋友圈',
|
||
];
|
||
|
||
/**
|
||
* 关联公司流量详情
|
||
*/
|
||
public function poolCompany()
|
||
{
|
||
return $this->belongsTo(TrafficPoolCompany::class, 'poolCompanyId', 'id');
|
||
}
|
||
|
||
/**
|
||
* 获取额外信息
|
||
* @param string $value
|
||
* @return array
|
||
*/
|
||
public function getExtraAttr($value)
|
||
{
|
||
return $value ? json_decode($value, true) : [];
|
||
}
|
||
|
||
/**
|
||
* 设置额外信息
|
||
* @param array $value
|
||
* @return string
|
||
*/
|
||
public function setExtraAttr($value)
|
||
{
|
||
return $value ? json_encode($value, JSON_UNESCAPED_UNICODE) : null;
|
||
}
|
||
|
||
/**
|
||
* 获取行为类型名称
|
||
* @return string
|
||
*/
|
||
public function getBehaviorTypeNameAttr()
|
||
{
|
||
return self::BEHAVIOR_TYPE_NAMES[$this->behaviorType] ?? '未知行为';
|
||
}
|
||
|
||
/**
|
||
* 记录消息行为
|
||
* @param int $poolCompanyId
|
||
* @param string $identifier
|
||
* @param int $companyId
|
||
* @param int $behaviorType 发送/接收
|
||
* @param int $messageId
|
||
* @param int $wechatAccountId
|
||
* @param array $extra
|
||
* @return static
|
||
*/
|
||
public static function recordMessageBehavior(int $poolCompanyId, string $identifier, int $companyId, int $behaviorType, int $messageId = null, int $wechatAccountId = null, array $extra = [])
|
||
{
|
||
$behavior = self::create([
|
||
'poolCompanyId' => $poolCompanyId,
|
||
'identifier' => $identifier,
|
||
'companyId' => $companyId,
|
||
'behaviorType' => $behaviorType,
|
||
'behaviorName' => self::BEHAVIOR_TYPE_NAMES[$behaviorType] ?? '消息',
|
||
'messageId' => $messageId,
|
||
'wechatAccountId' => $wechatAccountId,
|
||
'extra' => $extra,
|
||
'behaviorTime' => time(),
|
||
'createTime' => time(),
|
||
]);
|
||
|
||
// 更新流量统计
|
||
$poolCompany = TrafficPoolCompany::find($poolCompanyId);
|
||
if ($poolCompany) {
|
||
$poolCompany->incrementMsgCount(1);
|
||
}
|
||
|
||
return $behavior;
|
||
}
|
||
|
||
/**
|
||
* 记录订单行为
|
||
* @param int $poolCompanyId
|
||
* @param string $identifier
|
||
* @param int $companyId
|
||
* @param int $behaviorType
|
||
* @param string $orderId
|
||
* @param float $amount
|
||
* @param array $extra
|
||
* @return static
|
||
*/
|
||
public static function recordOrderBehavior(int $poolCompanyId, string $identifier, int $companyId, int $behaviorType, string $orderId, float $amount = 0, array $extra = [])
|
||
{
|
||
$behavior = self::create([
|
||
'poolCompanyId' => $poolCompanyId,
|
||
'identifier' => $identifier,
|
||
'companyId' => $companyId,
|
||
'behaviorType' => $behaviorType,
|
||
'behaviorName' => self::BEHAVIOR_TYPE_NAMES[$behaviorType] ?? '订单',
|
||
'orderId' => $orderId,
|
||
'amount' => $amount,
|
||
'extra' => $extra,
|
||
'behaviorTime' => time(),
|
||
'createTime' => time(),
|
||
]);
|
||
|
||
// 如果是支付行为,更新订单统计
|
||
if ($behaviorType === self::BEHAVIOR_TYPE_PAY) {
|
||
$poolCompany = TrafficPoolCompany::find($poolCompanyId);
|
||
if ($poolCompany) {
|
||
$poolCompany->incrementOrderStats($amount);
|
||
}
|
||
}
|
||
|
||
return $behavior;
|
||
}
|
||
|
||
/**
|
||
* 记录朋友圈互动行为
|
||
* @param int $poolCompanyId
|
||
* @param string $identifier
|
||
* @param int $companyId
|
||
* @param int $behaviorType 点赞/评论
|
||
* @param int $momentsId
|
||
* @param array $extra
|
||
* @return static
|
||
*/
|
||
public static function recordMomentsBehavior(int $poolCompanyId, string $identifier, int $companyId, int $behaviorType, int $momentsId, array $extra = [])
|
||
{
|
||
return self::create([
|
||
'poolCompanyId' => $poolCompanyId,
|
||
'identifier' => $identifier,
|
||
'companyId' => $companyId,
|
||
'behaviorType' => $behaviorType,
|
||
'behaviorName' => self::BEHAVIOR_TYPE_NAMES[$behaviorType] ?? '朋友圈互动',
|
||
'momentsId' => $momentsId,
|
||
'extra' => $extra,
|
||
'behaviorTime' => time(),
|
||
'createTime' => time(),
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 获取用户行为轨迹
|
||
* @param int $poolCompanyId
|
||
* @param int $limit
|
||
* @return \think\Collection
|
||
*/
|
||
public static function getUserJourney(int $poolCompanyId, int $limit = 50)
|
||
{
|
||
return self::where('poolCompanyId', $poolCompanyId)
|
||
->order('behaviorTime DESC')
|
||
->limit($limit)
|
||
->select();
|
||
}
|
||
}
|
||
|
||
|