NewsService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 App\Model\Good;
  10. use Hyperf\DbConnection\Db;
  11. use Hyperf\RpcServer\Annotation\RpcService;
  12. use App\Tools\Result;
  13. use Ramsey\Uuid\Uuid;
  14. use Hyperf\Utils\Random;
  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. var_dump($data, '----------12-----------1');
  224. unset($data['user_type']);
  225. // unset($data['web_site_id']);
  226. unset($data['nav_add_pool_id']);
  227. // $data['cat_arr_id'] = is_string($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  228. Db::beginTransaction();
  229. try {
  230. //处理投票
  231. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  232. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  233. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  234. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  235. unset($data['is_survey']);
  236. unset($data['survey_name']);
  237. unset($data['suvey_array']);
  238. unset($data['website_id']);
  239. $articleData = $data;
  240. unset($articleData['content']);
  241. $id = Article::insertGetId($articleData);
  242. $articleDataContent = [
  243. 'article_id' => $id,
  244. 'content' => $data['content'],
  245. ];
  246. ArticleData::insertGetId($articleDataContent);
  247. //处理投票
  248. if ($is_survey == 1) {
  249. //生成年月日时分秒+8位随机数
  250. $uuid = date('YmdHis') . rand(10000000, 99999999);
  251. $suveys_array = json_decode($suvey_array);
  252. var_dump($suveys_array, '---------------------1');
  253. var_dump($suvey_array, '---------------------2');
  254. $suvey_data = [];
  255. foreach ($suveys_array as $key => $value) {
  256. if ($value == '') {
  257. continue;
  258. }
  259. if (is_array($value)) {
  260. $suvey_data[] = [
  261. 'sur_id' => $uuid,
  262. 'art_id' => $id,
  263. 'website_id' => $website_id ?? 2,
  264. 'survey_name' => $survey_name,
  265. 'choice_name' => $value[1],
  266. 'is_other' => 1,
  267. 'other_id' => 0,
  268. 'results' => 0,
  269. ];
  270. } else {
  271. $suvey_data[] = [
  272. 'sur_id' => $uuid,
  273. 'art_id' => $id,
  274. 'website_id' => $website_id ?? 2,
  275. 'survey_name' => $survey_name,
  276. 'choice_name' => $value,
  277. 'is_other' => 0,
  278. 'other_id' => 0,
  279. 'results' => 0,
  280. ];
  281. }
  282. if (empty($suvey_data)) {
  283. throw new \Exception("投票数据为空");
  284. }
  285. }
  286. $result = ArticleSurvey::insert($suvey_data);
  287. if (!$result) {
  288. throw new \Exception("投票失败,ArticleSurvey插入失败");
  289. }
  290. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  291. if (!$result) {
  292. throw new \Exception("投票失败,更新主表失败");
  293. }
  294. }
  295. Db::commit();
  296. return Result::success(['id' => $id]);
  297. } catch (\Throwable $ex) {
  298. Db::rollBack();
  299. var_dump($ex->getMessage());
  300. return Result::error("创建失败", 0);
  301. }
  302. }
  303. /**
  304. * @param array $data
  305. * @return array
  306. */
  307. public function delArticle(array $data): array
  308. {
  309. $result = Article::where($data)->delete();
  310. //survey投票删除
  311. articleSurvey::where(['art_id' => $data['id']])->delete();
  312. if (!$result) {
  313. return Result::error("删除失败");
  314. }
  315. return Result::success($result);
  316. }
  317. /**
  318. * @param array $data
  319. * @return array
  320. */
  321. public function updateArticle(array $data): array
  322. {
  323. Db::beginTransaction();
  324. unset($data['user_type']);
  325. // unset($data['web_site_id']);
  326. unset($data['nav_add_pool_id']);
  327. try {
  328. //处理投票
  329. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  330. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  331. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  332. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  333. unset($data['is_survey']);
  334. unset($data['survey_name']);
  335. unset($data['suvey_array']);
  336. unset($data['website_id']);
  337. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  338. $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
  339. $articleData = $data;
  340. unset($articleData['content']);
  341. unset($articleData['status_name']);
  342. unset($articleData['name']);
  343. unset($articleData['content']);
  344. unset($articleData['pid_arr']);
  345. unset($articleData['pid']);
  346. $id = Article::where(['id' => $data['id']])->update($articleData);
  347. $articleDataContent = [
  348. 'content' => $data['content'],
  349. ];
  350. ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
  351. //处理投票
  352. $id = $data['id'];
  353. $surveydata = ArticleSurvey::where(['art_id' => $data['id']])->delete();
  354. var_dump($surveydata, 'suvey_array________delete');
  355. //处理投票
  356. if ($is_survey == 1) {
  357. //生成年月日时分秒+8位随机数
  358. $uuid = date('YmdHis') . rand(10000000, 99999999);
  359. $suveys_array = json_decode($suvey_array);
  360. var_dump($suveys_array, '---------------------1');
  361. var_dump($suvey_array, '---------------------2');
  362. $suvey_data = [];
  363. foreach ($suveys_array as $key => $value) {
  364. if ($value == '') {
  365. continue;
  366. }
  367. if (is_array($value)) {
  368. $suvey_data[] = [
  369. 'sur_id' => $uuid,
  370. 'art_id' => $id,
  371. 'website_id' => $website_id ?? 2,
  372. 'survey_name' => $survey_name,
  373. 'choice_name' => $value[1],
  374. 'is_other' => 1,
  375. 'other_id' => 0,
  376. 'results' => 0,
  377. ];
  378. } else {
  379. $suvey_data[] = [
  380. 'sur_id' => $uuid,
  381. 'art_id' => $id,
  382. 'website_id' => $website_id ?? 2,
  383. 'survey_name' => $survey_name,
  384. 'choice_name' => $value,
  385. 'is_other' => 0,
  386. 'other_id' => 0,
  387. 'results' => 0,
  388. ];
  389. }
  390. if (empty($suvey_data)) {
  391. throw new \Exception("投票数据为空");
  392. }
  393. }
  394. $result = ArticleSurvey::insert($suvey_data);
  395. if (!$result) {
  396. throw new \Exception("投票失败");
  397. }
  398. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  399. if (!$result) {
  400. throw new \Exception("投票失败");
  401. }
  402. }
  403. Db::commit();
  404. return Result::success([]);
  405. } catch (\Throwable $ex) {
  406. Db::rollBack();
  407. var_dump($ex->getMessage());
  408. return Result::error("更新失败", 0);
  409. }
  410. }
  411. /**
  412. * 更新资讯状态
  413. * @param array $data
  414. * @return array
  415. */
  416. public function upArticleStatus(array $data): array
  417. {
  418. $result = Article::where(['id' => $data['id']])->update($data);
  419. if ($result) {
  420. return Result::success();
  421. } else {
  422. return Result::error("更新状态失败", 0);
  423. }
  424. }
  425. /**
  426. * 获取新闻详情
  427. * @param array $data
  428. * @return array
  429. */
  430. public function getArticleInfo(array $data): array
  431. {
  432. $where = [
  433. 'article.id' => $data['id'],
  434. // 'article.status'=>1
  435. ];
  436. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  437. if (empty($result)) {
  438. return Result::error("查询失败", 0);
  439. }
  440. //添加投票
  441. $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
  442. $result['survey_array'] = $articleSurvey->toArray();
  443. if ($result) {
  444. return Result::success($result->toArray());
  445. } else {
  446. return Result::error("查询失败", 0);
  447. }
  448. return Result::success($result);
  449. }
  450. /**
  451. * 获取头条新闻
  452. * @param array $data
  453. * @return array
  454. */
  455. public function getWebsiteArticlett(array $data): array
  456. {
  457. $category = WebsiteCategory::where('website_id', $data['website_id'])->select('category_id')->get();
  458. $category = $category->toArray();
  459. $result = [];
  460. if ($category) {
  461. $category_ids = [];
  462. foreach ($category as $val) {
  463. array_push($category_ids, $val['category_id']);
  464. }
  465. if (isset($data['placeid'])) {
  466. $placeid = $data['placeid'] - 1;
  467. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset($placeid)->limit($data['pageSize'])->get();
  468. } else {
  469. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset(0)->limit($data['pageSize'])->get();
  470. }
  471. if (empty($result)) {
  472. return Result::error("暂无头条新闻", 0);
  473. }
  474. return Result::success($result);
  475. } else {
  476. return Result::error("本网站下暂无相关栏目", 0);
  477. }
  478. }
  479. /**
  480. * 获取模块新闻
  481. * @param array $data
  482. * @return array
  483. */
  484. public function getWebsiteModelArticles(array $data): array
  485. {
  486. $catid = $data['catid'];
  487. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  488. $category = $category->toArray();
  489. if (!empty($category)) {
  490. $where = [
  491. 'status' => 1,
  492. 'catid' => $catid,
  493. ];
  494. if ($data['level'] == 1) {
  495. $level = [
  496. 0 => '1',
  497. 1 => '4',
  498. 2 => '5',
  499. 3 => '0',
  500. ];
  501. $result = Article::where($where)->whereIn('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  502. } elseif ($data['level'] == 2) {
  503. $level = '2';
  504. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  505. } else {
  506. $level = '3';
  507. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  508. }
  509. $result = $result->toArray();
  510. if (!empty($result) && isset($data['placeid']) && !empty($data['placeid'])) {
  511. $placeid = $data['placeid'] - 1;
  512. if ($level == 2 || $level == 3) {
  513. $where = [
  514. 'level' => $level,
  515. ];
  516. $result = Article::where($where)
  517. ->orderBy("created_at", "desc")
  518. ->offset($placeid)
  519. ->limit($data['pagesize'])->get();
  520. } else {
  521. $result = Article::where($where)
  522. ->whereIn('level', $level)
  523. ->offset($placeid)
  524. ->orderBy("created_at", "desc")
  525. ->limit($data['pagesize'])->get();
  526. }
  527. }
  528. if (empty($result)) {
  529. return Result::error("此栏目暂无相关新闻", 0);
  530. }
  531. } else {
  532. return Result::error("此网站暂无此栏目", 0);
  533. }
  534. return Result::success($result);
  535. }
  536. /**
  537. *获取新闻列表
  538. * @param array $data
  539. * @return array
  540. */
  541. public function getWebsiteArticleList(array $data): array
  542. {
  543. $where[] = ['status', '=', 1];
  544. if (isset($data['keyword']) && !empty($data['keyword'])) {
  545. array_push($where, ['article.title', 'like', '%' . $data['keyword'] . '%']);
  546. }
  547. if (isset($data['catid']) && !empty($data['catid'])) {
  548. if (is_array($data['catid'])) {
  549. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  550. array_push($where, ['catid', 'in', $data['catid']]);
  551. } else {
  552. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  553. array_push($where, ['catid', '=', $data['catid']]);
  554. }
  555. if (empty($category)) {
  556. return Result::error("此网站暂无此栏目", 0);
  557. }
  558. }
  559. // return Result::success($where);
  560. $rep = Article::where(function ($query) use ($where) {
  561. foreach ($where as $condition) {
  562. if ($condition[1] === 'in') {
  563. $query->whereIn($condition[0], $condition[2]);
  564. } else {
  565. $query->where($condition[0], $condition[1], $condition[2]);
  566. }
  567. }
  568. })
  569. ->orderBy("created_at", "desc")
  570. ->limit($data['pageSize'])
  571. ->offset(($data['page'] - 1) * $data['pageSize'])
  572. ->get();
  573. $count = Article::where(function ($query) use ($where) {
  574. foreach ($where as $condition) {
  575. if ($condition[1] === 'in') {
  576. $query->whereIn($condition[0], $condition[2]);
  577. } else {
  578. $query->where($condition[0], $condition[1], $condition[2]);
  579. }
  580. }
  581. })->count();
  582. $data = [
  583. 'rows' => $rep->toArray(),
  584. 'count' => $count,
  585. ];
  586. if (empty($rep)) {
  587. return Result::error("没有信息数据");
  588. }
  589. return Result::success($data);
  590. }
  591. /**
  592. * 前端-获取新闻详情
  593. * @param array $data
  594. * @return array
  595. */
  596. public function selectWebsiteArticleInfo(array $data): array
  597. {
  598. $where = [
  599. 'article.id' => $data['id'],
  600. 'article.status' => 1,
  601. ];
  602. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  603. if (empty($result)) {
  604. return Result::error("查询失败", 0);
  605. }
  606. $category = WebsiteCategory::where('website_id', $data['website_id'])->where(['category_id' => $result['catid']])->first();
  607. if (empty($category)) {
  608. return Result::error("查询失败", 0);
  609. }
  610. $result['category_id'] = $category['category_id'];
  611. $result['cat_name'] = $category['name'];
  612. return Result::success($result);
  613. }
  614. /**
  615. * 前端-获取网站调查问卷
  616. * @param array $data
  617. * @return array
  618. */
  619. public function getWebsiteSurvey(array $data): array
  620. {
  621. if(isset($data['website_id']) && !empty($data['website_id'])){
  622. $website = Website::where('id',$data['website_id'])->first();
  623. if(empty($website)){
  624. return Result::error("暂无此网站",0);
  625. }
  626. }
  627. if(isset($data['art_id']) && !empty($data['art_id'])){
  628. $article = Article::where('id',$data['art_id'])->where('status',1)->first();
  629. if(empty($article)){
  630. return Result::error("暂无此文章",0);
  631. }
  632. // return Result::error($data,0);
  633. $where['art_id'] = $data['art_id'];
  634. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  635. } else {
  636. $survey = ArticleSurvey::where('website_id', $data['website_id'])->orderBy('created_at')->first();
  637. if (empty($survey)) {
  638. return Result::error("暂无调查问卷", 0);
  639. }
  640. $where['sur_id'] = $survey['sur_id'];
  641. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  642. }
  643. $result['survey'] = ArticleSurvey::where($where)->where('is_other',0)
  644. ->leftJoin('article','article_survey.art_id','article.id')
  645. ->select('article_survey.*','article.survey_type')
  646. ->get()->all();
  647. $result['other'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  648. $result['others'] = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->first();
  649. if(empty($result)){
  650. return Result::error("此文章暂无调查问卷",0);
  651. }
  652. return Result::success($result);
  653. }
  654. /**
  655. * 前端-添加网站调查问卷选项
  656. * @param array $data
  657. * @return array
  658. */
  659. public function addWebsiteSurveyOption(array $data): array
  660. {
  661. if (isset($data['website_id']) && !empty($data['website_id'])) {
  662. $website = Website::where('id', $data['website_id'])->first();
  663. if (empty($website)) {
  664. return Result::error("暂无此网站", 0);
  665. }
  666. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  667. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->where('is_other', 1)->where('other_id', 0)->first();
  668. if (empty($survey)) {
  669. return Result::error("此调查问卷不可添加选项", 0);
  670. }
  671. if (isset($data['choice_name']) && !empty($data['choice_name'])) {
  672. $choice = [
  673. 'art_id' => $survey['art_id'],
  674. 'website_id' => $data['website_id'],
  675. 'survey_name' => $survey['survey_name'],
  676. 'choice_name' => $data['choice_name'],
  677. 'sur_id' => $survey['sur_id'],
  678. 'is_other' => 1,
  679. 'other_id' => $survey['id'],
  680. ];
  681. $result = ArticleSurvey::insertGetId($choice);
  682. if (empty($result)) {
  683. return Result::error("添加失败", 0);
  684. }
  685. return Result::success($result);
  686. }
  687. }
  688. return Result::error("添加失败", 0);
  689. }
  690. return Result::error("添加失败", 0);
  691. }
  692. /**
  693. * 前端-调查问卷投票
  694. * @param array $data
  695. * @return array
  696. */
  697. public function addWebsiteSurveyVote(array $data): array
  698. {
  699. if(isset($data['website_id']) && !empty($data['website_id'])){
  700. $website = Website::where('id',$data['website_id'])->first();
  701. if(empty($website)){
  702. return Result::error("暂无此网站",0);
  703. }
  704. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  705. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->pluck('sur_id');
  706. if (empty($survey)) {
  707. return Result::error("此调查问卷不存在", 0);
  708. }
  709. // return Result::success($survey);
  710. // 调查问卷类型 0:单选 1:多选
  711. $type = Article::where('survey_id', $data['sur_id'])->pluck('survey_type');
  712. // return Result::success($type);
  713. if (empty($type) || ($type[0] != 1 && $type[0] != 0)) {
  714. return Result::error("此调查问卷不可投票", 0);
  715. }
  716. // return Result::success($type[0]);
  717. if (isset($data['choice_id']) && !empty($data['choice_id'])) {
  718. if ($type[0] == 0) {
  719. if (is_array($data['choice_id'])) {
  720. return Result::error("请选择一个选项!", 0);
  721. }
  722. $data['choice_id'] = [$data['choice_id']];
  723. } else {
  724. if (!is_array($data['choice_id'])) {
  725. return Result::error("请传递数组!", 0);
  726. }
  727. }
  728. // return Result::success($data['choice_id']);
  729. $choice['other'] = ArticleSurvey::whereIn('id', $data['choice_id'])
  730. ->where('website_id', $data['website_id'])
  731. ->where('is_other', 1)
  732. ->where('other_id', '!=', 0)
  733. ->first();
  734. // return Result::success($data);
  735. if (!empty($choice['other'])) {
  736. array_push($data['choice_id'], $choice['other']['other_id']);
  737. // return Result::success($data['choice_id']);
  738. }
  739. // return Result::success($data);
  740. $choice = ArticleSurvey::whereIn('id', $data['choice_id'])
  741. ->where('website_id', $data['website_id'])
  742. ->increment('results', 1);
  743. if (empty($choice)) {
  744. return Result::error("请选择已有的选项!", 0);
  745. }
  746. return Result::success($choice);
  747. }
  748. return Result::error("参数必填!");
  749. // if(isset($data['choice_id']) && !empty($data['choice_id'])){
  750. // $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  751. // }
  752. }
  753. return Result::error("此调查问卷不存在", 0);
  754. }
  755. return Result::error("参数必填!");
  756. }
  757. /**
  758. * 验证导航名称是否重复
  759. * @return void
  760. */
  761. public function checkCategoryName(array $data): array
  762. {
  763. $result = Category::when($data, function ($query) use ($data) {
  764. if (isset($data['name']) && $data['name']) {
  765. $query->where("name", $data['name']);
  766. }
  767. if (isset($data['id']) && $data['id']) {
  768. $query->where("id", "!=", $data['id']);
  769. }
  770. })->first();
  771. if ($result) {
  772. return Result::error("已存在");
  773. } else {
  774. return Result::success();
  775. }
  776. }
  777. //20250226 产品列表
  778. public function getGoodList(array $data): array
  779. {
  780. $where = [];
  781. //类型
  782. if (isset($data['type_id']) && $data['type_id']) {
  783. $where = [
  784. 'type_id' => $data['type_id'],
  785. ];
  786. }
  787. //名称
  788. if (isset($data['name']) && $data['name']) {
  789. $where = [
  790. 'name' => $data['name'],
  791. ];
  792. }
  793. //website_id
  794. if (isset($data['website_id']) && $data['website_id']) {
  795. $where = [
  796. 'website_id' => $data['website_id'],
  797. ];
  798. }
  799. //catid
  800. if (isset($data['catid']) && $data['catid']) {
  801. $where = [
  802. 'catid' => $data['catid'],
  803. ];
  804. }
  805. // $result = Good::where($where)
  806. // ->orderBy("updated_at", "desc")->paginate($data['pige_size'], ['*'], 'page', $data['page']);
  807. $result = Good::where($where)
  808. ->orderBy("updated_at", "desc")
  809. ->limit($data['page_size'])
  810. ->offset(($data['page'] - 1) * $data['page_size'])
  811. ->get();
  812. $count = Good::where($where)->count();
  813. $data = [
  814. 'rows' => $result->toArray(),
  815. 'count' => $count,
  816. ];
  817. if (empty($result)) {
  818. return Result::error("此栏目暂无相关产品", 0);
  819. }
  820. return Result::success($data);
  821. }
  822. public function getGoodInfo(array $data): array
  823. {
  824. $result = Good::where('id', $data['id'])->first();
  825. if (empty($result)) {
  826. return Result::error("此产品不存在", 0);
  827. }
  828. return Result::success($result);
  829. }
  830. public function addGood(array $data): array
  831. {
  832. $result = Good::create($data);
  833. if (empty($result)) {
  834. return Result::error("添加失败", 0);
  835. }
  836. return Result::success($result);
  837. }
  838. public function updateGood(array $data): array
  839. {
  840. $result = Good::where('id', $data['id'])->update($data);
  841. if (empty($result)) {
  842. return Result::error("更新失败", 0);
  843. }
  844. return Result::success($result);
  845. }
  846. public function delGood(array $data): array
  847. {
  848. $result = Good::where('id', $data['id'])->delete();
  849. if (empty($result)) {
  850. return Result::error("删除失败", 0);
  851. }
  852. return Result::success($result);
  853. }
  854. //20250226 产品列表
  855. /**
  856. * 后端-获取网站调查问卷列表
  857. * @param array $data
  858. * @return array
  859. */
  860. public function getSurveyList(array $data): array
  861. {
  862. $where = [];
  863. if(isset($data['survey_name']) &&!empty($data['survey_name'])){
  864. array_push($where,['survey_name','like','%'.$data['survey_name'].'%']);
  865. }
  866. if(isset($data['survey_type']) && $data['survey_type'] != null){
  867. array_push($where,['survey_type','=',$data['survey_type']]);
  868. }
  869. if(isset($data['is_survey']) && $data['is_survey'] != null){
  870. array_push($where,['is_survey','=',$data['is_survey']]);
  871. }
  872. // return Result::success($where);
  873. if(!empty($where)){
  874. $query = Article::where($where)->whereNotNull('survey_name');
  875. }else{
  876. $query = Article::whereNotNull('survey_name');
  877. }
  878. $count = $query->count();
  879. $survey = $query->orderByDesc('id')
  880. ->limit($data['pageSize'])
  881. ->offset(($data['page']-1)*$data['pageSize'])
  882. ->get();
  883. if(empty($survey->toArray())){
  884. return Result::error("暂无调查问卷!",0);
  885. }
  886. $result = [
  887. 'rows'=>$survey,
  888. 'count'=>$count
  889. ];
  890. return Result::success($result);
  891. }
  892. /**
  893. * 后端-获取网站调查问卷详情
  894. * @param array $data
  895. * @return array
  896. */
  897. public function getSurveyInfo(array $data): array
  898. {
  899. if(isset($data['sur_id']) &&!empty($data['sur_id'])){
  900. $where = [ 'sur_id'=>$data['sur_id']];
  901. $choose = ArticleSurvey::where($where)->where('is_other',0)
  902. ->leftJoin('article','article_survey.art_id','article.id')
  903. ->select('article_survey.*','article.survey_type')
  904. ->get()->all();
  905. if(empty($choose)){
  906. return Result::error("此调查问卷不存在",0);
  907. }
  908. $resultsArray = array_column($choose, 'results');
  909. $total = array_sum($resultsArray);
  910. $other = ArticleSurvey::where($where)->where('is_other',1)->where('other_id',0)->first();
  911. $others = ArticleSurvey::where($where)->where('is_other',1)->where('other_id','!=',0)->orderByDesc('created_at')->get()->all();
  912. // $total = 0;
  913. if(!empty($other)){
  914. $total = $total + $other['results'];
  915. if(!empty($others)){
  916. $other['hasChildren'] = true;
  917. // array_push($other,['hasChildren','=',true]);
  918. $other['children'] = $others;
  919. $other_choices = [$other->toArray()];
  920. $mer_choice = array_merge($choose,$other_choices);
  921. $value_choice = array_values($mer_choice);
  922. }
  923. else{
  924. // return Result::error('1111');
  925. $other_choices = [$other->toArray()];
  926. $other_choices = array_merge($choose,$other_choices);
  927. $value_choice = array_values($other_choices);
  928. // return Result::success($result);
  929. }
  930. }else{
  931. $value_choice = $choose;
  932. }
  933. $result = [
  934. 'choose'=>$value_choice,
  935. 'total'=>$total
  936. ];
  937. }
  938. return Result::success($result);
  939. }
  940. }