NewsService.php 40 KB

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