<?php
/**
 * Created by PhpStorm.
 * User: ywl
 * Date: 2017/4/14
 * Time: 11:38
 */

namespace App\Web\Services;


use App\Ad\Facades\SysAdFacade;
use App\Common\Facades\UserCenterFacade;
use App\Exceptions\ApiException;
use App\Services\CommonUserBaseService;
use App\Web\Facades\BlogFacade;
use App\Web\Facades\HelpFacade;
use App\Web\Facades\WebFacade;
use App\Web\Models\DictFunctionModel;
use App\Web\Models\DictIndustryModel;
use App\Web\Models\WebSeoModel;
use App\Web\Models\WebStaticPageModel;
use Illuminate\Support\Facades\Cache;


class DictService extends CommonUserBaseService
{
    protected $cache = true;

    protected $cacheBucket = 'Dict:';

    protected $dictIndustryModel;



    /**
     * WebService construct
     * @param DictFunctionModel $model
     * @param DictIndustryModel $dictIndustryModel
     * */
    public function __construct(DictFunctionModel $model,DictIndustryModel $dictIndustryModel)
    {
        $this->model = $model;
        $this->dictIndustryModel = $dictIndustryModel;
    }


    /**
     * 获取功能列表
     * */
    public function getDictFunctionList($params){
        $retData= $this->model->getFunctionList($params);
        return $retData;
    }

    /**
     * 功能保存
     * */
    public function saveDictFunction($params){
        $saveData=[];
        $nowTime=nowTime();
        $saveData['update_time']=$nowTime;
        $userId=$this->getAuthUserId();
        if(!empty($params['id'])){
            $saveData['id']=$params['id'];
            if(isset($params['name'])){
                $nameUnique =$this->model->checkFunctionNameUnique($params['name'],$params['id']);
                if (!$nameUnique) {
                    throw new ApiException(10018, ['name' => $params['name']]);
                }
                $saveData['name']=$params['name'];
            }
            if(isset($params['status'])){
                $saveData['status']=$params['status'];
            }
            if(isset($params['remark'])){
                $saveData['remark']=empty($params['remark'])?'':$params['remark'];
            }
        }else{
            $nameUnique =$this->model->checkFunctionNameUnique($params['name']);
            if (!$nameUnique) {
                throw new ApiException(10018, ['name' => $params['name']]);
            }
            $saveData['user_id']=$userId;
            $saveData['name']=$params['name'];
            $saveData['remark']=empty($params['remark'])?'':$params['remark'];
            $saveData['create_time']=$nowTime;
        }
        $id = $this->model->saveFunctionData($saveData);
        //更新所有页面缓存
        WebFacade::clearPageCache('urla:');
        return $id;
    }

    /**
     * 获取行业列表
     * */
    public function getDictIndustryList($params){
        $retData= $this->dictIndustryModel->getIndustryList($params);
        return $retData;
    }

    /**
     * 行业保存
     * */
    public function saveDictIndustry($params){
        $saveData=[];
        $nowTime=nowTime();
        $saveData['update_time']=$nowTime;
        $userId=$this->getAuthUserId();
        if(!empty($params['id'])){
            $saveData['id']=$params['id'];
            if(isset($params['name'])){
                $nameUnique =$this->dictIndustryModel->checkIndustryNameUnique($params['name'],$params['id']);
                if (!$nameUnique) {
                    throw new ApiException(10018, ['name' => $params['name']]);
                }
                $saveData['name']=$params['name'];
            }
            if(isset($params['status'])){
                $saveData['status']=$params['status'];
            }
            if(isset($params['remark'])){
                $saveData['remark']=empty($params['remark'])?'':$params['remark'];
            }
            if(isset($params['pid'])){
                $saveData['pid']=empty($params['pid'])?0:$params['pid'];;
            }
        }else{
            $nameUnique =$this->dictIndustryModel->checkIndustryNameUnique($params['name']);
            if (!$nameUnique) {
                throw new ApiException(10018, ['name' => $params['name']]);
            }
            $saveData['user_id']=$userId;
            $saveData['pid']=empty($params['pid'])?0:$params['pid'];;
            $saveData['name']=$params['name'];
            $saveData['remark']=empty($params['remark'])?'':$params['remark'];
            $saveData['create_time']=$nowTime;
        }
        $id = $this->dictIndustryModel->saveIndustryData($saveData);
        //更新所有页面缓存
        WebFacade::clearPageCache('urla:');
        return $id;
    }


}
