NewsService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 Hyperf\DbConnection\Db;
  9. use Hyperf\RpcServer\Annotation\RpcService;
  10. use App\Tools\Result;
  11. use Ramsey\Uuid\Uuid;
  12. use Hyperf\Utils\Random;
  13. #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  14. class NewsService implements NewsServiceInterface
  15. {
  16. /**
  17. * 获取导航池列表
  18. * @param array $data
  19. * @return array
  20. */
  21. public function getCategoryList(array $data): array
  22. {
  23. $where = [];
  24. if (isset($data['name']) && $data['name']) {
  25. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  26. }
  27. if (isset($data['department_id']) && $data['department_id']) {
  28. array_push($where, ['category.department_id', '=', $data['department_id']]);
  29. }
  30. $city_id = '';
  31. if (isset($data['city_id']) && $data['city_id']) {
  32. $city_id = intval($data['city_id']);
  33. }
  34. $rep = Category::where($where)
  35. ->when($city_id, function ($query) use ($city_id) {
  36. if (isset($city_id) && $city_id) {
  37. $query->whereJsonContains("category.city_arr_id", $city_id);
  38. }
  39. })
  40. ->leftJoin('district', 'category.city_id', 'district.id')
  41. ->leftJoin('department', 'category.department_id', 'department.id')
  42. ->select("category.*", "district.name as city_name", "department.name as department_name")
  43. ->limit($data['pageSize'])->orderByDesc('category.updated_at')->offset(($data['page'] - 1) * $data['pageSize'])->get();
  44. $count = Category::where($where)->when($city_id, function ($query) use ($city_id) {
  45. if (isset($city_id) && $city_id) {
  46. $query->whereJsonContains("category.city_arr_id", $city_id);
  47. }
  48. })->count();
  49. $data = [
  50. 'rows' => $rep->toArray(),
  51. 'count' => $count,
  52. ];
  53. if (empty($rep->toArray())) {
  54. return Result::error("没有导航池数据");
  55. }
  56. return Result::success($data);
  57. }
  58. public function myCategoryList(array $data): array
  59. {
  60. $sszq = $data['sszq'] ?? '';
  61. unset($data['sszq']);
  62. //1,2,3 根据这些webid,。从website_category表中取出对应的分类id,然后从category表中取出分类信息
  63. $catorytids = WebsiteCategory::whereIn('website_id', explode(',', $sszq))->get()->pluck('category_id')->toArray();
  64. $where[] = [
  65. 'pid', '=', $data['pid'],
  66. ];
  67. if (isset($data['name'])) {
  68. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  69. }
  70. var_dump($where);
  71. //根据分类id,从category表中取出分类信息
  72. $result = Category::where($where)
  73. ->whereIn('category.id', $catorytids)
  74. ->select('category.*', 'category.id as category_id')->get();
  75. if (empty($result)) {
  76. return Result::error("没有栏目数据");
  77. }
  78. return Result::success($result);
  79. }
  80. /**
  81. * @param array $data
  82. * @return array
  83. */
  84. public function categoryList(array $data): array
  85. {
  86. $where[] = [
  87. 'pid', '=', $data['pid'],
  88. ];
  89. if (isset($data['name'])) {
  90. array_push($where, ['category.name', 'like', '%' . $data['name'] . '%']);
  91. }
  92. var_dump($where);
  93. $result = Category::where($where)->select('category.*', 'category.id as category_id')->get();
  94. if (empty($result)) {
  95. return Result::error("没有栏目数据");
  96. }
  97. return Result::success($result);
  98. }
  99. /**
  100. * @param array $data
  101. * @return array
  102. */
  103. public function addCategory(array $data): array
  104. {
  105. $id = Category::insertGetId($data);
  106. if (empty($id)) {
  107. return Result::error("添加失败");
  108. }
  109. return Result::success(['id' => $id]);
  110. }
  111. /**
  112. * @param array $data
  113. * @return array
  114. */
  115. public function delCategory(array $data): array
  116. {
  117. $categoryList = Category::where(['pid' => $data['id']])->get();
  118. var_dump("分类列表:", $data, $categoryList);
  119. if ($categoryList->toArray()) {
  120. return Result::error("分类下面有子分类不能删除");
  121. }
  122. $articleList = Article::where(['catid' => $data['id']])->get();
  123. var_dump("文章列表:", $articleList);
  124. if ($articleList->toArray()) {
  125. return Result::error("分类下面有资讯不能删除");
  126. }
  127. $result = Category::where($data)->delete();
  128. if (!$result) {
  129. return Result::error("删除失败");
  130. }
  131. return Result::success($result);
  132. }
  133. /**
  134. * @param array $data
  135. * @return array
  136. */
  137. public function updateCategory(array $data): array
  138. {
  139. $where = [
  140. 'id' => $data['id'],
  141. ];
  142. $result = Category::where($where)->update($data);
  143. if ($result) {
  144. return Result::success($result);
  145. } else {
  146. return Result::error("更新失败");
  147. }
  148. }
  149. /**
  150. * 获取导航池信息
  151. * @param array $data
  152. * @return array
  153. */
  154. public function getCategoryInfo(array $data): array
  155. {
  156. $where = [
  157. 'id' => $data['id'],
  158. ];
  159. $result = Category::where($where)->first();
  160. if ($result) {
  161. return Result::success($result);
  162. } else {
  163. return Result::error("更新失败");
  164. }
  165. }
  166. /**
  167. * @param array $data
  168. * @return array
  169. */
  170. public function getArticleList(array $data): array
  171. {
  172. //判断是否是管理员'1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)'
  173. $type_id = $data['type_id'];
  174. unset($data['type_id']);
  175. $user_id = $data['user_id'];
  176. unset($data['user_id']);
  177. $where = [];
  178. if (isset($data['title']) && $data['title']) {
  179. array_push($where, ['article.title', 'like', '%' . $data['title'] . '%']);
  180. }
  181. if (isset($data['category_name']) && $data['category_name']) {
  182. array_push($where, ['category.name', 'like', '%' . $data['category_name'] . '%']);
  183. }
  184. if (isset($data['author']) && $data['author']) {
  185. array_push($where, ['article.author', '=', $data['author']]);
  186. }
  187. if (isset($data['islink']) && $data['islink'] !== "") {
  188. array_push($where, ['article.islink', '=', $data['islink']]);
  189. }
  190. if (isset($data['status']) && $data['status'] !== "") {
  191. array_push($where, ['article.status', '=', $data['status']]);
  192. }
  193. //不是管理员展示个人数据;
  194. if ($type_id != 10000) {
  195. $where[] = ['article.admin_user_id', '=', $user_id];
  196. }
  197. $rep = Article::where($where)
  198. ->whereNotIn('article.status', [404])
  199. ->leftJoin('category', 'article.catid', 'category.id')
  200. ->select("article.*", "category.name as category_name")
  201. ->orderBy("article.id", "desc")
  202. ->limit($data['pageSize'])
  203. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  204. $count = Article::where($where)->whereNotIn('article.status', [404])
  205. ->leftJoin('category', 'article.catid', 'category.id')->count();
  206. $data = [
  207. 'rows' => $rep->toArray(),
  208. 'count' => $count,
  209. ];
  210. if (empty($rep)) {
  211. return Result::error("没有信息数据");
  212. }
  213. return Result::success($data);
  214. }
  215. /**
  216. * @param array $data
  217. * @return array
  218. */
  219. public function addArticle(array $data): array
  220. {
  221. Db::beginTransaction();
  222. try {
  223. //处理投票
  224. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  225. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  226. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  227. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  228. unset($data['is_survey']);
  229. unset($data['survey_name']);
  230. unset($data['suvey_array']);
  231. unset($data['website_id']);
  232. $articleData = $data;
  233. unset($articleData['content']);
  234. $id = Article::insertGetId($articleData);
  235. $articleDataContent = [
  236. 'article_id' => $id,
  237. 'content' => $data['content'],
  238. ];
  239. ArticleData::insertGetId($articleDataContent);
  240. //处理投票
  241. if ($is_survey == 1) {
  242. //生成年月日时分秒+8位随机数
  243. $uuid = date('YmdHis') . rand(10000000, 99999999);
  244. $suveys_array = json_decode($suvey_array);
  245. var_dump($suveys_array, '---------------------1');
  246. var_dump($suvey_array, '---------------------2');
  247. $suvey_data = [];
  248. foreach ($suveys_array as $key => $value) {
  249. if ($value == '') {
  250. continue;
  251. }
  252. if (is_array($value)) {
  253. $suvey_data[] = [
  254. 'sur_id' => $uuid,
  255. 'art_id' => $id,
  256. 'website_id' => $website_id ?? 2,
  257. 'survey_name' => $survey_name,
  258. 'choice_name' => $value[1],
  259. 'is_other' => 1,
  260. 'orther_id' => 0,
  261. 'results' => 0,
  262. ];
  263. } else {
  264. $suvey_data[] = [
  265. 'sur_id' => $uuid,
  266. 'art_id' => $id,
  267. 'website_id' => $website_id ?? 2,
  268. 'survey_name' => $survey_name,
  269. 'choice_name' => $value,
  270. 'is_other' => 0,
  271. 'orther_id' => 0,
  272. 'results' => 0,
  273. ];
  274. }
  275. if (empty($suvey_data)) {
  276. throw new \Exception("投票数据为空");
  277. }
  278. }
  279. $result = ArticleSurvey::insert($suvey_data);
  280. if (!$result) {
  281. throw new \Exception("投票失败,ArticleSurvey插入失败");
  282. }
  283. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  284. if (!$result) {
  285. throw new \Exception("投票失败,更新主表失败");
  286. }
  287. }
  288. Db::commit();
  289. return Result::success(['id' => $id]);
  290. } catch (\Throwable $ex) {
  291. Db::rollBack();
  292. var_dump($ex->getMessage());
  293. return Result::error("创建失败", 0);
  294. }
  295. }
  296. /**
  297. * @param array $data
  298. * @return array
  299. */
  300. public function delArticle(array $data): array
  301. {
  302. $result = Article::where($data)->delete();
  303. //survey投票删除
  304. articleSurvey::where(['art_id' => $data['id']])->delete();
  305. if (!$result) {
  306. return Result::error("删除失败");
  307. }
  308. return Result::success($result);
  309. }
  310. /**
  311. * @param array $data
  312. * @return array
  313. */
  314. public function updateArticle(array $data): array
  315. {
  316. Db::beginTransaction();
  317. try {
  318. //处理投票
  319. $is_survey = isset($data['is_survey']) ? $data['is_survey'] : 0;
  320. $survey_name = isset($data['survey_name']) ? $data['survey_name'] : '';
  321. $suvey_array = isset($data['suvey_array']) ? $data['suvey_array'] : '';
  322. $website_id = isset($data['website_id']) ? $data['website_id'] : 2;
  323. unset($data['is_survey']);
  324. unset($data['survey_name']);
  325. unset($data['suvey_array']);
  326. unset($data['website_id']);
  327. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  328. $data['tag'] = isset($data['tag']) ? json_encode($data['tag']) : '';
  329. $articleData = $data;
  330. unset($articleData['content']);
  331. unset($articleData['status_name']);
  332. unset($articleData['name']);
  333. unset($articleData['content']);
  334. unset($articleData['pid_arr']);
  335. unset($articleData['pid']);
  336. $id = Article::where(['id' => $data['id']])->update($articleData);
  337. $articleDataContent = [
  338. 'content' => $data['content'],
  339. ];
  340. ArticleData::where(['article_id' => $data['id']])->update($articleDataContent);
  341. //处理投票
  342. $id = $data['id'];
  343. $surveydata = ArticleSurvey::where(['art_id' => $data['id']])->delete();
  344. var_dump($surveydata, 'suvey_array________delete');
  345. //处理投票
  346. if ($is_survey == 1) {
  347. //生成年月日时分秒+8位随机数
  348. $uuid = date('YmdHis') . rand(10000000, 99999999);
  349. $suveys_array = json_decode($suvey_array);
  350. var_dump($suveys_array, '---------------------1');
  351. var_dump($suvey_array, '---------------------2');
  352. $suvey_data = [];
  353. foreach ($suveys_array as $key => $value) {
  354. if ($value == '') {
  355. continue;
  356. }
  357. if (is_array($value)) {
  358. $suvey_data[] = [
  359. 'sur_id' => $uuid,
  360. 'art_id' => $id,
  361. 'website_id' => $website_id ?? 2,
  362. 'survey_name' => $survey_name,
  363. 'choice_name' => $value[1],
  364. 'is_other' => 1,
  365. 'orther_id' => 0,
  366. 'results' => 0,
  367. ];
  368. } else {
  369. $suvey_data[] = [
  370. 'sur_id' => $uuid,
  371. 'art_id' => $id,
  372. 'website_id' => $website_id ?? 2,
  373. 'survey_name' => $survey_name,
  374. 'choice_name' => $value,
  375. 'is_other' => 0,
  376. 'orther_id' => 0,
  377. 'results' => 0,
  378. ];
  379. }
  380. if (empty($suvey_data)) {
  381. throw new \Exception("投票数据为空");
  382. }
  383. }
  384. $result = ArticleSurvey::insert($suvey_data);
  385. if (!$result) {
  386. throw new \Exception("投票失败");
  387. }
  388. $result = Article::where('id', $id)->update(['survey_id' => $uuid, 'survey_name' => $survey_name, 'is_survey' => $is_survey]);
  389. if (!$result) {
  390. throw new \Exception("投票失败");
  391. }
  392. }
  393. Db::commit();
  394. return Result::success([]);
  395. } catch (\Throwable $ex) {
  396. Db::rollBack();
  397. var_dump($ex->getMessage());
  398. return Result::error("更新失败", 0);
  399. }
  400. }
  401. /**
  402. * 更新资讯状态
  403. * @param array $data
  404. * @return array
  405. */
  406. public function upArticleStatus(array $data): array
  407. {
  408. $result = Article::where(['id' => $data['id']])->update($data);
  409. if ($result) {
  410. return Result::success();
  411. } else {
  412. return Result::error("更新状态失败", 0);
  413. }
  414. }
  415. /**
  416. * @param array $data
  417. * @return array
  418. */
  419. public function getArticleInfo(array $data): array
  420. {
  421. $where = [
  422. 'article.id' => $data['id'],
  423. ];
  424. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  425. $articleSurvey = ArticleSurvey::where(['art_id' => $data['id']])->get();
  426. $result['survey_array'] = $articleSurvey->toArray();
  427. if ($result) {
  428. return Result::success($result->toArray());
  429. } else {
  430. return Result::error("查询失败", 0);
  431. }
  432. }
  433. /**
  434. * 获取新闻
  435. * @param array $data
  436. * @return array
  437. */
  438. public function getWebsiteArticlett(array $data): array
  439. {
  440. $category = WebsiteCategory::where('website_id', $data['website_id'])->select('category_id')->get();
  441. $category = $category->toArray();
  442. $result = [];
  443. if ($category) {
  444. $category_ids = [];
  445. foreach ($category as $val) {
  446. array_push($category_ids, $val['category_id']);
  447. }
  448. if (isset($data['placeid'])) {
  449. $placeid = $data['placeid'] - 1;
  450. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset($placeid)->limit($data['pageSize'])->get();
  451. } else {
  452. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("created_at", "desc")->offset(0)->limit($data['pageSize'])->get();
  453. }
  454. if (empty($result)) {
  455. return Result::error("暂无头条新闻", 0);
  456. }
  457. return Result::success($result);
  458. } else {
  459. return Result::error("本网站下暂无相关栏目", 0);
  460. }
  461. }
  462. /**
  463. * 获取模块新闻
  464. * @param array $data
  465. * @return array
  466. */
  467. public function getWebsiteModelArticles(array $data): array
  468. {
  469. $catid = $data['catid'];
  470. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  471. $category = $category->toArray();
  472. if (!empty($category)) {
  473. $where = [
  474. 'status' => 1,
  475. 'catid' => $catid,
  476. ];
  477. if ($data['level'] == 1) {
  478. $level = [
  479. 0 => '1',
  480. 1 => '4',
  481. 2 => '5',
  482. ];
  483. $result = Article::where($where)->whereIn('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  484. } elseif ($data['level'] == 2) {
  485. $level = '2';
  486. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  487. } else {
  488. $level = '3';
  489. $result = Article::where($where)->where('level', $level)->orderBy("created_at", "desc")->limit($data['pagesize'])->get();
  490. }
  491. $result = $result->toArray();
  492. if (!empty($result) && isset($data['placeid']) && !empty($data['placeid'])) {
  493. $placeid = $data['placeid'] - 1;
  494. if ($level == 2 || $level == 3) {
  495. $where = [
  496. 'level' => $level,
  497. ];
  498. $result = Article::where($where)
  499. ->orderBy("created_at", "desc")
  500. ->offset($placeid)
  501. ->limit($data['pagesize'])->get();
  502. } else {
  503. $result = Article::where($where)
  504. ->whereIn('level', $level)
  505. ->offset($placeid)
  506. ->orderBy("created_at", "desc")
  507. ->limit($data['pagesize'])->get();
  508. }
  509. }
  510. if (empty($result)) {
  511. return Result::error("此栏目暂无相关新闻", 0);
  512. }
  513. } else {
  514. return Result::error("此网站暂无此栏目", 0);
  515. }
  516. return Result::success($result);
  517. }
  518. }