NewsService.php 32 KB

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