<?php
namespace app\store\model\wxapp;
use app\common\exception\BaseException;
use app\store\model\Wxapp as WxappModel;
use app\common\model\wxapp\LiveRoom as LiveRoomModel;
use app\common\library\wechat\live\Room as LiveRoomApi;
class LiveRoom extends LiveRoomModel
{
public function getList($search = '')
{
!empty($search) && $this->where('room_name|anchor_name', 'like', "%{$search}%");
return $this->where('is_delete', '=', 0)
->order([
'is_top' => 'desc',
'live_status' => 'asc',
'create_time' => 'desc'
])
->paginate(15, false, [
'query' => \request()->request()
]);
}
public function setIsTop($isTop)
{
return $this->save(['is_top' => (int)$isTop]);
}
public function refreshLiveList()
{
$originRoomList = $this->getOriginRoomList();
$originRoomIds = $this->getOriginRoomIds($originRoomList);
$localRoomIds = $this->getLocalRoomIds();
$this->refreshLiveNew($localRoomIds, $originRoomIds, $originRoomList);
$this->refreshLiveRemove($localRoomIds, $originRoomIds);
$this->refreshLiveUpdate($localRoomIds, $originRoomIds, $originRoomList);
return true;
}
private function getOriginRoomList()
{
$wxConfig = WxappModel::getWxappCache();
$LiveRoomApi = new LiveRoomApi($wxConfig['app_id'], $wxConfig['app_secret']);
$response = $LiveRoomApi->getLiveRoomList();
if ($response === false) {
throw new BaseException(['msg' => '直播房间列表api请求失败:' . $LiveRoomApi->getError()]);
}
$originRoomList = [];
foreach ($response['room_info'] as $item) {
$originRoomList[$item['roomid']] = $item;
}
return $originRoomList;
}
private function getOriginRoomIds($originRoomList)
{
$originRoomIds = [];
foreach ($originRoomList as $item) {
$originRoomIds[] = $item['roomid'];
}
return $originRoomIds;
}
private function getLocalRoomIds()
{
return $this->where('is_delete', '=', 0)->column('room_id', 'id');
}
private function refreshLiveNew($localRoomIds, $originRoomIds, $originRoomList)
{
$newLiveRoomIds = array_values(array_diff($originRoomIds, $localRoomIds));
if (empty($newLiveRoomIds)) return true;
$saveData = [];
foreach ($newLiveRoomIds as $roomId) {
$item = $originRoomList[$roomId];
$saveData[] = [
'room_id' => $roomId,
'room_name' => $item['name'],
'cover_img' => $item['cover_img'],
'share_img' => $item['share_img'],
'anchor_name' => $item['anchor_name'],
'start_time' => $item['start_time'],
'end_time' => $item['end_time'],
'live_status' => $item['live_status'],
'wxapp_id' => self::$wxapp_id,
];
}
return $this->isUpdate(false)->saveAll($saveData, false);
}
private function refreshLiveUpdate($localRoomIds, $originRoomIds, $originRoomList)
{
$updatedLiveRoomIds = array_values(array_intersect($originRoomIds, $localRoomIds));
if (empty($updatedLiveRoomIds)) return true;
$idArr = $this->getLocalIdsByRoomIds($localRoomIds);
$saveData = [];
foreach ($updatedLiveRoomIds as $roomId) {
$item = $originRoomList[$roomId];
$saveData[] = [
'id' => $idArr[$roomId],
'room_id' => $roomId,
'room_name' => $item['name'],
'cover_img' => $item['cover_img'],
'share_img' => $item['share_img'],
'anchor_name' => $item['anchor_name'],
'start_time' => $item['start_time'],
'end_time' => $item['end_time'],
'live_status' => $item['live_status'],
'wxapp_id' => self::$wxapp_id,
];
}
return $this->isUpdate(true)->saveAll($saveData);
}
private function refreshLiveRemove($localRoomIds, $originRoomIds)
{
$removedLiveRoomIds = array_values(array_diff($localRoomIds, $originRoomIds));
if (empty($removedLiveRoomIds)) return true;
$removedIds = $this->getLocalIdsByRoomIds($localRoomIds, $removedLiveRoomIds);
return self::destroy(array_values($removedIds));
}
private function getLocalIdsByRoomIds($localRoomIds, $searchRoomIds = [])
{
$data = [];
foreach ($localRoomIds as $id => $roomId) {
if (empty($searchRoomIds) || in_array($roomId, $searchRoomIds)) {
$data[$roomId] = $id;
}
}
return $data;
}
}