NewsService.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. $result['survey_array'] = $articleSurvey->toArray();
  453. if ($result) {
  454. return Result::success($result->toArray());
  455. } else {
  456. return Result::error("查询失败", 0);
  457. }
  458. }
  459. /**
  460. * 获取新闻
  461. * @param array $data
  462. * @return array
  463. */
  464. public function getWebsiteArticlett(array $data): array
  465. {
  466. $category = WebsiteCategory::where('website_id', $data['website_id'])->select('category_id')->get();
  467. $category = $category->toArray();
  468. $result = [];
  469. if ($category) {
  470. $category_ids = [];
  471. foreach ($category as $val) {
  472. array_push($category_ids, $val['category_id']);
  473. }
  474. if (isset($data['placeid'])) {
  475. $placeid = $data['placeid'] - 1;
  476. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("updated_at", "desc")->offset($placeid)->limit($data['pageSize'])->get();
  477. } else {
  478. $result = Article::where('status', 1)->where('level', $data['level'])->whereIn("catid", $category_ids)->orderBy("updated_at", "desc")->offset(0)->limit($data['pageSize'])->get();
  479. }
  480. if (empty($result)) {
  481. return Result::error("暂无头条新闻", 0);
  482. }
  483. return Result::success($result);
  484. } else {
  485. return Result::error("本网站下暂无相关栏目", 0);
  486. }
  487. }
  488. /**
  489. * 获取模块新闻
  490. * @param array $data
  491. * @return array
  492. */
  493. public function getWebsiteModelArticles(array $data): array
  494. {
  495. $catid = $data['catid'];
  496. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $catid)->select('category_id')->get();
  497. $category = $category->toArray();
  498. if (!empty($category)) {
  499. $where = [
  500. 'status' => 1,
  501. 'catid' => $catid,
  502. ];
  503. if ($data['level'] == 1) {
  504. $level = [
  505. 0 => '1',
  506. 1 => '4',
  507. 2 => '5',
  508. 3 => '0',
  509. ];
  510. $result = Article::where($where)->whereIn('level',$level)->orderBy("updated_at","desc")->limit($data['pagesize'])->get();
  511. }elseif($data['level']==2){
  512. $level='2';
  513. $result = Article::where($where)->where('level',$level)->orderBy("updated_at","desc")->limit($data['pagesize'])->get();
  514. } else {
  515. $level = '3';
  516. $result = Article::where($where)->where('level', $level)->orderBy("updated_at", "desc")->limit($data['pagesize'])->get();
  517. }
  518. $result = $result->toArray();
  519. if (!empty($result) && isset($data['placeid']) && !empty($data['placeid'])) {
  520. $placeid = $data['placeid'] - 1;
  521. if ($level == 2 || $level == 3) {
  522. $where = [
  523. 'level' => $level,
  524. ];
  525. $result = Article::where($where)
  526. ->orderBy("updated_at", "desc")
  527. ->offset($placeid)
  528. ->limit($data['pagesize'])->get();
  529. } else {
  530. $result = Article::where($where)
  531. ->whereIn('level', $level)
  532. ->offset($placeid)
  533. ->orderBy("updated_at", "desc")
  534. ->limit($data['pagesize'])->get();
  535. }
  536. }
  537. if (empty($result)) {
  538. return Result::error("此栏目暂无相关新闻", 0);
  539. }
  540. } else {
  541. return Result::error("此网站暂无此栏目", 0);
  542. }
  543. return Result::success($result);
  544. }
  545. /**
  546. *获取新闻列表
  547. * @param array $data
  548. * @return array
  549. */
  550. public function getWebsiteArticleList(array $data): array
  551. {
  552. $where[] = ['status', '=', 1];
  553. if (isset($data['keyword']) && !empty($data['keyword'])) {
  554. array_push($where, ['article.title', 'like', '%' . $data['keyword'] . '%']);
  555. }
  556. if (isset($data['catid']) && !empty($data['catid'])) {
  557. if (is_array($data['catid'])) {
  558. $category = WebsiteCategory::where('website_id', $data['website_id'])->whereIn('category_id', $data['catid'])->pluck('category_id');
  559. array_push($where, ['catid', 'in', $data['catid']]);
  560. } else {
  561. $category = WebsiteCategory::where('website_id', $data['website_id'])->where('category_id', $data['catid'])->pluck('category_id');
  562. array_push($where, ['catid', '=', $data['catid']]);
  563. }
  564. if (empty($category)) {
  565. return Result::error("此网站暂无此栏目", 0);
  566. }
  567. }
  568. // return Result::success($where);
  569. $rep = Article::where(function ($query) use ($where) {
  570. foreach ($where as $condition) {
  571. if ($condition[1] === 'in') {
  572. $query->whereIn($condition[0], $condition[2]);
  573. } else {
  574. $query->where($condition[0], $condition[1], $condition[2]);
  575. }
  576. }
  577. })
  578. ->orderBy("updated_at", "desc")
  579. ->limit($data['pageSize'])
  580. ->offset(($data['page'] - 1) * $data['pageSize'])
  581. ->get();
  582. $count = Article::where(function ($query) use ($where) {
  583. foreach ($where as $condition) {
  584. if ($condition[1] === 'in') {
  585. $query->whereIn($condition[0], $condition[2]);
  586. } else {
  587. $query->where($condition[0], $condition[1], $condition[2]);
  588. }
  589. }
  590. })->count();
  591. $data = [
  592. 'rows' => $rep->toArray(),
  593. 'count' => $count,
  594. ];
  595. if (empty($rep)) {
  596. return Result::error("没有信息数据");
  597. }
  598. return Result::success($data);
  599. }
  600. /**
  601. * 前端-获取新闻详情
  602. * @param array $data
  603. * @return array
  604. */
  605. public function selectWebsiteArticleInfo(array $data): array
  606. {
  607. $where = [
  608. 'article.id' => $data['id'],
  609. 'article.status' => 1,
  610. ];
  611. $result = Article::where($where)->leftJoin("article_data", "article.id", "article_data.article_id")->first();
  612. if (empty($result)) {
  613. return Result::error("查询失败", 0);
  614. }
  615. $category = WebsiteCategory::where('website_id', $data['website_id'])->where(['category_id' => $result['catid']])->first();
  616. if (empty($category)) {
  617. return Result::error("查询失败", 0);
  618. }
  619. $result['category_id'] = $category['category_id'];
  620. $result['cat_name'] = $category['alias'];
  621. return Result::success($result);
  622. }
  623. /**
  624. * 前端-获取网站调查问卷
  625. * @param array $data
  626. * @return array
  627. */
  628. public function getWebsiteSurvey(array $data): array
  629. {
  630. if (isset($data['website_id']) && !empty($data['website_id'])) {
  631. $website = Website::where('id', $data['website_id'])->first();
  632. if (empty($website)) {
  633. return Result::error("暂无此网站", 0);
  634. }
  635. }
  636. if (isset($data['art_id']) && !empty($data['art_id'])) {
  637. $article = Article::where('id', $data['art_id'])->where('status', 1)->first();
  638. if (empty($article)) {
  639. return Result::error("暂无此文章", 0);
  640. }
  641. // return Result::error($data,0);
  642. $where['art_id'] = $data['art_id'];
  643. // $query = ArticleSurvey::where('art_id',$data['art_id']);
  644. } else {
  645. $survey = ArticleSurvey::where('website_id', $data['website_id'])->orderBy('created_at')->first();
  646. if (empty($survey)) {
  647. return Result::error("暂无调查问卷", 0);
  648. }
  649. $where['sur_id'] = $survey['sur_id'];
  650. // $query = ArticleSurvey::where('sur_id',$survey['sur_id']);
  651. }
  652. $result['survey'] = ArticleSurvey::where($where)->where('is_other', 0)
  653. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  654. ->select('article_survey.*', 'article.survey_type')
  655. ->get()->all();
  656. $result['other'] = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
  657. $result['others'] = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->first();
  658. if (empty($result)) {
  659. return Result::error("此文章暂无调查问卷", 0);
  660. }
  661. return Result::success($result);
  662. }
  663. /**
  664. * 前端-添加网站调查问卷选项
  665. * @param array $data
  666. * @return array
  667. */
  668. public function addWebsiteSurveyOption(array $data): array
  669. {
  670. if (isset($data['website_id']) && !empty($data['website_id'])) {
  671. $website = Website::where('id', $data['website_id'])->first();
  672. if (empty($website)) {
  673. return Result::error("暂无此网站", 0);
  674. }
  675. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  676. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->where('is_other', 1)->where('other_id', 0)->first();
  677. if (empty($survey)) {
  678. return Result::error("此调查问卷不可添加选项", 0);
  679. }
  680. if (isset($data['choice_name']) && !empty($data['choice_name'])) {
  681. $choice = [
  682. 'art_id' => $survey['art_id'],
  683. 'website_id' => $data['website_id'],
  684. 'survey_name' => $survey['survey_name'],
  685. 'choice_name' => $data['choice_name'],
  686. 'sur_id' => $survey['sur_id'],
  687. 'is_other' => 1,
  688. 'other_id' => $survey['id'],
  689. ];
  690. $result = ArticleSurvey::insertGetId($choice);
  691. if (empty($result)) {
  692. return Result::error("添加失败", 0);
  693. }
  694. return Result::success($result);
  695. }
  696. }
  697. return Result::error("添加失败", 0);
  698. }
  699. return Result::error("添加失败", 0);
  700. }
  701. /**
  702. * 前端-调查问卷投票
  703. * @param array $data
  704. * @return array
  705. */
  706. public function addWebsiteSurveyVote(array $data): array
  707. {
  708. if (isset($data['website_id']) && !empty($data['website_id'])) {
  709. $website = Website::where('id', $data['website_id'])->first();
  710. if (empty($website)) {
  711. return Result::error("暂无此网站", 0);
  712. }
  713. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  714. $survey = ArticleSurvey::where('sur_id', $data['sur_id'])->where('website_id', $data['website_id'])->pluck('sur_id');
  715. if (empty($survey)) {
  716. return Result::error("此调查问卷不存在", 0);
  717. }
  718. // return Result::success($survey);
  719. // 调查问卷类型 0:单选 1:多选
  720. $type = Article::where('survey_id', $data['sur_id'])->pluck('survey_type');
  721. // return Result::success($type);
  722. if (empty($type) || ($type[0] != 1 && $type[0] != 0)) {
  723. return Result::error("此调查问卷不可投票", 0);
  724. }
  725. // return Result::success($type[0]);
  726. if (isset($data['choice_id']) && !empty($data['choice_id'])) {
  727. if ($type[0] == 0) {
  728. if (is_array($data['choice_id'])) {
  729. return Result::error("请选择一个选项!", 0);
  730. }
  731. $data['choice_id'] = [$data['choice_id']];
  732. } else {
  733. if (!is_array($data['choice_id'])) {
  734. return Result::error("请传递数组!", 0);
  735. }
  736. }
  737. // return Result::success($data['choice_id']);
  738. $other = ArticleSurvey::whereIn('id', $data['choice_id'])
  739. ->where('website_id', $data['website_id'])
  740. ->where('is_other', 1)
  741. ->where('other_id', 0)
  742. ->first();
  743. if (!empty($other)) {
  744. return Result::error("请选择已有的选项!", 0);
  745. }
  746. $choice['other'] = ArticleSurvey::whereIn('id', $data['choice_id'])
  747. ->where('website_id', $data['website_id'])
  748. ->where('is_other', 1)
  749. ->where('other_id', '!=', 0)
  750. ->first();
  751. // return Result::success($data);
  752. if (!empty($choice['other'])) {
  753. array_push($data['choice_id'], $choice['other']['other_id']);
  754. // return Result::success($data['choice_id']);
  755. }
  756. // return Result::success($data);
  757. $choice = ArticleSurvey::whereIn('id', $data['choice_id'])
  758. ->where('website_id', $data['website_id'])
  759. ->increment('results', 1);
  760. if (empty($choice)) {
  761. return Result::error("请选择已有的选项!", 0);
  762. }
  763. return Result::success($choice);
  764. }
  765. return Result::error("参数必填!");
  766. // if(isset($data['choice_id']) && !empty($data['choice_id'])){
  767. // $choice = ArticleSurvey::whereIn('id',$data['choice_id'])->where('website_id',$data['website_id'])->where('is_other',1)->where('other_id',0)->first();
  768. // }
  769. }
  770. return Result::error("此调查问卷不存在", 0);
  771. }
  772. return Result::error("参数必填!");
  773. }
  774. /**
  775. * 后端-获取网站调查问卷列表
  776. * @param array $data
  777. * @return array
  778. */
  779. public function getSurveyList(array $data): array
  780. {
  781. $where = [];
  782. if (isset($data['survey_name']) && !empty($data['survey_name'])) {
  783. array_push($where, ['survey_name', 'like', '%' . $data['survey_name'] . '%']);
  784. }
  785. if (isset($data['survey_type']) && $data['survey_type'] != null) {
  786. array_push($where, ['survey_type', '=', $data['survey_type']]);
  787. }
  788. if (isset($data['is_survey']) && $data['is_survey'] != null) {
  789. array_push($where, ['is_survey', '=', $data['is_survey']]);
  790. }
  791. // return Result::success($where);
  792. if (!empty($where)) {
  793. $query = Article::where($where)->whereNotNull('survey_name');
  794. } else {
  795. $query = Article::whereNotNull('survey_name');
  796. }
  797. $count = $query->count();
  798. $survey = $query->orderByDesc('id')
  799. ->limit($data['pageSize'])
  800. ->offset(($data['page'] - 1) * $data['pageSize'])
  801. ->get();
  802. if (empty($survey->toArray())) {
  803. return Result::error("暂无调查问卷!", 0);
  804. }
  805. $result = [
  806. 'rows' => $survey,
  807. 'count' => $count,
  808. ];
  809. return Result::success($result);
  810. }
  811. /**
  812. * 后端-获取网站调查问卷详情
  813. * @param array $data
  814. * @return array
  815. */
  816. public function getSurveyInfo(array $data): array
  817. {
  818. if (isset($data['sur_id']) && !empty($data['sur_id'])) {
  819. $where = ['sur_id' => $data['sur_id']];
  820. $choose = ArticleSurvey::where($where)->where('is_other', 0)
  821. ->leftJoin('article', 'article_survey.art_id', 'article.id')
  822. ->select('article_survey.*', 'article.survey_type')
  823. ->get()->all();
  824. if (empty($choose)) {
  825. return Result::error("此调查问卷不存在", 0);
  826. }
  827. $resultsArray = array_column($choose, 'results');
  828. $total = array_sum($resultsArray);
  829. $other = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', 0)->first();
  830. $others = ArticleSurvey::where($where)->where('is_other', 1)->where('other_id', '!=', 0)->orderByDesc('created_at')->get()->all();
  831. // $total = 0;
  832. if (!empty($other)) {
  833. $total = $total + $other['results'];
  834. $other['choice_name'] = $other['choice_name'] . '(其他)';
  835. if (!empty($others)) {
  836. $other['hasChildren'] = true;
  837. // array_push($other,['hasChildren','=',true]);
  838. $other['children'] = $others;
  839. $other_choices = [$other->toArray()];
  840. $mer_choice = array_merge($choose, $other_choices);
  841. $value_choice = array_values($mer_choice);
  842. } else {
  843. // return Result::error('1111');
  844. $other_choices = [$other->toArray()];
  845. $other_choices = array_merge($choose, $other_choices);
  846. $value_choice = array_values($other_choices);
  847. // return Result::success($result);
  848. }
  849. } else {
  850. $value_choice = $choose;
  851. }
  852. $result = [
  853. 'choose' => $value_choice,
  854. 'total' => $total,
  855. ];
  856. }
  857. return Result::success($result);
  858. }
  859. /**
  860. * 验证导航名称是否重复
  861. * @return void
  862. */
  863. public function checkCategoryName(array $data): array
  864. {
  865. $result = Category::when($data, function ($query) use ($data) {
  866. if (isset($data['name']) && $data['name']) {
  867. $query->where("name", $data['name']);
  868. }
  869. if (isset($data['id']) && $data['id']) {
  870. $query->where("id", "!=", $data['id']);
  871. }
  872. })->first();
  873. if ($result) {
  874. return Result::error("已存在");
  875. } else {
  876. return Result::success();
  877. }
  878. }
  879. //20250226 产品列表
  880. public function getGoodList(array $data): array
  881. {
  882. $where = [];
  883. //类型
  884. if (isset($data['type_id']) && $data['type_id']) {
  885. $where = [
  886. 'type_id' => $data['type_id'],
  887. ];
  888. }
  889. //名称
  890. if (isset($data['name']) && $data['name']) {
  891. $where = [
  892. 'good.name' => $data['name'],
  893. ];
  894. }
  895. $where1 = [];
  896. //website_id
  897. // if (isset($data['website_id']) && $data['website_id']) {
  898. // $where1 = [
  899. // 'good.website_id', 'like', '%' . $data['website_id'] . '%',
  900. // ];
  901. // }
  902. // website_name
  903. if (isset($data['website_name']) && $data['website_name']) {
  904. $where1[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
  905. }
  906. // catid
  907. if (isset($data['category_name']) && $data['category_name']) {
  908. $where1[] = ['category.name', 'like', '%' . $data['category_name'] . '%'];
  909. }
  910. // $result = Good::where($where)
  911. // ->orderBy("updated_at", "desc")->paginate($data['pige_size'], ['*'], 'page', $data['page']);
  912. $result = Good::where($where)
  913. ->when(!empty($where1), function ($query) use ($where1) {
  914. return $query->where($where1);
  915. })
  916. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  917. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  918. ->leftJoin('category', 'good.catid', '=', 'category.id')
  919. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  920. ->orderBy("id", "desc")
  921. ->limit($data['page_size'])
  922. ->offset(($data['page'] - 1) * $data['page_size'])
  923. ->get();
  924. $count = Good::where($where)
  925. ->leftJoin('district', 'good.city_id', '=', 'district.id')
  926. ->leftJoin('website', 'good.website_id', '=', 'website.id')
  927. ->leftJoin('category', 'good.catid', '=', 'category.id')
  928. ->select('good.*', 'district.name as cityname', 'website.website_name as website_name', 'category.name as category_name')
  929. ->orderBy("updated_at", "desc")->count();
  930. $data = [
  931. 'rows' => $result->toArray(),
  932. 'count' => $count,
  933. ];
  934. if (empty($result)) {
  935. return Result::error("此栏目暂无相关产品", 0);
  936. }
  937. return Result::success($data);
  938. }
  939. public function getGoodInfo(array $data): array
  940. {
  941. $result = Good::where('id', $data['id'])->first();
  942. if (empty($result)) {
  943. return Result::error("此产品不存在", 0);
  944. }
  945. return Result::success($result);
  946. }
  947. public function addGood(array $data): array
  948. {
  949. // unset($data['city_arr_id']);
  950. // unset($data['cat_arr_id']);
  951. $data['city_id'] = end($data['city_arr_id']);
  952. $data['catid'] = end($data['cat_arr_id']);
  953. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  954. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  955. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  956. unset($data['imgUrl']);
  957. $result = Good::insert($data);
  958. if (empty($result)) {
  959. return Result::error("添加失败", 0);
  960. }
  961. return Result::success($result);
  962. }
  963. public function updateGood(array $data): array
  964. {
  965. $data['city_id'] = end($data['city_arr_id']);
  966. $data['catid'] = end($data['cat_arr_id']);
  967. $data['city_arr_id'] = isset($data['city_arr_id']) ? json_encode($data['city_arr_id']) : '';
  968. $data['cat_arr_id'] = isset($data['cat_arr_id']) ? json_encode($data['cat_arr_id']) : '';
  969. $data['imgurl'] = isset($data['imgurl']) ? json_encode($data['imgurl']) : '';
  970. //设置东八区
  971. date_default_timezone_set('Asia/Shanghai');
  972. $data['updated_at'] = date('Y-m-d H:i:s');
  973. $result = Good::where('id', $data['id'])->update($data);
  974. if (empty($result)) {
  975. return Result::error("更新失败", 0);
  976. }
  977. return Result::success($result);
  978. }
  979. public function delGood(array $data): array
  980. {
  981. $result = Good::where('id', $data['id'])->delete();
  982. if (empty($result)) {
  983. return Result::error("删除失败", 0);
  984. }
  985. return Result::success($result);
  986. }
  987. //20250226 产品列表
  988. }