<?php


namespace App\Web\Models;



use App\Models\BaseModel;
use Illuminate\Support\Facades\DB;


class SysGlobalConfigModel extends BaseModel
{

    /**
     * 状态字段
     */
    const DELETED_AT = 'status';

    /**
     * @var string
     */
    protected $table = 'sys_global_config';

    /**
     * 获取全局配置列表
     * */
    public function getGlobalConfigList($params){
        $where=[];
        if(isset($params['status'])){
            $where[]=['a.status','=',$params['status']];
        }else{
            $where[]=['a.status','<',2];
        }
        $query= $this->newInstance()->alias('a')->where($where);
        $list= $query->selectRaw('a.*')
            ->orderBy('id')
            ->get();
        if(!empty($list)){
            $list=$list->toArray();
        }else{
            $list=[];
        }
        return $list;
    }

    /**
     * 文章全局配置数据保存
     * @param array $data
     * */
    public function saveGlobalConfigData($data){
        if (!empty($data['id'])) {
            $id = $data['id'];
            $this->newInstance()->where('id', $data['id'])->update($data);
        } else {
            $id = $this->newInstance()->insertGetId($data);
        }
        return $id;
    }


    /**
     * 获取全局配置详情
     * @param array $params
     * */
    public function getGlobalConfigInfo($params=[]){
        if(empty($params)){
            return [];
        }
        $where=[];
        $where[]=['status','<',2];
        if(!empty($params['id'])){
            $where['id']=$params['id'];
        }
        if(!empty($params['global_key'])){
            $where['global_key']=$params['global_key'];
        }
        $info= $this->newInstance()->where($where)->first();
        if(!empty($info)){
            return $info->toArray();
        }else{
            return [];
        }
    }

    /**
     * 检查 global_key是否唯一
     * */
    public function checkGlobalKeyUnique($globalKey,$id=0){
        $where=[];
        if(!empty($id)){
            $where[]=['id', '<>', $id];
        }
        $where[]=['status', '<', 2];
        $where[]=['global_key', '=', trim($globalKey)];
        return $this->checkFieldUnique('global_key',$where);
    }


}
