129 lines
3.6 KiB
PHP
129 lines
3.6 KiB
PHP
<?php
|
||
|
||
namespace app\common\model;
|
||
|
||
use think\Model;
|
||
|
||
/**
|
||
* 流量池分组成员表模型类
|
||
* 表名:ck_traffic_pool_group_member
|
||
* 用途:手动添加到分组的成员(ruleType=2时使用)
|
||
*/
|
||
class TrafficPoolGroupMember extends Model
|
||
{
|
||
// 设置数据表名
|
||
protected $name = 'traffic_pool_group_member';
|
||
|
||
// 主键
|
||
protected $pk = 'id';
|
||
|
||
// 自动写入时间戳
|
||
protected $autoWriteTimestamp = 'createTime';
|
||
protected $createTime = 'createTime';
|
||
protected $updateTime = false;
|
||
|
||
// 添加方式常量
|
||
const ADD_TYPE_MANUAL = 1; // 手动
|
||
const ADD_TYPE_IMPORT = 2; // 批量导入
|
||
|
||
/**
|
||
* 关联分组
|
||
*/
|
||
public function group()
|
||
{
|
||
return $this->belongsTo(TrafficPoolGroup::class, 'groupId', 'id');
|
||
}
|
||
|
||
/**
|
||
* 关联公司流量详情
|
||
*/
|
||
public function poolCompany()
|
||
{
|
||
return $this->belongsTo(TrafficPoolCompany::class, 'poolCompanyId', 'id');
|
||
}
|
||
|
||
/**
|
||
* 批量添加成员到分组
|
||
* @param int $groupId
|
||
* @param array $poolCompanyIds
|
||
* @param int $companyId
|
||
* @param int $operatorId
|
||
* @param int $addType
|
||
* @return int 成功添加的数量
|
||
*/
|
||
public static function batchAddMembers(int $groupId, array $poolCompanyIds, int $companyId, int $operatorId = null, int $addType = self::ADD_TYPE_MANUAL)
|
||
{
|
||
$count = 0;
|
||
$time = time();
|
||
|
||
// 获取已存在的成员
|
||
$existIds = self::where('groupId', $groupId)
|
||
->whereIn('poolCompanyId', $poolCompanyIds)
|
||
->where('isDel', 0)
|
||
->column('poolCompanyId');
|
||
|
||
// 获取要添加的流量详情
|
||
$poolCompanies = TrafficPoolCompany::whereIn('id', $poolCompanyIds)
|
||
->where('companyId', $companyId)
|
||
->where('isDel', 0)
|
||
->column('identifier', 'id');
|
||
|
||
$insertData = [];
|
||
foreach ($poolCompanyIds as $poolCompanyId) {
|
||
if (in_array($poolCompanyId, $existIds)) {
|
||
continue; // 跳过已存在的
|
||
}
|
||
if (!isset($poolCompanies[$poolCompanyId])) {
|
||
continue; // 跳过不存在的
|
||
}
|
||
|
||
$insertData[] = [
|
||
'groupId' => $groupId,
|
||
'poolCompanyId' => $poolCompanyId,
|
||
'identifier' => $poolCompanies[$poolCompanyId],
|
||
'companyId' => $companyId,
|
||
'addType' => $addType,
|
||
'operatorId' => $operatorId,
|
||
'createTime' => $time,
|
||
'isDel' => 0,
|
||
];
|
||
}
|
||
|
||
if (!empty($insertData)) {
|
||
(new self())->saveAll($insertData);
|
||
$count = count($insertData);
|
||
|
||
// 更新分组成员数量缓存
|
||
TrafficPoolGroup::where('id', $groupId)->setInc('memberCount', $count);
|
||
}
|
||
|
||
return $count;
|
||
}
|
||
|
||
/**
|
||
* 批量移除成员
|
||
* @param int $groupId
|
||
* @param array $poolCompanyIds
|
||
* @return int
|
||
*/
|
||
public static function batchRemoveMembers(int $groupId, array $poolCompanyIds)
|
||
{
|
||
$count = self::where('groupId', $groupId)
|
||
->whereIn('poolCompanyId', $poolCompanyIds)
|
||
->where('isDel', 0)
|
||
->update([
|
||
'isDel' => 1,
|
||
'deleteTime' => time()
|
||
]);
|
||
|
||
if ($count > 0) {
|
||
// 更新分组成员数量缓存
|
||
TrafficPoolGroup::where('id', $groupId)->setDec('memberCount', $count);
|
||
}
|
||
|
||
return $count;
|
||
}
|
||
}
|
||
|
||
|