WebsiteService.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. //对比old 数组差异化,把差异化的删除
  743. $result = WebsiteCategory::where(['website_id'=>$data['website_id']])->get();
  744. $result = $result->toArray();
  745. $categoryIds = [];
  746. if($result){
  747. foreach ($result as $val){
  748. array_push($categoryIds,$val['category_id']);
  749. }
  750. }
  751. $differenceIDS = array_diff($data['old_category_arr_id'], $categoryIds);
  752. //有差异 删除
  753. if(count($differenceIDS)>0){
  754. WebsiteCategory::where(['website_id'=>$data['category_id']])->whereIn("category_id",$differenceIDS)->delete();
  755. }
  756. //新的数组重新创建
  757. if(count($data['new_category_arr_id'])>0){
  758. $arr = [];
  759. $categoryListIds = $data['new_category_arr_id'];
  760. if($categoryListIds){
  761. foreach ($categoryListIds as $v){
  762. $ids = $this->getUnderlingUIds(intval($v));
  763. $ids_arr = explode(",", $ids);
  764. array_push($arr,$ids_arr);
  765. }
  766. }
  767. $mergedArray = [];
  768. foreach ($arr as $subarray) {
  769. $mergedArray = array_merge($mergedArray, $subarray);
  770. }
  771. //查询出所有的分类进行分割插入 组装数据
  772. $categoryListData = Category::whereIn('id',$mergedArray)->get();
  773. $categoryListData = $categoryListData->toArray();
  774. $insertData = [];
  775. if($categoryListData){
  776. foreach ($categoryListData as $key=>$value){
  777. $insertData[$key]['website_id'] = $data['website_id'];
  778. $insertData[$key]['name'] = $value['name'];
  779. $insertData[$key]['sort'] = $value['sort'];
  780. $insertData[$key]['pid'] = $value['pid'];
  781. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  782. $insertData[$key]['seo_title'] = $value['seo_title'];
  783. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  784. $insertData[$key]['seo_description'] = $value['seo_description'];
  785. $insertData[$key]['alias'] = $value['name'];
  786. $insertData[$key]['category_id'] = $value['id'];
  787. }
  788. }
  789. WebsiteCategory::insert($insertData);
  790. Db::commit();
  791. }
  792. } catch(\Throwable $ex){
  793. Db::rollBack();
  794. var_dump($ex->getMessage());
  795. return Result::error("修改失败",0);
  796. }
  797. return Result::success();
  798. }
  799. /**
  800. * 获取网站列表
  801. * @param array $data
  802. * @return array
  803. */
  804. public function getWebsiteCategoryList(array $data): array
  805. {
  806. $where = [];
  807. if(isset($data['keyword']) && !empty($data['keyword'])){
  808. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  809. }
  810. if(isset($data['website_column_id']) && !empty($data['website_column_id'])){
  811. array_push($where,['website.website_column_id','=',$data['website_column_id']]);
  812. }
  813. $result = Website::where($where)
  814. ->with(["websiteCategory"=>function ($query) {
  815. $query->where(['pid'=>0])->select('website_id','name','category_id');
  816. }])
  817. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  818. ->get();
  819. $count = Website::where($where)->count();
  820. if (empty($result)) {
  821. return Result::error("没有数据",0);
  822. }
  823. $data = [
  824. 'rows'=>$result->toArray(),
  825. 'count'=>$count
  826. ];
  827. if($result){
  828. return Result::success($data);
  829. }else{
  830. return Result::error("查询失败",0);
  831. }
  832. }
  833. /**
  834. * 删除网站下的所有导航
  835. * @param array $data
  836. * @return array
  837. */
  838. public function delWebsiteAllCategory(array $data): array
  839. {
  840. $website_id = $data['website_id'];
  841. $result = WebsiteCategory::where(['website_id'=>$website_id])->delete();
  842. if($result){
  843. return Result::success($result);
  844. }else{
  845. return Result::error("删除失败",0);
  846. }
  847. }
  848. /**
  849. * 获取网站下的某一个导航
  850. * @param array $data
  851. * @return array
  852. */
  853. public function getWebsiteCategoryOnes(array $data): array
  854. {
  855. $website_id = $data['website_id'];
  856. $category_id = $data['category_id'];
  857. $result = WebsiteCategory::where(['website_category.website_id'=>$website_id,'website_category.category_id'=>$category_id])
  858. ->first();
  859. if($result){
  860. return Result::success($result);
  861. }else{
  862. return Result::error("查询失败",0);
  863. }
  864. }
  865. /**
  866. * 更新网闸下的某一个导航
  867. * @param array $data
  868. * @return array
  869. */
  870. public function upWebsiteCategoryones(array $data): array
  871. {
  872. $where = [
  873. 'website_id'=>$data['website_id'],
  874. 'category_id'=>$data['category_id'],
  875. ];
  876. $result = WebsiteCategory::where($where)->update($data);
  877. if($result){
  878. return Result::success($result);
  879. }else{
  880. return Result::error("更新失败",0);
  881. }
  882. }
  883. /**
  884. * 获取网站下的所有导航(包含子导航)
  885. * @param array $data
  886. * @return array
  887. */
  888. public function getWebsiteAllCategory(array $data): array
  889. {
  890. $where = [];
  891. if(isset($data['website_id']) && !empty($data['website_id'])){
  892. array_push($where,['website_category.website_id','=',$data['website_id']]);
  893. }
  894. if(isset($data['name']) && !empty($data['name'])){
  895. array_push($where,['website_category.name','like','%'.$data['name'].'%']);
  896. }
  897. if(isset($data['alias']) && !empty($data['alias'])){
  898. array_push($where,['website_category.alias','like','%'.$data['alias'].'%']);
  899. }
  900. if(isset($data['department_id']) && !empty($data['department_id'])){
  901. array_push($where,['category.department_id','=',$data['department_id']]);
  902. }
  903. if(isset($data['city_id']) && !empty($data['city_id'])){
  904. array_push($where,['category.city_id','=',$data['city_id']]);
  905. }
  906. $result = WebsiteCategory::where($where)
  907. ->leftJoin("category",'website_category.category_id','category.id')
  908. ->leftJoin("department",'category.department_id','department.id')
  909. ->leftJoin("district",'category.city_id','district.id')
  910. ->select("website_category.*","department.name as department_name","district.name as city_name")
  911. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  912. ->get();
  913. $count = WebsiteCategory::where($where)
  914. ->leftJoin("category",'website_category.category_id','category.id')
  915. ->leftJoin("department",'category.department_id','department.id')
  916. ->leftJoin("district",'category.city_id','district.id')
  917. ->count();
  918. if (empty($result)) {
  919. return Result::error("没有数据",0);
  920. }
  921. $data = [
  922. 'rows'=>$result->toArray(),
  923. 'count'=>$count
  924. ];
  925. if($result){
  926. return Result::success($data);
  927. }else{
  928. return Result::error("查询失败",0);
  929. }
  930. if($result){
  931. return Result::success($result);
  932. }else{
  933. return Result::error("查询失败",0);
  934. }
  935. }
  936. /**
  937. * 递归查询数据
  938. * @param $id
  939. * @param $ids
  940. * @return string
  941. */
  942. public function getUnderlingUIds($id, $ids='')
  943. {
  944. $back = Category::where(['pid'=>$id])->get();
  945. $back = $back->toArray();
  946. if (!empty($back) && is_array($back)) {
  947. foreach ($back as $v) {
  948. //防止当前人的ID重复去查询,形成恶性循环
  949. if ($v['id'] == $id) {
  950. continue;
  951. }
  952. $back2 = Category::where(['pid'=>$id])->count('id');
  953. if ($back2 > 0) {
  954. $ids = $this->getUnderlingUIds($v['id'],$ids);
  955. } else {
  956. $ids .= ','.$v['id'];
  957. }
  958. }
  959. }
  960. $ids = $id.','.$ids.',';
  961. $ids = str_replace(',,', ",", $ids);
  962. $ids = trim($ids, ',');
  963. return $ids;
  964. }
  965. /**
  966. * 检测网站名称是否重复
  967. * @param array $data
  968. * @return array
  969. */
  970. public function checkWebsiteName(array $data): array
  971. {
  972. if(isset($data['id'])){
  973. $data[] = ['id',"!=",$data['id']];
  974. unset($data['id']);
  975. }
  976. $websiteInfo = Website::query()->where($data)->first();
  977. if (empty($websiteInfo)) {
  978. return Result::error("找不到网站",0);
  979. }
  980. return Result::success($websiteInfo->toArray());
  981. }
  982. /**
  983. * 检测网站url是否重复
  984. * @param array $data
  985. * @return array
  986. */
  987. public function checkWebsiteUrl(array $data): array
  988. {
  989. $whereData = [];
  990. if(isset($data['id'])){
  991. $whereData = [['id',"!=",$data['id']]];
  992. unset($data['id']);
  993. }
  994. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  995. if (empty($websiteInfo)) {
  996. return Result::error("找不到URL",0);
  997. }
  998. return Result::success($websiteInfo->toArray());
  999. }
  1000. }