48 行
1.6 KiB
PHP
48 行
1.6 KiB
PHP
<?php
|
|
namespace app\common\library;
|
|
|
|
use think\Db;
|
|
|
|
/** Canonical site identities shared by management and screen APIs. */
|
|
class GranarySite
|
|
{
|
|
const NAMES = ['center' => '遵义中心库', 'xishui' => '习水库'];
|
|
const STORAGE = ['center' => '遵义直属库有限公司中心库区', 'xishui' => '中央储备粮遵义直属库有限公司习水分公司'];
|
|
|
|
public static function valid($site)
|
|
{
|
|
return is_string($site) && isset(self::NAMES[$site]);
|
|
}
|
|
|
|
public static function allowed($auth)
|
|
{
|
|
if ($auth->isSuperAdmin()) return array_keys(self::NAMES);
|
|
$scope = Db::name('admin')->where('id', $auth->id)->value('granary_sites');
|
|
return $scope === 'all' ? array_keys(self::NAMES) : (self::valid($scope) ? [$scope] : []);
|
|
}
|
|
|
|
public static function options(array $sites)
|
|
{
|
|
$options = [];
|
|
foreach ($sites as $site) $options[] = ['id' => $site, 'name' => self::NAMES[$site], 'storage' => self::STORAGE[$site]];
|
|
return $options;
|
|
}
|
|
|
|
public static function granaries(array $sites)
|
|
{
|
|
$rows = Db::name('granary')->where('site', 'in', $sites ?: ['__none__'])
|
|
->field('id,name,num,site,storage,varieties,granary_type')->order('site,num')->select();
|
|
foreach ($rows as &$row) {
|
|
$row['site_name'] = self::NAMES[$row['site']];
|
|
$row['label'] = $row['site_name'] . ' · ' . $row['name'];
|
|
}
|
|
unset($row);
|
|
return $rows;
|
|
}
|
|
|
|
public static function permits($gid, array $sites)
|
|
{
|
|
return (bool) Db::name('granary')->where('id', (int)$gid)->where('site', 'in', $sites ?: ['__none__'])->count();
|
|
}
|
|
}
|