WebsiteService.php 41 KB

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