NewsService.php 42 KB

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