Nessuna descrizione

StarLibraryService.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. namespace App\Services\Sys;
  3. use App\Imports\StarInformationImport;
  4. use App\Imports\StarInstitutionImport;
  5. use App\Models\Sys\SysGroup;
  6. use App\Models\Sys\SysRolePermission;
  7. use App\Models\Sys\SysStarInformation;
  8. use App\Models\Sys\SysStarInstitution;
  9. use App\Models\Sys\SysStarLibraryClassify;
  10. use App\Models\Sys\SysStarLibraryUpload;
  11. use App\Models\Sys\SysUserRole;
  12. use App\Models\Sys\SysUsers;
  13. use App\Support\Log;
  14. use Illuminate\Support\Facades\Auth;
  15. use Maatwebsite\Excel\Facades\Excel;
  16. use DB;
  17. class StarLibraryService
  18. {
  19. public static function infoUpload($ptype, $file)
  20. {
  21. if (!$file->isValid()) return [false, 1061];
  22. $fileExt = $file->getClientOriginalExtension();
  23. if ($fileExt != 'xlsx') return [false, 1062];
  24. $fileName = $file->getClientOriginalName();
  25. try {
  26. $sysUserId = Auth::id();
  27. // 插入上传记录
  28. $uploadInfo = SysStarLibraryUpload::query()
  29. ->create([
  30. 'file' => $fileName,
  31. 'ptype' => $ptype,
  32. 'sys_user_id' => $sysUserId
  33. ]);
  34. // 获取 groupId
  35. $groupId = self::getGroupId($sysUserId);
  36. if (empty($ptype) || empty($groupId) || empty($sysUserId)) {
  37. return [false, 400];
  38. }
  39. // 导入数据
  40. $import = new StarInformationImport($ptype, $groupId, $sysUserId);
  41. Excel::import($import, $file);
  42. $uploadInfo->update(['row_num' => $import->getRowCount(), 'status' => 1]);
  43. return [true, 0];
  44. } catch (\Throwable $e) {
  45. Log::error('达人信息导入失败', [
  46. 'msg' => $e->getMessage(),
  47. 'trace' => $e->getTraceAsString()
  48. ], 'StarLibraryService');
  49. return [false, 400];
  50. }
  51. }
  52. public static function instUpload($file)
  53. {
  54. if (!$file->isValid()) return [false, 1061];
  55. $fileExt = $file->getClientOriginalExtension();
  56. if ($fileExt != 'xlsx') return [false, 1062];
  57. $fileName = $file->getClientOriginalName();
  58. try {
  59. $sysUserId = Auth::id();
  60. // 插入上传记录
  61. $uploadInfo = SysStarLibraryUpload::query()
  62. ->create([
  63. 'file' => $fileName,
  64. 'ptype' => 0,
  65. 'sys_user_id' => $sysUserId
  66. ]);
  67. // 获取 groupId
  68. $groupId = self::getGroupId($sysUserId);
  69. if (empty($groupId) || empty($sysUserId)) {
  70. return [false, 400];
  71. }
  72. $import = new StarInstitutionImport($groupId, $sysUserId);
  73. Excel::import($import, $file);
  74. $uploadInfo->update(['row_num' => $import->getRowCount(), 'status' => 1]);
  75. return [true, 0];
  76. } catch (\Throwable $e) {
  77. Log::error('达人机构导入失败', [
  78. 'msg' => $e->getMessage(),
  79. 'trace' => $e->getTraceAsString()
  80. ], 'StarLibraryService');
  81. return [false, 400];
  82. }
  83. }
  84. public static function category($ptype)
  85. {
  86. try {
  87. $category = SysStarLibraryClassify::query()
  88. ->where('ptype', $ptype)
  89. ->where('enable', 1)
  90. ->value('categorys');
  91. $category = json_decode($category, true);
  92. return [$category, 0];
  93. } catch (\Throwable $e) {
  94. Log::error('获取类别失败', [
  95. 'msg' => $e->getMessage(),
  96. 'trace' => $e->getTraceAsString()
  97. ], 'StarLibraryService');
  98. return [[], 400];
  99. }
  100. }
  101. public static function infoList(
  102. $ptype, $catId, $insId, $minFans, $maxFans, $minQprice,
  103. $maxQprice, $keyword, $sortField, $sortOrder, $page, $pageSize
  104. )
  105. {
  106. try {
  107. $sysUserId = Auth::id();
  108. $groupId = self::getGroupId($sysUserId);
  109. $isViewContact = self::isViewContact(Auth::id());
  110. // 获取该所属字段
  111. $field = SysStarLibraryClassify::query()
  112. ->where('ptype', $ptype)
  113. ->where('enable', 1)
  114. ->value('fields');
  115. // 获取对象
  116. $query = SysStarInformation::query()
  117. ->where('ptype', $ptype)
  118. ->where('enable', 1);
  119. if (!empty($groupId))
  120. $query->where('group_id', $groupId);
  121. // 当前平台总数
  122. $extra = self::getAll($groupId);
  123. $extra['ptype_num'] = $query->count();
  124. // 指定字段
  125. $query->select(json_decode($field, true));
  126. // 筛选条件
  127. $isFilter = false;
  128. if (!empty($catId)) {
  129. $isFilter = true;
  130. $query->where('cat_id', $catId);
  131. }
  132. if (!empty($insId)) {
  133. $isFilter = true;
  134. $query->where('ins_id', $insId);
  135. }
  136. if (is_numeric($minFans)) {
  137. $isFilter = true;
  138. $query->where('fans', '>=', ($minFans * 10000));
  139. }
  140. if (is_numeric($maxFans)) {
  141. $isFilter = true;
  142. $query->where('fans', '<=', ($maxFans * 10000));
  143. }
  144. if (is_numeric($minQprice)) {
  145. $isFilter = true;
  146. $query->where('quoted_price_1', '>=', $minQprice);
  147. }
  148. if (is_numeric($maxQprice)) {
  149. $isFilter = true;
  150. $query->where('quoted_price_1', '<=', $maxQprice);
  151. }
  152. if (!empty($keyword)) {
  153. $isFilter = true;
  154. if (is_numeric($keyword)) {
  155. $query->where('accid', 'like', '%' . $keyword . '%');
  156. } else {
  157. $query->where('nickname', 'like', '%' . $keyword . '%');
  158. }
  159. }
  160. // 获取总数
  161. $total = $query->count();
  162. $extra['filt_num'] = $isFilter ? $total : 0;
  163. if ($total == 0) return [[], 0, $extra];
  164. // 获取列表
  165. $list = $query
  166. ->orderBy($sortField, $sortOrder)
  167. ->offset(($page - 1) * $pageSize)
  168. ->limit($pageSize)
  169. ->get()
  170. ->map(function ($item) use ($sysUserId, $isViewContact) {
  171. $item->fans = round($item->fans, 2);
  172. if (!$isViewContact && ($item->sys_user_id != $sysUserId)) {
  173. $item->contact = '***********';
  174. }
  175. return $item;
  176. });
  177. return [$list, $total, $extra];
  178. } catch (\Throwable $e) {
  179. Log::error('获取达人信息列表失败', [
  180. 'msg' => $e->getMessage(),
  181. 'trace' => $e->getTraceAsString()
  182. ], 'StarLibraryService');
  183. return [[], 0, []];
  184. }
  185. }
  186. public static function instList($catId, $keyword, $page, $pageSize)
  187. {
  188. try {
  189. $sysUserId = Auth::id();
  190. $groupId = self::getGroupId($sysUserId);
  191. $isViewContact = self::isViewContact(Auth::id());
  192. // 获取该所属字段
  193. $field = SysStarLibraryClassify::query()
  194. ->where('ptype', 0)
  195. ->where('enable', 1)
  196. ->value('fields');
  197. // 获取对象
  198. $query = SysStarInstitution::query()
  199. // ->where(function ($query) use ($catId, $keyword) {
  200. // })
  201. ->where('enable', 1);
  202. if (!empty($groupId))
  203. $query->where('group_id', $groupId);
  204. // 当前平台总数
  205. $extra = self::getAll($groupId);
  206. $extra['ptype_num'] = $query->count();
  207. // 指定字段
  208. $query->select(json_decode($field, true));
  209. // 筛选条件
  210. $isFilter = false;
  211. if (!empty($catId)) {
  212. $isFilter = true;
  213. $query->where('cat_id', $catId);
  214. }
  215. if (!empty($keyword)) {
  216. $isFilter = true;
  217. $query->where('name', 'like', '%' . $keyword . '%');
  218. }
  219. // 获取总数
  220. $total = $query->count();
  221. $extra['filt_num'] = $isFilter ? $total : 0;
  222. if ($total == 0) return [[], 0, $extra];
  223. $list = $query
  224. ->orderByDesc('created_at')
  225. ->offset(($page - 1) * $pageSize)
  226. ->limit($pageSize)
  227. ->get()
  228. ->map(function($item) use ($sysUserId, $isViewContact) {
  229. if (!$isViewContact && ($item->sys_user_id != $sysUserId)) {
  230. $item->contact = '***********';
  231. }
  232. return $item;
  233. });
  234. return [$list, $total, $extra];
  235. } catch (\Throwable $e) {
  236. Log::error('获取达人机构列表失败', [
  237. 'msg' => $e->getMessage(),
  238. 'trace' => $e->getTraceAsString()
  239. ], 'StarLibraryService');
  240. return [[], 0, []];
  241. }
  242. }
  243. public static function getAll($groupId=null)
  244. {
  245. $infoQuery = SysStarInformation::query()->where('enable', 1);
  246. $instQuery = SysStarInstitution::query()->where('enable', 1);
  247. if (!empty($groupId)) {
  248. $infoQuery->where('group_id', $groupId);
  249. $instQuery->where('group_id', $groupId);
  250. }
  251. return [
  252. 'info_num' => $infoQuery->count(),
  253. 'inst_num' => $instQuery->count()
  254. ];
  255. }
  256. public static function upList($page, $pageSize)
  257. {
  258. try {
  259. $query = SysStarLibraryUpload::query()->where('enable', 1);
  260. $total = $query->count();
  261. if ($total == 0) return [[], 0];
  262. $list = $query->orderByDesc('created_at')
  263. ->offset(($page - 1) * $pageSize)
  264. ->limit($pageSize)
  265. ->get();
  266. $ptypeListMap = [
  267. 0 => '机构',
  268. 1 => '快手',
  269. 2 => '抖音',
  270. 3 => '小红书'
  271. ];
  272. $sysUserListMap = SysUsers::query()
  273. ->whereIn('id', $list->pluck('sys_user_id'))
  274. ->pluck('name', 'id');
  275. foreach ($list as $item) {
  276. $item->sys_user_name = $sysUserListMap->get($item->sys_user_id) ?? null;
  277. $item->ptype_name = $ptypeListMap[$item->ptype] ?? null;
  278. }
  279. return [$list, $total];
  280. } catch (\Throwable $e) {
  281. Log::error('获取上传列表失败', [
  282. 'msg' => $e->getMessage(),
  283. 'trace' => $e->getTraceAsString()
  284. ], 'StarLibraryService');
  285. return [[], 0];
  286. }
  287. }
  288. public static function getGroupId($sysUserId)
  289. {
  290. try {
  291. $groupId = SysUsers::query()
  292. ->where('id', $sysUserId)
  293. ->where('enable', 1)
  294. ->value('group_id');
  295. return $groupId;
  296. } catch (\Throwable $e) {
  297. Log::error('获取用户组失败', [
  298. 'msg' => $e->getMessage(),
  299. 'trace' => $e->getTraceAsString()
  300. ], 'StarLibraryService');
  301. return null;
  302. }
  303. }
  304. public static function isViewContact($sysUserId)
  305. {
  306. try {
  307. # 获取角色集合
  308. $role_ids = SysUserRole::query()->where('sys_user_id', $sysUserId)->where('enable', 1)->pluck('role_id')->all();
  309. if (in_array(1, $role_ids)) {
  310. return true;
  311. }
  312. # 获取权限集合(是否包含联系方式)
  313. $permissionIds = SysRolePermission::query()->whereIn('role_id', $role_ids)->where('enable', 1)->pluck('permission_id')->all();
  314. if (in_array(19, $permissionIds)) {
  315. return true;
  316. }
  317. return false;
  318. } catch (\Throwable $e) {
  319. Log::error('获取联系方式是否可见', [
  320. 'msg' => $e->getMessage(),
  321. 'trace' => $e->getTraceAsString()
  322. ], 'StarLibraryService');
  323. return false;
  324. }
  325. }
  326. public static function groupList()
  327. {
  328. try {
  329. $list = SysGroup::query()->where('enable', 1)->orderByDesc('created_at')->get();
  330. return $list;
  331. } catch (\Throwable $e) {
  332. Log::error('获取用户组列表失败', [
  333. 'msg' => $e->getMessage(),
  334. 'trace' => $e->getTraceAsString()
  335. ], 'StarLibraryService');
  336. return [];
  337. }
  338. }
  339. public static function groupAdd($name)
  340. {
  341. try {
  342. $isExist = SysGroup::query()
  343. ->where('name', $name)
  344. ->where('enable', 1)
  345. ->exists();
  346. if ($isExist)
  347. return [false, 1070];
  348. SysGroup::query()
  349. ->insert([
  350. 'name' => $name
  351. ]);
  352. return [true, 0];
  353. } catch (\Throwable $e) {
  354. Log::error('添加用户组失败', [
  355. 'msg' => $e->getMessage(),
  356. 'trace' => $e->getTraceAsString()
  357. ], 'StarLibraryService');
  358. return [false, 400];
  359. }
  360. }
  361. public static function groupEdit($groupId, $name)
  362. {
  363. try {
  364. $groupInfo = SysGroup::query()
  365. ->where('id', $groupId)
  366. ->where('enable', 1)
  367. ->first();
  368. if (empty($groupInfo))
  369. return [false, 1071];
  370. $isExist = SysGroup::query()
  371. ->where('id', '!=', $groupId)
  372. ->where('name', $name)
  373. ->where('enable', 1)
  374. ->exists();
  375. if ($isExist)
  376. return [false, 1070];
  377. $groupInfo->update(['name' => $name]);
  378. return [true, 0];
  379. } catch (\Throwable $e) {
  380. Log::error('编辑用户组失败', [
  381. 'msg' => $e->getMessage(),
  382. 'trace' => $e->getTraceAsString()
  383. ], 'StarLibraryService');
  384. return [false, 400];
  385. }
  386. }
  387. public static function groupDel($groupId)
  388. {
  389. DB::beginTransaction();
  390. try {
  391. $groupInfo = SysGroup::query()
  392. ->where('id', $groupId)
  393. ->where('enable', 1)
  394. ->first();
  395. if (empty($groupInfo))
  396. return [false, 1071];
  397. // 更新账号表
  398. SysUsers::query()
  399. ->where('group_id', $groupId)
  400. ->update(['group_id' => null]);
  401. // 更新用户组表
  402. $groupInfo->update(['enable' => 0]);
  403. DB::commit();
  404. return [true, 0];
  405. } catch (\Throwable $e) {
  406. Log::error('删除用户组失败', [
  407. 'msg' => $e->getMessage(),
  408. 'trace' => $e->getTraceAsString()
  409. ], 'StarLibraryService');
  410. DB::rollBack();
  411. return [false, 400];
  412. }
  413. }
  414. }