NewsService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. var_dump($where, 'where-----------------');
  286. $result = Article::where($where)
  287. ->whereIn("catid", $category)
  288. ->where(function ($query) use ($data) {
  289. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  290. ->orWhereNull("ignore_ids");
  291. })
  292. //$data['level'] == 4 || $data['level'] == 5 查询随机
  293. ->when($data['level'] == 5, function ($query) {
  294. $query->inRandomOrder()
  295. //updated_at最近三十天;
  296. ->where('updated_at', '>', date("Y-m-d H:i:s", strtotime("-30 day")));
  297. })
  298. ->when($data['level'] != 5, function ($query) {
  299. $query->orderBy("updated_at", "desc");
  300. })
  301. ->when(!empty($data['level']), function ($query) use ($data) {
  302. if ($data['level'] != 4 && $data['level'] != 5) {
  303. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  304. ->orWhereNull("level");
  305. }
  306. })
  307. ->offset($placeid)
  308. ->limit($data['pageSize'])
  309. ->get();
  310. if (empty($result)) {
  311. return Result::error("暂无头条新闻", 0);
  312. }
  313. return Result::success($result);
  314. } else {
  315. return Result::error("本网站下暂无相关栏目", 0);
  316. }
  317. }
  318. /**
  319. * 获取模块新闻
  320. * @param array $data
  321. * @return array
  322. */
  323. public function getWebsiteModelArticles(array $data): array
  324. {
  325. $catid = $data['catid'];
  326. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  327. $category = $category->toArray();
  328. if (!empty($category)) {
  329. $where = [
  330. 'status' => 1,
  331. 'catid' => $catid,
  332. ];
  333. $placeid = isset($data['placeid']) && !empty($data['placeid']) ? $data['placeid'] - 1 : 0;
  334. // 级别:0:未分类 1:头条 2:轮播图 3:推荐图 4:热点资讯 5:资讯推荐
  335. if ($data['level'] == 1) {
  336. $result = Article::where($where)
  337. ->where(function ($query) use ($data) {
  338. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  339. ->orWhereNull("level")
  340. ->orWhereRaw("level = '[]'");
  341. })
  342. ->where(function ($query) use ($data) {
  343. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  344. ->orWhereNull("ignore_ids");
  345. })
  346. ->orderBy("updated_at", "desc")
  347. ->offset($placeid)
  348. ->limit($data['pagesize'])
  349. ->get();
  350. } elseif ($data['level'] == 2) {
  351. $level = '2';
  352. $result = Article::where($where)
  353. ->where(function ($query) use ($data) {
  354. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  355. ->orWhereNull("level")
  356. ->orWhereRaw("level = '[]'");
  357. })
  358. ->where(function ($query) use ($data) {
  359. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  360. ->orWhereNull("ignore_ids");
  361. })
  362. ->orderBy("updated_at", "desc")
  363. ->offset($placeid)
  364. ->limit($data['pagesize'])
  365. ->get();
  366. } else {
  367. $level = '3';
  368. $result = Article::where($where)
  369. ->where(function ($query) use ($data) {
  370. $query->whereRaw("JSON_CONTAINS(level, '" . intval($data['level']) . "') = 1")
  371. ->orWhereNull("level")
  372. ->orWhereRaw("level = '[]'");
  373. })
  374. ->where(function ($query) use ($data) {
  375. $query->whereRaw("JSON_CONTAINS(ignore_ids, '" . intval($data['website_id']) . "') = 0")
  376. ->orWhereNull("ignore_ids");
  377. })
  378. ->orderBy("updated_at", "desc")
  379. ->offset($placeid)
  380. ->limit($data['pagesize'])
  381. ->get();
  382. }
  383. if (empty($result)) {
  384. return Result::error("此栏目暂无相关新闻", 0);
  385. }
  386. } else {
  387. return Result::error("此网站暂无此栏目", 0);
  388. }
  389. return Result::success($result);
  390. }
  391. /**
  392. *获取新闻列表
  393. * @param array $data
  394. * @return array
  395. */
  396. public function getWebsiteArticleList(array $data): array
  397. {
  398. $where[] = ['status', '=', 1];
  399. if(isset($data['keyword']) && !empty($data['keyword'])){
  400. array_push($where,['article.title','like','%'.$data['keyword'].'%']);
  401. }
  402. if(isset($data['catid']) && !empty($data['catid'])){
  403. if(is_array($data['catid'])){
  404. $category = WebsiteCategory::where('website_id',$data['website_id'])->whereIn('category_id',$data['catid'])->pluck('category_id');
  405. array_push($where,['catid', 'in', $data['catid']]);
  406. }else{
  407. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->pluck('category_id');
  408. array_push($where,['catid', '=', $data['catid']]);
  409. }
  410. if(empty($category)){
  411. return Result::error("此网站暂无此栏目",0);
  412. }
  413. }
  414. // return Result::success($where);
  415. $rep = Article::where(function ($query) use ($where) {
  416. foreach ($where as $condition) {
  417. if ($condition[1] === 'in') {
  418. $query->whereIn($condition[0], $condition[2]);
  419. } else {
  420. $query->where($condition[0], $condition[1], $condition[2]);
  421. }
  422. }
  423. })
  424. ->where(function ($query) use ($data) {
  425. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  426. ->orWhereNull("ignore_ids");
  427. })
  428. ->orderBy("updated_at", "desc")
  429. ->limit($data['pageSize'])
  430. ->offset(($data['page'] - 1) * $data['pageSize'])
  431. ->get();
  432. $count = Article::where(function ($query) use ($where) {
  433. foreach ($where as $condition) {
  434. if ($condition[1] === 'in') {
  435. $query->whereIn($condition[0], $condition[2]);
  436. } else {
  437. $query->where($condition[0], $condition[1], $condition[2]);
  438. }
  439. }
  440. })->count();
  441. $data = [
  442. 'rows'=>$rep->toArray(),
  443. 'count'=>$count
  444. ];
  445. if(empty($rep)){
  446. return Result::error("没有信息数据");
  447. }
  448. return Result::success($data);
  449. }
  450. /**
  451. * 前端-获取新闻详情
  452. * @param array $data
  453. * @return array
  454. */
  455. public function selectWebsiteArticleInfo(array $data): array
  456. {
  457. $where = [
  458. 'article.id'=>$data['id'],
  459. 'article.status'=>1
  460. ];
  461. $result = Article::where($where)->leftJoin("article_data","article.id","article_data.article_id")
  462. ->where(function ($query) use ($data) {
  463. $query->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0")
  464. ->orWhereNull("ignore_ids");
  465. })
  466. ->first();
  467. if(empty($result)){
  468. return Result::error("暂无此新闻!",0);
  469. }
  470. $category = WebsiteCategory::where('website_id',$data['website_id'])->where(['category_id'=>$result['catid']])->first();
  471. if(empty($category)){
  472. return Result::error("查询失败",0);
  473. }
  474. $result['category_id'] = $category['category_id'];
  475. $result['cat_name'] = $category['alias'];
  476. return Result::success($result);
  477. }
  478. /**
  479. * 前端-获取网站调查问卷
  480. * @param array $data
  481. * @return array
  482. */
  483. public function getWebsiteSurvey(array $data): array
  484. {
  485. if(isset($data['website_id']) && !empty($data['website_id'])){
  486. $website = Website::where('id',$data['website_id'])->first();
  487. if(empty($website)){
  488. return Result::error("暂无此网站",0);
  489. }
  490. }
  491. if(isset($data['art_id']) && !empty($data['art_id'])){
  492. $article = Article::where('id',$data['art_id'])->where('status',1)->first();
  493. if(empty($article)){
  494. return Result::error("暂无此文章",0);
  495. }
  496. // return Result::error($data,0);
  497. $where['art_id'] = $data['art_id'];
  498. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  499. }else{
  500. $survey = ArticleSurvey::where('website_id',$data['website_id'])->orderBy('created_at')->first();
  501. if(empty($survey)){
  502. return Result::error("暂无调查问卷",0);
  503. }
  504. $where['sur_id'] = $survey['sur_id'];
  505. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  506. }
  507. $result = ArticleSurvey::where($where)
  508. ->where(function ($query) {
  509. $query->where('is_other', 0)
  510. ->orWhere(function ($subQuery) {
  511. $subQuery->where('is_other', 1)
  512. ->where('other_id', 0);
  513. });
  514. })
  515. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  516. ->select('article_survey.*', 'article.survey_type')
  517. ->get()->all();
  518. if(empty($result)){
  519. return Result::error("此文章暂无调查问卷",0);
  520. }
  521. return Result::success($result);
  522. }
  523. /**
  524. * 前端-添加网站调查问卷选项
  525. * @param array $data
  526. * @return array
  527. */
  528. public function addWebsiteSurveyOption(array $data): array
  529. {
  530. if(isset($data['website_id']) && !empty($data['website_id'])){
  531. $website = Website::where('id',$data['website_id'])->first();
  532. if(empty($website)){
  533. return Result::error("暂无此网站",0);
  534. }
  535. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  536. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  537. if(empty($survey)){
  538. return Result::error("此调查问卷不可添加选项",0);
  539. }
  540. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  541. $choice = [
  542. 'art_id'=>$survey['art_id'],
  543. 'website_id'=>$data['website_id'],
  544. 'survey_name'=>$survey['survey_name'],
  545. 'choice_name'=>$data['choice_name'],
  546. 'sur_id'=>$survey['sur_id'],
  547. 'is_other'=>1,
  548. 'other_id'=>$survey['id'],
  549. ];
  550. $result = ArticleSurvey::insertGetId($choice);
  551. if(empty($result)){
  552. return Result::error("添加失败",0);
  553. }
  554. return Result::success($result);
  555. }
  556. }
  557. return Result::error("添加失败",0);
  558. }
  559. return Result::error("添加失败",0);
  560. }
  561. /**
  562. * 前端-调查问卷投票
  563. * @param array $data
  564. * @return array
  565. */
  566. public function addWebsiteSurveyVote(array $data): array
  567. {
  568. if(isset($data['website_id']) && !empty($data['website_id'])){
  569. $website = Website::where('id',$data['website_id'])->first();
  570. if(empty($website)){
  571. return Result::error("暂无此网站",0);
  572. }
  573. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  574. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->pluck('sur_id');
  575. if(empty($survey)){
  576. return Result::error("此调查问卷不存在",0);
  577. }
  578. // return Result::success($survey);
  579. // 调查问卷类型 0:单选 1:多选
  580. $type = Article::where('survey_id',$data['sur_id'])->pluck('survey_type');
  581. // return Result::success($type);
  582. if(empty($type) || ($type[0]!= 1 && $type[0]!= 0)){
  583. return Result::error("此调查问卷不可投票",0);
  584. }
  585. // return Result::success($type[0]);
  586. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  587. if($type[0] == 0){
  588. if(is_array($data['choice_id'])){
  589. return Result::error("请选择一个选项!",0);
  590. }
  591. $data['choice_id'] = [$data['choice_id']];
  592. }else{
  593. if(!is_array($data['choice_id'])){
  594. return Result::error("请传递数组!",0);
  595. }
  596. }
  597. // return Result::success($data['choice_id']);
  598. $other = ArticleSurvey::whereIn('id',$data['choice_id'])
  599. ->where('website_id',$data['website_id'])
  600. ->where('is_other',1)
  601. ->where('other_id',0)
  602. ->first();
  603. if(!empty($other)){
  604. return Result::error("请选择已有的选项!",0);
  605. }
  606. $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
  607. ->where('website_id',$data['website_id'])
  608. ->where('is_other',1)
  609. ->where('other_id','!=',0)
  610. ->first();
  611. // return Result::success($data);
  612. if(!empty($choice['other'])){
  613. array_push($data['choice_id'],$choice['other']['other_id']);
  614. // return Result::success($data['choice_id']);
  615. }
  616. // return Result::success($data);
  617. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
  618. ->where('website_id',$data['website_id'])
  619. ->increment('results', 1);
  620. if(empty($choice)){
  621. return Result::error("请选择已有的选项!",0);
  622. }
  623. $retult_survey = ArticleSurvey::where('website_id', $data['website_id'])
  624. ->where('sur_id', $data['sur_id'])
  625. ->get();
  626. return Result::success($retult_survey);
  627. }
  628. return Result::error("参数必填!");
  629. }
  630. return Result::error("此调查问卷不存在",0);
  631. }
  632. return Result::error("参数必填!");
  633. }
  634. /**
  635. * 后端-获取网站调查问卷列表
  636. * @param array $data
  637. * @return array
  638. */
  639. public function getSurveyList(array $data): array
  640. {
  641. $where = [];
  642. if(isset($data['survey_name']) &&!empty($data['survey_name'])){
  643. array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
  644. }
  645. if(isset($data['survey_type']) && $data['survey_type'] != null){
  646. array_push($where,['survey_type','=',$data['survey_type']]);
  647. }
  648. if(isset($data['is_survey']) && $data['is_survey'] != null){
  649. array_push($where,['is_survey','=',$data['is_survey']]);
  650. }
  651. // return Result::success($where);
  652. if (!empty($where)) {
  653. $query = Article::where($where)->where(function ($q) {
  654. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  655. });
  656. } else {
  657. $query = Article::where(function ($q) {
  658. $q->whereNotNull('survey_name')->where('survey_name', '!=', '');
  659. });
  660. }
  661. $count = $query->count();
  662. $survey = $query->orderByDesc('id')
  663. ->limit($data['pageSize'])
  664. ->offset(($data['page']-1)*$data['pageSize'])
  665. ->get();
  666. if(empty($survey->toArray())){
  667. return Result::error("暂无调查问卷!",0);
  668. }
  669. $result = [
  670. 'rows'=>$survey,
  671. 'count'=>$count
  672. ];
  673. return Result::success($result);
  674. }
  675. /**
  676. * 后端-获取网站调查问卷详情
  677. * @param array $data
  678. * @return array
  679. */
  680. public function getSurveyInfo(array $data): array
  681. {
  682. if(isset($data['sur_id']) &&!empty($data['sur_id'])){
  683. $where = [ 'sur_id'=>$data['sur_id']];
  684. $choose = ArticleSurvey::where($where)->where('is_other',0)
  685. ->leftJoin('article','article_survey.art_id','article.id')
  686. ->select('article_survey.*','article.survey_type')
  687. ->get()->all();
  688. if(empty($choose)){
  689. return Result::error("此调查问卷不存在",0);
  690. }
  691. $resultsArray = array_column($choose, 'results');
  692. $total = array_sum($resultsArray);
  693. $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  694. $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
  695. // $total = 0;
  696. if(!empty($other)){
  697. $total = $total + $other['results'];
  698. $other['choice_name'] = $other['choice_name'].'(其他)';
  699. if(!empty($others)){
  700. $other['hasChildren'] = true;
  701. // array_push($other,['hasChildren','=',true]);
  702. $other['children'] = $others;
  703. $other_choices = [$other->toArray()];
  704. $mer_choice = array_merge($choose,$other_choices);
  705. $value_choice = array_values($mer_choice);
  706. }
  707. else{
  708. // return Result::error('1111');
  709. $other_choices = [$other->toArray()];
  710. $other_choices = array_merge($choose,$other_choices);
  711. $value_choice = array_values($other_choices);
  712. // return Result::success($result);
  713. }
  714. }else{
  715. $value_choice = $choose;
  716. }
  717. $result = [
  718. 'choose'=>$value_choice,
  719. 'total'=>$total
  720. ];
  721. }
  722. return Result::success($result);
  723. }
  724. /**
  725. * 前端-搜索新闻列表
  726. * @param array $data
  727. * @return array
  728. */
  729. public function selectWebsiteArticle(array $data): array
  730. {
  731. $where = [];
  732. // 初始化查询构造器
  733. $category = WebsiteCategory::where('website_id',$data['website_id'])->pluck('category_id');
  734. $query = Article::where('status', 1)
  735. ->whereIn('catid', $category)
  736. ->where(function ($query) use ($data) {
  737. $query->where(function ($subQuery) use ($data) {
  738. $subQuery->whereRaw("JSON_CONTAINS(ignore_ids, '".intval($data['website_id'])."') = 0");
  739. })->orWhereNull("ignore_ids");
  740. });
  741. // return Result::success($all_articles);
  742. // 检查是否存在 cityid 参数
  743. if (isset($data['cityid']) && !empty($data['cityid'])) {
  744. $query->whereRaw("JSON_CONTAINS(city_arr_id, '".intval($data['cityid'])."')");
  745. }
  746. // 检查是否存在 department_id 参数
  747. if (isset($data['department_id']) && !empty($data['department_id'])) {
  748. $query->whereRaw("JSON_CONTAINS(department_arr_id, '".intval($data['department_id'])."')");
  749. }
  750. // 检查是否存在 keyword 参数
  751. if (isset($data['keyword']) && !empty($data['keyword'])) {
  752. $query->where('title', 'like', '%'.$data['keyword'].'%');
  753. }
  754. // 计算总数
  755. $count = $query->count();
  756. // 分页查询
  757. $articles = $query->orderBy("updated_at", "desc")
  758. ->limit($data['pageSize'])
  759. ->offset(($data['page'] - 1) * $data['pageSize'])
  760. ->get()->all();
  761. if (empty($articles)) {
  762. return Result::error("没有符合条件的资讯数据");
  763. }
  764. $data = [
  765. 'rows' => $articles,
  766. 'count' => $count
  767. ];
  768. return Result::success($data);
  769. }
  770. }