NewsService.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. Db::beginTransaction();
  223. try {
  224. //处理投票
  225. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  226. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  227. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  228. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  229. unset($data['is_survey']);
  230. unset($data['survey_name']);
  231. unset($data['suvey_array']);
  232. unset($data['website_id']);
  233. $articleData = $data;
  234. unset($articleData['content']);
  235. $id = Article::insertGetId($articleData);
  236. $articleDataContent = [
  237. 'article_id' => $id,
  238. 'content' => $data['content'],
  239. ];
  240. ArticleData::insertGetId($articleDataContent);
  241. //处理投票
  242. if ($is_survey == 1) {
  243. //生成年月日时分秒+8位随机数
  244. $uuid = date('YmdHis') . rand(10000000, 99999999);
  245. $suveys_array = json_decode($suvey_array);
  246. var_dump($suveys_array, '---------------------1');
  247. var_dump($suvey_array, '---------------------2');
  248. $suvey_data = [];
  249. foreach ($suveys_array as $key => $value) {
  250. if ($value == '') {
  251. continue;
  252. }
  253. if (is_array($value)) {
  254. $suvey_data[] = [
  255. 'sur_id' => $uuid,
  256. 'art_id' => $id,
  257. 'website_id' => $website_id ?? 2,
  258. 'survey_name' => $survey_name,
  259. 'choice_name' => $value[1],
  260. 'is_other' => 1,
  261. 'other_id' => 0,
  262. 'results' => 0,
  263. ];
  264. } else {
  265. $suvey_data[] = [
  266. 'sur_id' => $uuid,
  267. 'art_id' => $id,
  268. 'website_id' => $website_id ?? 2,
  269. 'survey_name' => $survey_name,
  270. 'choice_name' => $value,
  271. 'is_other' => 0,
  272. 'other_id' => 0,
  273. 'results' => 0,
  274. ];
  275. }
  276. if (empty($suvey_data)) {
  277. throw new \Exception("投票数据为空");
  278. }
  279. }
  280. $result = ArticleSurvey::insert($suvey_data);
  281. if (!$result) {
  282. throw new \Exception("投票失败,ArticleSurvey插入失败");
  283. }
  284. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  285. if (!$result) {
  286. throw new \Exception("投票失败,更新主表失败");
  287. }
  288. }
  289. Db::commit();
  290. return Result::success(['id' => $id]);
  291. } catch (\Throwable $ex) {
  292. Db::rollBack();
  293. var_dump($ex->getMessage());
  294. return Result::error("创建失败", 0);
  295. }
  296. }
  297. /**
  298. * @param array $data
  299. * @return array
  300. */
  301. public function delArticle(array $data): array
  302. {
  303. $result = Article::where($data)->delete();
  304. //survey投票删除
  305. articleSurvey::where(['art_id' => $data['id']])->delete();
  306. if (!$result) {
  307. return Result::error("删除失败");
  308. }
  309. return Result::success($result);
  310. }
  311. /**
  312. * @param array $data
  313. * @return array
  314. */
  315. public function updateArticle(array $data): array
  316. {
  317. Db::beginTransaction();
  318. try {
  319. //处理投票
  320. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  321. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  322. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  323. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  324. unset($data['is_survey']);
  325. unset($data['survey_name']);
  326. unset($data['suvey_array']);
  327. unset($data['website_id']);
  328. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  329. $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
  330. $articleData = $data;
  331. unset($articleData['content']);
  332. unset($articleData['status_name']);
  333. unset($articleData['name']);
  334. unset($articleData['content']);
  335. unset($articleData['pid_arr']);
  336. unset($articleData['pid']);
  337. $id = Article::where(['id' => $data['id']])->update($articleData);
  338. $articleDataContent = [
  339. 'content' => $data['content'],
  340. ];
  341. ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
  342. //处理投票
  343. $id = $data['id'];
  344. $surveydata = ArticleSurvey::where(['art_id' => $data['id']])->delete();
  345. var_dump($surveydata, 'suvey_array________delete');
  346. //处理投票
  347. if ($is_survey == 1) {
  348. //生成年月日时分秒+8位随机数
  349. $uuid = date('YmdHis') . rand(10000000, 99999999);
  350. $suveys_array = json_decode($suvey_array);
  351. var_dump($suveys_array, '---------------------1');
  352. var_dump($suvey_array, '---------------------2');
  353. $suvey_data = [];
  354. foreach ($suveys_array as $key => $value) {
  355. if ($value == '') {
  356. continue;
  357. }
  358. if (is_array($value)) {
  359. $suvey_data[] = [
  360. 'sur_id' => $uuid,
  361. 'art_id' => $id,
  362. 'website_id' => $website_id ?? 2,
  363. 'survey_name' => $survey_name,
  364. 'choice_name' => $value[1],
  365. 'is_other' => 1,
  366. 'other_id' => 0,
  367. 'results' => 0,
  368. ];
  369. } else {
  370. $suvey_data[] = [
  371. 'sur_id' => $uuid,
  372. 'art_id' => $id,
  373. 'website_id' => $website_id ?? 2,
  374. 'survey_name' => $survey_name,
  375. 'choice_name' => $value,
  376. 'is_other' => 0,
  377. 'other_id' => 0,
  378. 'results' => 0,
  379. ];
  380. }
  381. if (empty($suvey_data)) {
  382. throw new \Exception("投票数据为空");
  383. }
  384. }
  385. $result = ArticleSurvey::insert($suvey_data);
  386. if (!$result) {
  387. throw new \Exception("投票失败");
  388. }
  389. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  390. if (!$result) {
  391. throw new \Exception("投票失败");
  392. }
  393. }
  394. Db::commit();
  395. return Result::success([]);
  396. } catch (\Throwable $ex) {
  397. Db::rollBack();
  398. var_dump($ex->getMessage());
  399. return Result::error("更新失败", 0);
  400. }
  401. }
  402. /**
  403. * 更新资讯状态
  404. * @param array $data
  405. * @return array
  406. */
  407. public function upArticleStatus(array $data): array
  408. {
  409. $result = Article::where(['id' => $data['id']])->update($data);
  410. if ($result) {
  411. return Result::success();
  412. } else {
  413. return Result::error("更新状态失败", 0);
  414. }
  415. }
  416. /**
  417. * 获取新闻详情
  418. * @param array $data
  419. * @return array
  420. */
  421. public function getArticleInfo(array $data): array
  422. {
  423. $where = [
  424. 'article.id'=>$data['id'],
  425. // 'article.status'=>1
  426. ];
  427. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  428. if (empty($result)) {
  429. return Result::error("查询失败", 0);
  430. }
  431. //添加投票
  432. $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
  433. $result['survey_array'] = $articleSurvey->toArray();
  434. if ($result) {
  435. return Result::success($result->toArray());
  436. } else {
  437. return Result::error("查询失败", 0);
  438. }
  439. return Result::success($result);
  440. }
  441. /**
  442. * 获取头条新闻
  443. * @param array $data
  444. * @return array
  445. */
  446. public function getWebsiteArticlett(array $data): array
  447. {
  448. $category = WebsiteCategory::where('website_id', $data['website_id'])->select('category_id')->get();
  449. $category = $category->toArray();
  450. $result = [];
  451. if ($category) {
  452. $category_ids = [];
  453. foreach ($category as $val) {
  454. array_push($category_ids, $val['category_id']);
  455. }
  456. if (isset($data['placeid'])) {
  457. $placeid = $data['placeid'] - 1;
  458. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset($placeid)->limit($data['pageSize'])->get();
  459. } else {
  460. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset(0)->limit($data['pageSize'])->get();
  461. }
  462. if (empty($result)) {
  463. return Result::error("暂无头条新闻", 0);
  464. }
  465. return Result::success($result);
  466. } else {
  467. return Result::error("本网站下暂无相关栏目", 0);
  468. }
  469. }
  470. /**
  471. * 获取模块新闻
  472. * @param array $data
  473. * @return array
  474. */
  475. public function getWebsiteModelArticles(array $data): array
  476. {
  477. $catid = $data['catid'];
  478. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  479. $category = $category->toArray();
  480. if (!empty($category)) {
  481. $where = [
  482. 'status' => 1,
  483. 'catid' => $catid,
  484. ];
  485. if ($data['level'] == 1) {
  486. $level = [
  487. 0 => '1',
  488. 1 => '4',
  489. 2 => '5',
  490. 3 => '0',
  491. ];
  492. $result = Article::where($where)->whereIn('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  493. } elseif ($data['level'] == 2) {
  494. $level = '2';
  495. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  496. } else {
  497. $level = '3';
  498. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  499. }
  500. $result = $result->toArray();
  501. if (!empty($result) && isset($data['placeid']) && !empty($data['placeid'])) {
  502. $placeid = $data['placeid'] - 1;
  503. if ($level == 2 || $level == 3) {
  504. $where = [
  505. 'level' => $level,
  506. ];
  507. $result = Article::where($where)
  508. ->orderBy("created_at", "desc")
  509. ->offset($placeid)
  510. ->limit($data['pagesize'])->get();
  511. } else {
  512. $result = Article::where($where)
  513. ->whereIn('level', $level)
  514. ->offset($placeid)
  515. ->orderBy("created_at", "desc")
  516. ->limit($data['pagesize'])->get();
  517. }
  518. }
  519. if (empty($result)) {
  520. return Result::error("此栏目暂无相关新闻", 0);
  521. }
  522. } else {
  523. return Result::error("此网站暂无此栏目", 0);
  524. }
  525. return Result::success($result);
  526. }
  527. /**
  528. *获取新闻列表
  529. * @param array $data
  530. * @return array
  531. */
  532. public function getWebsiteArticleList(array $data): array
  533. {
  534. $where[] = ['status', '=', 1];
  535. if(isset($data['keyword']) && !empty($data['keyword'])){
  536. array_push($where,['article.title','like','%'.$data['keyword'].'%']);
  537. }
  538. if(isset($data['catid']) && !empty($data['catid'])){
  539. if(is_array($data['catid'])){
  540. $category = WebsiteCategory::where('website_id',$data['website_id'])->whereIn('category_id',$data['catid'])->pluck('category_id');
  541. array_push($where,['catid', 'in', $data['catid']]);
  542. }else{
  543. $category = WebsiteCategory::where('website_id',$data['website_id'])->where('category_id',$data['catid'])->pluck('category_id');
  544. array_push($where,['catid', '=', $data['catid']]);
  545. }
  546. if (empty($category)) {
  547. return Result::error("此网站暂无此栏目", 0);
  548. }
  549. }
  550. // return Result::success($where);
  551. $rep = Article::where(function ($query) use ($where) {
  552. foreach ($where as $condition) {
  553. if ($condition[1] === 'in') {
  554. $query->whereIn($condition[0], $condition[2]);
  555. } else {
  556. $query->where($condition[0], $condition[1], $condition[2]);
  557. }
  558. }
  559. })
  560. ->orderBy("created_at", "desc")
  561. ->limit($data['pageSize'])
  562. ->offset(($data['page'] - 1) * $data['pageSize'])
  563. ->get();
  564. $count = Article::where(function ($query) use ($where) {
  565. foreach ($where as $condition) {
  566. if ($condition[1] === 'in') {
  567. $query->whereIn($condition[0], $condition[2]);
  568. } else {
  569. $query->where($condition[0], $condition[1], $condition[2]);
  570. }
  571. }
  572. })->count();
  573. $data = [
  574. 'rows' => $rep->toArray(),
  575. 'count' => $count,
  576. ];
  577. if(empty($rep)){
  578. return Result::error("没有信息数据");
  579. }
  580. return Result::success($data);
  581. }
  582. /**
  583. * 前端-获取新闻详情
  584. * @param array $data
  585. * @return array
  586. */
  587. public function selectWebsiteArticleInfo(array $data): array
  588. {
  589. $where = [
  590. 'article.id' => $data['id'],
  591. 'article.status' => 1,
  592. ];
  593. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  594. if (empty($result)) {
  595. return Result::error("查询失败", 0);
  596. }
  597. $category = WebsiteCategory::where('website_id', $data['website_id'])->where(['category_id' => $result['catid']])->first();
  598. if (empty($category)) {
  599. return Result::error("查询失败", 0);
  600. }
  601. $result['category_id'] = $category['category_id'];
  602. $result['cat_name'] = $category['name'];
  603. return Result::success($result);
  604. }
  605. /**
  606. * 前端-获取网站调查问卷
  607. * @param array $data
  608. * @return array
  609. */
  610. public function getWebsiteSurvey(array $data): array
  611. {
  612. if(isset($data['survey_id']) && !empty($data['survey_id'])){
  613. $website = Website::where('id',$data['website_id'])->first();
  614. if(empty($website)){
  615. return Result::error("暂无此网站",0);
  616. }
  617. }
  618. if(isset($data['art_id']) && !empty($data['art_id'])){
  619. $article = Article::where('id',$data['art_id'])->first();
  620. if(empty($article)){
  621. return Result::error("暂无此文章",0);
  622. }
  623. // return Result::error($data,0);
  624. $where['art_id'] = $data['art_id'];
  625. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  626. }else{
  627. $survey = ArticleSurvey::where('website_id',$data['website_id'])->orderBy('created_at')->first();
  628. if(empty($survey)){
  629. return Result::error("暂无调查问卷",0);
  630. }
  631. $where['sur_id'] = $survey['sur_id'];
  632. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  633. }
  634. $result['survey'] = ArticleSurvey::where($where)->where('is_other',0)
  635. ->leftJoin('article','article_survey.art_id','article.id')
  636. ->select('article_survey.*','article.survey_type')
  637. ->get()->all();
  638. $result['others'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  639. $result['other'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->first();
  640. if(empty($result)){
  641. return Result::error("此文章暂无调查问卷",0);
  642. }
  643. return Result::success($result);
  644. }
  645. /**
  646. * 前端-添加网站调查问卷选项
  647. * @param array $data
  648. * @return array
  649. */
  650. public function addWebsiteSurveyOption(array $data): array
  651. {
  652. if(isset($data['website_id']) && !empty($data['website_id'])){
  653. $website = Website::where('id',$data['website_id'])->first();
  654. if(empty($website)){
  655. return Result::error("暂无此网站",0);
  656. }
  657. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  658. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  659. if(empty($survey)){
  660. return Result::error("此调查问卷不可添加选项",0);
  661. }
  662. if(isset($data['choice_name']) &&!empty($data['choice_name'])){
  663. $choice = [
  664. 'art_id'=>$survey['art_id'],
  665. 'website_id'=>$data['website_id'],
  666. 'survey_name'=>$survey['survey_name'],
  667. 'choice_name'=>$data['choice_name'],
  668. 'sur_id'=>$survey['sur_id'],
  669. 'is_other'=>1,
  670. 'other_id'=>$survey['id'],
  671. ];
  672. $result = ArticleSurvey::insertGetId($choice);
  673. if(empty($result)){
  674. return Result::error("添加失败",0);
  675. }
  676. return Result::success($result);
  677. }
  678. }
  679. return Result::error("添加失败",0);
  680. }
  681. return Result::error("添加失败",0);
  682. }
  683. /**
  684. * 前端-调查问卷投票
  685. * @param array $data
  686. * @return array
  687. */
  688. public function addWebsiteSurveyVote(array $data): array
  689. {
  690. if(isset($data['website_id']) && !empty($data['website_id'])){
  691. $website = Website::where('id',$data['website_id'])->first();
  692. if(empty($website)){
  693. return Result::error("暂无此网站",0);
  694. }
  695. if(isset($data['sur_id']) && !empty($data['sur_id'])){
  696. $survey = ArticleSurvey::where('sur_id',$data['sur_id'])->where('website_id',$data['website_id'])->pluck('sur_id');
  697. if(empty($survey)){
  698. return Result::error("此调查问卷不存在",0);
  699. }
  700. // return Result::success($survey);
  701. // 调查问卷类型 0:单选 1:多选
  702. $type = Article::where('survey_id',$data['sur_id'])->pluck('survey_type');
  703. // return Result::success($type);
  704. if(empty($type) || ($type[0]!= 1 && $type[0]!= 0)){
  705. return Result::error("此调查问卷不可投票",0);
  706. }
  707. // return Result::success($type[0]);
  708. if(isset($data['choice_id']) &&!empty($data['choice_id'])){
  709. if($type[0] == 0){
  710. if(is_array($data['choice_id'])){
  711. return Result::error("请选择一个选项!",0);
  712. }
  713. $data['choice_id'] = [$data['choice_id']];
  714. }else{
  715. if(!is_array($data['choice_id'])){
  716. return Result::error("请传递数组!",0);
  717. }
  718. }
  719. // return Result::success($data['choice_id']);
  720. $choice['other'] = ArticleSurvey::whereIn('id',$data['choice_id'])
  721. ->where('website_id',$data['website_id'])
  722. ->where('is_other',1)
  723. ->where('other_id','!=',0)
  724. ->first();
  725. // return Result::success($data);
  726. if(!empty($choice['other'])){
  727. array_push($data['choice_id'],$choice['other']['other_id']);
  728. // return Result::success($data['choice_id']);
  729. }
  730. // return Result::success($data);
  731. $choice = ArticleSurvey::whereIn('id',$data['choice_id'])
  732. ->where('website_id',$data['website_id'])
  733. ->increment('results', 1);
  734. if(empty($choice)){
  735. return Result::error("请选择已有的选项!",0);
  736. }
  737. return Result::success($choice);
  738. }
  739. return Result::error("参数必填!");
  740. // if(isset($data['choice_id']) && !empty($data['choice_id'])){
  741. // $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  742. // }
  743. }
  744. return Result::error("此调查问卷不存在",0);
  745. }
  746. return Result::error("参数必填!");
  747. }
  748. /**
  749. * 验证导航名称是否重复
  750. * @return void
  751. */
  752. public function checkCategoryName(array $data): array
  753. {
  754. $result = Category::when($data, function ($query) use ($data) {
  755. if(isset($data['name']) && $data['name']) {
  756. $query->where("name", $data['name']);
  757. }
  758. if(isset($data['id']) && $data['id']) {
  759. $query->where("id","!=" ,$data['id']);
  760. }
  761. })->first();
  762. if($result){
  763. return Result::error("已存在");
  764. }else{
  765. return Result::success();
  766. }
  767. }
  768. }