WebsiteService.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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' => 1,
  454. 'id' => $data['ad_placeid']
  455. ];
  456. //查询这个广告位是否存在
  457. $ad_place = AdPlace::where($where)->orderBy('id','asc')->first();
  458. var_dump("==========",$ad_place);
  459. if(empty($ad_place)){
  460. return Result::error("error",0);
  461. }else{
  462. <<<<<<< HEAD
  463. $adplaceid = [];
  464. foreach($ad_place as $val){
  465. $adplaceid = $val;
  466. }
  467. =======
  468. $adplaceid = $ad_place['id'];
  469. //查找有没有广告
  470. >>>>>>> web_sannong_fr
  471. $ad=Ad::where('pid',$adplaceid['id'])->where('status',1)->orderBy('id','asc')->get();
  472. if(empty($ad)){
  473. //若没有生效的广告,则显示默认的缩略图
  474. $result=$adplaceid;
  475. return Result::success($result);
  476. }else{
  477. //查找在生效时间的广告
  478. $today = Carbon::now();
  479. foreach($ad as $i)
  480. {
  481. $starttime=Carbon::parse($i['fromtime']);
  482. $endtime=Carbon::parse($i['totime']);
  483. $time=$today->between($starttime,$endtime);
  484. if($time)
  485. {
  486. $result=$i;
  487. }
  488. }
  489. if(empty($result)){
  490. $result=$ad;
  491. return Result::success($result);
  492. }else{
  493. return Result::success($result);
  494. }
  495. }
  496. }
  497. }
  498. /**
  499. *
  500. * @param array $data
  501. * @return array
  502. */
  503. /**网站行政职能搜索 */
  504. public function selectWebsiteDepartment(array $data): array
  505. {
  506. $depart = Department::where('pid',0)->orderBy('id','asc')->limit(10)->get();
  507. if(isset($data['keyword']) && !empty($data['keyword'])){
  508. $departments= Department::where('name', 'like', '%' . $data['keyword'] . '%')->get();
  509. if(empty($departments)){
  510. $result['message']="未查询到与此相关职能部门";
  511. }else{
  512. $count = Department::where('name','like',"%{$data['keyword']}%")->count();
  513. $m = [
  514. 'department'=>$depart,
  515. 'type'=>$departments,
  516. 'count'=>$count
  517. ];
  518. }
  519. $result['sele'] = $m;
  520. return Result::success($result['sele']);
  521. }
  522. $result=$depart;
  523. return Result::success($result['data']);
  524. }
  525. /**
  526. *
  527. * @param array $data
  528. * @return array
  529. */
  530. /**
  531. * 搜索地区
  532. */
  533. public function selectWebsiteArea(array $data): array
  534. {
  535. $provinces=District::where('pid',0)->where('status',1)->get();
  536. if(isset($data['province'])){
  537. $province=District::where('pid',0)->where('status',1)->where('id',$data['province'])->orderBy('id')->get();
  538. $province=$province->toArray();
  539. if(!empty($province)){
  540. $citys=District::where('pid',$data['province'])->where('status',1)->orderBy('id')->get();
  541. if(!empty($citys) && isset($data['city']) && !empty($data['city'])){
  542. // $province = $province->toArray();
  543. $province_id=[];
  544. foreach($province as $val){
  545. array_push($province_id,$val['id']);
  546. }
  547. // var_dump($province_id);
  548. $city=District::whereIn('pid',$province_id)->where('status',1)->where('id',$data['city'])->orderBy('id')->get();
  549. if(!empty($city)){
  550. $city_id=[];
  551. foreach($city as $val){
  552. array_push($city_id,$val['id']);
  553. }
  554. $regions=District::whereIn('pid',$city_id)->where('status',1)->orderBy('id')->get();
  555. $result=[
  556. 'province' => $province,
  557. 'city' => $city,
  558. 'region' => $regions
  559. ];
  560. }else{
  561. return Result::error("未查询到此城市",0);
  562. }
  563. }else{
  564. $result=[
  565. 'province' => $province,
  566. 'city' => $citys,
  567. 'region' => null
  568. ];
  569. }
  570. }else{
  571. return Result::error("未查询到此省份",0);
  572. }
  573. }else{
  574. // $keys = array('data');
  575. $result = $provinces;
  576. }
  577. return Result::success($result);
  578. }
  579. /**
  580. * 获取栏目
  581. * @param array $data
  582. * @return array
  583. */
  584. public function getWebsiteModelCategory(array $data): array
  585. {
  586. $website_id=[
  587. 'website_id' => $data['website_id']
  588. ];
  589. $placeid=$data['placeid']-1;
  590. $pid=[
  591. 'pid' => $data['pid'],
  592. ];
  593. $num = $data['num'];
  594. $result=WebsiteCategory::where($website_id)->where($pid)->orderBy('sort')->offset($placeid)->limit($num)->get();
  595. $result = $result->toArray();
  596. if(!empty($result)){
  597. return Result::success($result);
  598. }else{
  599. return Result::error("本网站暂无栏目",0);
  600. }
  601. }
  602. /**
  603. * 获取友情链接
  604. * @param array $data
  605. * @return array
  606. */
  607. public function selectWebsiteLinks(array $data): array
  608. {
  609. $where = [
  610. 'website_id' => $data['website_id'],
  611. 'status' => 1,
  612. 'type' => $data['type']
  613. ];
  614. $num=$data['num'];
  615. $result=Link::where($where)->orderBy('id')->limit($num)->get();
  616. if(!empty($result)){
  617. return Result::success($result);
  618. }else{
  619. return Result::error("本网站暂无此类型友情链接",0);
  620. }
  621. }
  622. /**
  623. * 网站首页数据统计, 管理员
  624. * @return void
  625. */
  626. public function getAdminIndex(array $data): array
  627. {
  628. var_dump("用户类型:",$data['type_id']);
  629. switch ($data['type_id']){
  630. case 4:
  631. $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;');
  632. return Result::success($result);
  633. break;
  634. case 10000:
  635. $res = [];
  636. //网站
  637. $res['website']['count'] = 0;
  638. $res['website']['growth_rate'] = 0;
  639. //资讯
  640. $res['article']['count'] = 0;
  641. $res['article']['growth_rate'] = 0;
  642. //导航池
  643. $res['category']['count'] = 0;
  644. $res['category']['growth_rate'] = 0;
  645. //近一月数据
  646. $res['monthArticle']= [];
  647. //用户类型
  648. $res['userType'] = [];
  649. $res['website']['count'] = Website::where([])->count();
  650. $res['article']['count'] = Article::whereNotIn('status',['404'])->count();
  651. $res['category']['count'] = Category::where([])->count();
  652. $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;');
  653. $res['userType'] = User::where([])->selectRaw("count(*) as counts,type_id")->groupBy('type_id')->get();
  654. return Result::success($res);
  655. }
  656. return [];
  657. }
  658. /**
  659. * 获取模板类型
  660. * @return void
  661. */
  662. public function getTemplateClass(array $data): array
  663. {
  664. $where = [];
  665. if(isset($data['name']) && $data['name']){
  666. array_push($where,['name','like','%'.$data['name'].'%']);
  667. }
  668. $result = TemplateClass::where($where)->orderBy('sort','asc')->get();
  669. if(empty($result)){
  670. return Result::error("没有模板类型",0);
  671. }else{
  672. return Result::success($result);
  673. }
  674. }
  675. /**
  676. * 添加模板类型
  677. * @param
  678. * @return void
  679. */
  680. public function addTemplateClass(array $data): array
  681. {
  682. $insertData = [
  683. 'name'=>$data['name']
  684. ];
  685. $result = TemplateClass::insertGetId($insertData);
  686. if(empty($result)){
  687. return Result::error("创建失败",0);
  688. }else{
  689. return Result::success(["id"=>$result]);
  690. }
  691. }
  692. /**
  693. * 更新模板
  694. * @param array $data
  695. * @return array
  696. */
  697. public function upTemplateClass(array $data): array
  698. {
  699. $where = [
  700. 'id'=>$data['id']
  701. ];
  702. $insertData = [
  703. 'name'=>$data['name']
  704. ];
  705. $result = TemplateClass::where($where)->update($insertData);
  706. if(empty($result)){
  707. return Result::error("更新失败",0);
  708. }else{
  709. return Result::success();
  710. }
  711. }
  712. /**
  713. * 删除模板
  714. * @param array $data
  715. * @return array
  716. */
  717. public function delTemplateClass(array $data): array
  718. {
  719. $where = [
  720. 'id'=>$data['id']
  721. ];
  722. $result = TemplateClass::where($where)->delete();
  723. if(empty($result)){
  724. return Result::error("删除失败",0);
  725. }else{
  726. return Result::success();
  727. }
  728. }
  729. /**
  730. * 获取分类下的模板
  731. * @param array $data
  732. * @return array
  733. */
  734. public function getTemplate(array $data): array
  735. {
  736. $page = $data['page'];
  737. $pageSize = $data['pageSize'];
  738. $where = [];
  739. if(isset($data['template_class_id']) && $data['template_class_id']){
  740. array_push($where,['template_class_id','=',$data['template_class_id']]);
  741. }
  742. $result = Template::where($where)
  743. ->limit($pageSize)->offset(($page-1)*$pageSize)->get();
  744. $count = Template::where($where)->count();
  745. if (empty($result)) {
  746. return Result::error("没有数据",0);
  747. }
  748. $data = [
  749. 'rows'=>$result->toArray(),
  750. 'count'=>$count
  751. ];
  752. return Result::success($data);
  753. }
  754. /**
  755. * 创建模板
  756. * @param
  757. * @return void
  758. */
  759. public function addTemplate(array $data): array
  760. {
  761. $insertData = [
  762. 'template_name'=>$data['template_name'],
  763. 'template_img'=>json_encode($data['template_img']),
  764. 'template_class_id'=>$data['template_class_id'],
  765. ];
  766. $result = Template::insertGetId($insertData);
  767. if(empty($result)){
  768. return Result::error("创建模板失败",0);
  769. }else{
  770. return Result::success(["id"=>$result]);
  771. }
  772. }
  773. /**
  774. * 更新模板
  775. * @param array $data
  776. * @return array
  777. */
  778. public function upTemplate(array $data): array
  779. {
  780. $where = [
  781. 'id'=>$data['id']
  782. ];
  783. $insertData = [
  784. 'template_name'=>$data['template_name'],
  785. 'template_img'=>json_encode($data['template_img']),
  786. 'template_class_id'=>$data['template_class_id'],
  787. ];
  788. $result = Template::where($where)->update($insertData);
  789. if(empty($result)){
  790. return Result::error("更新模板失败",0);
  791. }else{
  792. return Result::success();
  793. }
  794. }
  795. /**
  796. * 删除模板
  797. * @param array $data
  798. * @return array
  799. */
  800. public function delTemplate(array $data): array
  801. {
  802. $where = [
  803. 'id'=>$data['id']
  804. ];
  805. $result = Template::where($where)->delete();
  806. if(empty($result)){
  807. return Result::error("删除模板失败",0);
  808. }else{
  809. return Result::success();
  810. }
  811. }
  812. /**
  813. * 搜索网站
  814. * @param array $data
  815. * @return array
  816. */
  817. public function websiteList(array $data): array
  818. {
  819. $where = [];
  820. if(isset($data['keyword']) && !empty($data['keyword'])){
  821. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  822. }
  823. $result = Website::where($where)->get();
  824. if($result){
  825. return Result::success($result);
  826. }else{
  827. return Result::error("没有网站",0);
  828. }
  829. }
  830. public function addWebsiteCategory(array $data): array
  831. {
  832. $website_id = $data['website_id'];
  833. $category_arr_id = $data['category_arr_id'];
  834. $categoryList = Category::whereIn('id',$category_arr_id)->get();
  835. $categoryListIds = [];
  836. if($categoryList){
  837. foreach ($categoryList->toArray() as $val){
  838. array_push($categoryListIds,$val['id']);
  839. }
  840. }
  841. $arr = [];
  842. if($categoryListIds){
  843. foreach ($categoryListIds as $v){
  844. $ids = $this->getUnderlingUIds(intval($v));
  845. $ids_arr = explode(",", $ids);
  846. array_push($arr,$ids_arr);
  847. }
  848. }
  849. $mergedArray = [];
  850. foreach ($arr as $subarray) {
  851. $mergedArray = array_merge($mergedArray, $subarray);
  852. }
  853. var_dump("所有:",$arr,$mergedArray);
  854. //查询出所有的分类进行分割插入 组装数据
  855. $categoryListData = Category::whereIn('id',$mergedArray)->get();
  856. $categoryListData = $categoryListData->toArray();
  857. $insertData = [];
  858. if($categoryListData){
  859. foreach ($categoryListData as $key=>$value){
  860. $insertData[$key]['website_id'] = $website_id;
  861. $insertData[$key]['name'] = $value['name'];
  862. $insertData[$key]['sort'] = $value['sort'];
  863. $insertData[$key]['pid'] = $value['pid'];
  864. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  865. $insertData[$key]['seo_title'] = $value['seo_title'];
  866. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  867. $insertData[$key]['seo_description'] = $value['seo_description'];
  868. $insertData[$key]['alias'] = $value['name'];
  869. $insertData[$key]['category_id'] = $value['id'];
  870. }
  871. }
  872. $result = WebsiteCategory::insert($insertData);
  873. var_dump("插入数据状态:",$result);
  874. if($result){
  875. return Result::success($result);
  876. }else{
  877. return Result::error("创建失败",0);
  878. }
  879. }
  880. /**
  881. * 删除网站导航
  882. * @param array $data
  883. * @return array
  884. */
  885. public function delWebsiteCategory(array $data): array
  886. {
  887. $website_id = $data['website_id']??0;
  888. $category_id = $data['category_id']??0;
  889. $ids = $this->getUnderlingUIds(intval($category_id));
  890. $ids_arr = explode(",", $ids);
  891. $result = WebsiteCategory::where(['website_id'=>$website_id])->whereIn("category_id",$ids_arr)->delete();
  892. if($result){
  893. return Result::success($result);
  894. }else{
  895. return Result::error("删除失败",0);
  896. }
  897. }
  898. /**
  899. * 获取网站导航
  900. * @param array $data
  901. * @return array
  902. */
  903. public function getAdminWebsiteCategory(array $data): array
  904. {
  905. $where = [
  906. 'website_id'=>$data['website_id'],
  907. 'pid'=>0
  908. ];
  909. $result = WebsiteCategory::where($where)->get();
  910. if($result){
  911. return Result::success($result);
  912. }else{
  913. return Result::error("查询失败",0);
  914. }
  915. }
  916. /**
  917. * 更新网站导航
  918. * @param array $data
  919. * @return array
  920. */
  921. public function upWebsiteCategory(array $data): array
  922. {
  923. Db::beginTransaction();
  924. try{
  925. //合并栏目id
  926. $reqIds = array_merge($data['old_category_arr_id'],$data['new_category_arr_id']);
  927. //对比old 数组差异化,把差异化的删除
  928. $result = WebsiteCategory::where(['website_id'=>$data['website_id'],'pid'=>0])->get();
  929. $result = $result->toArray();
  930. $categoryIds = [];
  931. if($result){
  932. foreach ($result as $val){
  933. array_push($categoryIds,$val['category_id']);
  934. }
  935. }
  936. //和原始数据对比取交际
  937. $reqidsIntersect = array_intersect($reqIds,$categoryIds);
  938. //再取差集 进行对比
  939. $differenceIDS = array_merge(array_diff($reqidsIntersect, $categoryIds),array_diff($categoryIds,$reqidsIntersect));
  940. var_dump("差集:",$differenceIDS);
  941. $arr_ids = [];
  942. if(count($differenceIDS)>0){
  943. foreach ($differenceIDS as $vv){
  944. $idV = $this->getUnderlingUIds(intval($vv));
  945. $ids_arrV = explode(",", $idV);
  946. array_push($arr_ids,$ids_arrV);
  947. }
  948. }
  949. $del_ids = array_reduce($arr_ids, 'array_merge', array());
  950. //有差异 删除
  951. if(count($del_ids)>0){
  952. WebsiteCategory::where(['website_id'=>$data['website_id']])->whereIn("category_id",$del_ids)->delete();
  953. }
  954. //传过来的值 和 交际 对比,选出要添加的值 进行插入
  955. $insertIDS = array_merge(array_diff($reqIds, $reqidsIntersect),array_diff($reqidsIntersect,$reqIds));
  956. var_dump("要存储的:",$insertIDS);
  957. //新的数组重新创建
  958. if(count($insertIDS)>0){
  959. $arr = [];
  960. $categoryListIds = $insertIDS;
  961. if($categoryListIds){
  962. foreach ($categoryListIds as $v){
  963. $ids = $this->getUnderlingUIds(intval($v));
  964. $ids_arr = explode(",", $ids);
  965. array_push($arr,$ids_arr);
  966. }
  967. }
  968. $mergedArray = [];
  969. foreach ($arr as $subarray) {
  970. $mergedArray = array_merge($mergedArray, $subarray);
  971. }
  972. var_dump("要插入的ID:",$mergedArray);
  973. //查询出所有的分类进行分割插入 组装数据
  974. $categoryListData = Category::whereIn('id',$mergedArray)->get();
  975. $categoryListData = $categoryListData->toArray();
  976. $insertData = [];
  977. if($categoryListData){
  978. foreach ($categoryListData as $key=>$value){
  979. $insertData[$key]['website_id'] = $data['website_id'];
  980. $insertData[$key]['name'] = $value['name'];
  981. $insertData[$key]['sort'] = $value['sort'];
  982. $insertData[$key]['pid'] = $value['pid'];
  983. $insertData[$key]['pid_arr'] = $value['pid_arr'];
  984. $insertData[$key]['seo_title'] = $value['seo_title'];
  985. $insertData[$key]['seo_keywords'] = $value['seo_keywords'];
  986. $insertData[$key]['seo_description'] = $value['seo_description'];
  987. $insertData[$key]['alias'] = $value['name'];
  988. $insertData[$key]['category_id'] = $value['id'];
  989. }
  990. }
  991. WebsiteCategory::insert($insertData);
  992. }
  993. Db::commit();
  994. } catch(\Throwable $ex){
  995. Db::rollBack();
  996. var_dump($ex->getMessage());
  997. return Result::error("修改失败",0);
  998. }
  999. return Result::success();
  1000. }
  1001. /**
  1002. * 获取网站列表
  1003. * @param array $data
  1004. * @return array
  1005. */
  1006. public function getWebsiteCategoryList(array $data): array
  1007. {
  1008. $where = [];
  1009. if(isset($data['keyword']) && !empty($data['keyword'])){
  1010. array_push($where,['website.website_name','like','%'.$data['keyword'].'%']);
  1011. }
  1012. if(isset($data['website_column_id']) && !empty($data['website_column_id'])){
  1013. array_push($where,['website.website_column_id','=',$data['website_column_id']]);
  1014. }
  1015. $result = Website::where($where)
  1016. ->with(["websiteCategory"=>function ($query) {
  1017. $query->where(['pid'=>0])->select('website_id','name','alias','category_id');
  1018. }])
  1019. ->orderBy('created_at','desc')
  1020. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  1021. ->get();
  1022. $count = Website::where($where)->count();
  1023. if (empty($result)) {
  1024. return Result::error("没有数据",0);
  1025. }
  1026. $data = [
  1027. 'rows'=>$result->toArray(),
  1028. 'count'=>$count
  1029. ];
  1030. if($result){
  1031. return Result::success($data);
  1032. }else{
  1033. return Result::error("查询失败",0);
  1034. }
  1035. }
  1036. /**
  1037. * 删除网站下的所有导航
  1038. * @param array $data
  1039. * @return array
  1040. */
  1041. public function delWebsiteAllCategory(array $data): array
  1042. {
  1043. $website_id = $data['website_id'];
  1044. $result = WebsiteCategory::where(['website_id'=>$website_id])->delete();
  1045. if($result){
  1046. return Result::success($result);
  1047. }else{
  1048. return Result::error("删除失败",0);
  1049. }
  1050. }
  1051. /**
  1052. * 获取网站下的某一个导航
  1053. * @param array $data
  1054. * @return array
  1055. */
  1056. public function getWebsiteCategoryOnes(array $data): array
  1057. {
  1058. $website_id = $data['website_id'];
  1059. $category_id = $data['category_id'];
  1060. $result = WebsiteCategory::where(['website_category.website_id'=>$website_id,'website_category.category_id'=>$category_id])
  1061. ->first();
  1062. if($result){
  1063. return Result::success($result);
  1064. }else{
  1065. return Result::error("查询失败",0);
  1066. }
  1067. }
  1068. /**
  1069. * 更新网闸下的某一个导航
  1070. * @param array $data
  1071. * @return array
  1072. */
  1073. public function upWebsiteCategoryones(array $data): array
  1074. {
  1075. $where = [
  1076. 'website_id'=>$data['website_id'],
  1077. 'category_id'=>$data['category_id'],
  1078. ];
  1079. $result = WebsiteCategory::where($where)->update($data);
  1080. if($result){
  1081. return Result::success($result);
  1082. }else{
  1083. return Result::error("更新失败",0);
  1084. }
  1085. }
  1086. /**
  1087. * 获取网站下的所有导航(包含子导航)
  1088. * @param array $data
  1089. * @return array
  1090. */
  1091. public function getWebsiteAllCategory(array $data): array
  1092. {
  1093. $where = [];
  1094. if(isset($data['website_id']) && !empty($data['website_id'])){
  1095. array_push($where,['website_category.website_id','=',$data['website_id']]);
  1096. }
  1097. if(isset($data['name']) && !empty($data['name'])){
  1098. array_push($where,['website_category.name','like','%'.$data['name'].'%']);
  1099. }
  1100. if(isset($data['alias']) && !empty($data['alias'])){
  1101. array_push($where,['website_category.alias','like','%'.$data['alias'].'%']);
  1102. }
  1103. if(isset($data['department_id']) && !empty($data['department_id'])){
  1104. array_push($where,['category.department_id','=',$data['department_id']]);
  1105. }
  1106. if(isset($data['city_id']) && !empty($data['city_id'])){
  1107. array_push($where,['category.city_id','=',$data['city_id']]);
  1108. }
  1109. $result = WebsiteCategory::where($where)
  1110. ->leftJoin("category",'website_category.category_id','category.id')
  1111. ->leftJoin("department",'category.department_id','department.id')
  1112. ->leftJoin("district",'category.city_id','district.id')
  1113. ->select("website_category.*","department.name as department_name","district.name as city_name")
  1114. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  1115. ->get();
  1116. $count = WebsiteCategory::where($where)
  1117. ->leftJoin("category",'website_category.category_id','category.id')
  1118. ->leftJoin("department",'category.department_id','department.id')
  1119. ->leftJoin("district",'category.city_id','district.id')
  1120. ->count();
  1121. if (empty($result)) {
  1122. return Result::error("没有数据",0);
  1123. }
  1124. $data = [
  1125. 'rows'=>$result->toArray(),
  1126. 'count'=>$count
  1127. ];
  1128. if($result){
  1129. return Result::success($data);
  1130. }else{
  1131. return Result::error("查询失败",0);
  1132. }
  1133. if($result){
  1134. return Result::success($result);
  1135. }else{
  1136. return Result::error("查询失败",0);
  1137. }
  1138. }
  1139. /**
  1140. * 递归查询数据
  1141. * @param $id
  1142. * @param $ids
  1143. * @return string
  1144. */
  1145. public function getUnderlingUIds($id, $ids='')
  1146. {
  1147. $back = Category::where(['pid'=>$id])->get();
  1148. $back = $back->toArray();
  1149. if (!empty($back) && is_array($back)) {
  1150. foreach ($back as $v) {
  1151. //防止当前人的ID重复去查询,形成恶性循环
  1152. if ($v['id'] == $id) {
  1153. continue;
  1154. }
  1155. $back2 = Category::where(['pid'=>$id])->count('id');
  1156. if ($back2 > 0) {
  1157. $ids = $this->getUnderlingUIds($v['id'],$ids);
  1158. } else {
  1159. $ids .= ','.$v['id'];
  1160. }
  1161. }
  1162. }
  1163. $ids = $id.','.$ids.',';
  1164. $ids = str_replace(',,', ",", $ids);
  1165. $ids = trim($ids, ',');
  1166. return $ids;
  1167. }
  1168. /**
  1169. * 检测网站名称是否重复
  1170. * @param array $data
  1171. * @return array
  1172. */
  1173. public function checkWebsiteName(array $data): array
  1174. {
  1175. if(isset($data['id'])){
  1176. $data[] = ['id',"!=",$data['id']];
  1177. unset($data['id']);
  1178. }
  1179. $websiteInfo = Website::query()->where($data)->first();
  1180. if (empty($websiteInfo)) {
  1181. return Result::error("找不到网站",0);
  1182. }
  1183. return Result::success($websiteInfo->toArray());
  1184. }
  1185. /**
  1186. * 检测网站url是否重复
  1187. * @param array $data
  1188. * @return array
  1189. */
  1190. public function checkWebsiteUrl(array $data): array
  1191. {
  1192. $whereData = [];
  1193. if(isset($data['id'])){
  1194. $whereData = [['id',"!=",$data['id']]];
  1195. unset($data['id']);
  1196. }
  1197. $websiteInfo = Website::query()->where($whereData)->whereJsonContains('website_url', $data['website_url'])->first();
  1198. if (empty($websiteInfo)) {
  1199. return Result::error("找不到URL",0);
  1200. }
  1201. return Result::success($websiteInfo->toArray());
  1202. }
  1203. }