Files
CKB-Interface/application/common/model/TrafficPoolV2.php
2026-02-04 11:02:33 +08:00

86 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\model;
use think\Model;
/**
* 流量池总表模型类V2版本
* 表名ck_traffic_pool
* 用途:存储全局唯一的流量标识,不区分公司
*/
class TrafficPoolV2 extends Model
{
// 设置数据表名(不带前缀)
protected $name = 'traffic_pool';
// 主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 标识类型常量
const IDENTIFIER_TYPE_WECHAT_ID = 1; // 微信ID
const IDENTIFIER_TYPE_WECHAT_ALIAS = 2; // 微信号
const IDENTIFIER_TYPE_MOBILE = 3; // 手机号
// 性别常量
const GENDER_UNKNOWN = 0;
const GENDER_MALE = 1;
const GENDER_FEMALE = 2;
/**
* 关联公司流量详情
*/
public function companies()
{
return $this->hasMany(TrafficPoolCompany::class, 'poolId', 'id');
}
/**
* 根据identifier查找或创建流量记录
* @param string $identifier 唯一标识
* @param array $data 额外数据
* @return static
*/
public static function findOrCreateByIdentifier(string $identifier, array $data = [])
{
$record = self::where('identifier', $identifier)->find();
if (!$record) {
$insertData = array_merge([
'identifier' => $identifier,
'identifierType' => self::IDENTIFIER_TYPE_WECHAT_ID,
'createTime' => time(),
], $data);
// 如果identifier是微信ID同时设置wechatId
if (empty($insertData['wechatId']) && $insertData['identifierType'] == self::IDENTIFIER_TYPE_WECHAT_ID) {
$insertData['wechatId'] = $identifier;
}
$record = self::create($insertData);
}
return $record;
}
/**
* 更新基础信息
* @param array $data
* @return bool
*/
public function updateBasicInfo(array $data)
{
$allowFields = ['nickname', 'avatar', 'gender', 'region', 'country', 'province', 'city', 'signature', 'wechatAlias', 'mobile'];
$updateData = array_intersect_key($data, array_flip($allowFields));
$updateData['updateTime'] = time();
$updateData['lastSeenTime'] = time();
return $this->save($updateData);
}
}