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(); } }