WebsiteService.php 39 KB

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