| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- namespace plugin\saiadmin\app\service;
- use plugin\saiadmin\exception\ApiException;
- use support\think\Db;
- /** 设备筛选和分成使用同一份数据库字典;每次请求读取,字典修改立即生效。 */
- class DeviceProductCatalog
- {
- public function categories(): array
- {
- $products = $this->dictionary('device_product');
- $mappings = $this->dictionary('device_type_product');
- $typesByProduct = [];
- $mappedTypes = [];
- foreach ($mappings as $mapping) {
- if (!ctype_digit((string) $mapping['value']) || (int) $mapping['value'] < 1) {
- throw new ApiException('设备类型产品映射的键值必须为有效设备类型编号');
- }
- $type = (int) $mapping['value'];
- if (isset($mappedTypes[$type])) {
- throw new ApiException('设备类型产品映射存在重复编号,请检查数据字典');
- }
- $mappedTypes[$type] = true;
- $typesByProduct[$mapping['label']][] = $type;
- }
- $categories = [];
- $productCodes = [];
- foreach ($products as $product) {
- $code = (string) $product['value'];
- if ($code === '' || isset($productCodes[$code])) {
- throw new ApiException('设备产品编码为空或重复,请检查数据字典');
- }
- $productCodes[$code] = true;
- if (!empty($typesByProduct[$code])) {
- $categories[] = ['value' => $code, 'label' => $product['label'], 'goods_types' => $typesByProduct[$code]];
- }
- }
- return $categories;
- }
- private function dictionary(string $code): array
- {
- return Db::table('sa_system_dict_data')->alias('d')
- ->join('sa_system_dict_type t', 't.id=d.type_id')
- ->where('t.code', $code)->where('t.status', 1)->whereNull('t.delete_time')
- ->where('d.status', 1)->whereNull('d.delete_time')
- ->field('d.label,d.value')->order('d.sort', 'desc')->order('d.id', 'asc')->select()->toArray();
- }
- }
|