SystemConfig.php 16.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 模型层:系统-参数配置 模型类
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Models\System
 * @package   App\Models\System
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年7月28日13:35:44
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Models\System;

use App\Models\BaseModel;
use App\Models\User\User;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

/**
 * Class SystemConfig
 *
 * @category  App\Models\System
 * @package   App\Models\System
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年7月28日13:35:44
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class SystemConfig extends BaseModel
{
    // 指定数据库表
    const TABLE = 'system_configs';
    protected $table = self::TABLE ;

    // 指定对象显示名称:方便系统统一查询和做其他处理
    const OBJ_NAME      = 'system-config';
    const OBJ_NAME_ZH   = '系统参数配置';

    /**
     * 业务常量配置
     */
    const KEY_OPTIONS = [
        [
            'key'   => 'belonged_org',
            'type'  => 'single_select',
        ],
        [
            'key'   => 'operation_org',
            'type'  => 'single_select',
        ],
        [
            'key'   => 'class_sponsor',
            'type'  => 'single_select',
        ],
        [
            'key'   => 'class_mode',
            'type'  => 'single_select',
        ],
        [
            'key'   => 'class_type',
            'type'  => 'single_select',
        ],
        [
            'key'   => 'class_policy',
            'type'  => 'single_select',
        ],
        [
            'key'   => 'class_location',
            'type'  => 'single_select',
        ],
        [
            'key'   => 'whether_lunch',
            'type'  => 'single_select',
        ],
        [
            'key'   => 'whether_nap',
            'type'  => 'single_select',
        ],
        [
            'key'           => 'class_service_hours',
            'type'          => 'single_select',
            'value_type'    => 'number',
        ],
        [
            'key'           => 'outdoor_play_facilities',
            'type'          => 'single_multiple_select',
            'other'         => 'text',
        ],
        [
            'key'           => 'indoor_play_facilities',
            'type'          => 'single_multiple_select',
            'other'         => 'text',

        ],
        [
            'key'           => 'family_committee',
            'type'          => 'single_multiple_select',
        ],
        [
            'key'           => 'caregiver_relationship',
            'type'          => 'single_select',
            'other'         => 'text',

        ],
        [
            'key'           => 'guardian_relationship',
            'type'          => 'single_select',
            'other'         => 'text',

        ],
        [
            'key'           => 'graduated_reason',
            'type'          => 'single_select',
        ],
        [
            'key'           => 'whether_file_card_account',
            'type'          => 'single_select',
        ],
        [
            'key'           => 'whether_five_guarantee_account',
            'type'          => 'single_select',
        ],
        [
            'key'           => 'whether_low_income_household',
            'type'          => 'single_select',
        ],
        [
            'key'           => 'whether_allergic',
            'type'          => 'single_multiple_select',
            'other'         => 'text',
        ],
        [
            'key'           => 'whether_disability',
            'type'          => 'single_multiple_select',
            'other'         => 'text',
        ],
        [
            'key'           => 'whether_left_behind_children',
            'type'          => 'single_to_single',
        ],

        // 教师
        [
            'key'           => 'marriage',
            'type'          => 'single_select',
        ],
        [
            'key'           => 'employment_type',
            'type'          => 'single_select',
        ],
        [
            'key'           => 'education',
            'type'          => 'single_select',
            'other'         => 'text',
        ],
        [
            'key'           => 'major',
            'type'          => 'single_select',
            'other'         => 'text',
        ],
        [
            'key'           => 'whether_reading',
            'type'          => 'single_to_single',
        ],
        [
            'key'           => 'qualification_certificate',
            'type'          => 'single_multiple_select',
            'other'         => 'text',
        ],

    ];



    protected $casts = [
        'value' => 'json',
    ];

    public function getValueAttribute($value)
    {
        return array_values(json_decode($value, true) ?: []);
    }

    public function setValueAttribute($value)
    {
        $this->attributes['value'] = my_json_encode(array_values($value));
    }


    /**
     * @return HasMany
     */
    public function items(): HasMany
    {
        return $this->hasMany(SystemConfigItem::class, 'config_id');
    }

    /**
     * @return array
     */
    public static function getGenderOptions(): array
    {
        $option = User::GENDER_OPTIONS;
        foreach ($option as &$item) {
            $item = __($item);
        }

        return  $option;
    }

    /**
     * @param string $type
     * @return array
     */
    public static function getRadioOption($type = 'yes'): array
    {
        if ($type === 'have') {
            return [
                0 => __('not'),
                1 => __('have')
            ];
        }
        return [
            0 => __('no'),
            1 => __('yes')
        ];
    }

    /**
     * @param string $type
     * @return array
     */
    public static function getRadioColorOption(): array
    {
        return [
            0 => 'info',
            1 => 'success'
        ];
    }

    /**
     * @return array
     */
    public static function  getMarriedOption(): array
    {
        $option = User::MARRIAGE_OPTIONS;
        foreach ($option as &$item) {
            $item = __($item);
        }

        return  $option;
        return [
            0 => __('unmarried'),
            1 => __('married')
        ];
    }

    /**
     * 根据区域的code获取区域的的代码集:主要为laravel-admin select组件使用
     *
     * @return array
     */
    public static function getKeyOptions()
    {
        $list = self::KEY_OPTIONS;
        $options = [];
        foreach ($list as $item) {
            $options[$item['key']] = __($item['key']);
        }

        return $options;
    }

    /**
     * 根据区域的code获取区域的的代码集:主要为laravel-admin select组件使用
     *
     * @return array
     */
    public static function getValuesByValueKey($key, $value_key)
    {
        $model = self::where('key', $key)->first();
        $value = $model->value;
        $value_keys = [];
        if ($value) {
            dump($value);
            dump($value_key);
            foreach ($value as $item) {
//                if (in_array($item['value_zh'], $value_zh_arr)) {
//                    $value_keys[] = $item['value_key'];
//                }
            }
        }


        return $value_keys;
    }

    /**
     *
     * @param $key
     * @param $value_zh
     * @return array
     */
    public static function getKeValueByValueZH($key, $value_zh)
    {
        $model = self::where('key', $key)->first();
        $value = $model->value;
        $value_key = '';
        if ($value) {
            foreach ($value as $item) {
                if ($item['value_zh'] === $value_zh) {
                    $value_key = $item['value_key'];
                    break;
                }
            }
        }

        return $value_key;
    }

    /**
     * 根据区域的code获取区域的的代码集:主要为laravel-admin select组件使用
     *
     * @return array
     */
    public static function getValueKeysByValueZH($key, $value_zh)
    {
        $model = self::where('key', $key)->first();
        $value = $model->value;
        $value_keys = [];
        if ($value) {
            $value_zh_arr = explode(',', $value_zh);
            foreach ($value as $item) {
                if (in_array($item['value_zh'], $value_zh_arr)) {
                    $value_keys[] = $item['value_key'];
                }
            }
        }

        if (Str::contains($value_zh, ',其他:')) {
            $value_keys[] = 'other';
        }
        return $value_keys;
    }

    /**
     * 根据区域的code获取区域的的代码集:主要为laravel-admin select组件使用
     *
     * @return string
     */
    public static function getValueOtherByValueZH($value_zh)
    {
        $separator = ',其他:';
        if (Str::contains($value_zh, $separator)) {
            return Arr::last(explode($separator, $value_zh));
        }

        return '';
    }

    /**
     * 获取单选空间展示的信息
     *
     * @param $key
     * @param $value
     * @param array $option
     * @return string|null
     */
    public static function getSingleSelectShowValue($key, $value, $option = [])
    {
        if (!$option) {
            $option = self::getSelectOptions($key);
        }
        $str = Arr::get($option, $value);

        return $str;
    }

    /**
     * 单选 到 输入框 显示
     *
     * @param string $key 配置 key
     * @param int $value 值
     * @param string $supplement 补充信息
     * @return string|null
     */
    public static function getSingleSelect2TextShowValue($key, $value, $supplement)
    {
        if ($value !== 'other') {
            $option = self::getSelectOptions($key);
            $str = Arr::get($option, $value);
        } else {
            $str = '其他:'.$supplement;
        }
        return $str;
    }

    /**
     * 根据 key-value 获取显示
     *
     * @param string $key 配置 key
     * @param int $value  值
     * @param string $supplement 补充信息
     * @param string $other other 信息
     * @return string|null
     */
    public static function getSingleSelect2SingleSelectShowValue($key, $value, $supplement, $other = '')
    {
        if ($value == config('constants.STATUS_OK')) {
            $option = self::getSelectOptions($key);
            $str = Arr::get($option, $supplement);
            if ($other) {
                $str .=':'.$other;
            }
        } else {
            $str = __('no');
        }
        return $str;
    }

    /**
     * 根据 key-value 获取显示
     *
     * @param string $key 配置 key
     * @param int $value  值
     * @param array $supplement 补充信息
     * @param string $other other 信息
     * @return string|null
     */
    public static function getSingleSelect2MultipleSelectShowValue($key, $value, $supplement, $other = '')
    {
        if ($value == config('constants.STATUS_OK')) {
            $option = self::getSelectOptions($key);
//            dump($key, $value, $supplement, $option);

            $str = '';
            if ($supplement) {
                foreach ($supplement as $item) {
                    $str .= ',' .Arr::get($option, $item);
                    if ($item === 'other') {
                        $str .=':'.$other;
                    }
                }
            }
            $str = ltrim($str, ',');
        } else {
            $str = __('no');
        }
//        dump($str);
//        dump('---------------------');
        return $str;
    }

    /**
     * 展示 多选 到 输入框
     *
     * @param string $key 配置 key
     * @param array $value 值
     * @param string $supplement 补充信息
     * @return string|null
     */
    public static function getMultipleSelect2TextShowValue($key, $value, $supplement)
    {
        if (is_array($value) && $value) {
            $option = self::getSelectOptions($key);
            $str = '';
            foreach ($value as $item) {
                $str .= ',' .Arr::get($option, $item);
                if ($item === 'other') {
                    $str .=':'.$supplement;
                }
            }
            $str = ltrim($str, ',');
        } else {
            $str = __('no');
        }
        return $str;
    }

    /**
     * 根据 key-value 获取显示
     *
     * @param $value
     * @param $supplement
     * @return string|null
     */
    public static function getShowValueInput($value, $supplement)
    {
        if ($value === config('constants.STATUS_OK')) {
            $str = '';
            if ($supplement) {
                foreach ($supplement as $item) {
                    $str .= ',' .Arr::get($item, 'name') . ':' . Arr::get($item, 'mobile');
                }
            }
            $str = ltrim($str, ',');
        } else {
            $str = __('no');
        }
        return $str;
    }

    /**
     * 根据区域的code获取区域的的代码集:主要为laravel-admin select组件使用
     *
     * @param string $key
     * @return array
     */
    public static function getSelectOptions($key = '')
    {
        if (!$key) {
            return [];
        }

        $language = request()->cookie(config('constants.COOKIE_KEY'), 'zh-CN');

        $cache_key = 'system-config-select'. $key .'-'. $language;

        // 从缓存中获取配置项
        $options = Cache::get($cache_key);
//        $options = [];

        if (!$options) {
            $model = self::where('status', config('constants.STATUS_OK'))->where('key', $key)->first();

            if (!$model) {
                return [];
            }
            $type = $model->type;
            $value = $model->value;
            $options = [];


            foreach ($value as $item) {
                if ($language === 'en') {
                    $options[$item['value_key']] = $item['value_en'];
                } else {
                    $options[$item['value_key']] = $item['value_zh'];
                }
            }

            $key_options = self::KEY_OPTIONS;

            $key_option = [];
            foreach ($key_options as $option) {
                if ($key === $option['key']) {
                    $key_option = $option;
                    break;
                }
            }
            if (key_exists('other', $key_option)) {
                $options['other'] = __('other');
            }

            Cache::put($cache_key, $options);
        }


        return $options;
    }

    /**
     * 根据区域的code获取区域的的代码集:主要为laravel-admin select组件使用
     *
     * @param string $key
     * @return array
     */
    public static function getSwitchOptions($key = '')
    {
        if (!$key) {
            return [];
        }

        // TODO
        $language = Cache::get('language', 'zh-CN');
        $language = request()->cookie(config('constants.COOKIE_KEY'), 'zh-CN');

        $cache_key = 'system-config-switch'. $key .'-'. $language;

        // 从缓存中获取配置项
        $options = Cache::get($cache_key);
        if (!$options) {
            $model = self::where('status', config('constants.STATUS_OK'))->where('key', $key)->first();
            $type = $model->type;
            $value = $model->value;
            $options = [];

//        if ($type === 'single_select') {
//            foreach ($value as $item) {
//                if ($language === 'en') {
//                    $options[$item['value_key']] = $item['value_en'];
//                } else {
//                    $options[$item['value_key']] = $item['value_zh'];
//
//                }
//            }
//        }

            foreach ($value as $item) {
                if ($language === 'en') {
                    $options[$item['value_key']] = $item['value_en'];
                } else {
                    $options[$item['value_key']] = $item['value_zh'];
                }
            }

            Cache::put($cache_key, $options);
        }

        return $options;
    }
}