<?php

namespace app\common\model;

use think\Model;
use Redis;
/**
 * 模型
 */
class Party extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    /**
     * 根据partyid 获取 party 信息
     */
    public function getPatyInfoByPartyId($party_id,$room_type,$type_id,$is_recommend,$all,$start = 0,$end = 9,$index) {
        if(!$party_id) return false;
        // 判断派对是否存在
        $redis = new Redis();
        $redisconfig = config("redis");
        $redis->connect($redisconfig["host"], $redisconfig["port"]);
        if(!is_array($party_id)) {
            return [];
        }
        $redisPartyInfo = [];
        foreach($party_id as $k => $v) {
            $getredisPartyInfo = $redis->get($room_type.'_'.$k);

            $foreachData = json_decode($getredisPartyInfo,true);

            if(!$foreachData || $foreachData["is_close"] == 1) continue;
            if(!$foreachData || $foreachData["is_online"] == 0) continue;
            if(!$foreachData || $foreachData["party_pass"] != '') continue;
            $mod = isset($foreachData["party_type"])?intval($foreachData["party_type"])%5:1;
            if(isset($foreachData["type_name"]) && $foreachData["type_name"]) {
                $type_name = $foreachData["type_name"];
            } else {
                $type_name = "普通房";
            }

            $hasdata = false;
            if($all == 1) {
                if($is_recommend == 1) {
                    if($foreachData["is_recommend"] == 1) $hasdata = true;
                } else {
                    $hasdata = true;
                }
            } elseif($type_id > 0) {
                if($type_id == $foreachData["party_type"]) $hasdata = true;
            }
            if($foreachData["status"] != 1) $hasdata = false;
            $hasdata && $redisPartyInfo[] = [
                "id" => $foreachData["id"],
                "party_id" => $foreachData["party_id"],
                "r_id" => $foreachData["party_id"],
                "party_name" => $foreachData["party_name"],
                "party_pass" => $foreachData["party_pass"]?$foreachData["party_pass"]:"",
                "party_hot" => $v,
                "party_logo" => $this->httpurl($foreachData["party_logo"]),
                "party_type" => $type_name,
                "avatar" => $room_type == 2 ? $foreachData["avatar"]:"",
                "party_type_color" => $mod == 0?5:$mod,
                "party_user" => isset($foreachData["party_user"]) ? array_values($foreachData["party_user"]) : [] // 当前麦位人头像
            ];

        }

        $resdata = [];
        if($index == 1) {
            $num = $end - $start + 1;
            $countData = count($redisPartyInfo);
            if($num > $countData) $num = $countData;
            if($redisPartyInfo) {
                $dataRand = array_rand($redisPartyInfo,$num);
                $dataRand = is_array($dataRand)?$dataRand:[$dataRand];
                if($redisPartyInfo) foreach($redisPartyInfo as $k => $v) if(in_array($k,$dataRand)) $resdata[] = $v;
            }

        } else {
            if($redisPartyInfo) foreach($redisPartyInfo as $k => $v) if($k >= $start && $k <= $end) $resdata[] = $v;
        }

        return $resdata;
    }

    /**
     * 判断当前url是否为全路径，并返回全路径
     */
    protected function httpurl($path) {
        // 获取当前域名
        if(strpos($path,'http://') === false && strpos($path,'https://') === false && strpos($path,'uploads') !== false) {
            $host = config("cos")['url'];
            $url = $host.$path;
        } else {
            $url = $path;
        }
        return $url;
    }
}
