NewsService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. $city_id='';
  30. if(isset($data['city_id']) && $data['city_id']){
  31. $city_id = intval($data['city_id']);
  32. }
  33. $rep = Category::where($where)
  34. ->when($city_id, function ($query) use ($city_id) {
  35. if(isset($city_id) && $city_id) {
  36. $query->whereJsonContains("category.city_arr_id", $city_id);
  37. }
  38. })
  39. ->leftJoin('district','category.city_id','district.id')
  40. ->leftJoin('department','category.department_id','department.id')
  41. ->select("category.*","district.name as city_name","department.name as department_name")
  42. ->limit($data['pageSize'])->orderByDesc('category.updated_at')->offset(($data['page']-1)*$data['pageSize'])->get();
  43. $count = Category::where($where)->when($city_id, function ($query) use ($city_id) {
  44. if(isset($city_id) && $city_id) {
  45. $query->whereJsonContains("category.city_arr_id", $city_id);
  46. }
  47. })->count();
  48. $data = [
  49. 'rows'=>$rep->toArray(),
  50. 'count'=>$count
  51. ];
  52. if(empty($rep->toArray())){
  53. return Result::error("没有导航池数据");
  54. }
  55. return Result::success($data);
  56. }
  57. /**
  58. * @param array $data
  59. * @return array
  60. */
  61. public function categoryList(array $data): array
  62. {
  63. $where[] = [
  64. 'pid','=',$data['pid']
  65. ];
  66. if(isset($data['name'])){
  67. array_push($where, ['category.name','like','%'.$data['name'].'%']);
  68. }
  69. var_dump($where);
  70. $result = Category::where($where)->select('category.*','category.id as category_id')->get();
  71. if(empty($result)){
  72. return Result::error("没有栏目数据");
  73. }
  74. return Result::success($result);
  75. }
  76. /**
  77. * @param array $data
  78. * @return array
  79. */
  80. public function addCategory(array $data): array
  81. {
  82. $id = Category::insertGetId($data);
  83. if(empty($id)){
  84. return Result::error("添加失败");
  85. }
  86. return Result::success(['id'=>$id]);
  87. }
  88. /**
  89. * @param array $data
  90. * @return array
  91. */
  92. public function delCategory(array $data): array
  93. {
  94. $categoryList = Category::where(['pid'=>$data['id']])->get();
  95. var_dump("分类列表:",$data,$categoryList);
  96. if($categoryList->toArray()){
  97. return Result::error("分类下面有子分类不能删除");
  98. }
  99. $articleList = Article::where(['catid'=>$data['id']])->get();
  100. var_dump("文章列表:",$articleList);
  101. if($articleList->toArray()){
  102. return Result::error("分类下面有资讯不能删除");
  103. }
  104. $result = Category::where($data)->delete();
  105. if(!$result){
  106. return Result::error("删除失败");
  107. }
  108. return Result::success($result);
  109. }
  110. /**
  111. * @param array $data
  112. * @return array
  113. */
  114. public function updateCategory(array $data): array
  115. {
  116. $where = [
  117. 'id'=>$data['id']
  118. ];
  119. $result = Category::where($where)->update($data);
  120. if($result){
  121. return Result::success($result);
  122. }else{
  123. return Result::error("更新失败");
  124. }
  125. }
  126. /**
  127. * 获取导航池信息
  128. * @param array $data
  129. * @return array
  130. */
  131. public function getCategoryInfo(array $data): array
  132. {
  133. $where = [
  134. 'id'=>$data['id']
  135. ];
  136. $result = Category::where($where)->first();
  137. if($result){
  138. return Result::success($result);
  139. }else{
  140. return Result::error("更新失败");
  141. }
  142. }
  143. /**
  144. * @param array $data
  145. * @return array
  146. */
  147. public function getArticleList(array $data): array
  148. {
  149. $where= [];
  150. if(isset($data['title']) && $data['title']){
  151. array_push($where,['article.title','like','%'.$data['title'].'%']);
  152. }
  153. if(isset($data['category_name']) && $data['category_name']){
  154. array_push($where,['category.name','like','%'.$data['category_name'].'%']);
  155. }
  156. if(isset($data['author']) && $data['author']){
  157. array_push($where,['article.author','=',$data['author']]);
  158. }
  159. if(isset($data['islink']) && $data['islink']!==""){
  160. array_push($where,['article.islink','=',$data['islink']]);
  161. }
  162. if(isset($data['status']) && $data['status']!==""){
  163. array_push($where,['article.status','=',$data['status']]);
  164. }
  165. $rep = Article::where($where)
  166. ->whereNotIn('article.status',[404])
  167. ->leftJoin('category','article.catid','category.id')
  168. ->select("article.*","category.name as category_name")
  169. ->orderBy("article.id","desc")
  170. ->limit($data['pageSize'])
  171. ->offset(($data['page']-1)*$data['pageSize'])->get();
  172. $count = Article::where($where)->whereNotIn('article.status',[404])
  173. ->leftJoin('category','article.catid','category.id')->count();
  174. $data = [
  175. 'rows'=>$rep->toArray(),
  176. 'count'=>$count
  177. ];
  178. if(empty($rep)){
  179. return Result::error("没有信息数据");
  180. }
  181. return Result::success($data);
  182. }
  183. /**
  184. * @param array $data
  185. * @return array
  186. */
  187. public function addArticle(array $data): array
  188. {
  189. Db::beginTransaction();
  190. try{
  191. $articleData = $data;
  192. unset($articleData['content']);
  193. $id = Article::insertGetId($articleData);
  194. $articleDataContent = [
  195. 'article_id'=>$id,
  196. 'content'=>$data['content']
  197. ];
  198. ArticleData::insertGetId($articleDataContent);
  199. Db::commit();
  200. } catch(\Throwable $ex){
  201. Db::rollBack();
  202. var_dump($ex->getMessage());
  203. return Result::error("创建失败",0);
  204. }
  205. return Result::success(['id'=>$id]);
  206. }
  207. /**
  208. * @param array $data
  209. * @return array
  210. */
  211. public function delArticle(array $data): array
  212. {
  213. $result = Article::where($data)->update(['status'=>404]);
  214. if(!$result){
  215. return Result::error("删除失败");
  216. }
  217. return Result::success($result);
  218. }
  219. /**
  220. * @param array $data
  221. * @return array
  222. */
  223. public function updateArticle(array $data): array
  224. {
  225. Db::beginTransaction();
  226. try{
  227. $data['cat_arr_id'] = isset($data['cat_arr_id'])?json_encode($data['cat_arr_id']):'';
  228. $data['tag'] = isset($data['tag'])?json_encode($data['tag']):'';
  229. $articleData = $data;
  230. unset($articleData['content']);
  231. unset($articleData['status_name']);
  232. unset($articleData['name']);
  233. unset($articleData['content']);
  234. unset($articleData['pid_arr']);
  235. unset($articleData['pid']);
  236. $id = Article::where(['id'=>$data['id']])->update($articleData);
  237. $articleDataContent = [
  238. 'content'=>$data['content']
  239. ];
  240. ArticleData::where(['article_id'=>$data['id']])->update($articleDataContent);
  241. } catch(\Throwable $ex){
  242. Db::rollBack();
  243. var_dump($ex->getMessage());
  244. return Result::error("更新失败",0);
  245. }
  246. return Result::success([]);
  247. }
  248. /**
  249. * 更新资讯状态
  250. * @param array $data
  251. * @return array
  252. */
  253. public function upArticleStatus(array $data):array
  254. {
  255. $result = Article::where(['id'=>$data['id']])->update($data);
  256. if($result){
  257. return Result::success();
  258. }else{
  259. return Result::error("更新状态失败",0);
  260. }
  261. }
  262. /**
  263. * 获取新闻详情
  264. * @param array $data
  265. * @return array
  266. */
  267. public function getArticleInfo(array $data): array
  268. {
  269. $where = [
  270. 'article.id'=>$data['id'],
  271. // 'article.status'=>1
  272. ];
  273. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")->first();
  274. if(empty($result)){
  275. return Result::error("查询失败",0);
  276. }
  277. return Result::success($result);
  278. }
  279. /**
  280. * 获取头条新闻
  281. * @param array $data
  282. * @return array
  283. */
  284. public function getWebsiteArticlett(array $data): array
  285. {
  286. $category = WebsiteCategory::where('website_id',$data['website_id'])->pluck('category_id');
  287. $result= [];
  288. if($category){
  289. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  290. $where = [
  291. 'status' => 1,
  292. 'level' => $data['level'],
  293. ];
  294. $result = Article::where($where)
  295. ->whereIn("catid", $category)
  296. ->where(function ($query) use ($data) {
  297. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  298. ->orWhereNull("ignore_ids");
  299. })
  300. ->orderBy("updated_at", "desc")
  301. ->offset($placeid)
  302. ->limit($data['pageSize'])
  303. ->get();
  304. if(empty($result)){
  305. return Result::error("暂无头条新闻",0);
  306. }
  307. return Result::success($result);
  308. }else{
  309. return Result::error("本网站下暂无相关栏目",0);
  310. }
  311. }
  312. /**
  313. * 获取模块新闻
  314. * @param array $data
  315. * @return array
  316. */
  317. public function getWebsiteModelArticles(array $data): array
  318. {
  319. $catid=$data['catid'];
  320. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$catid)->select('category_id')->get();
  321. $category = $category->toArray();
  322. if(!empty($category)){
  323. $where=[
  324. 'status' => 1,
  325. 'catid' => $catid
  326. ];
  327. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  328. // 级别:0:未分类 1:头条 2:轮播图 3:推荐图 4:热点资讯 5:资讯推荐
  329. if ($data['level'] == 1) {
  330. $level = [
  331. 0 => '1',
  332. 1 => '4',
  333. 2 => '5',
  334. 3 => '0',
  335. ];
  336. $result = Article::where($where)
  337. ->whereIn('level',$level)
  338. ->where(function ($query) use ($data) {
  339. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  340. ->orWhereNull("ignore_ids");
  341. })
  342. ->orderBy("updated_at","desc")
  343. ->offset($placeid)
  344. ->limit($data['pagesize'])
  345. ->get();
  346. }elseif($data['level']==2){
  347. $level='2';
  348. $result = Article::where($where)
  349. ->where('level',$level)
  350. ->where(function ($query) use ($data) {
  351. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  352. ->orWhereNull("ignore_ids");
  353. })
  354. ->orderBy("updated_at","desc")
  355. ->offset($placeid)
  356. ->limit($data['pagesize'])
  357. ->get();
  358. }else{
  359. $level='3';
  360. $result = Article::where($where)
  361. ->where('level',$level)
  362. ->where(function ($query) use ($data) {
  363. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  364. ->orWhereNull("ignore_ids");
  365. })
  366. ->orderBy("updated_at","desc")
  367. ->offset($placeid)
  368. ->limit($data['pagesize'])
  369. ->get();
  370. }
  371. if(empty($result)){
  372. return Result::error("此栏目暂无相关新闻",0);
  373. }
  374. }else{
  375. return Result::error("此网站暂无此栏目",0);
  376. }
  377. return Result::success($result);
  378. }
  379. /**
  380. *获取新闻列表
  381. * @param array $data
  382. * @return array
  383. */
  384. public function getWebsiteArticleList(array $data): array
  385. {
  386. $where[] = ['status', '=', 1];
  387. if(isset($data['keyword']) && !empty($data['keyword'])){
  388. array_push($where,['article.title','like','%'.$data['keyword'].'%']);
  389. }
  390. if(isset($data['catid']) && !empty($data['catid'])){
  391. if(is_array($data['catid'])){
  392. $category = WebsiteCategory::where('website_id',$data['website_id'])->whereIn('category_id',$data['catid'])->pluck('category_id');
  393. array_push($where,['catid', 'in', $data['catid']]);
  394. }else{
  395. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->pluck('category_id');
  396. array_push($where,['catid', '=', $data['catid']]);
  397. }
  398. if(empty($category)){
  399. return Result::error("此网站暂无此栏目",0);
  400. }
  401. }
  402. // return Result::success($where);
  403. $rep = Article::where(function ($query) use ($where) {
  404. foreach ($where as $condition) {
  405. if ($condition[1] === 'in') {
  406. $query->whereIn($condition[0], $condition[2]);
  407. } else {
  408. $query->where($condition[0], $condition[1], $condition[2]);
  409. }
  410. }
  411. })
  412. ->where(function ($query) use ($data) {
  413. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  414. ->orWhereNull("ignore_ids");
  415. })
  416. ->orderBy("updated_at", "desc")
  417. ->limit($data['pageSize'])
  418. ->offset(($data['page'] - 1) * $data['pageSize'])
  419. ->get();
  420. $count = Article::where(function ($query) use ($where) {
  421. foreach ($where as $condition) {
  422. if ($condition[1] === 'in') {
  423. $query->whereIn($condition[0], $condition[2]);
  424. } else {
  425. $query->where($condition[0], $condition[1], $condition[2]);
  426. }
  427. }
  428. })->count();
  429. $data = [
  430. 'rows'=>$rep->toArray(),
  431. 'count'=>$count
  432. ];
  433. if(empty($rep)){
  434. return Result::error("没有信息数据");
  435. }
  436. return Result::success($data);
  437. }
  438. /**
  439. * 前端-获取新闻详情
  440. * @param array $data
  441. * @return array
  442. */
  443. public function selectWebsiteArticleInfo(array $data): array
  444. {
  445. $where = [
  446. 'article.id'=>$data['id'],
  447. 'article.status'=>1
  448. ];
  449. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")
  450. ->where(function ($query) use ($data) {
  451. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  452. ->orWhereNull("ignore_ids");
  453. })
  454. ->first();
  455. if(empty($result)){
  456. return Result::error("暂无此新闻!",0);
  457. }
  458. $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
  459. if(empty($category)){
  460. return Result::error("查询失败",0);
  461. }
  462. $result['category_id'] = $category['category_id'];
  463. $result['cat_name'] = $category['alias'];
  464. return Result::success($result);
  465. }
  466. /**
  467. * 前端-获取网站调查问卷
  468. * @param array $data
  469. * @return array
  470. */
  471. public function getWebsiteSurvey(array $data): array
  472. {
  473. if(isset($data['website_id']) && !empty($data['website_id'])){
  474. $website = Website::where('id',$data['website_id'])->first();
  475. if(empty($website)){
  476. return Result::error("暂无此网站",0);
  477. }
  478. }
  479. if(isset($data['art_id']) && !empty($data['art_id'])){
  480. $article = Article::where('id',$data['art_id'])->where('status',1)->first();
  481. if(empty($article)){
  482. return Result::error("暂无此文章",0);
  483. }
  484. // return Result::error($data,0);
  485. $where['art_id'] = $data['art_id'];
  486. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  487. }else{
  488. $survey = ArticleSurvey::where('website_id',$data['website_id'])->orderBy('created_at')->first();
  489. if(empty($survey)){
  490. return Result::error("暂无调查问卷",0);
  491. }
  492. $where['sur_id'] = $survey['sur_id'];
  493. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  494. }
  495. $result['survey'] = ArticleSurvey::where($where)->where('is_other',0)
  496. ->leftJoin('article','article_survey.art_id','article.id')
  497. ->select('article_survey.*','article.survey_type')
  498. ->get()->all();
  499. $result['other'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  500. $result['others'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->first();
  501. if(empty($result)){
  502. return Result::error("此文章暂无调查问卷",0);
  503. }
  504. return Result::success($result);
  505. }
  506. /**
  507. * 前端-添加网站调查问卷选项
  508. * @param array $data
  509. * @return array
  510. */
  511. public function addWebsiteSurveyOption(array $data): array
  512. {
  513. if(isset($data['website_id']) && !empty($data['website_id'])){
  514. $website = Website::where('id',$data['website_id'])->first();
  515. if(empty($website)){
  516. return Result::error("暂无此网站",0);
  517. }
  518. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  519. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  520. if(empty($survey)){
  521. return Result::error("此调查问卷不可添加选项",0);
  522. }
  523. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  524. $choice = [
  525. 'art_id'=>$survey['art_id'],
  526. 'website_id'=>$data['website_id'],
  527. 'survey_name'=>$survey['survey_name'],
  528. 'choice_name'=>$data['choice_name'],
  529. 'sur_id'=>$survey['sur_id'],
  530. 'is_other'=>1,
  531. 'other_id'=>$survey['id'],
  532. ];
  533. $result = ArticleSurvey::insertGetId($choice);
  534. if(empty($result)){
  535. return Result::error("添加失败",0);
  536. }
  537. return Result::success($result);
  538. }
  539. }
  540. return Result::error("添加失败",0);
  541. }
  542. return Result::error("添加失败",0);
  543. }
  544. /**
  545. * 前端-调查问卷投票
  546. * @param array $data
  547. * @return array
  548. */
  549. public function addWebsiteSurveyVote(array $data): array
  550. {
  551. if(isset($data['website_id']) && !empty($data['website_id'])){
  552. $website = Website::where('id',$data['website_id'])->first();
  553. if(empty($website)){
  554. return Result::error("暂无此网站",0);
  555. }
  556. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  557. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->pluck('sur_id');
  558. if(empty($survey)){
  559. return Result::error("此调查问卷不存在",0);
  560. }
  561. // return Result::success($survey);
  562. // 调查问卷类型 0:单选 1:多选
  563. $type = Article::where('survey_id',$data['sur_id'])->pluck('survey_type');
  564. // return Result::success($type);
  565. if(empty($type) || ($type[0]!= 1 && $type[0]!= 0)){
  566. return Result::error("此调查问卷不可投票",0);
  567. }
  568. // return Result::success($type[0]);
  569. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  570. if($type[0] == 0){
  571. if(is_array($data['choice_id'])){
  572. return Result::error("请选择一个选项!",0);
  573. }
  574. $data['choice_id'] = [$data['choice_id']];
  575. }else{
  576. if(!is_array($data['choice_id'])){
  577. return Result::error("请传递数组!",0);
  578. }
  579. }
  580. // return Result::success($data['choice_id']);
  581. $other = ArticleSurvey::whereIn('id',$data['choice_id'])
  582. ->where('website_id',$data['website_id'])
  583. ->where('is_other',1)
  584. ->where('other_id',0)
  585. ->first();
  586. if(!empty($other)){
  587. return Result::error("请选择已有的选项!",0);
  588. }
  589. $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
  590. ->where('website_id',$data['website_id'])
  591. ->where('is_other',1)
  592. ->where('other_id','!=',0)
  593. ->first();
  594. // return Result::success($data);
  595. if(!empty($choice['other'])){
  596. array_push($data['choice_id'],$choice['other']['other_id']);
  597. // return Result::success($data['choice_id']);
  598. }
  599. // return Result::success($data);
  600. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
  601. ->where('website_id',$data['website_id'])
  602. ->increment('results', 1);
  603. if(empty($choice)){
  604. return Result::error("请选择已有的选项!",0);
  605. }
  606. return Result::success($choice);
  607. }
  608. return Result::error("参数必填!");
  609. // if(isset($data['choice_id']) && !empty($data['choice_id'])){
  610. // $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  611. // }
  612. }
  613. return Result::error("此调查问卷不存在",0);
  614. }
  615. return Result::error("参数必填!");
  616. }
  617. /**
  618. * 后端-获取网站调查问卷列表
  619. * @param array $data
  620. * @return array
  621. */
  622. public function getSurveyList(array $data): array
  623. {
  624. $where = [];
  625. if(isset($data['survey_name']) &&!empty($data['survey_name'])){
  626. array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
  627. }
  628. if(isset($data['survey_type']) && $data['survey_type'] != null){
  629. array_push($where,['survey_type','=',$data['survey_type']]);
  630. }
  631. if(isset($data['is_survey']) && $data['is_survey'] != null){
  632. array_push($where,['is_survey','=',$data['is_survey']]);
  633. }
  634. // return Result::success($where);
  635. if(!empty($where)){
  636. $query = Article::where($where)->whereNotNull('survey_name');
  637. }else{
  638. $query = Article::whereNotNull('survey_name');
  639. }
  640. $count = $query->count();
  641. $survey = $query->orderByDesc('id')
  642. ->limit($data['pageSize'])
  643. ->offset(($data['page']-1)*$data['pageSize'])
  644. ->get();
  645. if(empty($survey->toArray())){
  646. return Result::error("暂无调查问卷!",0);
  647. }
  648. $result = [
  649. 'rows'=>$survey,
  650. 'count'=>$count
  651. ];
  652. return Result::success($result);
  653. }
  654. /**
  655. * 后端-获取网站调查问卷详情
  656. * @param array $data
  657. * @return array
  658. */
  659. public function getSurveyInfo(array $data): array
  660. {
  661. if(isset($data['sur_id']) &&!empty($data['sur_id'])){
  662. $where = [ 'sur_id'=>$data['sur_id']];
  663. $choose = ArticleSurvey::where($where)->where('is_other',0)
  664. ->leftJoin('article','article_survey.art_id','article.id')
  665. ->select('article_survey.*','article.survey_type')
  666. ->get()->all();
  667. if(empty($choose)){
  668. return Result::error("此调查问卷不存在",0);
  669. }
  670. $resultsArray = array_column($choose, 'results');
  671. $total = array_sum($resultsArray);
  672. $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  673. $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
  674. // $total = 0;
  675. if(!empty($other)){
  676. $total = $total + $other['results'];
  677. $other['choice_name'] = $other['choice_name'].'(其他)';
  678. if(!empty($others)){
  679. $other['hasChildren'] = true;
  680. // array_push($other,['hasChildren','=',true]);
  681. $other['children'] = $others;
  682. $other_choices = [$other->toArray()];
  683. $mer_choice = array_merge($choose,$other_choices);
  684. $value_choice = array_values($mer_choice);
  685. }
  686. else{
  687. // return Result::error('1111');
  688. $other_choices = [$other->toArray()];
  689. $other_choices = array_merge($choose,$other_choices);
  690. $value_choice = array_values($other_choices);
  691. // return Result::success($result);
  692. }
  693. }else{
  694. $value_choice = $choose;
  695. }
  696. $result = [
  697. 'choose'=>$value_choice,
  698. 'total'=>$total
  699. ];
  700. }
  701. return Result::success($result);
  702. }
  703. /**
  704. * 前端-搜索新闻列表
  705. * @param array $data
  706. * @return array
  707. */
  708. public function selectWebsiteArticle(array $data): array
  709. {
  710. $where = [];
  711. // 初始化查询构造器
  712. $category = WebsiteCategory::where('website_id',$data['website_id'])->pluck('category_id');
  713. $query = Article::where('status', 1)
  714. ->whereIn('catid', $category)
  715. ->where(function ($query) use ($data) {
  716. $query->where(function ($subQuery) use ($data) {
  717. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  718. })->orWhereNull("ignore_ids");
  719. });
  720. // return Result::success($all_articles);
  721. // 检查是否存在 cityid 参数
  722. if (isset($data['cityid']) && !empty($data['cityid'])) {
  723. $query->whereRaw("JSON_CONTAINS(city_arr_id, '".intval($data['cityid'])."')");
  724. }
  725. // 检查是否存在 department_id 参数
  726. if (isset($data['department_id']) && !empty($data['department_id'])) {
  727. $query->whereRaw("JSON_CONTAINS(department_arr_id, '".intval($data['department_id'])."')");
  728. }
  729. // 检查是否存在 keyword 参数
  730. if (isset($data['keyword']) && !empty($data['keyword'])) {
  731. $query->where('title', 'like', '%'.$data['keyword'].'%');
  732. }
  733. // 计算总数
  734. $count = $query->count();
  735. // 分页查询
  736. $articles = $query->orderBy("updated_at", "desc")
  737. ->limit($data['pageSize'])
  738. ->offset(($data['page'] - 1) * $data['pageSize'])
  739. ->get()->all();
  740. if (empty($articles)) {
  741. return Result::error("没有符合条件的资讯数据");
  742. }
  743. $data = [
  744. 'rows' => $articles,
  745. 'count' => $count
  746. ];
  747. return Result::success($data);
  748. }
  749. }