WebsiteService.php 41 KB

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