NewsService.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Article;
  4. use App\Model\ArticleData;
  5. use App\Model\Category;
  6. use App\Model\Website;
  7. use App\Model\WebsiteCategory;
  8. use Hyperf\DbConnection\Db;
  9. use Hyperf\RpcServer\Annotation\RpcService;
  10. use App\Tools\Result;
  11. use App\Model\ArticleSurvey;
  12. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  13. class NewsService implements NewsServiceInterface
  14. {
  15. /**
  16. * 获取导航池列表
  17. * @param array $data
  18. * @return array
  19. */
  20. public function getCategoryList(array $data): array
  21. {
  22. $where = [];
  23. if(isset($data['name']) && $data['name']){
  24. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  25. }
  26. if(isset($data['department_id']) && $data['department_id']){
  27. array_push($where, ['category.department_id','=',$data['department_id']]);
  28. }
  29. if(isset($data['city_id']) && $data['city_id']){
  30. array_push($where, ['category.city_id','=',$data['city_id']]);
  31. }
  32. $rep = Category::where($where)
  33. ->leftJoin('district','category.city_id','district.id')
  34. ->leftJoin('department','category.department_id','department.id')
  35. ->select("category.*","district.name as city_name","department.name as department_name")
  36. ->limit($data['pageSize'])->orderBy("category.sort","desc")->orderByDesc('category.updated_at')->offset(($data['page']-1)*$data['pageSize'])->get();
  37. $count = Category::where($where)->count();
  38. $data = [
  39. 'rows'=>$rep->toArray(),
  40. 'count'=>$count
  41. ];
  42. if(empty($rep->toArray())){
  43. return Result::error("没有导航池数据");
  44. }
  45. return Result::success($data);
  46. }
  47. /**
  48. * @param array $data
  49. * @return array
  50. */
  51. public function categoryList(array $data): array
  52. {
  53. $where[] = [
  54. 'pid','=',$data['pid']
  55. ];
  56. if(isset($data['name'])){
  57. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  58. }
  59. var_dump($where);
  60. $result = Category::where($where)->select('category.*','category.id as category_id')->get();
  61. if(empty($result)){
  62. return Result::error("没有栏目数据");
  63. }
  64. return Result::success($result);
  65. }
  66. /**
  67. * @param array $data
  68. * @return array
  69. */
  70. public function addCategory(array $data): array
  71. {
  72. $id = Category::insertGetId($data);
  73. if(empty($id)){
  74. return Result::error("添加失败");
  75. }
  76. return Result::success(['id'=>$id]);
  77. }
  78. /**
  79. * @param array $data
  80. * @return array
  81. */
  82. public function delCategory(array $data): array
  83. {
  84. $categoryList = Category::where(['pid'=>$data['id']])->get();
  85. var_dump("分类列表:",$data,$categoryList);
  86. if($categoryList->toArray()){
  87. return Result::error("分类下面有子分类不能删除");
  88. }
  89. $articleList = Article::where(['catid'=>$data['id']])->get();
  90. var_dump("文章列表:",$articleList);
  91. if($articleList->toArray()){
  92. return Result::error("分类下面有资讯不能删除");
  93. }
  94. $result = Category::where($data)->delete();
  95. if(!$result){
  96. return Result::error("删除失败");
  97. }
  98. return Result::success($result);
  99. }
  100. /**
  101. * @param array $data
  102. * @return array
  103. */
  104. public function updateCategory(array $data): array
  105. {
  106. $where = [
  107. 'id'=>$data['id']
  108. ];
  109. $result = Category::where($where)->update($data);
  110. if($result){
  111. return Result::success($result);
  112. }else{
  113. return Result::error("更新失败");
  114. }
  115. }
  116. /**
  117. * 获取导航池信息
  118. * @param array $data
  119. * @return array
  120. */
  121. public function getCategoryInfo(array $data): array
  122. {
  123. $where = [
  124. 'id'=>$data['id']
  125. ];
  126. $result = Category::where($where)->first();
  127. if($result){
  128. return Result::success($result);
  129. }else{
  130. return Result::error("更新失败");
  131. }
  132. }
  133. /**
  134. * @param array $data
  135. * @return array
  136. */
  137. public function getArticleList(array $data): array
  138. {
  139. $where= [];
  140. if(isset($data['title']) && $data['title']){
  141. array_push($where,['article.title','like','%'.$data['title'].'%']);
  142. }
  143. if(isset($data['category_name']) && $data['category_name']){
  144. array_push($where,['category.name','like','%'.$data['category_name'].'%']);
  145. }
  146. if(isset($data['author']) && $data['author']){
  147. array_push($where,['article.author','=',$data['author']]);
  148. }
  149. if(isset($data['islink']) && $data['islink']!==""){
  150. array_push($where,['article.islink','=',$data['islink']]);
  151. }
  152. if(isset($data['status']) && $data['status']!==""){
  153. array_push($where,['article.status','=',$data['status']]);
  154. }
  155. $rep = Article::where($where)
  156. ->whereNotIn('article.status',[404])
  157. ->leftJoin('category','article.catid','category.id')
  158. ->select("article.*","category.name as category_name")
  159. ->orderBy("article.id","desc")
  160. ->limit($data['pageSize'])
  161. ->offset(($data['page']-1)*$data['pageSize'])->get();
  162. $count = Article::where($where)->whereNotIn('article.status',[404])
  163. ->leftJoin('category','article.catid','category.id')->count();
  164. $data = [
  165. 'rows'=>$rep->toArray(),
  166. 'count'=>$count
  167. ];
  168. if(empty($rep)){
  169. return Result::error("没有信息数据");
  170. }
  171. return Result::success($data);
  172. }
  173. /**
  174. * @param array $data
  175. * @return array
  176. */
  177. public function addArticle(array $data): array
  178. {
  179. Db::beginTransaction();
  180. try{
  181. $articleData = $data;
  182. unset($articleData['content']);
  183. $id = Article::insertGetId($articleData);
  184. $articleDataContent = [
  185. 'article_id'=>$id,
  186. 'content'=>$data['content']
  187. ];
  188. ArticleData::insertGetId($articleDataContent);
  189. Db::commit();
  190. } catch(\Throwable $ex){
  191. Db::rollBack();
  192. var_dump($ex->getMessage());
  193. return Result::error("创建失败",0);
  194. }
  195. return Result::success(['id'=>$id]);
  196. }
  197. /**
  198. * @param array $data
  199. * @return array
  200. */
  201. public function delArticle(array $data): array
  202. {
  203. $result = Article::where($data)->update(['status'=>404]);
  204. if(!$result){
  205. return Result::error("删除失败");
  206. }
  207. return Result::success($result);
  208. }
  209. /**
  210. * @param array $data
  211. * @return array
  212. */
  213. public function updateArticle(array $data): array
  214. {
  215. Db::beginTransaction();
  216. try{
  217. $data['cat_arr_id'] = isset($data['cat_arr_id'])?json_encode($data['cat_arr_id']):'';
  218. $data['tag'] = isset($data['tag'])?json_encode($data['tag']):'';
  219. $articleData = $data;
  220. unset($articleData['content']);
  221. unset($articleData['status_name']);
  222. unset($articleData['name']);
  223. unset($articleData['content']);
  224. unset($articleData['pid_arr']);
  225. unset($articleData['pid']);
  226. $id = Article::where(['id'=>$data['id']])->update($articleData);
  227. $articleDataContent = [
  228. 'content'=>$data['content']
  229. ];
  230. ArticleData::where(['article_id'=>$data['id']])->update($articleDataContent);
  231. } catch(\Throwable $ex){
  232. Db::rollBack();
  233. var_dump($ex->getMessage());
  234. return Result::error("更新失败",0);
  235. }
  236. return Result::success([]);
  237. }
  238. /**
  239. * 更新资讯状态
  240. * @param array $data
  241. * @return array
  242. */
  243. public function upArticleStatus(array $data):array
  244. {
  245. $result = Article::where(['id'=>$data['id']])->update($data);
  246. if($result){
  247. return Result::success();
  248. }else{
  249. return Result::error("更新状态失败",0);
  250. }
  251. }
  252. /**
  253. * 获取新闻详情
  254. * @param array $data
  255. * @return array
  256. */
  257. public function getArticleInfo(array $data): array
  258. {
  259. $where = [
  260. 'article.id'=>$data['id'],
  261. // 'article.status'=>1
  262. ];
  263. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
  264. if(empty($result)){
  265. return Result::error("查询失败",0);
  266. }
  267. return Result::success($result);
  268. }
  269. /**
  270. * 获取头条新闻
  271. * @param array $data
  272. * @return array
  273. */
  274. public function getWebsiteArticlett(array $data): array
  275. {
  276. $category = WebsiteCategory::where('website_id', $data['website_id'])->pluck('category_id');
  277. $result = [];
  278. if ($category) {
  279. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  280. $where = [
  281. 'status' => 1,
  282. ];
  283. var_dump($data, 'data-----------------');
  284. //如果是4:最新资讯(数据库已不存在) 5:资讯推荐(数据库已不存在);
  285. // 1:头条资讯;2:轮播图;6:热点资讯;(数据库)
  286. var_dump($where, 'where-----------------');
  287. $result = Article::where($where)
  288. ->whereIn("catid", $category)
  289. ->where(function ($query) use ($data) {
  290. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  291. ->orWhereNull("ignore_ids");
  292. })
  293. //$data['level'] == 4 || $data['level'] == 5 查询随机
  294. ->when($data['level'] == 5, function ($query) {
  295. $query->inRandomOrder()
  296. //updated_at最近三十天;
  297. ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
  298. })
  299. ->when($data['level'] == 4, function ($query) {
  300. $query->orderBy("updated_at", "desc");
  301. })
  302. ->when(!empty($data['level']), function ($query) use ($data) {
  303. if ($data['level'] != 4 && $data['level'] != 5) {
  304. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  305. ->orderBy("updated_at", "desc");
  306. }
  307. })
  308. ->offset($placeid)
  309. ->limit($data['pageSize'])
  310. ->get();
  311. if (empty($result)) {
  312. return Result::error("暂无头条新闻", 0);
  313. }
  314. return Result::success($result);
  315. } else {
  316. return Result::error("本网站下暂无相关栏目", 0);
  317. }
  318. }
  319. /**
  320. * 获取模块新闻
  321. * @param array $data
  322. * @return array
  323. */
  324. public function getWebsiteModelArticles(array $data): array
  325. {
  326. $catid = $data['catid'];
  327. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  328. $category = $category->toArray();
  329. if (!empty($category)) {
  330. $where = [
  331. 'status' => 1,
  332. 'catid' => $catid,
  333. ];
  334. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  335. // 1:文字新闻;2:轮播图;3:图文;
  336. // 级别:0:未分类
  337. // 3:推荐图片
  338. if ($data['level'] == 1) {
  339. $data['level'] = 0;
  340. }
  341. $result = Article::where($where)
  342. ->where(function ($query) use ($data) {
  343. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  344. ->orWhereNull("level")
  345. ->orWhereRaw("level = '[]'");
  346. })
  347. ->where(function ($query) use ($data) {
  348. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  349. ->orWhereNull("ignore_ids");
  350. })
  351. ->orderBy("updated_at", "desc")
  352. ->offset($placeid)
  353. ->limit($data['pagesize'])
  354. ->get();
  355. if (empty($result)) {
  356. return Result::error("此栏目暂无相关新闻", 0);
  357. }
  358. } else {
  359. return Result::error("此网站暂无此栏目", 0);
  360. }
  361. return Result::success($result);
  362. }
  363. /**
  364. *获取新闻列表
  365. * @param array $data
  366. * @return array
  367. */
  368. public function getWebsiteArticleList(array $data): array
  369. {
  370. $where[] = ['status', '=', 1];
  371. if(isset($data['keyword']) && !empty($data['keyword'])){
  372. array_push($where,['article.title','like','%'.$data['keyword'].'%']);
  373. }
  374. if(isset($data['catid']) && !empty($data['catid'])){
  375. if(is_array($data['catid'])){
  376. $category = WebsiteCategory::where('website_id',$data['website_id'])->whereIn('category_id',$data['catid'])->pluck('category_id');
  377. array_push($where,['catid', 'in', $data['catid']]);
  378. }else{
  379. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->pluck('category_id');
  380. array_push($where,['catid', '=', $data['catid']]);
  381. }
  382. if(empty($category)){
  383. return Result::error("此网站暂无此栏目",0);
  384. }
  385. }
  386. // return Result::success($where);
  387. $rep = Article::where(function ($query) use ($where) {
  388. foreach ($where as $condition) {
  389. if ($condition[1] === 'in') {
  390. $query->whereIn($condition[0], $condition[2]);
  391. } else {
  392. $query->where($condition[0], $condition[1], $condition[2]);
  393. }
  394. }
  395. })
  396. ->where(function ($query) use ($data) {
  397. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  398. ->orWhereNull("ignore_ids");
  399. })
  400. ->orderBy("updated_at", "desc")
  401. ->limit($data['pageSize'])
  402. ->offset(($data['page'] - 1) * $data['pageSize'])
  403. ->get();
  404. $count = Article::where(function ($query) use ($where) {
  405. foreach ($where as $condition) {
  406. if ($condition[1] === 'in') {
  407. $query->whereIn($condition[0], $condition[2]);
  408. } else {
  409. $query->where($condition[0], $condition[1], $condition[2]);
  410. }
  411. }
  412. })->count();
  413. $data = [
  414. 'rows'=>$rep->toArray(),
  415. 'count'=>$count
  416. ];
  417. if(empty($rep)){
  418. return Result::error("没有信息数据");
  419. }
  420. return Result::success($data);
  421. }
  422. /**
  423. * 前端-获取新闻详情
  424. * @param array $data
  425. * @return array
  426. */
  427. public function selectWebsiteArticleInfo(array $data): array
  428. {
  429. $where = [
  430. 'article.id'=>$data['id'],
  431. 'article.status'=>1
  432. ];
  433. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")
  434. ->where(function ($query) use ($data) {
  435. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  436. ->orWhereNull("ignore_ids");
  437. })
  438. ->first();
  439. if(empty($result)){
  440. return Result::error("暂无此新闻!",0);
  441. }
  442. $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
  443. if(empty($category)){
  444. return Result::error("查询失败",0);
  445. }
  446. $result['category_id'] = $category['category_id'];
  447. $result['cat_name'] = $category['alias'];
  448. return Result::success($result);
  449. }
  450. /**
  451. * 前端-获取网站调查问卷
  452. * @param array $data
  453. * @return array
  454. */
  455. public function getWebsiteSurvey(array $data): array
  456. {
  457. if(isset($data['website_id']) && !empty($data['website_id'])){
  458. $website = Website::where('id',$data['website_id'])->first();
  459. if(empty($website)){
  460. return Result::error("暂无此网站",0);
  461. }
  462. }
  463. if(isset($data['art_id']) && !empty($data['art_id'])){
  464. $article = Article::where('id',$data['art_id'])->where('status',1)->first();
  465. if(empty($article)){
  466. return Result::error("暂无此文章",0);
  467. }
  468. // return Result::error($data,0);
  469. $where['art_id'] = $data['art_id'];
  470. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  471. }else{
  472. $survey = Article::where(function ($query) {
  473. $query->whereRaw("JSON_CONTAINS(cat_arr_id, '28')")
  474. ->orWhereRaw("JSON_CONTAINS(cat_arr_id, '\"28\"')");
  475. })
  476. ->where('status',1)
  477. ->where('is_survey',1)
  478. ->select('survey_id')
  479. ->orderBy('updated_at','desc')
  480. ->first();
  481. if(empty($survey)){
  482. return Result::error("暂无调查问卷",0);
  483. }
  484. $where['sur_id'] = $survey['survey_id'];
  485. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  486. }
  487. // return Result::success($where);
  488. $result = ArticleSurvey::where($where)
  489. ->where(function ($query) {
  490. $query->where('is_other', 0)
  491. ->orWhere(function ($subQuery) {
  492. $subQuery->where('is_other', 1)
  493. ->where('other_id', 0);
  494. });
  495. })
  496. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  497. ->select('article_survey.*', 'article.survey_type')
  498. ->get()->all();
  499. if(empty($result)){
  500. return Result::error("此文章暂无调查问卷",0);
  501. }
  502. return Result::success($result);
  503. }
  504. /**
  505. * 前端-添加网站调查问卷选项
  506. * @param array $data
  507. * @return array
  508. */
  509. public function addWebsiteSurveyOption(array $data): array
  510. {
  511. if(isset($data['website_id']) && !empty($data['website_id'])){
  512. $website = Website::where('id',$data['website_id'])->first();
  513. if(empty($website)){
  514. return Result::error("暂无此网站",0);
  515. }
  516. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  517. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  518. if(empty($survey)){
  519. return Result::error("此调查问卷不可添加选项",0);
  520. }
  521. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  522. $choice = [
  523. 'art_id'=>$survey['art_id'],
  524. 'website_id'=>$data['website_id'],
  525. 'survey_name'=>$survey['survey_name'],
  526. 'choice_name'=>$data['choice_name'],
  527. 'sur_id'=>$survey['sur_id'],
  528. 'is_other'=>1,
  529. 'other_id'=>$survey['id'],
  530. ];
  531. $result = ArticleSurvey::insertGetId($choice);
  532. if(empty($result)){
  533. return Result::error("添加失败",0);
  534. }
  535. return Result::success($result);
  536. }
  537. }
  538. return Result::error("添加失败",0);
  539. }
  540. return Result::error("添加失败",0);
  541. }
  542. /**
  543. * 前端-调查问卷投票
  544. * @param array $data
  545. * @return array
  546. */
  547. public function addWebsiteSurveyVote(array $data): array
  548. {
  549. // return Result::success($data);
  550. if(isset($data['website_id']) && !empty($data['website_id'])){
  551. $website = Website::where('id',$data['website_id'])->first();
  552. if(empty($website)){
  553. return Result::error("暂无此网站",0);
  554. }
  555. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  556. $is_survey = ArticleSurvey::where('sur_id',$data['sur_id'])->first();
  557. // return Result::success($survey);
  558. if(empty($is_survey)){
  559. return Result::error("此调查问卷不存在",0);
  560. }
  561. // return Result::success($survey);
  562. // 调查问卷类型
  563. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  564. //多选 若是json型则转化成数组类型
  565. if (strpos($data['choice_id'], '[') === 0) {
  566. $data['choice_id'] = json_decode($data['choice_id'], true);
  567. } else {
  568. // 单选 也转换成数组
  569. $data['choice_id'] = [$data['choice_id']];
  570. }
  571. $data['choice_id'] = array_map('intval', $data['choice_id']);
  572. $other = ArticleSurvey::whereIn('id',$data['choice_id'])
  573. ->where('website_id',$data['website_id'])
  574. ->where('is_other',1)
  575. ->where('other_id',0)
  576. ->first();
  577. if(!empty($other)){
  578. return Result::error("请选择已有的选项!",0);
  579. }
  580. $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
  581. ->where('website_id',$data['website_id'])
  582. ->where('is_other',1)
  583. ->where('other_id','!=',0)
  584. ->first();
  585. // return Result::success($choice['other']);
  586. $choice_id = $data['choice_id'];
  587. if(!empty($choice['other'])){
  588. // array_push($data['choice_id'],$choice['other']['other_id']);
  589. if(!empty($choice_id)){
  590. $key = array_search($choice['other']['id'], $choice_id);
  591. if ($key!== false) {
  592. unset($choice_id[$key]);
  593. $choice_id = array_values($choice_id);
  594. }
  595. array_push($choice_id,$choice['other']['other_id']);
  596. }else{
  597. $choice_id[0] = $choice['other']['other_id'];
  598. }
  599. array_push($data['choice_id'],$choice['other']['other_id']);
  600. }
  601. // return Result::success($data);
  602. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
  603. ->where('website_id',$data['website_id'])
  604. ->increment('results', 1);
  605. if(empty($choice)){
  606. return Result::error("请选择已有的选项!",0);
  607. }
  608. $survey['data'] = ArticleSurvey::where('sur_id',$data['sur_id'])
  609. ->where('website_id',$data['website_id'])
  610. ->where('other_id', 0)
  611. ->get();
  612. $survey['choice'] = $choice_id;
  613. return Result::success($survey);
  614. }
  615. return Result::error("参数必填!");
  616. }
  617. return Result::error("此调查问卷不存在",0);
  618. }
  619. return Result::error("参数必填!");
  620. }
  621. /**
  622. * 后端-获取网站调查问卷列表
  623. * @param array $data
  624. * @return array
  625. */
  626. public function getSurveyList(array $data): array
  627. {
  628. $where = [];
  629. if(isset($data['survey_name']) &&!empty($data['survey_name'])){
  630. array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
  631. }
  632. if(isset($data['survey_type']) && $data['survey_type'] != null){
  633. array_push($where,['survey_type','=',$data['survey_type']]);
  634. }
  635. if(isset($data['is_survey']) && $data['is_survey'] != null){
  636. array_push($where,['is_survey','=',$data['is_survey']]);
  637. }
  638. // return Result::success($where);
  639. if (!empty($where)) {
  640. $query = Article::where($where)->where(function ($q) {
  641. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  642. });
  643. } else {
  644. $query = Article::where(function ($q) {
  645. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  646. });
  647. }
  648. $count = $query->count();
  649. $survey = $query->orderByDesc('id')
  650. ->limit($data['pageSize'])
  651. ->offset(($data['page']-1)*$data['pageSize'])
  652. ->get();
  653. if(empty($survey->toArray())){
  654. return Result::error("暂无调查问卷!",0);
  655. }
  656. $result = [
  657. 'rows'=>$survey,
  658. 'count'=>$count
  659. ];
  660. return Result::success($result);
  661. }
  662. /**
  663. * 后端-获取网站调查问卷详情
  664. * @param array $data
  665. * @return array
  666. */
  667. public function getSurveyInfo(array $data): array
  668. {
  669. if(isset($data['sur_id']) &&!empty($data['sur_id'])){
  670. $where = [ 'sur_id'=>$data['sur_id']];
  671. $choose = ArticleSurvey::where($where)->where('is_other',0)
  672. ->leftJoin('article','article_survey.art_id','article.id')
  673. ->select('article_survey.*','article.survey_type')
  674. ->get()->all();
  675. if(empty($choose)){
  676. return Result::error("此调查问卷不存在",0);
  677. }
  678. $resultsArray = array_column($choose, 'results');
  679. $total = array_sum($resultsArray);
  680. $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  681. $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
  682. // $total = 0;
  683. if(!empty($other)){
  684. $total = $total + $other['results'];
  685. $other['choice_name'] ='(其他)';
  686. if(!empty($others)){
  687. $other['hasChildren'] = true;
  688. // array_push($other,['hasChildren','=',true]);
  689. $other['children'] = $others;
  690. $other_choices = [$other->toArray()];
  691. $mer_choice = array_merge($choose,$other_choices);
  692. $value_choice = array_values($mer_choice);
  693. }
  694. else{
  695. // return Result::error('1111');
  696. $other_choices = [$other->toArray()];
  697. $other_choices = array_merge($choose,$other_choices);
  698. $value_choice = array_values($other_choices);
  699. // return Result::success($result);
  700. }
  701. }else{
  702. $value_choice = $choose;
  703. }
  704. $result = [
  705. 'choose'=>$value_choice,
  706. 'total'=>$total
  707. ];
  708. }
  709. return Result::success($result);
  710. }
  711. /**
  712. * 前端-搜索新闻列表
  713. * @param array $data
  714. * @return array
  715. */
  716. public function selectWebsiteArticle(array $data): array
  717. {
  718. $where = [];
  719. // 初始化查询构造器
  720. $category = WebsiteCategory::where('website_id',$data['website_id'])->pluck('category_id');
  721. $query = Article::where('status', 1)
  722. ->whereIn('catid', $category)
  723. ->where(function ($query) use ($data) {
  724. $query->where(function ($subQuery) use ($data) {
  725. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  726. })->orWhereNull("ignore_ids");
  727. });
  728. // return Result::success($all_articles);
  729. // 检查是否存在 cityid 参数
  730. if (isset($data['cityid']) && !empty($data['cityid'])) {
  731. $query->whereRaw("JSON_CONTAINS(city_arr_id, '".intval($data['cityid'])."')");
  732. }
  733. // 检查是否存在 department_id 参数
  734. if (isset($data['department_id']) && !empty($data['department_id'])) {
  735. $query->whereRaw("JSON_CONTAINS(department_arr_id, '".intval($data['department_id'])."')");
  736. }
  737. // 检查是否存在 keyword 参数
  738. if (isset($data['keyword']) && !empty($data['keyword'])) {
  739. $query->where('title', 'like', '%'.$data['keyword'].'%');
  740. }
  741. // 计算总数
  742. $count = $query->count();
  743. // 分页查询
  744. $articles = $query->orderBy("updated_at", "desc")
  745. ->limit($data['pageSize'])
  746. ->offset(($data['page'] - 1) * $data['pageSize'])
  747. ->get()->all();
  748. if (empty($articles)) {
  749. return Result::error("没有符合条件的资讯数据");
  750. }
  751. $data = [
  752. 'rows' => $articles,
  753. 'count' => $count
  754. ];
  755. return Result::success($data);
  756. }
  757. /**
  758. * 模块新闻加强版
  759. * @param array $data
  760. * @return array
  761. */
  762. public function getWebsiteCatidArticle(array $data): array
  763. {
  764. // return Result::success($data);
  765. $where = [
  766. // 'category.status' => 1,
  767. 'website_category.category_id' => $data['catid'],
  768. 'website_category.website_id' => $data['website_id'],
  769. // 'article.status' => 1,
  770. ];
  771. // $category = WebsiteCategory::where($where);
  772. if(isset($data['child_catnum']) && !empty($data['child_catnum'])){
  773. $child_catnum = $data['child_catnum']?? 1;
  774. $category['child'] = WebsiteCategory::where('pid',$data['catid'])->where('website_id',$data['website_id'])->select('category_id','alias')->limit($child_catnum)->get()->toArray();
  775. $childCategoryIds = array_column($category['child'], 'category_id');
  776. if(empty($childCategoryIds)){
  777. return Result::error("暂无子栏目",0);
  778. }
  779. $imgArticles = [];
  780. $textArticles = [];
  781. // return Result::success($childCategoryIds);
  782. if(isset($data['child_imgnum']) && !empty($data['child_imgnum']) && $data['child_imgnum']!=0){
  783. // 初始化子分类图片新闻和文字新闻数组
  784. // 查询所有子级栏目的图文新闻
  785. $imgArticles = Article::where('catid', $childCategoryIds[0])
  786. ->where('status', 1)
  787. ->where('imgurl', '!=', '')
  788. ->select('*')
  789. ->orderBy('updated_at', 'desc')
  790. ->limit($data['child_imgnum'])
  791. ->get();
  792. }
  793. if( isset($data['child_textnum']) && !empty($data['child_textnum']) && $data['child_textnum']!=0){
  794. // 查询所有子级栏目的文字新闻
  795. $textArticles = Article::where('catid', $childCategoryIds[0])
  796. ->where('status', 1)
  797. // ->where(function ($query) {
  798. // $query->whereNull('imgurl')
  799. // ->orWhere('imgurl', '');
  800. // })
  801. ->select('*')
  802. ->orderBy('updated_at', 'desc')
  803. ->limit($data['child_textnum'])
  804. ->get();
  805. }
  806. // 遍历子分类,将图文新闻和文字新闻分别添加到子分类中
  807. $category['child'] = array_map(function ($child) use ($imgArticles, $textArticles) {
  808. $child['img'] = $imgArticles? $imgArticles->where('catid', $child['category_id']) : [];
  809. $child['text'] = $textArticles? $textArticles->where('catid', $child['category_id']) : [];
  810. return $child;
  811. }, $category['child']);
  812. }
  813. // }
  814. if (isset($data['img_num']) && !empty($data['img_num'])) {
  815. $category['img'] = WebsiteCategory::where($where)
  816. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  817. ->where('article.status', 1)
  818. ->where('article.imgurl', '!=', '')
  819. ->select('article.*','website_category.category_id','website_category.alias')
  820. ->orderBy('article.updated_at', 'desc')
  821. ->limit($data['img_num'])
  822. ->get();
  823. }
  824. if (isset($data['text_num']) && !empty($data['text_num'])) {
  825. $category['text'] = WebsiteCategory::where($where)
  826. ->leftJoin('article', 'article.catid', 'website_category.category_id')
  827. ->where('article.status', 1)
  828. // ->where(function ($query) {
  829. // $query->whereNull('article.imgurl')
  830. // ->orWhere('article.imgurl', '');
  831. // })
  832. ->select('article.*','website_category.category_id','website_category.alias')
  833. ->orderBy('article.updated_at', 'desc')
  834. ->limit($data['text_num'])
  835. ->get();
  836. }
  837. // $category = $category->get();
  838. if(empty($category)){
  839. return Result::error("查询失败", 0);
  840. }
  841. return Result::success($category);
  842. }
  843. /**
  844. * 模块新闻加强plus版
  845. * @param array $data
  846. * @return array
  847. */
  848. public function getWebsiteAllArticle(array $data): array
  849. {
  850. // 修正传入的字符串,将单引号替换为双引号
  851. $input['id'] = $data['id'];
  852. $input['website_id'] = $data['website_id'];
  853. // 将 JSON 字符串转换为 PHP 数组
  854. $data = json_decode($input['id'], true);
  855. // 使用 array_map 处理每个元素
  856. $result = array_map(function ($item) use ($input) {
  857. list($parentCatId, $parentImgNum, $parentTextNum) = explode(',', $item['parent']);
  858. $website = [
  859. 'website_id' => $input['website_id'],
  860. ];
  861. // 查询栏目名称
  862. $category = WebsiteCategory::where('category_id', $parentCatId)->where($website)->first(['alias', 'category_id']);
  863. if(empty($category)){
  864. $imgArticles = [];
  865. $textArticles = [];
  866. // return Result::error("暂无此栏目",0);
  867. }else{
  868. // 查询图片新闻
  869. $imgArticles = Article::where('catid', $parentCatId)
  870. ->where('imgurl', '!=', '')
  871. // ->where($website)
  872. ->limit($parentImgNum)
  873. ->get()->all();
  874. // 查询文字新闻
  875. $textArticles = Article::where('catid', $parentCatId)
  876. // ->where($website)
  877. ->limit($parentTextNum)
  878. ->get()->all();
  879. }
  880. $resultItem = [
  881. 'alias' => $category ? $category->alias : null,
  882. 'category_id' => $parentCatId,
  883. 'imgnum' => $imgArticles,
  884. 'textnum' => $textArticles
  885. ];
  886. if (!empty($item['child']) && $item['child'] != "") {
  887. // 查询第一个pid等于parent中第一个参数的category来获取child的category_id
  888. $childCategory = WebsiteCategory::where('pid', $parentCatId)->where($website)->first();
  889. if ($childCategory) {
  890. list($childCatId, $childImgNum, $childTextNum) = explode(',', $item['child']);
  891. // 查询子栏目名称
  892. $childCategoryInfo = WebsiteCategory::where('category_id', $childCatId)->where($website)->first(['alias', 'category_id']);
  893. if(empty($childCategoryInfo)){
  894. $childImgArticles = [];
  895. $childTextArticles = [];
  896. }else{
  897. // 查询子栏目图片新闻
  898. $childImgArticles = Article::where('catid', $childCatId)
  899. ->where('imgurl', '!=', '')
  900. // ->where($website)
  901. ->limit($childImgNum)
  902. ->get()->all();
  903. // 查询子栏目文字新闻
  904. $childTextArticles = Article::where('catid', $childCatId)
  905. // ->where($website)
  906. ->limit($childTextNum)
  907. ->get()->all();
  908. }
  909. $resultItem['child'] = [
  910. 'alias' => $childCategoryInfo ? $childCategoryInfo->alias : null,
  911. 'category_id' => $childCatId,
  912. 'imgnum' => $childImgArticles,
  913. 'textnum' => $childTextArticles
  914. ];
  915. }
  916. }
  917. return $resultItem;
  918. }, $data);
  919. return Result::success($result);
  920. // return Result::success($data);
  921. }
  922. }