<?php

namespace app\common\model;

class SinglePageModel extends BaseModel
{
    // 设置表名
    protected $name = 'single_page';

    // 短信配置
    const CODE = [
        'privacy',
        'service',
        'about',
    ];
    const CODE_TITLE = [
        'privacy' => '隐私政策',
        'service' => '用户协议',
        'about' => '关于我们',
    ];

    public static function getConfigValue($code)
    {
        $res = [];
        if (is_array($code)) {
            $list = self::where('code', 'in', $code)->select();
            if ($list->isEmpty()) {
                return [];
            }
            foreach ($list as $v) {
                $res[$v['code']] = $v['value'];
            }
        } elseif (is_string($code)) {
            $info = self::where('code', $code)->find();
            if (empty($info)) {
                return '';
            }
            $res = $info['value'];
        }

        return $res;
    }

    public static function setConfigValue($code, $value = '')
    {
        if (is_array($code)) {
            foreach ($code as $k => $v) {
                self::setConfigValueSingle($k, $v);
            }
        } elseif (is_string($code)) {
            self::setConfigValueSingle($code, $value);
        }
    }

    public static function setConfigValueSingle($code, $value = '')
    {
        if (in_array($code,self::CODE)) {
            $info = self::where('code', $code)->find();
            if (empty($info)) {
                self::create(['code' => $code, 'value' => $value]);
            } else {
                $info->value = $value;
                $info->save();
            }
        }

    }
}