WebsiteService.php 35 KB

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