NewsService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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\WebsiteCategory;
  7. use App\Model\ArticleSurvey;
  8. use App\Model\Good;
  9. use Hyperf\DbConnection\Db;
  10. use Hyperf\RpcServer\Annotation\RpcService;
  11. use App\Tools\Result;
  12. use Ramsey\Uuid\Uuid;
  13. use Hyperf\Utils\Random;
  14. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  15. class NewsService implements NewsServiceInterface
  16. {
  17. /**
  18. * 获取导航池列表
  19. * @param array $data
  20. * @return array
  21. */
  22. public function getCategoryList(array $data): array
  23. {
  24. $where = [];
  25. if (isset($data['name']) && $data['name']) {
  26. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  27. }
  28. if (isset($data['department_id']) && $data['department_id']) {
  29. array_push($where, ['category.department_id', '=', $data['department_id']]);
  30. }
  31. $city_id = '';
  32. if (isset($data['city_id']) && $data['city_id']) {
  33. $city_id = intval($data['city_id']);
  34. }
  35. $rep = Category::where($where)
  36. ->when($city_id, function ($query) use ($city_id) {
  37. if (isset($city_id) && $city_id) {
  38. $query->whereJsonContains("category.city_arr_id", $city_id);
  39. }
  40. })
  41. ->leftJoin('district', 'category.city_id', 'district.id')
  42. ->leftJoin('department', 'category.department_id', 'department.id')
  43. ->select("category.*", "district.name as city_name", "department.name as department_name")
  44. ->limit($data['pageSize'])->orderByDesc('category.updated_at')->offset(($data['page'] - 1) * $data['pageSize'])->get();
  45. $count = Category::where($where)->when($city_id, function ($query) use ($city_id) {
  46. if (isset($city_id) && $city_id) {
  47. $query->whereJsonContains("category.city_arr_id", $city_id);
  48. }
  49. })->count();
  50. $data = [
  51. 'rows' => $rep->toArray(),
  52. 'count' => $count,
  53. ];
  54. if (empty($rep->toArray())) {
  55. return Result::error("没有导航池数据");
  56. }
  57. return Result::success($data);
  58. }
  59. public function myCategoryList(array $data): array
  60. {
  61. $sszq = $data['sszq'] ?? '';
  62. unset($data['sszq']);
  63. //1,2,3 根据这些webid,。从website_category表中取出对应的分类id,然后从category表中取出分类信息
  64. $catorytids = WebsiteCategory::whereIn('website_id', explode(',', $sszq))->get()->pluck('category_id')->toArray();
  65. $where[] = [
  66. 'pid', '=', $data['pid'],
  67. ];
  68. if (isset($data['name'])) {
  69. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  70. }
  71. var_dump($where);
  72. //根据分类id,从category表中取出分类信息
  73. $result = Category::where($where)
  74. ->whereIn('category.id', $catorytids)
  75. ->select('category.*', 'category.id as category_id')->get();
  76. if (empty($result)) {
  77. return Result::error("没有栏目数据");
  78. }
  79. return Result::success($result);
  80. }
  81. /**
  82. * @param array $data
  83. * @return array
  84. */
  85. public function categoryList(array $data): array
  86. {
  87. $where[] = [
  88. 'pid', '=', $data['pid'],
  89. ];
  90. if (isset($data['name'])) {
  91. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  92. }
  93. var_dump($where);
  94. $result = Category::where($where)->select('category.*', 'category.id as category_id')->get();
  95. if (empty($result)) {
  96. return Result::error("没有栏目数据");
  97. }
  98. return Result::success($result);
  99. }
  100. /**
  101. * @param array $data
  102. * @return array
  103. */
  104. public function addCategory(array $data): array
  105. {
  106. $id = Category::insertGetId($data);
  107. if (empty($id)) {
  108. return Result::error("添加失败");
  109. }
  110. return Result::success(['id' => $id]);
  111. }
  112. /**
  113. * @param array $data
  114. * @return array
  115. */
  116. public function delCategory(array $data): array
  117. {
  118. $categoryList = Category::where(['pid' => $data['id']])->get();
  119. var_dump("分类列表:", $data, $categoryList);
  120. if ($categoryList->toArray()) {
  121. return Result::error("分类下面有子分类不能删除");
  122. }
  123. $articleList = Article::where(['catid' => $data['id']])->get();
  124. var_dump("文章列表:", $articleList);
  125. if ($articleList->toArray()) {
  126. return Result::error("分类下面有资讯不能删除");
  127. }
  128. $result = Category::where($data)->delete();
  129. if (!$result) {
  130. return Result::error("删除失败");
  131. }
  132. return Result::success($result);
  133. }
  134. /**
  135. * @param array $data
  136. * @return array
  137. */
  138. public function updateCategory(array $data): array
  139. {
  140. $where = [
  141. 'id' => $data['id'],
  142. ];
  143. $result = Category::where($where)->update($data);
  144. if ($result) {
  145. return Result::success($result);
  146. } else {
  147. return Result::error("更新失败");
  148. }
  149. }
  150. /**
  151. * 获取导航池信息
  152. * @param array $data
  153. * @return array
  154. */
  155. public function getCategoryInfo(array $data): array
  156. {
  157. $where = [
  158. 'id' => $data['id'],
  159. ];
  160. $result = Category::where($where)->first();
  161. if ($result) {
  162. return Result::success($result);
  163. } else {
  164. return Result::error("更新失败");
  165. }
  166. }
  167. /**
  168. * @param array $data
  169. * @return array
  170. */
  171. public function getArticleList(array $data): array
  172. {
  173. //判断是否是管理员'1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)'
  174. $type_id = $data['type_id'];
  175. unset($data['type_id']);
  176. $user_id = $data['user_id'];
  177. unset($data['user_id']);
  178. $where = [];
  179. if (isset($data['title']) && $data['title']) {
  180. array_push($where, ['article.title', 'like', '%' . $data['title'] . '%']);
  181. }
  182. if (isset($data['category_name']) && $data['category_name']) {
  183. array_push($where, ['category.name', 'like', '%' . $data['category_name'] . '%']);
  184. }
  185. if (isset($data['author']) && $data['author']) {
  186. array_push($where, ['article.author', '=', $data['author']]);
  187. }
  188. if (isset($data['islink']) && $data['islink'] !== "") {
  189. array_push($where, ['article.islink', '=', $data['islink']]);
  190. }
  191. if (isset($data['status']) && $data['status'] !== "") {
  192. array_push($where, ['article.status', '=', $data['status']]);
  193. }
  194. //不是管理员展示个人数据;
  195. if ($type_id != 10000) {
  196. $where[] = ['article.admin_user_id', '=', $user_id];
  197. }
  198. $rep = Article::where($where)
  199. ->whereNotIn('article.status', [404])
  200. ->leftJoin('category', 'article.catid', 'category.id')
  201. ->select("article.*", "category.name as category_name")
  202. ->orderBy("article.id", "desc")
  203. ->limit($data['pageSize'])
  204. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  205. $count = Article::where($where)->whereNotIn('article.status', [404])
  206. ->leftJoin('category', 'article.catid', 'category.id')->count();
  207. $data = [
  208. 'rows' => $rep->toArray(),
  209. 'count' => $count,
  210. ];
  211. if (empty($rep)) {
  212. return Result::error("没有信息数据");
  213. }
  214. return Result::success($data);
  215. }
  216. /**
  217. * @param array $data
  218. * @return array
  219. */
  220. public function addArticle(array $data): array
  221. {
  222. var_dump($data, '----------12-----------1');
  223. unset($data['user_type']);
  224. // unset($data['web_site_id']);
  225. unset($data['nav_add_pool_id']);
  226. // $data['cat_arr_id'] = is_string($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  227. Db::beginTransaction();
  228. try {
  229. //处理投票
  230. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  231. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  232. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  233. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  234. unset($data['is_survey']);
  235. unset($data['survey_name']);
  236. unset($data['suvey_array']);
  237. unset($data['website_id']);
  238. $articleData = $data;
  239. unset($articleData['content']);
  240. $id = Article::insertGetId($articleData);
  241. $articleDataContent = [
  242. 'article_id' => $id,
  243. 'content' => $data['content'],
  244. ];
  245. ArticleData::insertGetId($articleDataContent);
  246. //处理投票
  247. if ($is_survey == 1) {
  248. //生成年月日时分秒+8位随机数
  249. $uuid = date('YmdHis') . rand(10000000, 99999999);
  250. $suveys_array = json_decode($suvey_array);
  251. var_dump($suveys_array, '---------------------1');
  252. var_dump($suvey_array, '---------------------2');
  253. $suvey_data = [];
  254. foreach ($suveys_array as $key => $value) {
  255. if ($value == '') {
  256. continue;
  257. }
  258. if (is_array($value)) {
  259. $suvey_data[] = [
  260. 'sur_id' => $uuid,
  261. 'art_id' => $id,
  262. 'website_id' => $website_id ?? 2,
  263. 'survey_name' => $survey_name,
  264. 'choice_name' => $value[1],
  265. 'is_other' => 1,
  266. 'orther_id' => 0,
  267. 'results' => 0,
  268. ];
  269. } else {
  270. $suvey_data[] = [
  271. 'sur_id' => $uuid,
  272. 'art_id' => $id,
  273. 'website_id' => $website_id ?? 2,
  274. 'survey_name' => $survey_name,
  275. 'choice_name' => $value,
  276. 'is_other' => 0,
  277. 'orther_id' => 0,
  278. 'results' => 0,
  279. ];
  280. }
  281. if (empty($suvey_data)) {
  282. throw new \Exception("投票数据为空");
  283. }
  284. }
  285. $result = ArticleSurvey::insert($suvey_data);
  286. if (!$result) {
  287. throw new \Exception("投票失败,ArticleSurvey插入失败");
  288. }
  289. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  290. if (!$result) {
  291. throw new \Exception("投票失败,更新主表失败");
  292. }
  293. }
  294. Db::commit();
  295. return Result::success(['id' => $id]);
  296. } catch (\Throwable $ex) {
  297. Db::rollBack();
  298. var_dump($ex->getMessage());
  299. return Result::error("创建失败", 0);
  300. }
  301. }
  302. /**
  303. * @param array $data
  304. * @return array
  305. */
  306. public function delArticle(array $data): array
  307. {
  308. $result = Article::where($data)->delete();
  309. //survey投票删除
  310. articleSurvey::where(['art_id' => $data['id']])->delete();
  311. if (!$result) {
  312. return Result::error("删除失败");
  313. }
  314. return Result::success($result);
  315. }
  316. /**
  317. * @param array $data
  318. * @return array
  319. */
  320. public function updateArticle(array $data): array
  321. {
  322. Db::beginTransaction();
  323. unset($data['user_type']);
  324. // unset($data['web_site_id']);
  325. unset($data['nav_add_pool_id']);
  326. try {
  327. //处理投票
  328. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  329. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  330. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  331. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  332. unset($data['is_survey']);
  333. unset($data['survey_name']);
  334. unset($data['suvey_array']);
  335. unset($data['website_id']);
  336. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  337. $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
  338. $articleData = $data;
  339. unset($articleData['content']);
  340. unset($articleData['status_name']);
  341. unset($articleData['name']);
  342. unset($articleData['content']);
  343. unset($articleData['pid_arr']);
  344. unset($articleData['pid']);
  345. $id = Article::where(['id' => $data['id']])->update($articleData);
  346. $articleDataContent = [
  347. 'content' => $data['content'],
  348. ];
  349. ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
  350. //处理投票
  351. $id = $data['id'];
  352. $surveydata = ArticleSurvey::where(['art_id' => $data['id']])->delete();
  353. var_dump($surveydata, 'suvey_array________delete');
  354. //处理投票
  355. if ($is_survey == 1) {
  356. //生成年月日时分秒+8位随机数
  357. $uuid = date('YmdHis') . rand(10000000, 99999999);
  358. $suveys_array = json_decode($suvey_array);
  359. var_dump($suveys_array, '---------------------1');
  360. var_dump($suvey_array, '---------------------2');
  361. $suvey_data = [];
  362. foreach ($suveys_array as $key => $value) {
  363. if ($value == '') {
  364. continue;
  365. }
  366. if (is_array($value)) {
  367. $suvey_data[] = [
  368. 'sur_id' => $uuid,
  369. 'art_id' => $id,
  370. 'website_id' => $website_id ?? 2,
  371. 'survey_name' => $survey_name,
  372. 'choice_name' => $value[1],
  373. 'is_other' => 1,
  374. 'orther_id' => 0,
  375. 'results' => 0,
  376. ];
  377. } else {
  378. $suvey_data[] = [
  379. 'sur_id' => $uuid,
  380. 'art_id' => $id,
  381. 'website_id' => $website_id ?? 2,
  382. 'survey_name' => $survey_name,
  383. 'choice_name' => $value,
  384. 'is_other' => 0,
  385. 'orther_id' => 0,
  386. 'results' => 0,
  387. ];
  388. }
  389. if (empty($suvey_data)) {
  390. throw new \Exception("投票数据为空");
  391. }
  392. }
  393. $result = ArticleSurvey::insert($suvey_data);
  394. if (!$result) {
  395. throw new \Exception("投票失败");
  396. }
  397. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  398. if (!$result) {
  399. throw new \Exception("投票失败");
  400. }
  401. }
  402. Db::commit();
  403. return Result::success([]);
  404. } catch (\Throwable $ex) {
  405. Db::rollBack();
  406. var_dump($ex->getMessage());
  407. return Result::error("更新失败", 0);
  408. }
  409. }
  410. /**
  411. * 更新资讯状态
  412. * @param array $data
  413. * @return array
  414. */
  415. public function upArticleStatus(array $data): array
  416. {
  417. $result = Article::where(['id' => $data['id']])->update($data);
  418. if ($result) {
  419. return Result::success();
  420. } else {
  421. return Result::error("更新状态失败", 0);
  422. }
  423. }
  424. /**
  425. * @param array $data
  426. * @return array
  427. */
  428. public function getArticleInfo(array $data): array
  429. {
  430. $where = [
  431. 'article.id' => $data['id'],
  432. ];
  433. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  434. $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
  435. $result['survey_array'] = $articleSurvey->toArray();
  436. if ($result) {
  437. return Result::success($result->toArray());
  438. } else {
  439. return Result::error("查询失败", 0);
  440. }
  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. ];
  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. //20250226 产品列表
  528. public function getGoodList(array $data): array
  529. {
  530. $where = [];
  531. //类型
  532. if (isset($data['type_id']) && $data['type_id']) {
  533. $where = [
  534. 'type_id' => $data['type_id'],
  535. ];
  536. }
  537. //名称
  538. if (isset($data['name']) && $data['name']) {
  539. $where = [
  540. 'name' => $data['name'],
  541. ];
  542. }
  543. //website_id
  544. if (isset($data['website_id']) && $data['website_id']) {
  545. $where = [
  546. 'website_id' => $data['website_id'],
  547. ];
  548. }
  549. //catid
  550. if (isset($data['catid']) && $data['catid']) {
  551. $where = [
  552. 'catid' => $data['catid'],
  553. ];
  554. }
  555. // $result = Good::where($where)
  556. // ->orderBy("updated_at", "desc")->paginate($data['pige_size'], ['*'], 'page', $data['page']);
  557. $result = Good::where($where)
  558. ->orderBy("updated_at", "desc")
  559. ->limit($data['page_size'])
  560. ->offset(($data['page'] - 1) * $data['page_size'])
  561. ->get();
  562. $count = Good::where($where)->count();
  563. $data = [
  564. 'rows' => $result->toArray(),
  565. 'count' => $count,
  566. ];
  567. if (empty($result)) {
  568. return Result::error("此栏目暂无相关产品", 0);
  569. }
  570. return Result::success($data);
  571. }
  572. public function getGoodInfo(array $data): array
  573. {
  574. $result = Good::where('id', $data['id'])->first();
  575. if (empty($result)) {
  576. return Result::error("此产品不存在", 0);
  577. }
  578. return Result::success($result);
  579. }
  580. public function addGood(array $data): array
  581. {
  582. $result = Good::create($data);
  583. if (empty($result)) {
  584. return Result::error("添加失败", 0);
  585. }
  586. return Result::success($result);
  587. }
  588. public function updateGood(array $data): array
  589. {
  590. $result = Good::where('id', $data['id'])->update($data);
  591. if (empty($result)) {
  592. return Result::error("更新失败", 0);
  593. }
  594. return Result::success($result);
  595. }
  596. public function delGood(array $data): array
  597. {
  598. $result = Good::where('id', $data['id'])->delete();
  599. if (empty($result)) {
  600. return Result::error("删除失败", 0);
  601. }
  602. return Result::success($result);
  603. }
  604. //20250226 产品列表
  605. }