<?php
/**
 * 地区数据(中国)
 * Created by PhpStorm.
 * User: QianNiao-C
 * Date: 2019/10/26
 * Time: 9:57
 */

namespace Jindouyun\Controller\Common;

use Mall\Framework\Core\ErrorCode;

use JinDouYun\Cache\SysAreaChinaCache;
use Jindouyun\Controller\BaseController;
use JinDouYun\Model\SysAreaChina\MSysAreaChina;


class SysAreaChina extends BaseController
{
    private $objMSysAreaChina;
    private $objSysAreaChinaCache;

    public function __construct($isCheckAcl = false, $isMustLogin = false)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMSysAreaChina = new MSysAreaChina();
        $this->objSysAreaChinaCache = new SysAreaChinaCache('mapping');
    }

    /**
     * 获取所有省份
     */
    public function getAllProvince()
    {
        if($this->objSysAreaChinaCache->isHaveProvinceKey()) {
            $returnData = $this->objSysAreaChinaCache->getAllProvince();//获取缓存
        }else{
            $result = $this->objMSysAreaChina->getAllProvince();
            if (!$result->isSuccess()) {
                parent::sendOutput($result->getData(), ErrorCode::$dberror);
            }
            $returnData = $result->getData();
            if(!empty($returnData)){
                $this->objSysAreaChinaCache->cacheAllProvince($returnData);
            }
        }

        parent::sendOutput($returnData, 0, ['pageTotal'=>count($returnData)]);
    }

    /**
     * 根据省份id获取所有城市
     */
    public function getAllCityByProvinceCode()
    {
        $code = $this->request->param('request_id');
        if (empty($code)) {
            $this->sendOutput('请指定省份id', ErrorCode::$paramError);
        }

        if($this->objSysAreaChinaCache->isHaveCityKeyByProvinceCode($code)){
            $returnData = $this->objSysAreaChinaCache->getCityByProvinceCode($code);
        }else{
            $result = $this->objMSysAreaChina->getAllCityByProvinceCode(['pcode' => $code]);
            if (!$result->isSuccess()) {
                parent::sendOutput($result->getData(), ErrorCode::$dberror);
            }
            $returnData = $result->getData();
            if(!empty($returnData)){
                $this->objSysAreaChinaCache->cacheCityByCode($returnData, $code);
            }
        }

        parent::sendOutput($returnData, 0, ['pageTotal'=>count($returnData)]);
    }

    /**
     * 获取指定城市下的所有区
     */
    public function getAllAreaByCityCode()
    {
        $code = $this->request->param('request_id');
        if (empty($code)) {
            $this->sendOutput('请指定城市id', ErrorCode::$paramError);
        }


        if($this->objSysAreaChinaCache->isHaveAreaKeyByCityCode($code)){
            $returnData = $this->objSysAreaChinaCache->getAreaByCityCode($code);
        }else{
            $result = $this->objMSysAreaChina->getAllAreaByCityCode(['pcode' => $code]);
            if (!$result->isSuccess()) {
                parent::sendOutput($result->getData(), ErrorCode::$dberror);
            }
            $returnData = $result->getData();
            if(!empty($returnData)){
                $this->objSysAreaChinaCache->cacheAreaByCode($returnData, $code);
            }
        }

        parent::sendOutput($returnData, 0, ['pageTotal'=>count($returnData)]);
    }
}