where('us_id', $adminId) ->where('type', $type) ->where('enable', 1) ->first(); if (empty($grUseInfo)) { $configGroupInfo = SearchConfigGroup::query() ->where('us_id', $adminId) ->where('type', $type) ->where('default', 1) ->first(); DB::beginTransaction(); try { if (empty($configGroupInfo)) { $configGroupInfo = SearchConfigGroup::query() ->create([ 'title' => '默认模板', 'us_id' => $adminId, 'type' => $type, 'config' => json_encode($config['default']), 'default' => 1 ]); } SearchConfigUse::query()->insert([ 'us_id' => $adminId, 'type' => $type, 'gr_id' => $configGroupInfo->id ]); DB::commit(); } catch (\Throwable $e) { Log::error('添加筛选模板异常', [ 'param' => [ 'type' => $type, ], 'msg' => $e->getMessage(), 'trace' => $e->getTrace() ], 'SearchConfigService'); DB::rollBack(); return [[], 3101]; } } else { $configGroupInfo = self::selectDeal($grUseInfo, $adminId, $type); } $result = []; $select = json_decode($configGroupInfo->config, true); $selectFlip = array_flip($select); foreach ($config['seting'] as $group) { foreach ($group['list'] as $value) { if (isset($selectFlip[$value['key']])) { $result[$selectFlip[$value['key']]] = $value; } } } ksort($result); return [array_values($result), 0]; } public static function configInfo($type, $grId) { $configGroupInfo = SearchConfigGroup::query() ->where('id', $grId) ->where('enable', 1) ->first(); if (empty($configGroupInfo)) return [[], 3102]; $select = json_decode($configGroupInfo->config, true); // 获取config $config = SearchConfigGroup::getConfigByType($type); $selectFlip = array_flip($select); foreach ($config['seting'] as &$group) { foreach ($group['list'] as &$value) { if (isset($selectFlip[$value['key']])) { $value['is_select'] = 1; $value['order'] = $selectFlip[$value['key']]; } else { $value['is_select'] = 0; $value['order'] = -1; } } } return [$config['seting'], 0]; } public static function groupList($type) { // 登录用户信息 $adminId = Auth::id(); // 查看使用 $grUseInfo = SearchConfigUse::query() ->where('us_id', $adminId) ->where('type', $type) ->where('enable', 1) ->first(); if (empty($grUseInfo)) return []; $configGroupInfo = self::selectDeal($grUseInfo, $adminId, $type); $groupList = SearchConfigGroup::query() ->select(['id', 'title', 'default']) ->where('type', $type) ->where(function ($query) use ($adminId) { $query->where(function ($query) use ($adminId) { $query->where('us_id', $adminId)->where('default', 1); } )->orWhere(function ($query) { $query->where('default', 0); } ); }) ->where('enable', 1) ->orderByDesc('default') ->get(); if ($groupList->isEmpty()) return []; foreach ($groupList as $group) { $group->is_select = ($group->id == $configGroupInfo->id) ? 1 : 0; } return $groupList; } public static function groupAdd($type, $title, $columns) { // 登录用户信息 $adminId = Auth::id(); // 检测字段 $allColmun = SearchConfigGroup::getAllColumn($type); foreach ($columns as $column) { if (!in_array($column, $allColmun)) return [false, 100]; } // 保存 SearchConfigGroup::query() ->insert([ 'title' => $title, 'us_id' => $adminId, 'type' => $type, 'config' => json_encode($columns), 'default' => 0 ]); return [true, 0]; } public static function groupEdit($type, $grId, $columns, $title, $enable) { // 登录用户信息 $adminId = Auth::id(); // 检测是不是自己创建 $configGroupInfo = SearchConfigGroup::query() ->where('type', $type) ->where('id', $grId) ->where('us_id', $adminId) ->where('enable', 1) ->first(); if (empty($configGroupInfo)) return [false, 3103]; $upData = []; // 检测字段 if (!empty($columns)) { $allColmun = SearchConfigGroup::getAllColumn($type); foreach ($columns as $column) { if (!in_array($column, $allColmun)) return [false, 100]; } $upData['config'] = json_encode($columns); } if (!empty($title)) $upData['title'] = $title; // 模板删除 if (!is_null($enable)) { if (($enable == 0) && $configGroupInfo->default) return [false, 3104]; $upData['enable'] = $enable; } $configGroupInfo->update($upData); return [true, 0]; } public static function groupApply($type, $grId) { // 登录用户信息 $adminId = Auth::id(); SearchConfigUse::query() ->updateOrInsert([ 'us_id' => $adminId, 'type' => $type ], ['gr_id' => $grId, 'enable' => 1]); return [true, 0]; } protected static function selectDeal($grUseInfo, $adminId, $type) { $configGroupInfo = SearchConfigGroup::query()->where('id', $grUseInfo->gr_id)->where('enable', 1)->first(); if (empty($configGroupInfo)) { $configGroupInfo = SearchConfigGroup::query() ->where('us_id', $adminId) ->where('type', $type) ->where('default', 1) ->first(); $grUseInfo->update(['gr_id' => $configGroupInfo->id]); } return $configGroupInfo; } public static function configDetail($type) { // 登录用户信息 $adminId = Auth::id(); $grId = SearchConfigUse::query() ->where('us_id', $adminId) ->where('type', $type) ->value('gr_id'); if (empty($grId)) return [[], 1060]; $configGroupInfo = SearchConfigGroup::query()->find($grId); if (empty($configGroupInfo)) return [[], 1060]; $select = json_decode($configGroupInfo->config, true); // 获取config $config = SearchConfigGroup::getConfigByType($type); $selectFlip = array_flip($select); foreach ($config['seting'] as &$group) { foreach ($group['list'] as &$value) { if (isset($selectFlip[$value['key']])) { $value['is_select'] = 1; } else { $value['is_select'] = 0; } } } return [$config['seting'], 0]; } public static function confEdit($type, $columns) { // 登录用户信息 $adminId = Auth::id(); $grId = SearchConfigUse::query() ->where('us_id', $adminId) ->where('type', $type) ->value('gr_id'); if (empty($grId)) return [[], 1060]; $configGroupInfo = SearchConfigGroup::query()->find($grId); if (empty($configGroupInfo)) return [[], 1060]; $upData = []; // 检测字段 if (!empty($columns)) { $allColmun = SearchConfigGroup::getAllColumn($type); foreach ($columns as $column) { if (!in_array($column, $allColmun)) return [false, 100]; } $upData['config'] = json_encode($columns); } $configGroupInfo->update($upData); return [true, 0]; } }