NewsService.php 31 KB

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