123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- <?php
- namespace App\Services\Data;
- use App\Models\SearchConfigGroup;
- use App\Models\SearchConfigUse;
- use App\Support\Log;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- class SearchConfigService
- {
- public static function configCheck($type)
- {
- // 登录用户信息
- $adminId = Auth::id();
- // 获取config
- $config = SearchConfigGroup::getConfigByType($type);
- // 查看使用
- $grUseInfo = SearchConfigUse::query()
- ->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];
- }
- }
|