暂无描述

SearchConfigService.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace App\Services\Data;
  3. use App\Models\SearchConfigGroup;
  4. use App\Models\SearchConfigUse;
  5. use App\Support\Log;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\DB;
  8. class SearchConfigService
  9. {
  10. public static function configCheck($type)
  11. {
  12. // 登录用户信息
  13. $adminId = Auth::id();
  14. // 获取config
  15. $config = SearchConfigGroup::getConfigByType($type);
  16. // 查看使用
  17. $grUseInfo = SearchConfigUse::query()
  18. ->where('us_id', $adminId)
  19. ->where('type', $type)
  20. ->where('enable', 1)
  21. ->first();
  22. if (empty($grUseInfo)) {
  23. $configGroupInfo = SearchConfigGroup::query()
  24. ->where('us_id', $adminId)
  25. ->where('type', $type)
  26. ->where('default', 1)
  27. ->first();
  28. DB::beginTransaction();
  29. try {
  30. if (empty($configGroupInfo)) {
  31. $configGroupInfo = SearchConfigGroup::query()
  32. ->create([
  33. 'title' => '默认模板',
  34. 'us_id' => $adminId,
  35. 'type' => $type,
  36. 'config' => json_encode($config['default']),
  37. 'default' => 1
  38. ]);
  39. }
  40. SearchConfigUse::query()->insert([
  41. 'us_id' => $adminId,
  42. 'type' => $type,
  43. 'gr_id' => $configGroupInfo->id
  44. ]);
  45. DB::commit();
  46. } catch (\Throwable $e) {
  47. Log::error('添加筛选模板异常', [
  48. 'param' => [
  49. 'type' => $type,
  50. ],
  51. 'msg' => $e->getMessage(),
  52. 'trace' => $e->getTrace()
  53. ], 'SearchConfigService');
  54. DB::rollBack();
  55. return [[], 3101];
  56. }
  57. } else {
  58. $configGroupInfo = self::selectDeal($grUseInfo, $adminId, $type);
  59. }
  60. $result = [];
  61. $select = json_decode($configGroupInfo->config, true);
  62. $selectFlip = array_flip($select);
  63. foreach ($config['seting'] as $group) {
  64. foreach ($group['list'] as $value) {
  65. if (isset($selectFlip[$value['key']])) {
  66. $result[$selectFlip[$value['key']]] = $value;
  67. }
  68. }
  69. }
  70. ksort($result);
  71. return [array_values($result), 0];
  72. }
  73. public static function configInfo($type, $grId)
  74. {
  75. $configGroupInfo = SearchConfigGroup::query()
  76. ->where('id', $grId)
  77. ->where('enable', 1)
  78. ->first();
  79. if (empty($configGroupInfo)) return [[], 3102];
  80. $select = json_decode($configGroupInfo->config, true);
  81. // 获取config
  82. $config = SearchConfigGroup::getConfigByType($type);
  83. $selectFlip = array_flip($select);
  84. foreach ($config['seting'] as &$group) {
  85. foreach ($group['list'] as &$value) {
  86. if (isset($selectFlip[$value['key']])) {
  87. $value['is_select'] = 1;
  88. $value['order'] = $selectFlip[$value['key']];
  89. } else {
  90. $value['is_select'] = 0;
  91. $value['order'] = -1;
  92. }
  93. }
  94. }
  95. return [$config['seting'], 0];
  96. }
  97. public static function groupList($type)
  98. {
  99. // 登录用户信息
  100. $adminId = Auth::id();
  101. // 查看使用
  102. $grUseInfo = SearchConfigUse::query()
  103. ->where('us_id', $adminId)
  104. ->where('type', $type)
  105. ->where('enable', 1)
  106. ->first();
  107. if (empty($grUseInfo)) return [];
  108. $configGroupInfo = self::selectDeal($grUseInfo, $adminId, $type);
  109. $groupList = SearchConfigGroup::query()
  110. ->select(['id', 'title', 'default'])
  111. ->where('type', $type)
  112. ->where(function ($query) use ($adminId) {
  113. $query->where(function ($query) use ($adminId)
  114. {
  115. $query->where('us_id', $adminId)->where('default', 1);
  116. }
  117. )->orWhere(function ($query)
  118. {
  119. $query->where('default', 0);
  120. }
  121. );
  122. })
  123. ->where('enable', 1)
  124. ->orderByDesc('default')
  125. ->get();
  126. if ($groupList->isEmpty()) return [];
  127. foreach ($groupList as $group) {
  128. $group->is_select = ($group->id == $configGroupInfo->id) ? 1 : 0;
  129. }
  130. return $groupList;
  131. }
  132. public static function groupAdd($type, $title, $columns)
  133. {
  134. // 登录用户信息
  135. $adminId = Auth::id();
  136. // 检测字段
  137. $allColmun = SearchConfigGroup::getAllColumn($type);
  138. foreach ($columns as $column) {
  139. if (!in_array($column, $allColmun)) return [false, 100];
  140. }
  141. // 保存
  142. SearchConfigGroup::query()
  143. ->insert([
  144. 'title' => $title,
  145. 'us_id' => $adminId,
  146. 'type' => $type,
  147. 'config' => json_encode($columns),
  148. 'default' => 0
  149. ]);
  150. return [true, 0];
  151. }
  152. public static function groupEdit($type, $grId, $columns, $title, $enable)
  153. {
  154. // 登录用户信息
  155. $adminId = Auth::id();
  156. // 检测是不是自己创建
  157. $configGroupInfo = SearchConfigGroup::query()
  158. ->where('type', $type)
  159. ->where('id', $grId)
  160. ->where('us_id', $adminId)
  161. ->where('enable', 1)
  162. ->first();
  163. if (empty($configGroupInfo)) return [false, 3103];
  164. $upData = [];
  165. // 检测字段
  166. if (!empty($columns)) {
  167. $allColmun = SearchConfigGroup::getAllColumn($type);
  168. foreach ($columns as $column) {
  169. if (!in_array($column, $allColmun)) return [false, 100];
  170. }
  171. $upData['config'] = json_encode($columns);
  172. }
  173. if (!empty($title)) $upData['title'] = $title;
  174. // 模板删除
  175. if (!is_null($enable)) {
  176. if (($enable == 0) && $configGroupInfo->default) return [false, 3104];
  177. $upData['enable'] = $enable;
  178. }
  179. $configGroupInfo->update($upData);
  180. return [true, 0];
  181. }
  182. public static function groupApply($type, $grId)
  183. {
  184. // 登录用户信息
  185. $adminId = Auth::id();
  186. SearchConfigUse::query()
  187. ->updateOrInsert([
  188. 'us_id' => $adminId,
  189. 'type' => $type
  190. ], ['gr_id' => $grId, 'enable' => 1]);
  191. return [true, 0];
  192. }
  193. protected static function selectDeal($grUseInfo, $adminId, $type)
  194. {
  195. $configGroupInfo = SearchConfigGroup::query()->where('id', $grUseInfo->gr_id)->where('enable', 1)->first();
  196. if (empty($configGroupInfo)) {
  197. $configGroupInfo = SearchConfigGroup::query()
  198. ->where('us_id', $adminId)
  199. ->where('type', $type)
  200. ->where('default', 1)
  201. ->first();
  202. $grUseInfo->update(['gr_id' => $configGroupInfo->id]);
  203. }
  204. return $configGroupInfo;
  205. }
  206. public static function configDetail($type)
  207. {
  208. // 登录用户信息
  209. $adminId = Auth::id();
  210. $grId = SearchConfigUse::query()
  211. ->where('us_id', $adminId)
  212. ->where('type', $type)
  213. ->value('gr_id');
  214. if (empty($grId)) return [[], 1060];
  215. $configGroupInfo = SearchConfigGroup::query()->find($grId);
  216. if (empty($configGroupInfo)) return [[], 1060];
  217. $select = json_decode($configGroupInfo->config, true);
  218. // 获取config
  219. $config = SearchConfigGroup::getConfigByType($type);
  220. $selectFlip = array_flip($select);
  221. foreach ($config['seting'] as &$group) {
  222. foreach ($group['list'] as &$value) {
  223. if (isset($selectFlip[$value['key']])) {
  224. $value['is_select'] = 1;
  225. } else {
  226. $value['is_select'] = 0;
  227. }
  228. }
  229. }
  230. return [$config['seting'], 0];
  231. }
  232. public static function confEdit($type, $columns)
  233. {
  234. // 登录用户信息
  235. $adminId = Auth::id();
  236. $grId = SearchConfigUse::query()
  237. ->where('us_id', $adminId)
  238. ->where('type', $type)
  239. ->value('gr_id');
  240. if (empty($grId)) return [[], 1060];
  241. $configGroupInfo = SearchConfigGroup::query()->find($grId);
  242. if (empty($configGroupInfo)) return [[], 1060];
  243. $upData = [];
  244. // 检测字段
  245. if (!empty($columns)) {
  246. $allColmun = SearchConfigGroup::getAllColumn($type);
  247. foreach ($columns as $column) {
  248. if (!in_array($column, $allColmun)) return [false, 100];
  249. }
  250. $upData['config'] = json_encode($columns);
  251. }
  252. $configGroupInfo->update($upData);
  253. return [true, 0];
  254. }
  255. }