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(); } }