流量池服务端

This commit is contained in:
wong
2026-02-02 11:00:26 +08:00
parent f0c278a35f
commit 519a9b1d1b
22 changed files with 7239 additions and 2252 deletions

View File

@@ -0,0 +1,200 @@
<?php
namespace app\common\model;
use think\Model;
use think\Db;
/**
* 公司流量详情表模型类
* 表名ck_traffic_pool_company
* 用途:存储流量在各公司的详细信息,支持多租户
*/
class TrafficPoolCompany extends Model
{
// 设置数据表名
protected $name = 'traffic_pool_company';
// 主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 好友状态常量
const FRIEND_STATUS_NOT_ADDED = 0; // 未加
const FRIEND_STATUS_PENDING = 1; // 待通过
const FRIEND_STATUS_PASSED = 2; // 已通过
const FRIEND_STATUS_DELETED = 3; // 已删除(我删除对方)
const FRIEND_STATUS_BE_DELETED = 4; // 被删除(对方删除我)
// 客户等级常量
const LEVEL_NORMAL = 0; // 普通
const LEVEL_IMPORTANT = 1; // 重要
const LEVEL_VIP = 2; // VIP
// 意向度常量
const INTENTION_UNKNOWN = 0; // 未知
const INTENTION_LOW = 1; // 低
const INTENTION_MEDIUM = 2; // 中
const INTENTION_HIGH = 3; // 高
// 生命周期常量
const LIFECYCLE_NEW = 1; // 新流量
const LIFECYCLE_FOLLOWING = 2; // 跟进中
const LIFECYCLE_CONVERTED = 3; // 已成交
const LIFECYCLE_SILENT = 4; // 沉默
const LIFECYCLE_LOST = 5; // 流失
// 状态常量
const STATUS_DISABLED = 0; // 禁用
const STATUS_NORMAL = 1; // 正常
const STATUS_BLACKLIST = 2; // 黑名单
// 分配状态常量
const ALLOCATE_STATUS_NOT = 0; // 未分配
const ALLOCATE_STATUS_ALLOCATED = 1; // 已分配
const ALLOCATE_STATUS_RECYCLED = 2; // 已回收
/**
* 关联流量池总表
*/
public function pool()
{
return $this->belongsTo(TrafficPoolV2::class, 'poolId', 'id');
}
/**
* 关联来源记录
*/
public function sources()
{
return $this->hasMany(TrafficPoolSource::class, 'poolCompanyId', 'id');
}
/**
* 关联标签记录
*/
public function tags()
{
return $this->hasMany(TrafficPoolTag::class, 'poolCompanyId', 'id');
}
/**
* 关联行为记录
*/
public function behaviors()
{
return $this->hasMany(TrafficPoolBehavior::class, 'poolCompanyId', 'id');
}
/**
* 关联分配记录
*/
public function allotRecords()
{
return $this->hasMany(TrafficPoolAllotRecord::class, 'poolCompanyId', 'id');
}
/**
* 根据identifier和companyId查找或创建
* @param string $identifier
* @param int $companyId
* @param int $poolId
* @param array $data
* @return static
*/
public static function findOrCreateByIdentifierAndCompany(string $identifier, int $companyId, int $poolId, array $data = [])
{
$record = self::where('identifier', $identifier)
->where('companyId', $companyId)
->where('isDel', 0)
->find();
if (!$record) {
$insertData = array_merge([
'poolId' => $poolId,
'identifier' => $identifier,
'companyId' => $companyId,
'createTime' => time(),
], $data);
$record = self::create($insertData);
}
return $record;
}
/**
* 原子更新消息统计
* @param int $count 增加的消息数量
* @return bool
*/
public function incrementMsgCount(int $count = 1)
{
return Db::table($this->getTable())
->where('id', $this->id)
->inc('totalMsgCount', $count)
->inc('rfmF', $count)
->update([
'lastMsgTime' => time(),
'lastInteractTime' => time(),
'updateTime' => time()
]);
}
/**
* 原子更新订单统计
* @param float $amount 订单金额
* @return bool
*/
public function incrementOrderStats(float $amount)
{
return Db::table($this->getTable())
->where('id', $this->id)
->inc('totalOrderCount', 1)
->inc('totalOrderAmount', $amount)
->inc('rfmM', $amount)
->update([
'lastOrderTime' => time(),
'lastInteractTime' => time(),
'updateTime' => time()
]);
}
/**
* 计算RFM R值最后互动距今天数
* @return int
*/
public function getRfmRAttr()
{
if (empty($this->lastInteractTime)) {
return 9999; // 未互动过
}
return (int) floor((time() - $this->lastInteractTime) / 86400);
}
/**
* 获取自定义字段
* @param string $value
* @return array
*/
public function getCustomFieldsAttr($value)
{
return $value ? json_decode($value, true) : [];
}
/**
* 设置自定义字段
* @param array $value
* @return string
*/
public function setCustomFieldsAttr($value)
{
return $value ? json_encode($value, JSON_UNESCAPED_UNICODE) : null;
}
}