<?php

namespace app\common\model;

class SettingModel extends BaseModel
{
    // 设置表名
    protected $name = 'setting';

    // 短信配置
    const SMS      = [
        'sms_chuanglan_appkey',
        'sms_chuanglan_secret',
        'sms_type',
        'sms_verify_expire',
    ];
    const SYSTEM   = [
        'site_name',
    ];
    const SMS_TYPE = [
        ['value' => '\\chuanglan\\Chuanglan', 'text' => '创蓝短信'],
    ];
    const TALENT   = [
        'talent_level_1',
        'talent_level_2',
        'talent_level_3',
        'talent_level_4',
        'talent_level_5',
        'talent_level_6',
        'talent_level_7',
        'talent_industry_trade',
        'talent_industry_goods',
        'talent_industry_clothing',
        'talent_industry_drug',
        'talent_industry_medical',
        'talent_industry_hotel',
        'talent_industry_traffic',
        'talent_industry_other',
        'talent_age_1',
        'talent_age_2',
        'talent_age_3',
        'talent_age_4',
        'talent_education_1',
        'talent_education_2',
        'talent_education_3',
        'talent_problem_total',
        'talent_problem_deal',
        'talent_problem_talent',
        'talent_problem_determine',
        'talent_problem_traffic',
        'talent_problem_house',
        'talent_problem_activity',
        'talent_problem_children',
        'talent_problem_spouse',
        'talent_problem_other',
        'talent_channel_call',
        'talent_channel_wechat',
        'talent_channel_door',
        'talent_channel_scene',
        'talent_channel_other',
        'talent_total',
    ];
    const ODD_JOB = [
        'obb_job_deal_num',
        'obb_job_deal_money',
        'obb_job_service_num',
    ];
    const INDUSTRY = [
        'industry_institution_num',
        'industry_institution_turnover',
        'industry_institution_employee',
        'industry_institution_type_software',
        'industry_institution_type_consult',
        'industry_institution_type_train',
        'industry_institution_type_flexible',
        'industry_institution_type_dispatch',
        'industry_institution_type_talent',
        'industry_institution_type_evaluation',
        'industry_institution_type_labor',
        'industry_institution_type_insurance',
        'industry_institution_type_other',
        'industry_enterprise_num',
        'industry_service_num',
    ];
    const INDUSTRY_TYPE = ['软件技术', '管理咨询', '培训教育', '灵活用工', '派遣外包', '人才猎聘', '测评背调', '劳务输出','商业保险','其他'];

    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 = '')
    {
        $info = self::where('code', $code)->find();
        if (empty($info)) {
            self::create(['code' => $code, 'value' => $value]);
        } else {
            $info->value = $value;
            $info->save();
        }
    }
}