WebsiteService.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\Category;
  5. use App\Model\LetterOfComplaint;
  6. use App\Model\TemplateClass;
  7. use App\Model\Template;
  8. use App\Model\User;
  9. use App\Model\WebsiteRole;
  10. use App\Model\WebsiteRoleUser;
  11. use App\Model\Website;
  12. use App\Model\WebsiteColumn;
  13. use Hyperf\DbConnection\Db;
  14. use Hyperf\RpcServer\Annotation\RpcService;
  15. use App\Tools\Result;
  16. use App\Model\WebsiteCategory;
  17. use function PHPUnit\Framework\isNull;
  18. #[RpcService(name: "WebsiteService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  19. class WebsiteService implements WebsiteServiceInterface
  20. {
  21. /**
  22. * @param string $keyword
  23. * @param int $page
  24. * @param int $pageSize
  25. * @return array
  26. */
  27. public function getWebsitetList(array $data): array
  28. {
  29. $where = [];
  30. if(isset($data['keyword']) && !empty($data['keyword'])){
  31. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  32. }
  33. if(isset($data['website_column_id']) && !empty($data['website_column_id'])){
  34. array_push($where,['website.website_column_id','=',$data['website_column_id']]);
  35. }
  36. if(isset($data['city_id']) && !empty($data['city_id'])){
  37. array_push($where,['website.city_id','=',$data['city_id']]);
  38. }
  39. $result = Website::where($where)
  40. ->leftJoin("website_column","website.website_column_id","website_column.id")
  41. ->leftJoin("district","district.id","website.city_id")
  42. ->select("website.*","website_column.column_name","district.name as city_name")
  43. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("website.id","desc")->get();
  44. $count = Website::where($where)->count();
  45. if (empty($result)) {
  46. return Result::error("没有数据",0);
  47. }
  48. $data = [
  49. 'rows'=>$result->toArray(),
  50. 'count'=>$count
  51. ];
  52. return Result::success($data);
  53. }
  54. /**
  55. * @param array $data
  56. * @return array
  57. */
  58. public function createWebsite(array $data): array
  59. {
  60. var_dump("网站数据:",$data);
  61. $insertData = [
  62. 'website_name'=>$data['website_name'],
  63. 'logo'=>$data['logo']??'',
  64. 'website_url'=>$data['website_url']??'',
  65. 'city_id'=>$data['city_id']??0,
  66. 'website_column_id'=>$data['website_column_id'],
  67. 'title'=>$data['title']??'',
  68. 'keywords'=>$data['keywords']??'',
  69. 'description'=>$data['description']??'',
  70. 'status'=>$data['status']??0,
  71. 'website_column_arr_id'=>$data['website_column_arr_id'],
  72. 'city_arr_id'=>$data['city_arr_id']??[0],
  73. 'template_id' =>$data['template_id']??0,
  74. ];
  75. $result = Website::insertGetId($insertData);
  76. if(empty($result)){
  77. return Result::error("创建失败",0);
  78. }else{
  79. return Result::success(["id"=>$result]);
  80. }
  81. }
  82. /**
  83. * @param int $id
  84. * @param array $data
  85. * @return array
  86. */
  87. public function updateWebsite(int $id,array $data): array
  88. {
  89. $insertData = [
  90. 'website_name'=>$data['website_name'],
  91. 'logo'=>$data['logo']??'',
  92. 'website_url'=>$data['website_url']??'',
  93. 'city_id'=>$data['city_id']??0,
  94. 'website_column_id'=>$data['website_column_id'],
  95. 'title'=>$data['title']??'',
  96. 'keywords'=>$data['keywords']??'',
  97. 'description'=>$data['description']??'',
  98. 'status'=>$data['status']??0,
  99. 'website_column_arr_id'=>$data['website_column_arr_id'],
  100. 'city_arr_id'=>$data['city_arr_id']??[0],
  101. 'template_id' =>$data['template_id']??0,
  102. ];
  103. $result = Website::where('id',$id)->update($insertData);
  104. var_dump("更新站点",$result);
  105. if(empty($result)){
  106. return Result::error("更新失败",0);
  107. }else{
  108. return Result::success();
  109. }
  110. }
  111. /**
  112. * @param int $id
  113. * @return array
  114. */
  115. public function delWebsite(int $id): array
  116. {
  117. $result = Website::where('id',$id )->delete();
  118. if(empty($result)){
  119. return Result::error("删除失败",0);
  120. }else{
  121. return Result::success();
  122. }
  123. }
  124. /**
  125. * @param int $id
  126. * @return array
  127. */
  128. public function getWebsiteInfo(int $id): array
  129. {
  130. $where = [
  131. ['website.id','=',$id]
  132. ];
  133. $result = Website::where($where )
  134. ->leftJoin("template","template.id","website.template_id")
  135. ->select("website.*","template.template_name","template.template_img")
  136. ->first();
  137. if(empty($result)){
  138. return Result::error("数据不存在",0);
  139. }else{
  140. return Result::success($result->toArray());
  141. }
  142. }
  143. /**
  144. * 查询所有的站点栏目
  145. * @return array
  146. */
  147. public function getWebsiteColumn(array $data): array
  148. {
  149. $result = WebsiteColumn::where($data)->get();
  150. if(empty($result)){
  151. return Result::error("数据不存在",0);
  152. }else{
  153. return Result::success($result->toArray());
  154. }
  155. }
  156. /**
  157. * @param string $keyword
  158. * @param int $page
  159. * @param int $pageSize
  160. * @return array
  161. */
  162. public function getWebsiteColumnList(array $data):array
  163. {
  164. $where = [];
  165. if(isset($data['keyword']) && $data['keyword']){
  166. array_push($where,['website_column.column_name','like','%'.$data['keyword'].'%']);
  167. }
  168. $result = WebsiteColumn::where($where)
  169. ->leftJoin("website_column as wc","website_column.pid","wc.id")
  170. ->select("website_column.*","wc.column_name as parent_column_name")
  171. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->get();
  172. $count = WebsiteColumn::where($where)->count();
  173. if (empty($result)) {
  174. return Result::error("没有数据",0);
  175. }
  176. $data = [
  177. 'rows'=>$result->toArray(),
  178. 'count'=>$count
  179. ];
  180. return Result::success($data);
  181. }
  182. /**
  183. * @param array $data
  184. * @return array
  185. */
  186. public function createWebsiteColumn(array $data): array
  187. {
  188. $insertData = [
  189. 'column_name'=>$data['column_name'],
  190. 'pid'=>$data['pid']??'',
  191. 'column_arr_id'=>$data['column_arr_id']??[0],
  192. 'sort'=>$data['sort']??0,
  193. 'remark'=>$data['remark']??'',
  194. ];
  195. $result = WebsiteColumn::insertGetId($insertData);
  196. if(empty($result)){
  197. return Result::error("创建失败",0);
  198. }else{
  199. return Result::success(["id"=>$result]);
  200. }
  201. }
  202. /**
  203. * @param int $id
  204. * @param array $data
  205. * @return array
  206. */
  207. public function updateWebsiteColumn(int $id,array $data): array
  208. {
  209. $insertData = [
  210. 'column_name'=>$data['column_name'],
  211. 'pid'=>$data['pid']??'',
  212. 'column_arr_id'=>$data['column_arr_id']??[0],
  213. 'sort'=>$data['sort']??0,
  214. 'remark'=>$data['remark']??'',
  215. ];
  216. $result = WebsiteColumn::where('id',$id)->update($insertData);
  217. if(empty($result)){
  218. return Result::error("更新失败",0);
  219. }else{
  220. return Result::success();
  221. }
  222. }
  223. /**
  224. * @param int $id
  225. * @return array
  226. */
  227. public function delWebsiteColumn(int $id): array
  228. {
  229. $list = WebsiteColumn::where(['pid'=>$id])->get();
  230. if($list){
  231. return Result::error("存在子网系,不能删除,请先删除子网系",0);
  232. }
  233. $result = WebsiteColumn::where('id',$id )->delete();
  234. if(empty($result)){
  235. return Result::error("删除失败",0);
  236. }else{
  237. return Result::success();
  238. }
  239. }
  240. /**
  241. * @param string $keyword
  242. * @param int $page
  243. * @param int $pageSize
  244. * @return array
  245. */
  246. public function getWebsiteRoleList(string $keyword,int $page,int $pageSize,int $websiteId):array
  247. {
  248. $where = [
  249. ['role.role_name','like','%'.$keyword.'%'],
  250. ['website_role.website_id','=',$websiteId],
  251. ];
  252. $result = WebsiteRole::where($where)
  253. ->leftJoin("role","role.id","website_role.role_id")
  254. ->select("role.*","website_role.type","website_role.role_id","website_role.id as website_role_id","website_role.website_id")
  255. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  256. $count = WebsiteRole::where($where) ->leftJoin("role","role.id","website_role.role_id")->count();
  257. if (empty($result)) {
  258. return Result::error("没有数据",0);
  259. }
  260. $data = [
  261. 'rows'=>$result->toArray(),
  262. 'count'=>$count
  263. ];
  264. return Result::success($data);
  265. }
  266. /**
  267. * @param array $data
  268. * @return array
  269. */
  270. public function createWebsiteRole(array $data): array
  271. {
  272. $insertData = [
  273. 'website_id'=>$data['website_id'],
  274. 'role_id'=>$data['role_id']??''
  275. ];
  276. $info = WebsiteRole::where($insertData)->first();
  277. if($info){
  278. return Result::error("不能重复添加角色",0);
  279. }
  280. $insertData['admin_user_id'] = $data['admin_user_id']??'';
  281. $insertData['type'] = $data['type']??'';
  282. $result = WebsiteRole::insertGetId($insertData);
  283. if(empty($result)){
  284. return Result::error("创建失败",0);
  285. }else{
  286. return Result::success(["id"=>$result]);
  287. }
  288. }
  289. /**
  290. * 暂时用不上
  291. * @param int $id
  292. * @param array $data
  293. * @return array
  294. */
  295. public function updateWebsiteRole(int $id,array $data): array
  296. {
  297. $insertData = [
  298. 'website_id'=>$data['website_id'],
  299. 'type'=>$data['type']??'',
  300. ];
  301. $result = WebsiteRole::where('id',$id)->update($insertData);
  302. if(empty($result)){
  303. return Result::error("更新失败",0);
  304. }else{
  305. return Result::success();
  306. }
  307. }
  308. /**
  309. * @param int $id
  310. * @return array
  311. */
  312. public function delWebsiteRole(int $id): array
  313. {
  314. $result = WebsiteRole::where('id',$id )->delete();
  315. if(empty($result)){
  316. return Result::error("删除失败",0);
  317. }else{
  318. return Result::success();
  319. }
  320. }
  321. /**
  322. * @param string $keyword
  323. * @param int $page
  324. * @param int $pageSize
  325. * @return array
  326. */
  327. public function getWebsiteRoleUserList(string $keyword,int $page,int $pageSize,int $websiteId,int $roleId):array
  328. {
  329. $where = [
  330. ['website_role_user.website_id','=',$websiteId],
  331. ['website_role_user.role_id','=',$roleId],
  332. ];
  333. $count = WebsiteRoleUser::where($where)->count();
  334. $where[] = ['u.user_name','like','%'.$keyword.'%'];
  335. $result = WebsiteRoleUser::where($where)
  336. ->leftJoin("user as u","website_role_user.user_id","u.id")
  337. ->leftJoin("website as w","website_role_user.website_id","u.id")
  338. ->leftJoin("role as r","website_role_user.role_id","r.id")
  339. ->select("u.*","u.user_name",'w.website_name','r.role_name','website_role_user.id as website_role_user_id','website_role_user.updated_at as user_update_at')
  340. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  341. if (empty($result)) {
  342. return Result::error("没有数据",0);
  343. }
  344. $data = [
  345. 'rows'=>$result->toArray(),
  346. 'count'=>$count
  347. ];
  348. return Result::success($data);
  349. }
  350. /**
  351. * @param array $data
  352. * @return array
  353. */
  354. public function createWebsiteRoleUser(array $data): array
  355. {
  356. $insertData = [
  357. 'website_id'=>$data['website_id'],
  358. 'user_id'=>$data['user_id']??'',
  359. ];
  360. $info = WebsiteRoleUser::where($insertData)->first();
  361. if($info){
  362. return Result::error("不能重复添加角色用户",0);
  363. }
  364. $insertData['role_id'] = $data['role_id']??'';
  365. $insertData['admin_user_id'] = $data['admin_user_id']??'';
  366. $insertData['type'] = $data['type']??'';
  367. $result = WebsiteRoleUser::insertGetId($insertData);
  368. if(empty($result)){
  369. return Result::error("创建失败",0);
  370. }else{
  371. return Result::success(["id"=>$result]);
  372. }
  373. }
  374. /**
  375. * @param int $id
  376. * @param array $data
  377. * @return array
  378. */
  379. public function updateWebsiteRoleUser(int $id,array $data): array
  380. {
  381. $insertData = [
  382. 'website_id'=>$data['website_id'],
  383. 'type'=>$data['type']??'',
  384. 'type_id'=>$data['type_id']??'',
  385. 'role_id'=>$data['role_id']??'',
  386. ];
  387. $result = WebsiteRoleUser::where('id',$id)->update($insertData);
  388. if(empty($result)){
  389. return Result::error("更新失败",0);
  390. }else{
  391. return Result::success();
  392. }
  393. }
  394. /**
  395. * @param int $id
  396. * @return array
  397. */
  398. public function delWebsiteRoleUser(int $id): array
  399. {
  400. $result = WebsiteRoleUser::where('id',$id )->delete();
  401. if(empty($result)){
  402. return Result::error("删除失败",0);
  403. }else{
  404. return Result::success();
  405. }
  406. }
  407. /**
  408. * 根据域名获取网站 站点id
  409. * @param array $data
  410. * @return array
  411. */
  412. public function getWebsiteId(array $data): array
  413. {
  414. $result = Website::whereJsonContains('website_url',$data['website_url'])->first();
  415. if(empty($result)){
  416. return Result::error("查询站点失败",0);
  417. }else{
  418. return Result::success($result);
  419. }
  420. }
  421. /**
  422. * 查询网站下面的导航
  423. * @param array $data
  424. * @return array
  425. */
  426. public function getWebsiteCategory(array $data): array
  427. {
  428. $where = [
  429. 'website_id'=>$data['website_id'],
  430. 'pid'=>0
  431. ];
  432. $result = WebsiteCategory::where($where)->orderBy('sort','asc')->get();
  433. if(empty($result)){
  434. return Result::error("查询站点栏目失败",0);
  435. }else{
  436. return Result::success($result);
  437. }
  438. }
  439. /**
  440. * 网站首页数据统计, 管理员
  441. * @return void
  442. */
  443. public function getAdminIndex(array $data): array
  444. {
  445. var_dump("用户类型:",$data['type_id']);
  446. switch ($data['type_id']){
  447. case 4:
  448. $result = Db::select('SELECT DATE(created_at) AS date,COUNT(*) AS total_count FROM letter_of_complaint WHERE created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(created_at) ORDER BY date ASC;');
  449. return Result::success($result);
  450. break;
  451. case 10000:
  452. $res = [];
  453. //网站
  454. $res['website']['count'] = 0;
  455. $res['website']['growth_rate'] = 0;
  456. //资讯
  457. $res['article']['count'] = 0;
  458. $res['article']['growth_rate'] = 0;
  459. //导航池
  460. $res['category']['count'] = 0;
  461. $res['category']['growth_rate'] = 0;
  462. //近一月数据
  463. $res['monthArticle']= [];
  464. //用户类型
  465. $res['userType'] = [];
  466. $res['website']['count'] = Website::where([])->count();
  467. $res['article']['count'] = Article::whereNotIn('status',['404'])->count();
  468. $res['category']['count'] = Category::where([])->count();
  469. $res['monthArticle'] = Db::select('SELECT DATE(created_at) AS date,COUNT(*) AS total_count FROM article WHERE created_at >= CURDATE() - INTERVAL 30 DAY GROUP BY DATE(created_at) ORDER BY date ASC;');
  470. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  471. return Result::success($res);
  472. }
  473. return [];
  474. }
  475. /**
  476. * 获取模板类型
  477. * @return void
  478. */
  479. public function getTemplateClass(array $data): array
  480. {
  481. $where = [];
  482. if(isset($data['name']) && $data['name']){
  483. array_push($where,['name','like','%'.$data['name'].'%']);
  484. }
  485. $result = TemplateClass::where($where)->orderBy('sort','asc')->get();
  486. if(empty($result)){
  487. return Result::error("没有模板类型",0);
  488. }else{
  489. return Result::success($result);
  490. }
  491. }
  492. /**
  493. * 添加模板类型
  494. * @param
  495. * @return void
  496. */
  497. public function addTemplateClass(array $data): array
  498. {
  499. $insertData = [
  500. 'name'=>$data['name']
  501. ];
  502. $result = TemplateClass::insertGetId($insertData);
  503. if(empty($result)){
  504. return Result::error("创建失败",0);
  505. }else{
  506. return Result::success(["id"=>$result]);
  507. }
  508. }
  509. /**
  510. * 更新模板
  511. * @param array $data
  512. * @return array
  513. */
  514. public function upTemplateClass(array $data): array
  515. {
  516. $where = [
  517. 'id'=>$data['id']
  518. ];
  519. $insertData = [
  520. 'name'=>$data['name']
  521. ];
  522. $result = TemplateClass::where($where)->update($insertData);
  523. if(empty($result)){
  524. return Result::error("更新失败",0);
  525. }else{
  526. return Result::success();
  527. }
  528. }
  529. /**
  530. * 删除模板
  531. * @param array $data
  532. * @return array
  533. */
  534. public function delTemplateClass(array $data): array
  535. {
  536. $where = [
  537. 'id'=>$data['id']
  538. ];
  539. $result = TemplateClass::where($where)->delete();
  540. if(empty($result)){
  541. return Result::error("删除失败",0);
  542. }else{
  543. return Result::success();
  544. }
  545. }
  546. /**
  547. * 获取分类下的模板
  548. * @param array $data
  549. * @return array
  550. */
  551. public function getTemplate(array $data): array
  552. {
  553. $page = $data['page'];
  554. $pageSize = $data['pageSize'];
  555. $where = [];
  556. if(isset($data['template_class_id']) && $data['template_class_id']){
  557. array_push($where,['template_class_id','=',$data['template_class_id']]);
  558. }
  559. $result = Template::where($where)
  560. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  561. $count = Template::where($where)->count();
  562. if (empty($result)) {
  563. return Result::error("没有数据",0);
  564. }
  565. $data = [
  566. 'rows'=>$result->toArray(),
  567. 'count'=>$count
  568. ];
  569. return Result::success($data);
  570. }
  571. /**
  572. * 创建模板
  573. * @param
  574. * @return void
  575. */
  576. public function addTemplate(array $data): array
  577. {
  578. $insertData = [
  579. 'template_name'=>$data['template_name'],
  580. 'template_img'=>json_encode($data['template_img']),
  581. 'template_class_id'=>$data['template_class_id'],
  582. ];
  583. $result = Template::insertGetId($insertData);
  584. if(empty($result)){
  585. return Result::error("创建模板失败",0);
  586. }else{
  587. return Result::success(["id"=>$result]);
  588. }
  589. }
  590. /**
  591. * 更新模板
  592. * @param array $data
  593. * @return array
  594. */
  595. public function upTemplate(array $data): array
  596. {
  597. $where = [
  598. 'id'=>$data['id']
  599. ];
  600. $insertData = [
  601. 'template_name'=>$data['template_name'],
  602. 'template_img'=>json_encode($data['template_img']),
  603. 'template_class_id'=>$data['template_class_id'],
  604. ];
  605. $result = Template::where($where)->update($insertData);
  606. if(empty($result)){
  607. return Result::error("更新模板失败",0);
  608. }else{
  609. return Result::success();
  610. }
  611. }
  612. /**
  613. * 删除模板
  614. * @param array $data
  615. * @return array
  616. */
  617. public function delTemplate(array $data): array
  618. {
  619. $where = [
  620. 'id'=>$data['id']
  621. ];
  622. $result = Template::where($where)->delete();
  623. if(empty($result)){
  624. return Result::error("删除模板失败",0);
  625. }else{
  626. return Result::success();
  627. }
  628. }
  629. /**
  630. * 搜索网站
  631. * @param array $data
  632. * @return array
  633. */
  634. public function websiteList(array $data): array
  635. {
  636. $where = [];
  637. if(isset($data['keyword']) && !empty($data['keyword'])){
  638. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  639. }
  640. $result = Website::where($where)->get();
  641. if($result){
  642. return Result::success($result);
  643. }else{
  644. return Result::error("没有网站",0);
  645. }
  646. }
  647. public function addWebsiteCategory(array $data): array
  648. {
  649. $website_id = $data['website_id'];
  650. $category_arr_id = $data['category_arr_id'];
  651. $categoryList = Category::whereIn('id',$category_arr_id)->get();
  652. $categoryListIds = [];
  653. if($categoryList){
  654. foreach ($categoryList->toArray() as $val){
  655. array_push($categoryListIds,$val['id']);
  656. }
  657. }
  658. $arr = [];
  659. if($categoryListIds){
  660. foreach ($categoryListIds as $v){
  661. $ids = $this->getUnderlingUIds(intval($v));
  662. $ids_arr = explode(",", $ids);
  663. array_push($arr,$ids_arr);
  664. }
  665. }
  666. $mergedArray = [];
  667. foreach ($arr as $subarray) {
  668. $mergedArray = array_merge($mergedArray, $subarray);
  669. }
  670. var_dump("所有:",$arr,$mergedArray);
  671. //查询出所有的分类进行分割插入 组装数据
  672. $categoryListData = Category::whereIn('id',$mergedArray)->get();
  673. $categoryListData = $categoryListData->toArray();
  674. $insertData = [];
  675. if($categoryListData){
  676. foreach ($categoryListData as $key=>$value){
  677. $insertData[$key]['website_id'] = $website_id;
  678. $insertData[$key]['name'] = $value['name'];
  679. $insertData[$key]['sort'] = $value['sort'];
  680. $insertData[$key]['pid'] = $value['pid'];
  681. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  682. $insertData[$key]['seo_title'] = $value['seo_title'];
  683. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  684. $insertData[$key]['seo_description'] = $value['seo_description'];
  685. $insertData[$key]['alias'] = $value['name'];
  686. $insertData[$key]['category_id'] = $value['id'];
  687. }
  688. }
  689. $result = WebsiteCategory::insert($insertData);
  690. var_dump("插入数据状态:",$result);
  691. if($result){
  692. return Result::success($result);
  693. }else{
  694. return Result::error("创建失败",0);
  695. }
  696. }
  697. /**
  698. * 删除网站导航
  699. * @param array $data
  700. * @return array
  701. */
  702. public function delWebsiteCategory(array $data): array
  703. {
  704. $website_id = $data['website_id']??0;
  705. $category_id = $data['category_id']??0;
  706. $ids = $this->getUnderlingUIds(intval($category_id));
  707. $ids_arr = explode(",", $ids);
  708. $result = WebsiteCategory::where(['website_id'=>$website_id])->whereIn("category_id",$ids_arr)->delete();
  709. if($result){
  710. return Result::success($result);
  711. }else{
  712. return Result::error("删除失败",0);
  713. }
  714. }
  715. /**
  716. * 获取网站导航
  717. * @param array $data
  718. * @return array
  719. */
  720. public function getAdminWebsiteCategory(array $data): array
  721. {
  722. $where = [
  723. 'website_id'=>$data['website_id'],
  724. 'pid'=>0
  725. ];
  726. $result = WebsiteCategory::where($where)->get();
  727. if($result){
  728. return Result::success($result);
  729. }else{
  730. return Result::error("查询失败",0);
  731. }
  732. }
  733. /**
  734. * 更新网站导航
  735. * @param array $data
  736. * @return array
  737. */
  738. public function upWebsiteCategory(array $data): array
  739. {
  740. Db::beginTransaction();
  741. try{
  742. //合并栏目id
  743. $reqIds = array_merge($data['old_category_arr_id'],$data['new_category_arr_id']);
  744. //对比old 数组差异化,把差异化的删除
  745. $result = WebsiteCategory::where(['website_id'=>$data['website_id'],'pid'=>0])->get();
  746. $result = $result->toArray();
  747. $categoryIds = [];
  748. if($result){
  749. foreach ($result as $val){
  750. array_push($categoryIds,$val['category_id']);
  751. }
  752. }
  753. //和原始数据对比取交际
  754. $reqidsIntersect = array_intersect($reqIds,$categoryIds);
  755. //再取差集 进行对比
  756. $differenceIDS = array_merge(array_diff($reqidsIntersect, $categoryIds),array_diff($categoryIds,$reqidsIntersect));
  757. var_dump("差集:",$differenceIDS);
  758. $arr_ids = [];
  759. if(count($differenceIDS)>0){
  760. foreach ($differenceIDS as $vv){
  761. $idV = $this->getUnderlingUIds(intval($vv));
  762. $ids_arrV = explode(",", $idV);
  763. array_push($arr_ids,$ids_arrV);
  764. }
  765. }
  766. $del_ids = array_reduce($arr_ids, 'array_merge', array());
  767. //有差异 删除
  768. if(count($del_ids)>0){
  769. WebsiteCategory::where(['website_id'=>$data['website_id']])->whereIn("category_id",$del_ids)->delete();
  770. }
  771. //传过来的值 和 交际 对比,选出要添加的值 进行插入
  772. $insertIDS = array_merge(array_diff($reqIds, $reqidsIntersect),array_diff($reqidsIntersect,$reqIds));
  773. var_dump("要存储的:",$insertIDS);
  774. //新的数组重新创建
  775. if(count($insertIDS)>0){
  776. $arr = [];
  777. $categoryListIds = $insertIDS;
  778. if($categoryListIds){
  779. foreach ($categoryListIds as $v){
  780. $ids = $this->getUnderlingUIds(intval($v));
  781. $ids_arr = explode(",", $ids);
  782. array_push($arr,$ids_arr);
  783. }
  784. }
  785. $mergedArray = [];
  786. foreach ($arr as $subarray) {
  787. $mergedArray = array_merge($mergedArray, $subarray);
  788. }
  789. var_dump("要插入的ID:",$mergedArray);
  790. //查询出所有的分类进行分割插入 组装数据
  791. $categoryListData = Category::whereIn('id',$mergedArray)->get();
  792. $categoryListData = $categoryListData->toArray();
  793. $insertData = [];
  794. if($categoryListData){
  795. foreach ($categoryListData as $key=>$value){
  796. $insertData[$key]['website_id'] = $data['website_id'];
  797. $insertData[$key]['name'] = $value['name'];
  798. $insertData[$key]['sort'] = $value['sort'];
  799. $insertData[$key]['pid'] = $value['pid'];
  800. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  801. $insertData[$key]['seo_title'] = $value['seo_title'];
  802. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  803. $insertData[$key]['seo_description'] = $value['seo_description'];
  804. $insertData[$key]['alias'] = $value['name'];
  805. $insertData[$key]['category_id'] = $value['id'];
  806. }
  807. }
  808. WebsiteCategory::insert($insertData);
  809. }
  810. Db::commit();
  811. } catch(\Throwable $ex){
  812. Db::rollBack();
  813. var_dump($ex->getMessage());
  814. return Result::error("修改失败",0);
  815. }
  816. return Result::success();
  817. }
  818. /**
  819. * 获取网站列表
  820. * @param array $data
  821. * @return array
  822. */
  823. public function getWebsiteCategoryList(array $data): array
  824. {
  825. $where = [];
  826. if(isset($data['keyword']) && !empty($data['keyword'])){
  827. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  828. }
  829. if(isset($data['website_column_id']) && !empty($data['website_column_id'])){
  830. array_push($where,['website.website_column_id','=',$data['website_column_id']]);
  831. }
  832. $result = Website::where($where)
  833. ->with(["websiteCategory"=>function ($query) {
  834. $query->where(['pid'=>0])->select('website_id','name','alias','category_id');
  835. }])
  836. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  837. ->get();
  838. $count = Website::where($where)->count();
  839. if (empty($result)) {
  840. return Result::error("没有数据",0);
  841. }
  842. $data = [
  843. 'rows'=>$result->toArray(),
  844. 'count'=>$count
  845. ];
  846. if($result){
  847. return Result::success($data);
  848. }else{
  849. return Result::error("查询失败",0);
  850. }
  851. }
  852. /**
  853. * 删除网站下的所有导航
  854. * @param array $data
  855. * @return array
  856. */
  857. public function delWebsiteAllCategory(array $data): array
  858. {
  859. $website_id = $data['website_id'];
  860. $result = WebsiteCategory::where(['website_id'=>$website_id])->delete();
  861. if($result){
  862. return Result::success($result);
  863. }else{
  864. return Result::error("删除失败",0);
  865. }
  866. }
  867. /**
  868. * 获取网站下的某一个导航
  869. * @param array $data
  870. * @return array
  871. */
  872. public function getWebsiteCategoryOnes(array $data): array
  873. {
  874. $website_id = $data['website_id'];
  875. $category_id = $data['category_id'];
  876. $result = WebsiteCategory::where(['website_category.website_id'=>$website_id,'website_category.category_id'=>$category_id])
  877. ->first();
  878. if($result){
  879. return Result::success($result);
  880. }else{
  881. return Result::error("查询失败",0);
  882. }
  883. }
  884. /**
  885. * 更新网闸下的某一个导航
  886. * @param array $data
  887. * @return array
  888. */
  889. public function upWebsiteCategoryones(array $data): array
  890. {
  891. $where = [
  892. 'website_id'=>$data['website_id'],
  893. 'category_id'=>$data['category_id'],
  894. ];
  895. $result = WebsiteCategory::where($where)->update($data);
  896. if($result){
  897. return Result::success($result);
  898. }else{
  899. return Result::error("更新失败",0);
  900. }
  901. }
  902. /**
  903. * 获取网站下的所有导航(包含子导航)
  904. * @param array $data
  905. * @return array
  906. */
  907. public function getWebsiteAllCategory(array $data): array
  908. {
  909. $where = [];
  910. if(isset($data['website_id']) && !empty($data['website_id'])){
  911. array_push($where,['website_category.website_id','=',$data['website_id']]);
  912. }
  913. if(isset($data['name']) && !empty($data['name'])){
  914. array_push($where,['website_category.name','like','%'.$data['name'].'%']);
  915. }
  916. if(isset($data['alias']) && !empty($data['alias'])){
  917. array_push($where,['website_category.alias','like','%'.$data['alias'].'%']);
  918. }
  919. if(isset($data['department_id']) && !empty($data['department_id'])){
  920. array_push($where,['category.department_id','=',$data['department_id']]);
  921. }
  922. if(isset($data['city_id']) && !empty($data['city_id'])){
  923. array_push($where,['category.city_id','=',$data['city_id']]);
  924. }
  925. $result = WebsiteCategory::where($where)
  926. ->leftJoin("category",'website_category.category_id','category.id')
  927. ->leftJoin("department",'category.department_id','department.id')
  928. ->leftJoin("district",'category.city_id','district.id')
  929. ->select("website_category.*","department.name as department_name","district.name as city_name")
  930. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  931. ->get();
  932. $count = WebsiteCategory::where($where)
  933. ->leftJoin("category",'website_category.category_id','category.id')
  934. ->leftJoin("department",'category.department_id','department.id')
  935. ->leftJoin("district",'category.city_id','district.id')
  936. ->count();
  937. if (empty($result)) {
  938. return Result::error("没有数据",0);
  939. }
  940. $data = [
  941. 'rows'=>$result->toArray(),
  942. 'count'=>$count
  943. ];
  944. if($result){
  945. return Result::success($data);
  946. }else{
  947. return Result::error("查询失败",0);
  948. }
  949. if($result){
  950. return Result::success($result);
  951. }else{
  952. return Result::error("查询失败",0);
  953. }
  954. }
  955. /**
  956. * 递归查询数据
  957. * @param $id
  958. * @param $ids
  959. * @return string
  960. */
  961. public function getUnderlingUIds($id, $ids='')
  962. {
  963. $back = Category::where(['pid'=>$id])->get();
  964. $back = $back->toArray();
  965. if (!empty($back) && is_array($back)) {
  966. foreach ($back as $v) {
  967. //防止当前人的ID重复去查询,形成恶性循环
  968. if ($v['id'] == $id) {
  969. continue;
  970. }
  971. $back2 = Category::where(['pid'=>$id])->count('id');
  972. if ($back2 > 0) {
  973. $ids = $this->getUnderlingUIds($v['id'],$ids);
  974. } else {
  975. $ids .= ','.$v['id'];
  976. }
  977. }
  978. }
  979. $ids = $id.','.$ids.',';
  980. $ids = str_replace(',,', ",", $ids);
  981. $ids = trim($ids, ',');
  982. return $ids;
  983. }
  984. /**
  985. * 检测网站名称是否重复
  986. * @param array $data
  987. * @return array
  988. */
  989. public function checkWebsiteName(array $data): array
  990. {
  991. if(isset($data['id'])){
  992. $data[] = ['id',"!=",$data['id']];
  993. unset($data['id']);
  994. }
  995. $websiteInfo = Website::query()->where($data)->first();
  996. if (empty($websiteInfo)) {
  997. return Result::error("找不到网站",0);
  998. }
  999. return Result::success($websiteInfo->toArray());
  1000. }
  1001. /**
  1002. * 检测网站url是否重复
  1003. * @param array $data
  1004. * @return array
  1005. */
  1006. public function checkWebsiteUrl(array $data): array
  1007. {
  1008. $whereData = [];
  1009. if(isset($data['id'])){
  1010. $whereData = [['id',"!=",$data['id']]];
  1011. unset($data['id']);
  1012. }
  1013. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1014. if (empty($websiteInfo)) {
  1015. return Result::error("找不到URL",0);
  1016. }
  1017. return Result::success($websiteInfo->toArray());
  1018. }
  1019. }