WebsiteService.php 30 KB

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