<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/8/18
 * Time: 上午10:46
 * 手机端和PC端楼盘相关共用部分
 */

namespace common\fm;

use common\models\CategoryTelCity;
use common\models\Characteristic;
use common\models\HousePriceRecord;
use Yii;
use yii\helpers\ArrayHelper;

class   HouseHandle
{
    /**
     * 楼盘首页没有区域需要用到的跳转(商务通需要用到)
     */
    public function CityLocation()
    {
        $view = Yii::$app->view;
        $mainUrl = $view->params['main_city_url'];


        if (!is_numeric($_GET['hid'])) {
            $ex = explode('-', $_GET['hid']);
            if (!empty($ex[1]) && is_numeric($ex[1])) {
                $_GET['hid'] = $ex[1];
            } else {
                header('Location:/');
                exit;
            }
        } else {
            $inp = Yii::$app->request->get();
            $m = new \common\models\House();
            $r = $m->FindById($inp['hid']);

            if ($r) {
                $city = \common\models\CategoryCity::findOne($r->city);
                if ($city['pid'] != 0) {
                    $father_city = \common\models\CategoryCity::findOne($city['pid']);
                    if (!empty($father_city['a'])) {
                        $httpUrl = 'http://' . $father_city['a'] . '.' . $mainUrl;
                    }
                }
                header('Location:' . $httpUrl . '/house/' . $city['pinyin'] . '-' . $inp['hid'] . '/');
                exit;
            }

//            $url = Yii::$app->request->getHostInfo();
            header('Location:http://www.' . $mainUrl . '/house/default-' . $inp['hid'] . '/');
            exit;
        }

    }


    /**
     * 楼盘首页没有区域需要用到的跳转(商务通需要用到)
     */
    public function MobileCityLocation()
    {

        $view = Yii::$app->view;
        $mainUrl = $view->params['main_city_url'];


        if (!is_numeric($_GET['hid'])) {
            $ex = explode('-', $_GET['hid']);
            if (!empty($ex[1]) && is_numeric($ex[1])) {
                $_GET['hid'] = $ex[1];
            } else {
                header('Location:/');
                exit;
            }
        } else {
            $inp = Yii::$app->request->get();
            $m = new \common\models\House();
            $r = $m->FindById($inp['hid']);

            if ($r) {
                $city = \common\models\CategoryCity::findOne($r->city);
                if ($city['pid'] != 0) {
                    $father_city = \common\models\CategoryCity::findOne($city['pid']);
                    if (!empty($father_city['a'])) {
                        $httpUrl = 'http://m' . $father_city['a'] . '.' . $mainUrl;
                    }
                }
                header('Location:' . $httpUrl . '/house/' . $city['pinyin'] . '-' . $inp['hid'] . '/');
                exit;
            }

//            $url = Yii::$app->request->getHostInfo();
            header('Location:http://m.' . $mainUrl . '/house/default-' . $inp['hid'] . '/');
            exit;
        }

    }

    //转换特色主题
    public static function ChangeCharacteristic($Characteristic)
    {
        if (empty($Characteristic)) return [];
        $cacheName = 'params:characteristic';
        $cache = Yii::$app->redis;
        $cacheContent = $cache->get($cacheName);
        $arr = [];
        if ($cacheContent) {
            $cid = json_decode($cacheContent, true);
        } else {
            $characteristic = new \common\models\Characteristic();
            $row = $characteristic->getListAll();
            $cid = array_column($row, 'name', 'id');
            $cache->set($cacheName, json_encode($cid));
        }

        if ($cid) {
            if (!empty($Characteristic) && is_string($Characteristic)) {
                $cha = json_decode($Characteristic, true);
                if (count($cha) > 3) {
                    $rand_keys = array_rand($cha, 3);
                    isset($cid[$cha[$rand_keys[0]]]) ? $arr[] = $cid[$cha[$rand_keys[0]]] : '';
                    isset($cid[$cha[$rand_keys[1]]]) ? $arr[] = $cid[$cha[$rand_keys[1]]] : '';
                    isset($cid[$cha[$rand_keys[2]]]) ? $arr[] = $cid[$cha[$rand_keys[2]]] : '';
                } else {
                    foreach ($cha as $v) {
                        isset($cid[$v]) ? $arr[] = $cid[$v] : '';
                    }
                }
            }
        }
        return $arr;
    }

    //随机电话
    public static function ChangeCityTel($city)
    {
        $cacheName = 'params:citytel';
        $cache = Yii::$app->redis;
        $cacheContent = $cache->get($cacheName);
        if ($cacheContent) {
            $cityTel = json_decode($cacheContent, true);
        } else {
            $Tel = new \common\models\CategoryTelCity();
            $telRow = $Tel->QgetAll();
            if ($telRow) {
                $cityTel = [];
                foreach ($telRow as &$v) {
                    $cityTel[$v['cid']] = explode(',', $v['tel']);
                }
                $cache->set($cacheName, json_encode($cityTel));
            }
        }

        if (isset($cityTel[$city]) && !empty($cityTel[$city])) {
            $rand = array_rand($cityTel[$city], 1);
            return $cityTel[$city][$rand];
        }
        return Yii::$app->params['default_dialtel'];
    }


    //楼盘价格走势
    public static function HousePrice($Id)
    {
        $list = (new HousePriceRecord())->GetPrice($Id);
        if ($list != null) {
            $arr = [];
            foreach ($list as $val) {
                $arr['categories'][] = $val['create_time'];
                $arr['data'][] = $val['price'];
                $arr['unit'][] = $val['price_unit'];
            }
            return $arr;
        }
    }

    /**
     * 根据价格单位返回是均价还是总价
     * @param $priceunit
     * @return string
     */
    public static function priceUnit($priceunit)
    {
        if (strpos($priceunit, '㎡') !== false) {
            return '均';
        }

        if (strpos($priceunit, '套') !== false) {
            return '总';
        }
    }
	
	//楼盘随机电话
	public static function RandTel($city)
	{
		$tel = new CategoryTelCity();
		$tel->cid = $city;
		$telModel = $tel->CityTelOne();
		if ($telModel != null) {
			$telArr = explode(',', $telModel['tel']);
			$randNum = array_rand($telArr, 1);
			return $telArr[$randNum];
		}
	}
	
	//特色主题 超过3个随机返回3个元素
	public static function Subject($id, $num = 3)
	{
		$char = new Characteristic();
		if (is_array($id) && !empty($id)) {
			$char->id = $id;
			$charMode = array_column($char->getList([], ['id', 'name']), 'name', 'id');
			if ($charMode != null) {
				$count = count($charMode);
				$arr = [];
				if ($count >= 3) {
					$rand = array_rand($charMode, $num);
					foreach ($rand as $key => $val) {
						$arr[$key] = $charMode[$val];
					}
				} else {
					$arr = $charMode;
				}
				return $arr;
			}
		}
	}

}