流量池服务端

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,168 @@
<?php
namespace app\common\model;
use think\Model;
/**
* 流量分配记录表模型类
* 表名ck_traffic_pool_allot_record
* 用途:记录流量的分配历史
*/
class TrafficPoolAllotRecord extends Model
{
// 设置数据表名
protected $name = 'traffic_pool_allot_record';
// 主键
protected $pk = 'id';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
// 分配类型常量
const ALLOT_TYPE_FIRST = 1; // 首次分配
const ALLOT_TYPE_REASSIGN = 2; // 重新分配
const ALLOT_TYPE_RECYCLE = 3; // 回收后分配
// 状态常量
const STATUS_ACTIVE = 1; // 生效中
const STATUS_EXPIRED = 2; // 已过期
const STATUS_RECYCLED = 3; // 已回收
/**
* 关联公司流量详情
*/
public function poolCompany()
{
return $this->belongsTo(TrafficPoolCompany::class, 'poolCompanyId', 'id');
}
/**
* 创建分配记录
* @param int $poolCompanyId
* @param string $identifier
* @param int $companyId
* @param string $toWechatId
* @param int $toAccountId
* @param int $toUserId
* @param int $expireDays
* @param int $operatorId
* @param array $fromInfo [fromWechatId, fromAccountId, fromUserId]
* @return static
*/
public static function createAllotRecord(
int $poolCompanyId,
string $identifier,
int $companyId,
string $toWechatId,
int $toAccountId = null,
int $toUserId = null,
int $expireDays = 30,
int $operatorId = null,
array $fromInfo = []
) {
// 判断分配类型
$existRecord = self::where('poolCompanyId', $poolCompanyId)
->where('status', self::STATUS_ACTIVE)
->find();
$allotType = self::ALLOT_TYPE_FIRST;
if ($existRecord) {
// 将原记录设为已回收
$existRecord->save([
'status' => self::STATUS_RECYCLED,
'updateTime' => time()
]);
$allotType = self::ALLOT_TYPE_REASSIGN;
}
// 检查之前是否有过分配记录(判断是否回收后分配)
$hasHistoryRecord = self::where('poolCompanyId', $poolCompanyId)
->where('status', 'in', [self::STATUS_EXPIRED, self::STATUS_RECYCLED])
->count();
if ($hasHistoryRecord && $allotType === self::ALLOT_TYPE_FIRST) {
$allotType = self::ALLOT_TYPE_RECYCLE;
}
$expireTime = $expireDays > 0 ? time() + ($expireDays * 86400) : null;
$record = self::create([
'poolCompanyId' => $poolCompanyId,
'identifier' => $identifier,
'companyId' => $companyId,
'allotType' => $allotType,
'fromWechatId' => $fromInfo['fromWechatId'] ?? null,
'fromAccountId' => $fromInfo['fromAccountId'] ?? null,
'fromUserId' => $fromInfo['fromUserId'] ?? null,
'toWechatId' => $toWechatId,
'toAccountId' => $toAccountId,
'toUserId' => $toUserId,
'expireDays' => $expireDays,
'expireTime' => $expireTime,
'status' => self::STATUS_ACTIVE,
'operatorId' => $operatorId,
'createTime' => time(),
]);
// 更新公司流量详情表的归属信息
TrafficPoolCompany::where('id', $poolCompanyId)->update([
'ownerWechatId' => $toWechatId,
'ownerAccountId' => $toAccountId,
'ownerUserId' => $toUserId,
'allocateStatus' => TrafficPoolCompany::ALLOCATE_STATUS_ALLOCATED,
'allocateTime' => time(),
'expireTime' => $expireTime,
'updateTime' => time()
]);
return $record;
}
/**
* 回收分配
* @param int $poolCompanyId
* @param int $operatorId
* @return bool
*/
public static function recycleAllot(int $poolCompanyId, int $operatorId = null)
{
// 更新当前生效的分配记录
$activeRecord = self::where('poolCompanyId', $poolCompanyId)
->where('status', self::STATUS_ACTIVE)
->find();
if ($activeRecord) {
$activeRecord->save([
'status' => self::STATUS_RECYCLED,
'updateTime' => time()
]);
}
// 更新公司流量详情表
return TrafficPoolCompany::where('id', $poolCompanyId)->update([
'ownerWechatId' => null,
'ownerAccountId' => null,
'ownerUserId' => null,
'allocateStatus' => TrafficPoolCompany::ALLOCATE_STATUS_RECYCLED,
'expireTime' => null,
'updateTime' => time()
]);
}
/**
* 获取流量的分配历史
* @param int $poolCompanyId
* @return \think\Collection
*/
public static function getAllotHistory(int $poolCompanyId)
{
return self::where('poolCompanyId', $poolCompanyId)
->order('createTime DESC')
->select();
}
}